← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/bug-1817550-Statistics-hook-Winconditions into lp:widelands

 

hessenfarmer has proposed merging lp:~widelands-dev/widelands/bug-1817550-Statistics-hook-Winconditions into lp:widelands.

Commit message:
- Added statistics hook for territorial Win conditions
- Added statistics hook for artifacts wincondition
- fixed a bug in artifacts which led to coroutine error if Player defeated when reaching win condition

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1645837 in widelands: "Number of conquered artifacts should be shown in statistics"
  https://bugs.launchpad.net/widelands/+bug/1645837
  Bug #1817550 in widelands: "Add statistics hook for Territorial functions"
  https://bugs.launchpad.net/widelands/+bug/1817550

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/bug-1817550-Statistics-hook-Winconditions/+merge/365086

This is for Build 21. 

Win condition statictic Windows will be shown showing the percentage of territory owned for territorial and artifacts owned for artifact wc.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/bug-1817550-Statistics-hook-Winconditions into lp:widelands.
=== added file 'data/images/wui/stats/genstats_artifacts.png'
Binary files data/images/wui/stats/genstats_artifacts.png	1970-01-01 00:00:00 +0000 and data/images/wui/stats/genstats_artifacts.png	2019-03-26 07:43:29 +0000 differ
=== added file 'data/images/wui/stats/genstats_territorial_big.png'
Binary files data/images/wui/stats/genstats_territorial_big.png	1970-01-01 00:00:00 +0000 and data/images/wui/stats/genstats_territorial_big.png	2019-03-26 07:43:29 +0000 differ
=== added file 'data/images/wui/stats/genstats_territorial_small.png'
Binary files data/images/wui/stats/genstats_territorial_small.png	1970-01-01 00:00:00 +0000 and data/images/wui/stats/genstats_territorial_small.png	2019-03-26 07:43:29 +0000 differ
=== modified file 'data/scripting/win_conditions/artifacts.lua'
--- data/scripting/win_conditions/artifacts.lua	2019-02-23 09:12:18 +0000
+++ data/scripting/win_conditions/artifacts.lua	2019-03-26 07:43:29 +0000
@@ -15,6 +15,7 @@
 local wc_descname = _("Artifacts")
 local wc_version = 1
 local wc_desc = _ "Search for ancient artifacts. Once all of them are found, the team who owns most of them will win the game."
+local wc_artifacts = _"Artifacts owned"
 
 -- Table of all artifacts to conquer
 local artifact_fields = {}
@@ -50,7 +51,22 @@
          end
       end
 
+      local artifacts_owner = {}
       local plrs = wl.Game().players
+
+      -- statistic variables and functions
+      -- initializing artifacts owned table
+      local artifacts_per_player = {}
+      -- funtion to calculate actual number of owned artifacts per player
+      local function _calcowned()
+         for idx, plr in ipairs(wl.Game().players) do
+            artifacts_per_player[plr.number] = 0
+         end
+         for idx, plr in pairs(artifacts_owner) do
+         artifacts_per_player[plr.number] = artifacts_per_player[plr.number] + 1
+         end
+      end
+
       if #artifact_fields == 0 then
          for idx, plr in ipairs(plrs) do
             send_message(plr, _"No Artifacts", p(_"There are no artifacts on this map. This should not happen. Please file a bug report on %s and specify your Widelands version and the map you tried to load."):bformat("https://wl.widelands.org/wiki/ReportingBugs/";), {popup = true})
@@ -85,11 +101,22 @@
             end
          end
       end
+      
+
+      -- Install statistics hook
+      hooks.custom_statistic = {
+         name = wc_artifacts,
+         pic = "images/wui/stats/genstats_artifacts.png",
+         calculator = function(p)
+            _calcowned(p)
+            return artifacts_per_player[p.number] or 0
+         end,
+      }
 
       -- Iterate all players, if one is defeated, remove him
       -- from the list, send him a defeated message and give him full vision
       -- Check if all artifacts have been found (i.e. controlled by a player)
-      local artifacts_owner = {}
+
       repeat
          sleep(1000)
          check_player_defeated(plrs, lost_game.title, lost_game.body, wc_descname, wc_version)
@@ -115,7 +142,7 @@
 
       -- All artifacts are found, the game is over.
       local artifacts_per_team = {}
-      for idx, plr in ipairs(plrs) do
+      for idx, plr in ipairs(wl.Game().players) do
          artifacts_per_team[_getkey(plr)] = 0
       end
 
@@ -164,7 +191,6 @@
          msg = msg .. p((_"Team %1$i (%2$s) owns %3$s."):bformat(t[1].team, members, artifacts))
       end
 
-
       for idx, plr in ipairs(plrs) do
          local key = _getkey(plr)
          -- If two or more teams have the same amount of artifacts, they are all considered winners.
@@ -177,4 +203,5 @@
          end
       end
    end,
+
 }

=== modified file 'data/scripting/win_conditions/territorial_lord.lua'
--- data/scripting/win_conditions/territorial_lord.lua	2019-03-09 10:55:24 +0000
+++ data/scripting/win_conditions/territorial_lord.lua	2019-03-26 07:43:29 +0000
@@ -22,7 +22,8 @@
    "area. The winner will be the player or the team that is able to keep " ..
    "that area for at least 20 minutes."
 )
-
+-- TRANSLATORS: subtext of the territorial time statisitcs hook. Keep it short and consistent with the translation of the Win condition.
+local wc_terr_lord = _"Territory percentage"
 local fields = 0
 
 return {
@@ -57,6 +58,16 @@
          end
       end
 
+   -- Install statistics hook
+   hooks.custom_statistic = {
+      name = wc_terr_lord,
+      pic = "images/wui/stats/genstats_territorial_small.png",
+      calculator = function(p)
+         local pts = count_owned_valuable_fields_for_all_players(wl.Game().players)
+         return (pts[p.number]*100//fields)
+      end,
+   }
+
       -- here is the main loop!!!
       while true do
          -- Sleep 30 seconds == STATISTICS_SAMPLE_TIME

=== modified file 'data/scripting/win_conditions/territorial_time.lua'
--- data/scripting/win_conditions/territorial_time.lua	2019-03-09 10:55:24 +0000
+++ data/scripting/win_conditions/territorial_time.lua	2019-03-26 07:43:29 +0000
@@ -26,7 +26,8 @@
    "that area for at least 20 minutes, or the one with the most territory " ..
    "after 4 hours, whichever comes first."
 )
-
+-- TRANSLATORS: subtext of the territorial time statisitcs hook. Keep it short and consistent with the translation of the Win condition.
+local wc_terr_time = _"Territory percentage"
 local fields = 0
 
 return {
@@ -73,6 +74,16 @@
          end
       end
 
+   -- Install statistics hook
+   hooks.custom_statistic = {
+      name = wc_terr_time,
+      pic = "images/wui/stats/genstats_territorial_small.png",
+      calculator = function(p)
+         local pts = count_owned_valuable_fields_for_all_players(wl.Game().players)
+         return (pts[p.number]*100//fields)
+      end,
+   }
+
       -- here is the main loop!!!
       while true do
          -- Sleep 30 seconds == STATISTICS_SAMPLE_TIME


Follow ups