1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-14 02:33:51 +02:00

code review

This commit is contained in:
Laserlicht 2024-08-03 17:55:43 +02:00
parent f0c0beb9e0
commit 9d64059496
2 changed files with 22 additions and 33 deletions

View File

@ -51,9 +51,8 @@ StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, cons
data.numberArtifacts = Statistic::getNumberOfArts(ps); data.numberArtifacts = Statistic::getNumberOfArts(ps);
data.armyStrength = Statistic::getArmyStrength(ps, true); data.armyStrength = Statistic::getArmyStrength(ps, true);
data.income = Statistic::getIncome(gs, ps); data.income = Statistic::getIncome(gs, ps);
data.mapVisitedRatio = Statistic::getMapVisitedRatio(gs, ps->color); data.mapExploredRatio = Statistic::getMapExploredRatio(gs, ps->color);
data.obeliskVisited = Statistic::getObeliskVisited(gs, ps->team); data.obeliskVisitedRatio = Statistic::getObeliskVisitedRatio(gs, ps->team);
data.mightMagicRatio = Statistic::getMightMagicRatio(ps);
data.numMines = Statistic::getNumMines(gs, ps); data.numMines = Statistic::getNumMines(gs, ps);
data.score = scenarioHighScores.calculate().total; data.score = scenarioHighScores.calculate().total;
data.maxHeroLevel = Statistic::findBestHero(gs, ps->color) ? Statistic::findBestHero(gs, ps->color)->level : 0; data.maxHeroLevel = Statistic::findBestHero(gs, ps->color) ? Statistic::findBestHero(gs, ps->color)->level : 0;
@ -85,9 +84,8 @@ std::string StatisticDataSet::toCsv()
ss << "NumberArtifacts" << ";"; ss << "NumberArtifacts" << ";";
ss << "ArmyStrength" << ";"; ss << "ArmyStrength" << ";";
ss << "Income" << ";"; ss << "Income" << ";";
ss << "MapVisitedRatio" << ";"; ss << "MapExploredRatio" << ";";
ss << "ObeliskVisited" << ";"; ss << "ObeliskVisitedRatio" << ";";
ss << "MightMagicRatio" << ";";
ss << "Score" << ";"; ss << "Score" << ";";
ss << "MaxHeroLevel" << ";"; ss << "MaxHeroLevel" << ";";
ss << "NumBattlesNeutral" << ";"; ss << "NumBattlesNeutral" << ";";
@ -116,9 +114,8 @@ std::string StatisticDataSet::toCsv()
ss << entry.numberArtifacts << ";"; ss << entry.numberArtifacts << ";";
ss << entry.armyStrength << ";"; ss << entry.armyStrength << ";";
ss << entry.income << ";"; ss << entry.income << ";";
ss << entry.mapVisitedRatio << ";"; ss << entry.mapExploredRatio << ";";
ss << entry.obeliskVisited << ";"; ss << entry.obeliskVisitedRatio << ";";
ss << entry.mightMagicRatio << ";";
ss << entry.score << ";"; ss << entry.score << ";";
ss << entry.maxHeroLevel << ";"; ss << entry.maxHeroLevel << ";";
ss << entry.numBattlesNeutral << ";"; ss << entry.numBattlesNeutral << ";";
@ -225,10 +222,10 @@ int Statistic::getIncome(const CGameState * gs, const PlayerState * ps)
return totalIncome; return totalIncome;
} }
double Statistic::getMapVisitedRatio(const CGameState * gs, PlayerColor player) float Statistic::getMapExploredRatio(const CGameState * gs, PlayerColor player)
{ {
double visible = 0.0; float visible = 0.0;
double numTiles = 0.0; float numTiles = 0.0;
for(int layer = 0; layer < (gs->map->twoLevel ? 2 : 1); layer++) for(int layer = 0; layer < (gs->map->twoLevel ? 2 : 1); layer++)
for(int y = 0; y < gs->map->height; ++y) for(int y = 0; y < gs->map->height; ++y)
@ -264,9 +261,9 @@ const CGHeroInstance * Statistic::findBestHero(const CGameState * gs, const Play
return h[best]; return h[best];
} }
std::vector<std::vector<PlayerColor>> Statistic::getRank(std::vector<TStat> stats) std::vector<std::vector<PlayerColor>> Statistic::getRank(std::vector<std::pair<PlayerColor, si64>> stats)
{ {
std::sort(stats.begin(), stats.end(), [](const TStat & a, const TStat & b) { return a.second > b.second; }); std::sort(stats.begin(), stats.end(), [](const std::pair<PlayerColor, si64> & a, const std::pair<PlayerColor, si64> & b) { return a.second > b.second; });
//put first element //put first element
std::vector< std::vector<PlayerColor> > ret; std::vector< std::vector<PlayerColor> > ret;
@ -301,15 +298,11 @@ int Statistic::getObeliskVisited(const CGameState * gs, const TeamID & t)
return 0; return 0;
} }
double Statistic::getMightMagicRatio(const PlayerState * ps) float Statistic::getObeliskVisitedRatio(const CGameState * gs, const TeamID & t)
{ {
double numMight = 0; if(!gs->map->obeliskCount)
return 0;
for(auto h : ps->heroes) return (float)getObeliskVisited(gs, t) / (float)gs->map->obeliskCount;
if(h->type->heroClass->affinity == CHeroClass::EClassAffinity::MIGHT)
numMight++;
return numMight / ps->heroes.size();
} }
std::map<EGameResID, int> Statistic::getNumMines(const CGameState * gs, const PlayerState * ps) std::map<EGameResID, int> Statistic::getNumMines(const CGameState * gs, const PlayerState * ps)

View File

@ -34,9 +34,8 @@ struct DLL_LINKAGE StatisticDataSetEntry
int numberArtifacts; int numberArtifacts;
si64 armyStrength; si64 armyStrength;
int income; int income;
double mapVisitedRatio; float mapExploredRatio;
int obeliskVisited; float obeliskVisitedRatio;
double mightMagicRatio;
std::map<EGameResID, int> numMines; std::map<EGameResID, int> numMines;
int score; int score;
int maxHeroLevel; int maxHeroLevel;
@ -62,9 +61,8 @@ struct DLL_LINKAGE StatisticDataSetEntry
h & numberArtifacts; h & numberArtifacts;
h & armyStrength; h & armyStrength;
h & income; h & income;
h & mapVisitedRatio; h & mapExploredRatio;
h & obeliskVisited; h & obeliskVisitedRatio;
h & mightMagicRatio;
h & numMines; h & numMines;
h & score; h & score;
h & maxHeroLevel; h & maxHeroLevel;
@ -118,16 +116,14 @@ class DLL_LINKAGE Statistic
{ {
static std::vector<const CGMine *> getMines(const CGameState * gs, const PlayerState * ps); static std::vector<const CGMine *> getMines(const CGameState * gs, const PlayerState * ps);
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, bool withTownGarrison = false); static si64 getArmyStrength(const PlayerState * ps, bool withTownGarrison = false);
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 float getMapExploredRatio(const CGameState * gs, PlayerColor player);
static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color); static const CGHeroInstance * findBestHero(const CGameState * gs, const PlayerColor & color);
static std::vector<std::vector<PlayerColor>> getRank(std::vector<TStat> stats); static std::vector<std::vector<PlayerColor>> getRank(std::vector<std::pair<PlayerColor, si64>> stats);
static int getObeliskVisited(const CGameState * gs, const TeamID & t); static int getObeliskVisited(const CGameState * gs, const TeamID & t);
static double getMightMagicRatio(const PlayerState * ps); static float getObeliskVisitedRatio(const CGameState * gs, const TeamID & t);
static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps); static std::map<EGameResID, int> getNumMines(const CGameState * gs, const PlayerState * ps);
}; };