-
Notifications
You must be signed in to change notification settings - Fork 14
/
statistics.cpp
462 lines (407 loc) · 13.8 KB
/
statistics.cpp
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
/*
OOPoker
Copyright (c) 2010 Lode Vandevenne
All rights reserved.
This file is part of OOPoker.
OOPoker is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OOPoker is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OOPoker. If not, see <http://www.gnu.org/licenses/>.
*/
#include "statistics.h"
#include <sstream>
double PlayerStats::getVPIP () const
{
/*
VP$IP Percentage: Voluntary Put Money In Pot: number of deals
where the player put money in the pot pre-flop. Voluntary means
that if the player is big blind and could check, it does NOT count
towards this percentage. In other words, this is the amount of times
the player chose to pay to see the flop. If the player first voluntary
put money in the pot, but later folded due to a raise, his first
raise is still added to this statistic. If he called the raise, this
is NOT counted double towards this statistic, only the first time he
put money in the pot counts.
This statistic indicates players tightness.
This is normally a percentage, but is given as a value in the range 0.0 - 1.0 (where 1.0 means 100%)
Tight: Less than 0.24
Neutral: Between 0.24-0.3
Loose: Greater than 0.3
*/
return (double)(deal_preflop_calls + deal_preflop_bets + deal_preflop_raises) / (double)deals;
}
double PlayerStats::getPFR() const
{
//Pre-Flop Raise percentage
return (double)(deal_preflop_bets + deal_preflop_raises) / (double)deals;
}
double PlayerStats::get3BetPF() const
{
return (double)(deal_preflop_raises) / (double)deals;
}
double PlayerStats::getWSD() const
{
//low means the player plays solid. High means the player overplays his cards.
return (double)showdowns_seen / (double)flops_seen;
}
double PlayerStats::getWSDW() const
{
return (double)wins_showdown / (double)showdowns_seen;
}
double PlayerStats::getAF() const
{
/*
Aggression factor:
Passive: Less than 1.5
Neutral: Between 1-1.5
Aggressive: Greater than 1.5
Measured only after the flop, so all preflop actions are subtracted.
*/
return (double)((bets - preflop_bets) + (raises - preflop_raises)) / (double)(calls - preflop_calls);
}
std::string statisticsToString(const PlayerStats& stats)
{
std::stringstream ss;
ss << std::endl << "Player Stats for "<< stats.name << ", AI: " << stats.ai << std::endl;
ss << "General: " << "deals: " << stats.deals << ", actions: " << stats.actions << ", preflop actions: " << stats.preflop_actions << std::endl;
ss << "Rounds Seen: " << "flops: " << stats.flops_seen << ", turns: " << stats.turns_seen << ", rivers: " << stats.rivers_seen << ", showdowns: " << stats.showdowns_seen << std::endl;
ss << "Wins: " << "total: " << stats.wins_total << ", showdown: " << stats.wins_showdown << ", bluff: " << stats.wins_bluff << std::endl;
ss << "Chips: " << "won: " << stats.chips_won << ", lost: " << stats.chips_lost << ", bought: " << stats.chips_bought << ", forced bets: " << stats.forced_bets << std::endl;
//ss << "Actions: [Fold, Check, Call, Bet, Raise, All-In]" << std::endl;
//ss << "Total: " << stats.folds << " " << stats.checks << " " << stats.calls << " " << stats.bets << " " << stats.raises << " " << stats.allins << std::endl;
//ss << "Pre-flop: " << stats.preflop_folds << " " << stats.preflop_checks << " " << stats.preflop_calls << " " << stats.preflop_bets << " " << stats.preflop_raises << " " << stats.preflop_allins << std::endl;
//ss << "Flop: " << stats.flop_folds << " " << stats.flop_checks << " " << stats.flop_calls << " " << stats.flop_bets << " " << stats.flop_raises << " " << stats.flop_allins << std::endl;
//ss << "Turn: " << stats.turn_folds << " " << stats.turn_checks << " " << stats.turn_calls << " " << stats.turn_bets << " " << stats.turn_raises << " " << stats.turn_allins << std::endl;
//ss << "River: " << stats.river_folds << " " << stats.river_checks << " " << stats.river_calls << " " << stats.river_bets << " " << stats.river_raises << " " << stats.river_allins << std::endl;
ss << "Pre-Flop Stats: " << "VP$IP: " << stats.getVPIP() << ", PFR: " << stats.getPFR() << ", 3Bet: " << stats.get3BetPF() << std::endl;
ss << "Post-Flop Stats: " << "AF: " << stats.getAF() << ", WSD: " << stats.getWSD() << ", WSDW: " << stats.getWSDW() << std::endl;
return ss.str();
}
std::string statisticsToString(const StatKeeper& statKeeper)
{
std::string result;
std::vector<std::string> players;
statKeeper.getAllPlayers(players);
for(size_t i = 0; i < players.size(); i++)
{
const PlayerStats * stats = statKeeper.getPlayerStats(players[i]);
result += statisticsToString(*stats);
result += "\n";
}
return result;
}
PlayerStats::PlayerStats(const std::string& name)
{
//set everything automatically to 0
size_t start_mem = sizeof(std::string) * 2;
char* c = (char*)(this) + start_mem;
for(size_t i = 0; i < sizeof(PlayerStats) - start_mem; i++) c[i] = 0;
this->name = name;
this->ai = "";
}
TableStats::TableStats()
{
//set everything automatically to 0
char* c = (char*)this;
for(size_t i = 0; i < sizeof(TableStats); i++) c[i] = 0;
}
StatKeeper::MyPlayerInfo::MyPlayerInfo(const std::string& name)
: stats(name)
, stack(0)
, wager(0)
{
}
StatKeeper::StatKeeper()
: round(R_PRE_FLOP)
{
}
StatKeeper::~StatKeeper()
{
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
delete it->second;
}
}
void StatKeeper::getAllPlayers(std::vector<std::string>& players) const
{
for(std::map<std::string, MyPlayerInfo*>::const_iterator it = statmap.begin(); it != statmap.end(); ++it)
{
players.push_back(it->first);
}
}
StatKeeper::MyPlayerInfo* StatKeeper::getPlayerStatsInternal(const std::string& player)
{
if(statmap.find(player) == statmap.end())
{
statmap[player] = new MyPlayerInfo(player);
}
return statmap[player];
}
void StatKeeper::onEvent(const Event& event)
{
MyPlayerInfo* info = 0;
if(!event.player.empty()) info = getPlayerStatsInternal(event.player);
PlayerStats* stats = &info->stats;
int* round_folds = 0;
int* round_checks = 0;
int* round_calls = 0;
int* round_bets = 0;
int* round_raises = 0;
int* round_allins = 0;
int* round_actions = 0;
if(round == R_PRE_FLOP)
{
round_folds = &stats->preflop_folds;
round_checks = &stats->preflop_checks;
round_calls = &stats->preflop_calls;
round_bets = &stats->preflop_bets;
round_raises = &stats->preflop_raises;
round_allins = &stats->preflop_allins;
round_actions = &stats->preflop_actions;
}
else if(round == R_FLOP)
{
round_folds = &stats->flop_folds;
round_checks = &stats->flop_checks;
round_calls = &stats->flop_calls;
round_bets = &stats->flop_bets;
round_raises = &stats->flop_raises;
round_allins = &stats->flop_allins;
round_actions = &stats->flop_actions;
}
else if(round == R_TURN)
{
round_folds = &stats->turn_folds;
round_checks = &stats->turn_checks;
round_calls = &stats->turn_calls;
round_bets = &stats->turn_bets;
round_raises = &stats->turn_raises;
round_allins = &stats->turn_allins;
round_actions = &stats->turn_actions;
}
else if(round == R_RIVER)
{
round_folds = &stats->river_folds;
round_checks = &stats->river_checks;
round_calls = &stats->river_calls;
round_bets = &stats->river_bets;
round_raises = &stats->river_raises;
round_allins = &stats->river_allins;
round_actions = &stats->river_actions;
}
int numchips_placed = 0; //used for detecting all-in
switch(event.type)
{
case E_QUIT:
{
info->joined = false;
break;
}
case E_JOIN:
{
info->joined = true;
stats->chips_bought += event.chips;
info->stack += event.chips;
break;
}
case E_REBUY:
{
stats->chips_bought += event.chips;
info->stack += event.chips;
break;
}
case E_NEW_DEAL:
{
round = R_PRE_FLOP;
highestBet = 0;
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
p->wager = 0;
p->folded = false;
p->deal_stat = 0;
p->deal_preflop_stat = 0;
if(p->joined)
{
p->stats.deals++;
}
}
break;
}
case E_POT_DIVISION:
{
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
if(p->joined)
{
int d = p->deal_stat;
if(d == 0) p->stats.deal_first_action_folds++;
else if(d == 1) p->stats.deal_checks++;
else if(d == 2) p->stats.deal_calls++;
else if(d == 3) p->stats.deal_bets++;
else if(d == 4) p->stats.deal_raises++;
d = p->deal_preflop_stat;
if(d == 0) p->stats.deal_preflop_first_action_folds++;
else if(d == 1) p->stats.deal_preflop_checks++;
else if(d == 2) p->stats.deal_preflop_calls++;
else if(d == 3) p->stats.deal_preflop_bets++;
else if(d == 4) p->stats.deal_preflop_raises++;
}
}
break;
}
case E_REVEAL_AI: stats->ai = event.ai; break; //this is the only event we can finally read the ai from!
case E_SMALL_BLIND:
{
stats->forced_bets += event.chips;
highestBet = event.chips;
numchips_placed = event.chips;
break;
}
case E_BIG_BLIND:
{
stats->forced_bets += event.chips;
if(event.chips > highestBet) highestBet = event.chips; //it could be that the player is all-in and has less chips
numchips_placed = event.chips;
break;
}
case E_ANTE:
{
stats->forced_bets += event.chips;
if(event.chips > highestBet) highestBet = event.chips; //happens e.g. if ante is bigger than the stack of the small blind and the big blind when they're both all-in
numchips_placed = event.chips;
break;
}
case E_FLOP:
{
round = R_FLOP;
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
if(!p->folded) p->stats.flops_seen++;
}
break;
}
case E_TURN:
{
round = R_TURN;
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
if(!p->folded) p->stats.turns_seen++;
}
break;
}
case E_RIVER:
{
round = R_RIVER;
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
if(!p->folded) p->stats.rivers_seen++;
}
break;
}
case E_SHOWDOWN:
{
round = R_SHOWDOWN;
for(std::map<std::string, MyPlayerInfo*>::iterator it = statmap.begin(); it != statmap.end(); ++it)
{
MyPlayerInfo* p = it->second;
if(!p->folded) p->stats.showdowns_seen++;
}
break;
}
case E_FOLD:
{
info->folded = true;
stats->folds++;
(*round_folds)++;
stats->actions++;
(*round_actions)++;
break;
};
case E_CHECK:
{
stats->checks++;
(*round_checks)++;
stats->actions++;
(*round_actions)++;
if(info->deal_stat < 1) info->deal_stat = 1;
if(round == R_PRE_FLOP && info->deal_preflop_stat < 1) info->deal_preflop_stat = 1;
break;
};
case E_CALL:
{
stats->calls++;
(*round_calls)++;
stats->actions++;
(*round_actions)++;
if(info->deal_stat < 2) info->deal_stat = 2;
if(round == R_PRE_FLOP && info->deal_preflop_stat < 2) info->deal_preflop_stat = 2;
numchips_placed = highestBet - info->wager;
break;
}
case E_RAISE:
{
int callAmount = highestBet - info->wager;
if(callAmount == 0) //bet
{
stats->bets++;
(*round_bets)++;
if(info->deal_stat < 3) info->deal_stat = 3;
if(round == R_PRE_FLOP && info->deal_preflop_stat < 3) info->deal_preflop_stat = 3;
}
else //raise
{
stats->raises++;
(*round_raises)++;
if(info->deal_stat < 4) info->deal_stat = 4;
if(round == R_PRE_FLOP && info->deal_preflop_stat < 4) info->deal_preflop_stat = 4;
}
stats->actions++;
(*round_actions)++;
numchips_placed = callAmount + event.chips; //since the event only contains the raise amount, we need to add the call amount to get the total amount of chips the player moved to the table
highestBet += event.chips;
break;
}
case E_WIN:
{
stats->chips_won += event.chips;
info->stack += event.chips;
stats->wins_total++;
if(round == R_SHOWDOWN) stats->wins_showdown++;
else stats->wins_bluff++;
}
default: break;
}
if(numchips_placed > 0)
{
stats->chips_lost += numchips_placed;
info->stack -= numchips_placed;
info->wager += numchips_placed;
bool allin = info->stack <= 0;
if(allin)
{
stats->allins++;
(*round_allins)++;
}
}
}
const PlayerStats* StatKeeper::getPlayerStats(const std::string& player) const
{
std::map<std::string, MyPlayerInfo*>::const_iterator it = statmap.find(player);
if(it == statmap.end()) return 0;
else return &statmap.find(player)->second->stats;
}
const TableStats* StatKeeper::getTableStats() const
{
return &tableStats;
}