File: GameStatistics.cpp

package info (click to toggle)
vcmi 1.6.5%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 32,060 kB
  • sloc: cpp: 238,971; python: 265; sh: 224; xml: 157; ansic: 78; objc: 61; makefile: 49
file content (394 lines) | stat: -rw-r--r-- 12,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * GameStatistics.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "GameStatistics.h"
#include "../CPlayerState.h"
#include "../constants/StringConstants.h"
#include "../VCMIDirs.h"
#include "CGameState.h"
#include "TerrainHandler.h"
#include "StartInfo.h"
#include "HighScore.h"
#include "../mapObjects/CGHeroInstance.h"
#include "../mapObjects/CGTownInstance.h"
#include "../mapObjects/CGObjectInstance.h"
#include "../mapObjects/MiscObjects.h"
#include "../mapping/CMap.h"
#include "../entities/building/CBuilding.h"


VCMI_LIB_NAMESPACE_BEGIN

void StatisticDataSet::add(StatisticDataSetEntry entry)
{
	data.push_back(entry);
}

StatisticDataSetEntry StatisticDataSet::createEntry(const PlayerState * ps, const CGameState * gs)
{
	StatisticDataSetEntry data;

	HighScoreParameter param = HighScore::prepareHighScores(gs, ps->color, false);
	HighScoreCalculation scenarioHighScores;
	scenarioHighScores.parameters.push_back(param);
	scenarioHighScores.isCampaign = false;

	data.map = gs->map->name.toString();
	data.timestamp = std::time(nullptr);
	data.day = gs->getDate(Date::DAY);
	data.player = ps->color;
	data.playerName = gs->getStartInfo()->playerInfos.at(ps->color).name;
	data.team = ps->team;
	data.isHuman = ps->isHuman();
	data.status = ps->status;
	data.resources = ps->resources;
	data.numberHeroes = ps->getHeroes().size();
	data.numberTowns = gs->howManyTowns(ps->color);
	data.numberArtifacts = Statistic::getNumberOfArts(ps);
	data.numberDwellings = Statistic::getNumberOfDwellings(ps);
	data.armyStrength = Statistic::getArmyStrength(ps, true);
	data.totalExperience = Statistic::getTotalExperience(ps);
	data.income = Statistic::getIncome(gs, ps);
	data.mapExploredRatio = Statistic::getMapExploredRatio(gs, ps->color);
	data.obeliskVisitedRatio = Statistic::getObeliskVisitedRatio(gs, ps->team);
	data.townBuiltRatio = Statistic::getTownBuiltRatio(ps);
	data.hasGrail = param.hasGrail;
	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.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.eventCapturedTown = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).lastCapturedTownDay == gs->getDate(Date::DAY) : false;
	data.eventDefeatedStrongestHero = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).lastDefeatedStrongestHeroDay == gs->getDate(Date::DAY) : false;
	data.movementPointsUsed = gs->statistic.accumulatedValues.count(ps->color) ? gs->statistic.accumulatedValues.at(ps->color).movementPointsUsed : 0;

	return data;
}

std::string StatisticDataSet::toCsv(std::string sep)
{
	std::stringstream ss;

	auto resources = std::vector<EGameResID>{EGameResID::GOLD, EGameResID::WOOD, EGameResID::MERCURY, EGameResID::ORE, EGameResID::SULFUR, EGameResID::CRYSTAL, EGameResID::GEMS};

	ss << "Map" << sep;
	ss << "Timestamp" << sep;
	ss << "Day" << sep;
	ss << "Player" << sep;
	ss << "PlayerName" << sep;
	ss << "Team" << sep;
	ss << "IsHuman" << sep;
	ss << "Status" << sep;
	ss << "NumberHeroes" << sep;
	ss << "NumberTowns" << sep;
	ss << "NumberArtifacts" << sep;
	ss << "NumberDwellings" << sep;
	ss << "ArmyStrength" << sep;
	ss << "TotalExperience" << sep;
	ss << "Income" << sep;
	ss << "MapExploredRatio" << sep;
	ss << "ObeliskVisitedRatio" << sep;
	ss << "TownBuiltRatio" << sep;
	ss << "HasGrail" << sep;
	ss << "Score" << sep;
	ss << "MaxHeroLevel" << sep;
	ss << "NumBattlesNeutral" << sep;
	ss << "NumBattlesPlayer" << sep;
	ss << "NumWinBattlesNeutral" << sep;
	ss << "NumWinBattlesPlayer" << sep;
	ss << "NumHeroSurrendered" << sep;
	ss << "NumHeroEscaped" << sep;
	ss << "EventCapturedTown" << sep;
	ss << "EventDefeatedStrongestHero" << sep;
	ss << "MovementPointsUsed";
	for(auto & resource : resources)
		ss << sep << GameConstants::RESOURCE_NAMES[resource];
	for(auto & resource : resources)
		ss << sep << GameConstants::RESOURCE_NAMES[resource] + "Mines";
	for(auto & resource : resources)
		ss << sep << GameConstants::RESOURCE_NAMES[resource] + "SpentResourcesForArmy";
	for(auto & resource : resources)
		ss << sep << GameConstants::RESOURCE_NAMES[resource] + "SpentResourcesForBuildings";
	for(auto & resource : resources)
		ss << sep << GameConstants::RESOURCE_NAMES[resource] + "TradeVolume";
	ss << "\r\n";

	for(auto & entry : data)
	{
		ss << entry.map << sep;
		ss << vstd::getFormattedDateTime(entry.timestamp, "%Y-%m-%dT%H:%M:%S") << sep;
		ss << entry.day << sep;
		ss << GameConstants::PLAYER_COLOR_NAMES[entry.player] << sep;
		ss << entry.playerName << sep;
		ss << entry.team.getNum() << sep;
		ss << entry.isHuman << sep;
		ss << static_cast<int>(entry.status) << sep;
		ss << entry.numberHeroes << sep;
		ss << entry.numberTowns <<  sep;
		ss << entry.numberArtifacts << sep;
		ss << entry.numberDwellings << sep;
		ss << entry.armyStrength << sep;
		ss << entry.totalExperience << sep;
		ss << entry.income << sep;
		ss << entry.mapExploredRatio << sep;
		ss << entry.obeliskVisitedRatio << sep;
		ss << entry.townBuiltRatio << sep;
		ss << entry.hasGrail << sep;
		ss << entry.score << sep;
		ss << entry.maxHeroLevel << sep;
		ss << entry.numBattlesNeutral << sep;
		ss << entry.numBattlesPlayer << sep;
		ss << entry.numWinBattlesNeutral << sep;
		ss << entry.numWinBattlesPlayer << sep;
		ss << entry.numHeroSurrendered << sep;
		ss << entry.numHeroEscaped << sep;
		ss << entry.eventCapturedTown << sep;
		ss << entry.eventDefeatedStrongestHero << sep;
		ss << entry.movementPointsUsed;
		for(auto & resource : resources)
			ss << sep << entry.resources[resource];
		for(auto & resource : resources)
			ss << sep << entry.numMines[resource];
		for(auto & resource : resources)
			ss << sep << entry.spentResourcesForArmy[resource];
		for(auto & resource : resources)
			ss << sep << entry.spentResourcesForBuildings[resource];
		for(auto & resource : resources)
			ss << sep << entry.tradeVolume[resource];
		ss << "\r\n";
	}

	return ss.str();
}

std::string StatisticDataSet::writeCsv()
{
	const boost::filesystem::path outPath = VCMIDirs::get().userCachePath() / "statistic";
	boost::filesystem::create_directories(outPath);

	const boost::filesystem::path filePath = outPath / (vstd::getDateTimeISO8601Basic(std::time(nullptr)) + ".csv");
	std::ofstream file(filePath.c_str());
	std::string csv = toCsv(";");
	file << csv;

	return filePath.string();
}

std::vector<const CGMine *> Statistic::getMines(const CGameState * gs, const PlayerState * ps)
{
	std::vector<const CGMine *> tmp;

	std::vector<const CGObjectInstance *> ownedObjects;
	for(const CGObjectInstance * obj : gs->map->objects)
	{
		if(obj && obj->tempOwner == ps->color)
			ownedObjects.push_back(obj);
	}
	/// This is code from CPlayerSpecificInfoCallback::getMyObjects
	/// I'm really need to find out about callback interface design...

	for(const auto * object : ownedObjects)
	{
		//Mines
		if ( object->ID == Obj::MINE )
		{
			const auto * mine = dynamic_cast<const CGMine *>(object);
			assert(mine);

			tmp.push_back(mine);
		}
	}

	return tmp;
}

//calculates total number of artifacts that belong to given player
int Statistic::getNumberOfArts(const PlayerState * ps)
{
	int ret = 0;
	for(auto h : ps->getHeroes())
	{
		ret += h->artifactsInBackpack.size() + h->artifactsWorn.size();
	}
	return ret;
}

int Statistic::getNumberOfDwellings(const PlayerState * ps)
{
	int ret = 0;
	for(const auto * obj : ps->getOwnedObjects())
		if (!obj->asOwnable()->providedCreatures().empty())
			ret	+= 1;

	return ret;
}

// get total strength of player army
si64 Statistic::getArmyStrength(const PlayerState * ps, bool withTownGarrison)
{
	si64 str = 0;

	for(auto h : ps->getHeroes())
	{
		if(!h->inTownGarrison || withTownGarrison)		//original h3 behavior
			str += h->getArmyStrength();
	}
	return str;
}

// get total experience of all heroes
si64 Statistic::getTotalExperience(const PlayerState * ps)
{
	si64 tmp = 0;

	for(auto h : ps->getHeroes())
		tmp += h->exp;
	
	return tmp;
}

// get total gold income
int Statistic::getIncome(const CGameState * gs, const PlayerState * ps)
{
	int totalIncome = 0;

	//Heroes can produce gold as well - skill, specialty or arts
	for(const auto & h : ps->getHeroes())
		totalIncome += h->dailyIncome()[EGameResID::GOLD];

	//Add town income of all towns
	for(const auto & t : ps->getTowns())
		totalIncome += t->dailyIncome()[EGameResID::GOLD];

	for(const CGMine * mine : getMines(gs, ps))
			totalIncome += mine->dailyIncome()[EGameResID::GOLD];

	return totalIncome;
}

float Statistic::getMapExploredRatio(const CGameState * gs, PlayerColor player)
{
	float visible = 0.0;
	float numTiles = 0.0;

	for(int layer = 0; layer < (gs->map->twoLevel ? 2 : 1); layer++)
		for(int y = 0; y < gs->map->height; ++y)
			for(int x = 0; x < gs->map->width; ++x)
			{
				TerrainTile tile = gs->map->getTile(int3(x, y, layer));

				if(tile.blocked() && !tile.visitable())
					continue;

				if(gs->isVisible(int3(x, y, layer), player))
					visible++;
				numTiles++;
			}
	
	return visible / numTiles;
}

const CGHeroInstance * Statistic::findBestHero(const CGameState * gs, const PlayerColor & color)
{
	const auto &h = gs->players.at(color).getHeroes();
	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<std::pair<PlayerColor, si64>> stats)
{
	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
	std::vector< std::vector<PlayerColor> > ret;
	ret.push_back( { stats[0].first } );

	//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
			ret.push_back( { stats[g].first });
		}
	}

	return ret;
}

int Statistic::getObeliskVisited(const CGameState * gs, const TeamID & t)
{
	if(gs->map->obelisksVisited.count(t))
		return gs->map->obelisksVisited.at(t);
	else
		return 0;
}

float Statistic::getObeliskVisitedRatio(const CGameState * gs, const TeamID & t)
{
	if(!gs->map->obeliskCount)
		return 0;
	return static_cast<float>(getObeliskVisited(gs, t)) / gs->map->obeliskCount;
}

std::map<EGameResID, int> Statistic::getNumMines(const CGameState * gs, const PlayerState * ps)
{
	std::map<EGameResID, int> tmp;

	for(auto & res : EGameResID::ALL_RESOURCES())
		tmp[res] = 0;

	for(const CGMine * mine : getMines(gs, ps))
		tmp[mine->producedResource]++;
	
	return tmp;
}

float Statistic::getTownBuiltRatio(const PlayerState * ps)
{
	float built = 0.0;
	float total = 0.0;

	for(const auto & t : ps->getTowns())
	{
		built += t->getBuildings().size();
		for(const auto & b : t->getTown()->buildings)
			if(!t->forbiddenBuildings.count(b.first))
				total += 1;
	}

	if(total < 1)
		return 0;
	
	return built / total;
}

VCMI_LIB_NAMESPACE_END