-
Notifications
You must be signed in to change notification settings - Fork 0
/
IcFile.class.php
423 lines (324 loc) · 14.3 KB
/
IcFile.class.php
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
<?php
class IcFile
{
const STATE_INIT = 'init';
const STATE_SEATS = 'seats';
const STATE_CARDS = 'cards';
const STATE_FLOP = 'flop';
const STATE_TURN = 'turn';
const STATE_RIVER = 'river';
const STATE_SUMMARY = 'summary';
protected $state_str = array(
self::STATE_INIT => 'Ignition Hand ',
self::STATE_SEATS => 'Seat ',
self::STATE_CARDS => '*** HOLE CARDS ***',
self::STATE_FLOP => '*** FLOP ***',
self::STATE_TURN => '*** TURN ***',
self::STATE_RIVER => '*** RIVER ***',
self::STATE_SUMMARY => '*** SUMMARY ***',
);
protected $state_tree = array(
self::STATE_INIT => array(
self::STATE_SEATS,
),
self::STATE_SEATS => array(
self::STATE_CARDS,
self::STATE_SUMMARY,
),
self::STATE_CARDS => array(
self::STATE_FLOP,
self::STATE_SUMMARY
),
self::STATE_FLOP => array(
self::STATE_TURN,
self::STATE_SUMMARY,
),
self::STATE_TURN => array(
self::STATE_RIVER,
self::STATE_SUMMARY,
),
self::STATE_RIVER => array(
self::STATE_SUMMARY,
),
self::STATE_SUMMARY => array(
self::STATE_INIT,
),
);
const STREET_FLOP = 'flop';
const STREET_TURN = 'turn';
const STREET_RIVER = 'river';
protected $igntion2ps;
// Open file descriptor
protected $fh;
// Game information parsed from file name
protected $info;
// Current line number in file
protected $lineno;
// Current hand overall
protected $handno;
// Hand parser state
protected $state;
// Flag for parser to re-process current line
protected $rerun;
// End-of-file flag for current file
protected $eof;
// Object where parser collects current hand's data, to be then converted
protected $hand;
public function __construct(Ic2Ps $ic2ps)
{
$this->ic2ps = $ic2ps;
$this->lineno = 0;
$this->handno = 0;
$this->state = self::STATE_INIT;
$this->rerun = false;
$this->eof = false;
}
public function getFilename()
{
return $this->ic2ps->getIcHhFilename();
}
public function getFullPath()
{
return $this->ic2ps->getIcHhDir()
. '/' . $this->ic2ps->getIcAccountDir()
. '/' . $this->ic2ps->getIcHhFilename();
}
/**
* Get Ignition hand history file account ID
*/
public function getIcAccountId()
{
return $this->ic2ps->getIcAccountId();
}
public function getPsTableName($table)
{
return $this->ic2ps->getPsTableName($table);
}
public function getPsHandId($hand_id)
{
return $this->ic2ps->getPsHandId($hand_id);
}
public function getSb()
{
return $this->info['sb'];
}
public function getBb()
{
return $this->info['bb'];
}
public function getPsAccountId()
{
return $this->ic2ps->getPsAccountId();
}
public function convertToPsFormat()
{
return $this->hand->convertToPsFormat();
}
/**
* Check if file is an Ignition hand history and open it for reading
*/
public function open()
{
$REGEX_FILENAME = '/^HH(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})-(?<hour>\d{2})(?<minute>\d{2})(?<second>\d{2}) - (?<id>\d+) - (?<format>[A-Z]+) - \$(?<sb>[0-9.]+)-\$(?<bb>[0-9.]+) - (?<game>[A-Z]+) - (?<limit>[A-Z]+) - TBL No.(?<table>\d+)\.txt$/';
if (! preg_match($REGEX_FILENAME, $this->getFilename(), $this->info))
return false; // ignore file
if (($this->fh = @fopen($this->getFullPath(), 'r')) === FALSE)
throw new Exception('Failed to open file ' . $this->getFullPath());
return true;
}
/**
* Update Ignition hand history file parser state
*
* Look for state change lines, check if state can be changed, change state and mark line for
* rerun when needed.
*/
protected function updateState()
{
if (! array_key_exists($this->state, $this->state_tree))
return false;
foreach ($this->state_tree[$this->state] as $state) {
if (strpos($this->line, $this->state_str[$state]) === 0) {
$this->previous_state = $this->state;
$this->state = $state;
if (strlen($this->line) > strlen($this->state_str[$state]))
$this->rerun = true; // There'll be additional info on this line
return true;
}
}
return false;
}
protected function skipIgnoredLine()
{
$REGEX_IGNORE = array(
'/^((?<player>.*) : )?Table enter user$/',
'/^((?<player>.*) : )?Table leave user$/',
'/^((?<player>.*) : )?Seat sit down$/',
'/^((?<player>.*) : )?Seat sit out$/',
'/^((?<player>.*) : )?Seat stand$/',
'/^((?<player>.*) : )?Seat re-join$/',
'/^((?<player>.*) : )?Table deposit (?<chips>\$[0-9.]+)$/',
);
if ($this->eof)
return false;
if ($this->line == '')
return true;
foreach ($REGEX_IGNORE as $regex) {
if (preg_match($regex, $this->line))
return true;
}
return false;
}
/**
* Parse Ignition hand street action lines
*/
function parseAction()
{
$action_regexs = array(
'REGEX_ACTION' => '/^(?<player>.*?)(?<me> \[ME\])? : (?<action>Folds|Checks|Calls|Bets|Raises|All-in|All-in\(raise\)|Hand result(-Side pot)?)( \((auth|timeout|disconnect)\))?( \$(?<chips>[0-9.]+)( to \$(?<to_chips>[0-9.]+))?)?$/',
'REGEX_RETURN' => '/^(?<player>.*?)(?<me> \[ME\])? : (?<action>Return) uncalled portion of bet \$(?<chips>[0-9.]+)$/',
'REGEX_NOSHOW' => '/^(?<player>.*?)(?<me> \[ME\])? : (?<action>Does not show|Mucks|Folds)( & shows)? \[(?<card1>[2-9TJQKA][cdhs]) (?<card2>[2-9TJQKA][cdhs])\]( Show1 \[(?<show1>[2-9TJQKA][cdhs])\])?( \((?<ranking>.*)\))?$/',
'REGEX_SHOWDOWN' => '/^(?<player>.*?)(?<me> \[ME\])? : (?<action>Showdown) \[(?<card1>[2-9TJQKA][cdhs]) (?<card2>[2-9TJQKA][cdhs]) (?<card3>[2-9TJQKA][cdhs]) (?<card4>[2-9TJQKA][cdhs]) (?<card5>[2-9TJQKA][cdhs])\] \((?<ranking>.*)\)$/',
// ignore, not an actual showdown
'REGEX_SHOWDOWN_NOT' => '/^(?<player>.*?)(?<me> \[ME\])? : (?<action>Showdown\(High Card\))$/',
);
$state_property = array(
self::STATE_CARDS => 'preflop',
self::STATE_FLOP => 'flop',
self::STATE_TURN => 'turn',
self::STATE_RIVER => 'river',
);
$prop_name = $state_property[$this->state];
foreach ($action_regexs as $regex) {
if (preg_match($regex, $this->line, $this->hand->{$prop_name}['action'][])) {
return true;
}
}
throw new IcFileException('Unexpected action line');
}
protected function parseStreet($street, $regex)
{
if (empty($this->hand->{$street}['cards'])) {
if (! preg_match($regex, $this->line, $this->hand->{$street}['cards']))
throw new IcFileException(sprintf('Expecting %s line', strtoupper($street)));
return;
}
$this->parseAction();
}
/**
* Parse Ignition hand history file and return next hand converted to Poker Stars format
*/
public function convertNextHand()
{
while ($this->rerun || ($this->line = fgets($this->fh)) || ($this->eof = feof($this->fh))) {
if ($this->eof && $this->state != self::STATE_SUMMARY)
throw new IcFileException('Unexpected end of file');
if ($this->eof || $this->rerun) { // Processing same line after a state change.
$this->rerun = false;
} else {
$this->line = trim($this->line);
$this->lineno++;
if ($this->updateState())
continue;
}
if ($this->skipIgnoredLine())
continue;
switch ($this->state) {
case self::STATE_INIT:
if (! empty($this->previous_state)) {
try {
// Convert complete hand
$this->convertToPsFormat();
$this->previous_state = null;
$this->rerun = true;
return $this->hand;
} catch (Exception $e) {
throw new HandException('%s in hand #%s' . $e->getMessage(), $this->hand->info['id']);
}
}
// EOF in the correct spot
if ($this->eof)
break;
$this->hand = new Hand($this);
$REGEX_INIT = '/^(?<casino>Ignition) Hand #(?<id>\d+) TBL#(?<table>\d+) (?<game>[A-Z]+) (?<limit>[^-]+) - (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}) (?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})$/';
if (! preg_match($REGEX_INIT, $this->line, $this->hand->info))
throw new IcFileException('Expecting init line');
$this->handno++;
$this->handlineno = $this->lineno;
break;
case self::STATE_SEATS:
$REGEX_SEAT = '/^Seat (?<seatno>\d+): (?<player>.*?)(?<me> \[ME\])? \(\$(?<chips>[0-9.]+) in chips\)$/';
if (preg_match($REGEX_SEAT, $this->line, $this->hand->seats[]))
break;
$REGEX_DEALER = '/^((?<player>.*?)(?<me> \[ME\])? : )?Set dealer( \[(?<seatno>\d+)\])?$/';
if (empty($this->hand->dealer) && preg_match($REGEX_DEALER, $this->line, $this->hand->dealer))
break;
$REGEX_SB = '/^(?<player>.*?)(?<me> \[ME\])? : (?<blind>Small Blind) \$(?<chips>[0-9.]+)$/';
if (empty($this->hand->posts['sb']) && preg_match($REGEX_SB, $this->line, $this->hand->posts['sb']))
break;
$REGEX_BB = '/^(?<player>.*?)(?<me> \[ME\])? : (?<blind>Big blind) \$(?<chips>[0-9.]+)$/';
if (empty($this->hand->posts['bb']) && preg_match($REGEX_BB, $this->line, $this->hand->posts['bb']))
break;
$REGEX_SITOUT = '/^(?<player>.*?)(?<me> \[ME\])? : Sitout \(wait for bb\)$/';
if (preg_match($REGEX_SITOUT, $this->line, $this->hand->posts['sitout'][]))
break;
$REGEX_RETURN_PRE = '/^(?<player>.*?)(?<me> \[ME\])? : Return uncalled blind \$(?<chips>[0-9.]+)$/';
if (preg_match($REGEX_RETURN_PRE, $this->line, $this->hand->posts['return'][]))
break;
$REGEX_POST = '/^(?<player>.*?)(?<me> \[ME\])? : Posts( (?<dead>dead))? chip \$(?<chips>[0-9.]+)$/';
if (preg_match($REGEX_POST, $this->line, $this->hand->posts['other'][]))
break;
throw new IcFileException('Unexpected init section line');
case self::STATE_CARDS: // PREFLOP
// Small Blind [ME] : Card dealt to a spot [4c 4d]
$REGEX_DEALT = '/^(?<player>.*?)(?<me> \[ME\])? : Card dealt to a spot \[(?<card1>[2-9TJQKA][cdhs]) (?<card2>[2-9TJQKA][cdhs])\]$/';
if (preg_match($REGEX_DEALT, $this->line, $this->hand->preflop['hole_cards'][]))
break;
$this->parseAction();
break;
case self::STATE_FLOP:
$REGEX_FLOP = '/^\*\*\* FLOP \*\*\* \[([2-9TJQKA][cdhs]) ([2-9TJQKA][cdhs]) ([2-9TJQKA][cdhs])\]$/';
$this->parseStreet(self::STREET_FLOP, $REGEX_FLOP);
break;
case self::STATE_TURN:
$REGEX_TURN = '/^\*\*\* TURN \*\*\* \[[2-9TJQKA][cdhs] [2-9TJQKA][cdhs] [2-9TJQKA][cdhs]\] \[([2-9TJQKA][cdhs])\]$/';
$this->parseStreet(self::STREET_TURN, $REGEX_TURN);
break;
case self::STATE_RIVER:
$REGEX_RIVER = '/^\*\*\* RIVER \*\*\* \[[2-9TJQKA][cdhs] [2-9TJQKA][cdhs] [2-9TJQKA][cdhs] [2-9TJQKA][cdhs]\] \[([2-9TJQKA][cdhs])\]$/';
$this->parseStreet(self::STREET_RIVER, $REGEX_RIVER);
break;
case self::STATE_SUMMARY:
if ($this->previous_state == self::STATE_SEATS) {
// abort mission, this hand ended before cards were dealt
$this->previous_state = null;
$this->state = self::STATE_INIT;
}
// Total Pot($0.90)
$REGEX_TOTAL = '/^Total Pot\(\$(?<chips>[0-9.]+)\)$/';
if (empty($this->hand->summary['pot']) && preg_match($REGEX_TOTAL, $this->line, $this->hand->summary['pot']))
break;
// Board [8d 4h Ah Th 6s]
$REGEX_BOARD = '/^Board \[([2-9TJQKA][cdhs]) ([2-9TJQKA][cdhs]) ([2-9TJQKA][cdhs]) ([2-9TJQKA][cdhs])? ([2-9TJQKA][cdhs])?\]$/';
if (empty($this->hand->summary['board']) && preg_match($REGEX_BOARD, $this->line, $this->hand->summary['board']))
break;
// Seat+1: Dealer Folded before the FLOP
$REGEX_FOLDED = '/^Seat\+(?<seatno>\d+): (?<player>.*?) (?<action_full>(?<action>Folded) (?<street>(on|before) the (FLOP|TURN|RIVER)))$/';
if (preg_match($REGEX_FOLDED, $this->line, $this->hand->summary['seats'][]))
break;
// Seat+4: Small Blind Mucked [2d 3s]
$REGEX_MUCKED = '/^Seat\+(?<seatno>\d+): (?<player>.*?) \[(?<action>Mucked)\] \[(?<card1>[2-9TJQKA][cdhs]) (?<card2>[2-9TJQKA][cdhs])\]$/';
if (preg_match($REGEX_MUCKED, $this->line, $this->hand->summary['seats'][]))
break;
// Seat+5: Big Blind $0.86 [Does not show]
$REGEX_WON = '/^Seat\+(?<seatno>\d+): (?<player>.*?) \$(?<chips>[0-9.]+) (\[Does not show\]| with (?<ranking>.*) \[(?<hand>[^[]*)\])$/';
if (preg_match($REGEX_WON, $this->line, $this->hand->summary['seats'][]))
break;
break;
}
if ($this->eof) {
fclose($this->fh);
break;
}
}
}
}