← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/production_statistics into lp:widelands

 

TiborB has proposed merging lp:~widelands-dev/widelands/production_statistics into lp:widelands.

Commit message:
New method for Player in LUA: get_produced_wares_count()

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/production_statistics/+merge/314283

The player in the LUA has new method: get_produced_wares_count() that returns count of produced wares for single ware, list of wares, or "all"
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/production_statistics into lp:widelands.
=== modified file 'src/logic/player.cc'
--- src/logic/player.cc	2016-12-18 17:02:44 +0000
+++ src/logic/player.cc	2017-01-07 20:28:54 +0000
@@ -1080,11 +1080,24 @@
 void Player::ware_produced(DescriptionIndex const wareid) {
 	assert(ware_productions_.size() == egbase().tribes().nrwares());
 	assert(egbase().tribes().ware_exists(wareid));
-
 	++current_produced_statistics_[wareid];
 }
 
 /**
+ * Return count of produced wares for ware index
+ */
+uint32_t Player::get_current_produced_statistics_(uint8_t const wareid) {
+	assert (wareid < egbase().tribes().nrwares());
+	assert (wareid < ware_productions_.size());
+	uint32_t sum = 0;
+	for (const auto stat : *get_ware_production_statistics(wareid)) {
+		sum += stat;
+	}
+	return sum;
+}
+
+
+/**
  * Some units from one kind of ware were consumed.
  * Update the corresponding statistics
  *

=== modified file 'src/logic/player.h'
--- src/logic/player.h	2016-10-21 08:21:41 +0000
+++ src/logic/player.h	2017-01-07 20:28:54 +0000
@@ -522,6 +522,8 @@
 		return economies_.size();
 	}
 
+	uint32_t get_current_produced_statistics_(uint8_t);
+
 	// Military stuff
 	void drop_soldier(PlayerImmovable&, Soldier&);
 	void change_training_options(TrainingSite&, TrainingAttribute attr, int32_t val);

=== modified file 'src/scripting/lua_game.cc'
--- src/scripting/lua_game.cc	2016-11-30 20:02:32 +0000
+++ src/scripting/lua_game.cc	2017-01-07 20:28:54 +0000
@@ -99,6 +99,7 @@
    METHOD(LuaPlayer, get_suitability),
    METHOD(LuaPlayer, allow_workers),
    METHOD(LuaPlayer, switchplayer),
+   METHOD(LuaPlayer, get_produced_wares_count),
    {nullptr, nullptr},
 };
 const PropertyType<LuaPlayer> LuaPlayer::Properties[] = {
@@ -872,6 +873,67 @@
 	return 0;
 }
 
+/* RST
+   .. method:: produced_wares_count(what)
+
+      Returns count of wares produced byt the player up to now.
+      'what' can be either an "all" or single ware name or an array of names. If single
+      ware name is given, integer is returned, otherwise the table is returned.
+     
+*/
+int LuaPlayer::get_produced_wares_count(lua_State* L) {
+	Player& p = get(L, get_egbase(L));
+	const TribeDescr& tribe = p.tribe();
+	int32_t nargs = lua_gettop(L);
+	if (nargs != 2) {
+		report_error(L, "One argument is required for produced_wares_count()");
+	}
+	std::vector<DescriptionIndex> requested_wares;
+	DescriptionIndex single_ware = INVALID_INDEX;
+	if (lua_isstring(L, 2)) {
+		std::string what = luaL_checkstring(L, -1);
+		if (what != "all") {
+			single_ware = tribe.ware_index(what);
+			if (single_ware == INVALID_INDEX) {
+				report_error(L, "Unrecognized ware %s", what.c_str());
+			}
+		}
+	} else {
+		/* array of names */
+		luaL_checktype(L, 2, LUA_TTABLE);
+		lua_pushnil(L);
+		while (lua_next(L, 2) != 0) {
+			std::string what = luaL_checkstring(L, -1);
+			lua_pop(L, 1);
+			requested_wares.push_back(tribe.ware_index(what));
+			if (requested_wares.back() == INVALID_INDEX) {
+				report_error(L, "Unrecognized ware %s", what.c_str());
+			}
+		}
+	}
+
+	if (single_ware != INVALID_INDEX) {
+		lua_pushuint32(L, p.get_current_produced_statistics_(single_ware));
+	} else if (requested_wares.empty()) {
+		// we return all wares
+		lua_newtable(L);
+		for (const DescriptionIndex& idx : tribe.wares()) {
+			lua_pushstring(L, tribe.get_ware_descr(idx)->name());
+			lua_pushuint32(L, p.get_current_produced_statistics_(idx));
+			lua_settable(L, -3);
+		}
+	} else {
+		// we return requested wares
+		lua_newtable(L);
+		for (const DescriptionIndex& idx : requested_wares) {
+			lua_pushstring(L, tribe.get_ware_descr(idx)->name());
+			lua_pushuint32(L, p.get_current_produced_statistics_(idx));
+			lua_settable(L, -3);
+		}
+	}
+	return 1;
+}
+
 /*
  ==========================================================
  C METHODS

=== modified file 'src/scripting/lua_game.h'
--- src/scripting/lua_game.h	2016-08-04 15:49:05 +0000
+++ src/scripting/lua_game.h	2017-01-07 20:28:54 +0000
@@ -76,6 +76,7 @@
 	int set_team(lua_State* L);
 	int get_see_all(lua_State* L);
 	int set_see_all(lua_State* L);
+	int get_produced_wares_count(lua_State* L);
 
 	/*
 	 * Lua methods

=== modified file 'test/maps/expedition.wmf/scripting/init.lua'
--- test/maps/expedition.wmf/scripting/init.lua	2016-01-28 05:24:34 +0000
+++ test/maps/expedition.wmf/scripting/init.lua	2017-01-07 20:28:54 +0000
@@ -316,6 +316,7 @@
    hq:set_wares("log", 100)
    port:set_wares("blackwood", 100)
 
+   log_starting_count = p1:get_produced_wares_count("log")
 
    port:start_expedition()
    wait_for_message("Expedition")
@@ -360,6 +361,11 @@
    -- Check that the first lumberjack house got his worker.
    assert_equal(1, map:get_field(17, 1).immovable:get_workers("barbarians_lumberjack"))
 
+   -- were some logs produced in the meantime?
+   assert(p1:get_produced_wares_count("log") > log_starting_count)
+   -- various ways to get produced wares count should return the same results
+   assert_equal(p1:get_produced_wares_count("log"), (p1:get_produced_wares_count("all"))["log"])
+
    print("# All Tests passed.")
    wl.ui.MapView():close()
 end

=== modified file 'test/maps/expedition.wmf/scripting/test_check_transportation_works_one_ship.lua'
--- test/maps/expedition.wmf/scripting/test_check_transportation_works_one_ship.lua	2015-10-31 12:11:44 +0000
+++ test/maps/expedition.wmf/scripting/test_check_transportation_works_one_ship.lua	2017-01-07 20:28:54 +0000
@@ -1,4 +1,8 @@
 run(function()
+   game = wl.Game()
+   map = game.map
+   p1 = game.players[1]
+
    create_one_ship()
 
    test_transporting_works()


Follow ups