Browse Source

code review

Laserlicht 1 year ago
parent
commit
e0ab760a2f

+ 11 - 24
lib/gameState/GameStatistics.cpp

@@ -62,16 +62,16 @@ StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, cons
 	data.numMines = Statistic::getNumMines(gs, ps);
 	data.score = scenarioHighScores.calculate().total;
 	data.maxHeroLevel = Statistic::findBestHero(gs, ps->color) ? Statistic::findBestHero(gs, ps->color)->level : 0;
-	data.numBattlesNeutral = gs->statistic.values.numBattlesNeutral.count(ps->color) ? gs->statistic.values.numBattlesNeutral.at(ps->color) : 0;
-	data.numBattlesPlayer = gs->statistic.values.numBattlesPlayer.count(ps->color) ? gs->statistic.values.numBattlesPlayer.at(ps->color) : 0;
-	data.numWinBattlesNeutral = gs->statistic.values.numWinBattlesNeutral.count(ps->color) ? gs->statistic.values.numWinBattlesNeutral.at(ps->color) : 0;
-	data.numWinBattlesPlayer = gs->statistic.values.numWinBattlesPlayer.count(ps->color) ? gs->statistic.values.numWinBattlesPlayer.at(ps->color) : 0;
-	data.numHeroSurrendered = gs->statistic.values.numHeroSurrendered.count(ps->color) ? gs->statistic.values.numHeroSurrendered.at(ps->color) : 0;
-	data.numHeroEscaped = gs->statistic.values.numHeroEscaped.count(ps->color) ? gs->statistic.values.numHeroEscaped.at(ps->color) : 0;
-	data.spentResourcesForArmy = gs->statistic.values.spentResourcesForArmy.count(ps->color) ? gs->statistic.values.spentResourcesForArmy.at(ps->color) : TResources();
-	data.spentResourcesForBuildings = gs->statistic.values.spentResourcesForBuildings.count(ps->color) ? gs->statistic.values.spentResourcesForBuildings.at(ps->color) : TResources();
-	data.tradeVolume = gs->statistic.values.tradeVolume.count(ps->color) ? gs->statistic.values.tradeVolume.at(ps->color) : TResources();
-	data.movementPointsUsed = gs->statistic.values.movementPointsUsed.count(ps->color) ? gs->statistic.values.movementPointsUsed.at(ps->color) : 0;
+	data.numBattlesNeutral = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numBattlesNeutral : 0;
+	data.numBattlesPlayer = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numBattlesPlayer : 0;
+	data.numWinBattlesNeutral = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numWinBattlesNeutral : 0;
+	data.numWinBattlesPlayer = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numWinBattlesPlayer : 0;
+	data.numHeroSurrendered = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numHeroSurrendered : 0;
+	data.numHeroEscaped = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).numHeroEscaped : 0;
+	data.spentResourcesForArmy = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).spentResourcesForArmy : TResources();
+	data.spentResourcesForBuildings = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).spentResourcesForBuildings : TResources();
+	data.tradeVolume = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).tradeVolume : TResources();
+	data.movementPointsUsed = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).movementPointsUsed : 0;
 
 	return data;
 }
@@ -236,31 +236,18 @@ int Statistic::getIncome(const CGameState * gs, const PlayerState * ps)
 {
 	int percentIncome = gs->getStartInfo()->getIthPlayersSettings(ps->color).handicap.percentIncome;
 	int totalIncome = 0;
-	const CGObjectInstance * heroOrTown = nullptr;
 
 	//Heroes can produce gold as well - skill, specialty or arts
 	for(const auto & h : ps->heroes)
-	{
 		totalIncome += h->valOfBonuses(Selector::typeSubtype(BonusType::GENERATE_RESOURCE, BonusSubtypeID(GameResID(GameResID::GOLD)))) * percentIncome / 100;
 
-		if(!heroOrTown)
-			heroOrTown = h;
-	}
-
 	//Add town income of all towns
 	for(const auto & t : ps->towns)
-	{
 		totalIncome += t->dailyIncome()[EGameResID::GOLD];
 
-		if(!heroOrTown)
-			heroOrTown = t;
-	}
-
 	for(const CGMine * mine : getMines(gs, ps))
-	{
-		if (mine->producedResource == EGameResID::GOLD)
+		if(mine->producedResource == EGameResID::GOLD)
 			totalIncome += mine->getProducedQuantity();
-	}
 
 	return totalIncome;
 }

+ 13 - 13
lib/gameState/GameStatistics.h

@@ -100,18 +100,18 @@ public:
 	static StatisticDataSetEntry createEntry(const PlayerState * ps, const CGameState * gs);
     std::string toCsv();
 
-	struct ValueStorage // holds some actual values needed for stats
+	struct PlayerAccumulatedValueStorage // holds some actual values needed for stats
 	{
-		std::map<PlayerColor, int> numBattlesNeutral;
-		std::map<PlayerColor, int> numBattlesPlayer;
-		std::map<PlayerColor, int> numWinBattlesNeutral;
-		std::map<PlayerColor, int> numWinBattlesPlayer;
-		std::map<PlayerColor, int> numHeroSurrendered;
-		std::map<PlayerColor, int> numHeroEscaped;
-		std::map<PlayerColor, TResources> spentResourcesForArmy;
-		std::map<PlayerColor, TResources> spentResourcesForBuildings;
-		std::map<PlayerColor, TResources> tradeVolume;
-		std::map<PlayerColor, si64> movementPointsUsed;
+		int numBattlesNeutral;
+		int numBattlesPlayer;
+		int numWinBattlesNeutral;
+		int numWinBattlesPlayer;
+		int numHeroSurrendered;
+		int numHeroEscaped;
+		TResources spentResourcesForArmy;
+		TResources spentResourcesForBuildings;
+		TResources tradeVolume;
+		si64 movementPointsUsed;
 
 		template <typename Handler> void serialize(Handler &h)
 		{
@@ -127,12 +127,12 @@ public:
 			h & movementPointsUsed;
 		}
 	};
-	ValueStorage values;
+	std::map<PlayerColor, PlayerAccumulatedValueStorage> accumulatedValues;
 
 	template <typename Handler> void serialize(Handler &h)
 	{
 		h & data;
-		h & values;
+		h & accumulatedValues;
 	}
 };
 

+ 6 - 6
server/CGameHandler.cpp

@@ -1351,7 +1351,7 @@ bool CGameHandler::moveHero(ObjectInstanceID hid, int3 dst, EMovementMode moveme
 
 		turnTimerHandler->setEndTurnAllowed(h->getOwner(), !movingOntoWater && !movingOntoObstacle);
 		doMove(TryMoveHero::SUCCESS, lookForGuards, visitDest, LEAVING_TILE);
-		gs->statistic.values.movementPointsUsed[asker] += tmh.movePoints;
+		gs->statistic.accumulatedValues[asker].movementPointsUsed += tmh.movePoints;
 		return true;
 	}
 }
@@ -2466,7 +2466,7 @@ bool CGameHandler::buildStructure(ObjectInstanceID tid, BuildingID requestedID,
 	if(!force)
 	{
 		giveResources(t->tempOwner, -requestedBuilding->resources);
-		gs->statistic.values.spentResourcesForBuildings[t->tempOwner] += requestedBuilding->resources;
+		gs->statistic.accumulatedValues[t->tempOwner].spentResourcesForBuildings += requestedBuilding->resources;
 	}
 
 	//We know what has been built, apply changes. Do this as final step to properly update town window
@@ -2571,7 +2571,7 @@ bool CGameHandler::recruitCreatures(ObjectInstanceID objid, ObjectInstanceID dst
 	//recruit
 	TResources cost = (c->getFullRecruitCost() * cram);
 	giveResources(army->tempOwner, -cost);
-	gs->statistic.values.spentResourcesForArmy[army->tempOwner] += cost;
+	gs->statistic.accumulatedValues[army->tempOwner].spentResourcesForArmy += cost;
 
 	SetAvailableCreatures sac;
 	sac.tid = objid;
@@ -2624,7 +2624,7 @@ bool CGameHandler::upgradeCreature(ObjectInstanceID objid, SlotID pos, CreatureI
 
 	//take resources
 	giveResources(player, -totalCost);
-	gs->statistic.values.spentResourcesForArmy[player] += totalCost;
+	gs->statistic.accumulatedValues[player].spentResourcesForArmy += totalCost;
 
 	//upgrade creature
 	changeStackType(StackLocation(obj, pos), upgID.toCreature());
@@ -3249,8 +3249,8 @@ bool CGameHandler::tradeResources(const IMarket *market, ui32 amountToSell, Play
 	giveResource(player, toSell, -b1 * amountToBoy);
 	giveResource(player, toBuy, b2 * amountToBoy);
 
-	gs->statistic.values.tradeVolume[player][toSell] += -b1 * amountToBoy;
-	gs->statistic.values.tradeVolume[player][toBuy] += b2 * amountToBoy;
+	gs->statistic.accumulatedValues[player].tradeVolume[toSell] += -b1 * amountToBoy;
+	gs->statistic.accumulatedValues[player].tradeVolume[toBuy] += b2 * amountToBoy;
 
 	return true;
 }

+ 8 - 8
server/battles/BattleResultProcessor.cpp

@@ -500,17 +500,17 @@ void BattleResultProcessor::endBattleConfirm(const CBattleInfoCallback & battle)
 	// add statistic
 	if(battle.sideToPlayer(0) == PlayerColor::NEUTRAL || battle.sideToPlayer(1) == PlayerColor::NEUTRAL)
 	{
-		gameHandler->gameState()->statistic.values.numBattlesNeutral[battle.sideToPlayer(0)]++;
-		gameHandler->gameState()->statistic.values.numBattlesNeutral[battle.sideToPlayer(1)]++;
+		gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(0)].numBattlesNeutral++;
+		gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(1)].numBattlesNeutral++;
 		if(!finishingBattle->isDraw())
-			gameHandler->gameState()->statistic.values.numWinBattlesNeutral[battle.sideToPlayer(finishingBattle->winnerSide)]++;
+			gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesNeutral++;
 	}
 	else
 	{
-		gameHandler->gameState()->statistic.values.numBattlesPlayer[battle.sideToPlayer(0)]++;
-		gameHandler->gameState()->statistic.values.numBattlesPlayer[battle.sideToPlayer(1)]++;
+		gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(0)].numBattlesPlayer++;
+		gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(1)].numBattlesPlayer++;
 		if(!finishingBattle->isDraw())
-			gameHandler->gameState()->statistic.values.numWinBattlesPlayer[battle.sideToPlayer(finishingBattle->winnerSide)]++;
+			gameHandler->gameState()->statistic.accumulatedValues[battle.sideToPlayer(finishingBattle->winnerSide)].numWinBattlesPlayer++;
 	}
 
 	BattleResultAccepted raccepted;
@@ -573,13 +573,13 @@ void BattleResultProcessor::battleAfterLevelUp(const BattleID & battleID, const
 
 	if (result.result == EBattleResult::SURRENDER)
 	{
-		gameHandler->gameState()->statistic.values.numHeroSurrendered[finishingBattle->loser]++;
+		gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroSurrendered++;
 		gameHandler->heroPool->onHeroSurrendered(finishingBattle->loser, finishingBattle->loserHero);
 	}
 
 	if (result.result == EBattleResult::ESCAPE)
 	{
-		gameHandler->gameState()->statistic.values.numHeroEscaped[finishingBattle->loser]++;
+		gameHandler->gameState()->statistic.accumulatedValues[finishingBattle->loser].numHeroEscaped++;
 		gameHandler->heroPool->onHeroEscaped(finishingBattle->loser, finishingBattle->loserHero);
 	}