-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.cpp
More file actions
672 lines (576 loc) · 13.6 KB
/
Copy pathgame.cpp
File metadata and controls
672 lines (576 loc) · 13.6 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#include <cmath>
#include <set>
#include "blitzmap.h"
#include "cplusplus.h"
#include "game.h"
#include "logging.h"
#include "stringtools.h"
/*!
*/
Game::Game(uint32_t id, const std::string &map, uint32_t timestamp, uint32_t fps, uint32_t duration) :
_id(id),
_mapName(map),
_timestamp(timestamp),
_fps(fps),
_seconds(duration)
{
// Nothing to do.
}
/*!
*/
uint32_t Game::id() const
{
return _id;
}
/*!
*/
uint32_t Game::playerCount() const
{
return static_cast<uint32_t>(_participants.size());
}
/*!
*/
void Game::determineWinner()
{
int result = 0;
for (Participant &participant : _participants)
{
result += participant.hasWon ? 1 : -1;
}
if (result == 0)
{
// Number of winners matches number of losers.
return;
}
result = 0;
// Try again based on points.
for (Participant &participant : _participants)
{
result += (participant.points > 0) ? 1 : -1;
participant.hasWon = participant.points > 0;
Log::info() << " Trying to determine winner. Player '" << participant.playerName << "' got " << participant.points << " points.";
}
Log::warning(result != 0) << "Unable to determine winners in game " << _id << ". This game will probably be invalid.";
}
/*!
*/
bool Game::isBot() const
{
uint32_t bot = dts::to_underlying(KnownPlayers::BlitzBot);
return std::find_if(_participants.begin(), _participants.end(),
[bot](const Participant& p) { return p.userId == bot; }) != _participants.end();
}
/*!
*/
bool Game::isVs(uint32_t player1, uint32_t player2) const
{
if (_participants.size() != 2)
{
Log::warning() << "Not a 1v1 game.";
return false;
}
return (_participants[0].userId == player1 && _participants[1].userId == player2)
|| (_participants[0].userId == player2 && _participants[1].userId == player1);
}
/*!
*/
bool Game::isVs(KnownPlayers player1, KnownPlayers player2) const
{
return isVs(dts::to_underlying(player1), dts::to_underlying(player2));
}
/*!
*/
void Game::addPlayer(
uint32_t index,
const std::string &playerName,
factions::Faction faction,
bool hasWon,
int points,
double elo,
double deviation)
{
_participants.emplace_back(Participant{index, playerName, faction, hasWon, points, elo, deviation});
}
/*!
*/
uint32_t Game::userId(uint32_t index) const
{
if (index >= static_cast<uint32_t>(_participants.size()))
{
Log::error() << "Asking for player " << index << ", but maximum index is " << (_participants.size() - 1) << ".";
return 0;
}
return _participants[index].userId;
}
/*!
*/
bool Game::hasWon(uint32_t index) const
{
if (index > playerCount())
{
Log::warning() << "Player index " << index << " out of range in game " << _id << ".";
return false;
}
return _participants[index].hasWon;
}
/*!
*/
std::string Game::playerName(uint32_t index) const
{
if (index >= static_cast<uint32_t>(_participants.size()))
{
Log::error() << "Player index " << index << " out of range in game " << _id << " while asking for name.";
return "";
}
return _participants[index].playerName;
}
/*!
*/
void Game::setTimestamp(uint32_t timestamp)
{
_timestamp = timestamp;
}
/*!
*/
uint32_t Game::timestamp() const
{
return _timestamp;
}
/*!
*/
void Game::setLadderAbbreviation(const std::string &ladderAbbreviation)
{
_ladderAbbreviation = ladderAbbreviation;
}
/*!
*/
std::string Game::ladderAbbreviation() const
{
return _ladderAbbreviation;
}
/*!
*/
void Game::setPlayer(uint32_t index, uint32_t userId)
{
if (index >= _participants.size())
{
Log::error() << "Cannot set player with index " << index << " in game " << _id
<< ", because the game has only " << (_participants.size() + 1) << " players.";
return;
}
_participants[index].userId = userId;
}
/*!
*/
std::chrono::year_month_day Game::date() const
{
std::chrono::sys_seconds tp_since_epoch{std::chrono::seconds{_timestamp}};
std::chrono::sys_days days = std::chrono::floor<std::chrono::days>(tp_since_epoch);
return std::chrono::year_month_day{days};
}
/*!
*/
std::chrono::sys_days Game::sysDate() const
{
using namespace std::chrono;
auto date = this->date();
return sys_days{ year{ date.year() } / month{ date.month() } / day{ date.day() } };
}
/*!
*/
/*
std::tuple<std::chrono::year_month_day, std::chrono::hh_mm_ss<std::chrono::seconds>> Game::dateTime() const
{
using namespace std::chrono;
sys_seconds tp_since_epoch{seconds{_timestamp}};
sys_days days = floor<days>(tp_since_epoch);
year_month_day ymd{days};
hh_mm_ss hms{tp_since_epoch - days};
return {ymd, hms};
}*/
/*!
*/
void Game::setDuration(uint32_t seconds)
{
_seconds = seconds;
}
/*!
*/
uint32_t Game::duration() const
{
return _seconds;
}
/*!
*/
void Game::setFps(uint32_t fps)
{
_fps = fps;
}
/*!
*/
uint32_t Game::fps() const
{
return _fps;
}
/*!
*/
factions::Faction Game::faction(uint32_t index) const
{
if (index >= static_cast<uint32_t>(_participants.size()))
{
Log::error() << "Faction index of " << index << " is out of game for game " << _id << ".";
return factions::UnknownFaction;
}
return _participants[index].faction;
}
/*!
*/
void Game::setMap(uint32_t mapIndex)
{
_map = mapIndex;
}
/*!
*/
uint32_t Game::map() const
{
return _map;
}
/*!
*/
void Game::setGameType(gametypes::GameType gameType)
{
_gameType = gameType;
}
/*!
*/
gametypes::GameType Game::gameType() const
{
return _gameType;
}
void Game::setMapName(const std::string &mapName)
{
_mapName = mapName;
// Need to set the map index as well.
int mapIndex = blitzmap::toIndex(mapName);
if (mapIndex < 0)
{
// TODO:
// std::stringstream ss;
// ss << "Unknown map '" << mapName.toStdString() << "' passed to Game::setMapName.";
// throw std::runtime_error(ss.str());
}
_map = static_cast<uint32_t>(mapIndex);
}
/*!
*/
std::string Game::mapName() const
{
return _mapName;
}
/*!
*/
bool Game::isValid() const
{
if (_participants.size() == 4)
{
std::set<uint32_t> userIds;
int result = 0;
for (size_t i = 0; i < _participants.size(); i++)
{
if (_participants[i].userId == 0)
{
Log::info() << "Unable to resolve all player of game " << _id << ". This game is invalid.";
return false;
}
result += _participants[i].hasWon ? 1 : -1;
userIds.insert(_participants[i].userId);
}
if (result != 0)
{
Log::warning() << "Winning and losing players of game " << _id << " do not line up.";
return false;
}
if (isDraw())
{
Log::warning() << "There is no draw in 2v2 games. Game " << _id << " is invalid.";
return false;
}
if (userIds.size() != 4)
{
Log::info() << "Participants of game " << _id << " are duplicates. This game is invalid.";
return false;
}
return _id != 0 && _timestamp != 0;
}
else if (_participants.size() == 2)
{
if (_participants[0].userId == _participants[1].userId)
{
Log::info() << "Game " << _id << " is between duplicates. This game is invalid.";
return false;
}
bool isValid = (
_id != 0 &&
_timestamp != 0 &&
_participants[0].userId != 0 &&
_participants[1].userId != 0 &&
(_isDraw || ((_participants[0].hasWon && !_participants[1].hasWon) || (!_participants[0].hasWon && _participants[1].hasWon))));
return isValid;
}
return false;
}
/*!
*/
void Game::setWasDisconnected(bool disconnected)
{
_wasDisconnected = disconnected;
}
/*!
*/
bool Game::wasDisconnected() const
{
return _wasDisconnected;
}
/*!
*/
double Game::differenceForGreatestDefeat() const
{
if (_isDraw)
{
return 0.0;
}
double winnerElo = 0.0;
double loserElo = 0.0;
for (const Participant &participant : _participants)
{
if (participant.hasWon)
{
winnerElo += (participant.elo + participant.deviation);
}
else
{
loserElo += (participant.elo - participant.deviation);
}
}
return loserElo - winnerElo;
}
/*!
*/
bool Game::isUnderdogWin() const
{
if (_participants.size() != 2)
{
Log::error() << "Underdog win is only viable for a 1v1 game.";
return 0.0;
}
if (_isDraw)
{
return false;
}
else
{
return (_participants[0].hasWon && _participants[0].elo < _participants[1].elo)
|| (_participants[1].hasWon && _participants[1].elo < _participants[0].elo);
}
}
bool Game::hasValidResult() const
{
if (_isDraw)
{
return true;
}
// Number of winning and losing players need to be equal in order
// to have a valid game result.
int result = 0;
for (size_t i = 0; i < _participants.size(); i++)
{
result += _participants[i].hasWon ? 1 : -1;
}
return (result == 0);
}
/*!
*/
std::string Game::factionResult(bool winnerFirst) const
{
if (_participants.size() != 2)
{
Log::error() << "Faction result is only viable for a 1v1 game.";
return "";
}
std::string faction1 = factions::letter(_participants[0].faction);
std::string faction2 = factions::letter(_participants[1].faction);
if (_participants[0].hasWon)
{
faction1 = stringtools::toUpper(faction1);
}
else if (_participants[1].hasWon)
{
faction2 = stringtools::toUpper(faction2);
}
if (winnerFirst)
{
if (_participants[0].hasWon)
{
return faction1 + "v" + faction2;
}
else
{
return faction2 + "v" + faction1;
}
}
return faction1 + "v" + faction2;
}
/*!
*/
void Game::setRatingAndDeviation(uint32_t playerIndex, double rating, double deviation)
{
if (playerIndex >= playerCount())
{
Log::error() << "Player index " << playerIndex << " is out of bounds.";
}
else
{
_participants[playerIndex].elo = rating;
_participants[playerIndex].deviation = deviation;
}
}
/*!
*/
double Game::rating(uint32_t index) const
{
if (index >= static_cast<uint32_t>(_participants.size()))
{
Log::error() << "Index " << index << " is not within [0, " << (_participants.size() - 1) << "] while getting the players rating of a game.";
}
return _participants[index].elo;
}
/*!
*/
double Game::ratingDifference() const
{
if (_participants.size() != 2)
{
Log::error() << "Rating difference is only viable for a 1v1 game.";
return 0.0;
}
return std::abs(_participants[0].elo - _participants[1].elo);
}
/*!
*/
double Game::deviation(uint32_t index) const
{
if (index >= static_cast<uint32_t>(_participants.size()))
{
Log::error() << "Index " << index << " is not within [0, " << (_participants.size() - 1) << "] while getting the players deviation of a game.";
}
return _participants[index].deviation;
}
/*!
*/
int Game::winnerIndex() const
{
if (_participants.size() != 2)
{
Log::error() << "Only 1v1 games have a winner index.";
return -1;
}
else if (_isDraw)
{
Log::warning() << "Asking for winner index in a drawn game.";
return -1;
}
if (_participants[0].hasWon)
{
return 0;
}
else if (_participants[1].hasWon)
{
return 1;
}
else
{
Log::warning() << "Unable to determine winner in game " << _id << ".";
return -1;
}
}
int Game::playerIndex(uint32_t playerId) const
{
for (size_t i = 0; i < _participants.size(); i++)
{
if (_participants[i].userId == playerId)
{
return i;
}
}
return -1;
}
/*!
*/
bool Game::isDraw() const
{
return _isDraw;
}
/*!
*/
void Game::setIsDraw(bool isDraw)
{
_isDraw = isDraw;
}
/*!
*/
factions::Setup Game::setup() const
{
if (_participants.size() != 2)
{
Log::error() << "Factions setup is only viable for a 1v1 game.";
return factions::Setup::UnknownSetup;
}
return factions::fromFactions(_participants[0].faction, _participants[1].faction);
}
/*!
*/
uint32_t Game::mateIndex(uint32_t index) const
{
if (_participants.size() != 4)
{
Log::error() << "Only 2v2 game has a mate.";
return index;
}
for (size_t i = 0; i < _participants.size(); i++)
{
if (i != index && _participants[i].hasWon == _participants[index].hasWon)
{
return i;
}
}
Log::error() << "Mate not found in game " << _id << ".";
return index;
}
std::pair<uint32_t, uint32_t> Game::opponentsIndices(uint32_t index) const
{
if (playerCount() != 4)
{
Log::error() << "Opponents index only viable for a 2v2 game.";
return {index, index};
}
bool isFirst = true;
std::pair<uint32_t, uint32_t> result;
for (size_t i = 0; i < _participants.size(); i++)
{
if (_participants[i].hasWon != _participants[index].hasWon)
{
if (isFirst)
{
result.first = i;
isFirst = false;
}
else
{
result.second = i;
return result;
}
}
}
Log::error() << "Opponents not found in game " << _id << ". This does not seems to be a valid 2v2 game.";
return {index, index};
}