Browse Source

refectoring: remove statsHLP

Laserlicht 1 năm trước cách đây
mục cha
commit
33b2633775

+ 2 - 58
lib/gameState/CGameState.cpp

@@ -1538,62 +1538,6 @@ bool CGameState::checkForStandardLoss(const PlayerColor & player) const
 	return pState.checkVanquished();
 	return pState.checkVanquished();
 }
 }
 
 
-struct statsHLP
-{
-	using TStat = std::pair<PlayerColor, si64>;
-	//converts [<player's color, value>] to vec[place] -> platers
-	static std::vector< std::vector< PlayerColor > > getRank( std::vector<TStat> stats )
-	{
-		std::sort(stats.begin(), stats.end(), statsHLP());
-
-		//put first element
-		std::vector< std::vector<PlayerColor> > ret;
-		std::vector<PlayerColor> tmp;
-		tmp.push_back( stats[0].first );
-		ret.push_back( tmp );
-
-		//the rest of elements
-		for(int g=1; g<stats.size(); ++g)
-		{
-			if(stats[g].second == stats[g-1].second)
-			{
-				(ret.end()-1)->push_back( stats[g].first );
-			}
-			else
-			{
-				//create next occupied rank
-				std::vector<PlayerColor> tmp;
-				tmp.push_back(stats[g].first);
-				ret.push_back(tmp);
-			}
-		}
-
-		return ret;
-	}
-
-	bool operator()(const TStat & a, const TStat & b) const
-	{
-		return a.second > b.second;
-	}
-
-	static const CGHeroInstance * findBestHero(CGameState * gs, const PlayerColor & color)
-	{
-		std::vector<ConstTransitivePtr<CGHeroInstance> > &h = gs->players[color].heroes;
-		if(h.empty())
-			return nullptr;
-		//best hero will be that with highest exp
-		int best = 0;
-		for(int b=1; b<h.size(); ++b)
-		{
-			if(h[b]->exp > h[best]->exp)
-			{
-				best = b;
-			}
-		}
-		return h[best];
-	}
-};
-
 void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
 void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
 {
 {
 	auto playerInactive = [&](const PlayerColor & color) 
 	auto playerInactive = [&](const PlayerColor & color) 
@@ -1613,7 +1557,7 @@ void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
 			stat.second = VAL_GETTER; \
 			stat.second = VAL_GETTER; \
 			stats.push_back(stat); \
 			stats.push_back(stat); \
 		} \
 		} \
-		tgi.FIELD = statsHLP::getRank(stats); \
+		tgi.FIELD = Statistic::getRank(stats); \
 	}
 	}
 
 
 	for(auto & elem : players)
 	for(auto & elem : players)
@@ -1635,7 +1579,7 @@ void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
 		{
 		{
 			if(playerInactive(player.second.color))
 			if(playerInactive(player.second.color))
 				continue;
 				continue;
-			const CGHeroInstance * best = statsHLP::findBestHero(this, player.second.color);
+			const CGHeroInstance * best = Statistic::findBestHero(this, player.second.color);
 			InfoAboutHero iah;
 			InfoAboutHero iah;
 			iah.initFromHero(best, (level >= 2) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC);
 			iah.initFromHero(best, (level >= 2) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC);
 			iah.army.clear();
 			iah.army.clear();

+ 46 - 0
lib/gameState/GameStatistics.cpp

@@ -187,4 +187,50 @@ double Statistic::getMapVisitedRatio(const CGameState * gs, PlayerColor player)
 	return visible / numTiles;
 	return visible / numTiles;
 }
 }
 
 
+const CGHeroInstance * Statistic::findBestHero(CGameState * gs, const PlayerColor & color)
+{
+	std::vector<ConstTransitivePtr<CGHeroInstance> > &h = gs->players[color].heroes;
+	if(h.empty())
+		return nullptr;
+	//best hero will be that with highest exp
+	int best = 0;
+	for(int b=1; b<h.size(); ++b)
+	{
+		if(h[b]->exp > h[best]->exp)
+		{
+			best = b;
+		}
+	}
+	return h[best];
+}
+
+std::vector<std::vector<PlayerColor>> Statistic::getRank(std::vector<TStat> stats)
+{
+	std::sort(stats.begin(), stats.end(), [](const TStat & a, const TStat & b) { return a.second > b.second; });
+
+	//put first element
+	std::vector< std::vector<PlayerColor> > ret;
+	std::vector<PlayerColor> tmp;
+	tmp.push_back( stats[0].first );
+	ret.push_back( tmp );
+
+	//the rest of elements
+	for(int g=1; g<stats.size(); ++g)
+	{
+		if(stats[g].second == stats[g-1].second)
+		{
+			(ret.end()-1)->push_back( stats[g].first );
+		}
+		else
+		{
+			//create next occupied rank
+			std::vector<PlayerColor> tmp;
+			tmp.push_back(stats[g].first);
+			ret.push_back(tmp);
+		}
+	}
+
+	return ret;
+}
+
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END

+ 5 - 0
lib/gameState/GameStatistics.h

@@ -16,6 +16,7 @@ VCMI_LIB_NAMESPACE_BEGIN
 
 
 struct PlayerState;
 struct PlayerState;
 class CGameState;
 class CGameState;
+class CGHeroInstance;
 
 
 struct DLL_LINKAGE StatisticDataSetEntry
 struct DLL_LINKAGE StatisticDataSetEntry
 {
 {
@@ -67,10 +68,14 @@ public:
 class DLL_LINKAGE Statistic
 class DLL_LINKAGE Statistic
 {
 {
 public:
 public:
+	using TStat = std::pair<PlayerColor, si64>;
+
     static int getNumberOfArts(const PlayerState * ps);
     static int getNumberOfArts(const PlayerState * ps);
 	static si64 getArmyStrength(const PlayerState * ps);
 	static si64 getArmyStrength(const PlayerState * ps);
 	static int getIncome(const CGameState * gs, const PlayerState * ps);
 	static int getIncome(const CGameState * gs, const PlayerState * ps);
 	static double getMapVisitedRatio(const CGameState * gs, PlayerColor player);
 	static double getMapVisitedRatio(const CGameState * gs, PlayerColor player);
+	static const CGHeroInstance * findBestHero(CGameState * gs, const PlayerColor & color);
+	static std::vector<std::vector<PlayerColor>> getRank(std::vector<TStat> stats);
 };
 };
 
 
 VCMI_LIB_NAMESPACE_END
 VCMI_LIB_NAMESPACE_END