-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
479 lines (386 loc) · 14.4 KB
/
parser.js
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
var _ = require('underscore');
var parsedHand;
var bigBlindName;
var Player = function(name, chips){
this.name = name;
// force it to string so it has a return pattern, regardless if chips is cash, indicated by $ or just chips
this.chips = chips + "";
this.position = "";
this.hand = [];
this.invested = 0;
};
var TABLEMAP = ['BB', 'SB', 'BTN', 'CO', 'HJ', 'MP+1', 'MP', 'UTG+1', 'UTG'];
// poorly tested
var mapPlayers = function(players, bigBlindName){
var i = 0;
while(players[i].name !== bigBlindName){
i++;
}
for(var j = 0; j < players.length; j++){
// send i to the end of the array
if(i < 0) i = players.length-1;
players[i].position = TABLEMAP[j];
i--;
}
};
var createParsedHand = function(){
// object with all the relevant info to be displayed
var parsedHand = {};
parsedHand.platforms = parsedHand.gameStyle = parsedHand.language = parsedHand.turn = parsedHand.river = parsedHand.pot = parsedHand.ante = parsedHand.blinds = "";
parsedHand.flopPot = parsedHand.turnPot = parsedHand.riverPot = 0
// todo: refactor players to use objects called players inside the array. object should contain name, chips properties and hand if available
parsedHand.players = [];
parsedHand.ownHand = [];
parsedHand.preFlopActions = [];
parsedHand.table = [];
parsedHand.flop = [];
parsedHand.flopActions = [];
parsedHand.turnActions = [];
parsedHand.riverActions = [];
parsedHand.hero = {};
return parsedHand;
};
var checkRunTwice = function(parsedHand, handHistory){
// indexof is faster
// regex to search for a hand was run twice on the whole text
var runTwiceRegex = /Hand was run twice/ig;
parsedHand.runTwice = runTwiceRegex.test(handHistory);
// in case its a run it twice
if(parsedHand.runTwice){
parsedHand.secondFlop = [];
parsedHand.secondTurn = "";
parsedHand.secondRiver = "";
}
};
// not being tested
var getPot = function(parsedHand ,handHistory){
// var potRegex = /.+collected \$?\w+(\.\w\w)? from (side |main )?pot/g;
var potRegex = /Total pot \$?\d+(\.\d\d)? \| Rake \$?\d(\.\d\d)?/ig
var match = handHistory.match(potRegex)
// not tested
if(!match) return null;
parsedHand.pot = match[0] +".";
};
var prepareHandHistory = function(handHistory){
// not being tested
if(handHistory.indexOf("\n") === -1) return null;
//not being tested
var bigBlindRegex = /(.+)\: posts big blind/g
bigBlindName = bigBlindRegex.exec(handHistory)[1];
handHistory = handHistory.split("\n");
// not being tested
if(handHistory[0].indexOf("file") !== -1 || handHistory[0].indexOf("File") !== -1){
handHistory.shift();
}
return handHistory;
};
var getHandIdentifier = function(handHistory){
return handHistory[0];
};
// accepting only hold'em and no limit
var getHandStyle = function(handIdentifier){
if(handIdentifier.indexOf("Hold'em No Limit") !== -1){
return "Hold'em No Limit";
} else if(handIdentifier.indexOf("Omaha Pot Limit") !== -1){
return "Omaha Pot Limit";
} else {
return false;
}
};
// not tested
var getBlinds = function(handIdentifier){
var blindsRegex = /\((.+)\)/;
parsedHand.blinds = blindsRegex.exec(handIdentifier)[1];
};
var config = function(handHistory){
parsedHand = createParsedHand();
checkRunTwice(parsedHand, handHistory);
getPot(parsedHand, handHistory);
return prepareHandHistory(handHistory);
};
// not tested
var findHero = function(players, text){
var hero = text.substring(9, text.length-1);
for(var i = 0; i < players.length; i++){
if(players[i].name === hero){
parsedHand.hero = players[i];
break;
}
}
};
// go throgh all actions, and find out the value of the pot
var updatePot = function(players, actions, pot){
// todo: refactor handle functions into a single function changing regex depending on a argument with the action
var handlePosts = function(text){
var postsRegex = /blind \$?(\d+\.?\d?\d?)/;
var textvalue = postsRegex.exec(text)[1];
return parseFloat(textvalue, 10);
};
var handleCalls = function(text){
var callsRegex = /calls \$?(\d+\.?\d?\d?)/;
var textvalue = callsRegex.exec(text)[1];
return parseFloat(textvalue, 10);
};
var handleRaises = function(text){
var raisesRegex = /\d to \$?(\d+\.?\d?\d?)/;
var textvalue = raisesRegex.exec(text)[1];
return parseFloat(textvalue, 10);
};
var handleBets = function(text){
var betsRegex = /bets \$?(\d+\.?\d?\d?)/;
var textvalue = betsRegex.exec(text)[1];
return parseFloat(textvalue, 10);
};
// sort by the name of the player, not loosing original order of actions from same player
var orderedActions = _.sortBy(actions, function(action){
return action.split(": ")[0];
});
var currentPlayer = orderedActions[0].split(": ")[0];
var currentSum = 0;
_.forEach(orderedActions, function(val){
if(val.indexOf('folds') !== -1) return null;
if(val.split(": ")[0] !== currentPlayer){
pot += currentSum;
// todo: update the player with the ammount invested in the pot so far
currentPlayer = val.split(": ")[0];
currentSum = 0;
}
if(val.indexOf('posts') !== -1) {
currentSum += handlePosts(val);
} else if(val.indexOf('calls') !== -1) {
currentSum += handleCalls(val);
} else if(val.indexOf('raises') !== -1) {
// not += because its easier to just get the final value its raised to
currentSum = handleRaises(val);
} else if(val.indexOf('bets') !== -1){
currentSum += handleBets(val);
}
});
pot += currentSum;
return pot;
};
// TODO: make it oop
// TODO: Ignore 'uncalled bet' and 'collected from pot' on actions
// TODO: make tests for errors
var handParser = function(handHistory){
handHistory = config(handHistory);
var partialPot = 0;
// if handHistory is not a valid input, return null; not being tested
// TODO: better error feedback
if(!handHistory) return null;
var handIdentifier = getHandIdentifier(handHistory);
// TODO: prepare code for other platforms
if(handIdentifier.indexOf('PokerStars') !== -1){
parsedHand.platforms = "PS"
} else {
// if platform is not identified, not being tested
return null;
}
getBlinds(handIdentifier);
// TODO: prepare for different styles
// need to improve this part
parsedHand.gameStyle = getHandStyle(handIdentifier);
// TODO: need to test for a input of an unidentified game style
if(!parsedHand.gameStyle) return null;
parsedHand.language = handHistory[1].indexOf("Hand") !== -1 ? null : 'en';
var row = 2;
if(parsedHand.language === 'en'){
//prevent rebuys from getting in the way
while( handHistory[row].indexOf('re-buys and receives') !== -1 ){
row++;
}
var player;
var chips;
while( handHistory[row].indexOf('Seat ') === 0 ){
// TODO: Get chips with a regex the same regex used to get the player
var endOfNumber = handHistory[row].indexOf(' in chips');
var numIndex = endOfNumber;
while( handHistory[row][numIndex] !== '('){
numIndex--;
}
chips = handHistory[row].substring(numIndex +1, endOfNumber);
var playerRegex = /Seat \w\: (.+) \(\$?\w+/;
playerName = playerRegex.exec(handHistory[row])[1];
player = new Player(playerName, chips);
parsedHand.players.push(player);
row++;
}
mapPlayers(parsedHand.players, bigBlindName);
//advance to the hand row, needs to save actions
while( handHistory[row].indexOf(" ") !== 0 && handHistory[row].indexOf("*** ") !== 0){
// ignores ante
if( handHistory[row].indexOf("posts the ante") === -1){
parsedHand.preFlopActions.push(handHistory[row]);
} else if(!parsedHand.ante) {
var anteRegex = /posts the ante (\w+)/i;
parsedHand.ante = anteRegex.exec(handHistory[row])[1];
partialPot = parseInt(parsedHand.ante, 10) * parsedHand.players.length;
}
row++;
}
// treating for when no hand is initially known
if(handHistory[row+1].indexOf('Dealt to') === 0){
var ownHand = handHistory[++row].split('[');
findHero(parsedHand.players, ownHand[0]);
if(parsedHand.gameStyle == 'Omaha Pot Limit'){
parsedHand.ownHand = ownHand[ownHand.length-1].substring(0, 11).split(" ");
} else {
parsedHand.ownHand = ownHand[ownHand.length-1].substring(0, 5).split(" ");
}
}
row++;
//advance to the next event
while( handHistory[row].indexOf(" ") !== 0 && handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.preFlopActions.push(handHistory[row]);
row++;
}
// todo: test this
parsedHand.flopPot = updatePot(parsedHand.players, parsedHand.preFlopActions, partialPot)
parsedHand.riverPot = parsedHand.turnPot = parsedHand.flopPot
//check if there's a FLOP
if(handHistory[row].indexOf("FLOP") !== -1){
//get the FLOP cards
parsedHand.flop = handHistory[row].split('[');
parsedHand.flop = parsedHand.flop[1].substring(0, 8).split(" ");
parsedHand.table = parsedHand.flop.slice();
row++;
// advance to the next event
while( handHistory[row].indexOf(" ") !== 0 && handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.flopActions.push(handHistory[row]);
row++;
}
// todo: test this
if(parsedHand.flopActions.length){
parsedHand.riverPot = parsedHand.turnPot = updatePot(parsedHand.players, parsedHand.flopActions, parsedHand.flopPot);
}
// check if there's a TURN
if( handHistory[row].indexOf("TURN") !== -1){
//get the TURN card
parsedHand.turn = handHistory[row].split('[');
parsedHand.turn = parsedHand.turn[2].substring(0, 2);
parsedHand.table.push(parsedHand.turn);
row++;
// advance to the next event
while( handHistory[row].indexOf(" ") !== 0 && handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.turnActions.push(handHistory[row]);
row++;
}
// todo: test this
if(parsedHand.turnActions.length){
parsedHand.riverPot = updatePot(parsedHand.players, parsedHand.turnActions, parsedHand.turnPot);
}
if( handHistory[row].indexOf("RIVER") !== -1){
//get the RIVER card
parsedHand.river = handHistory[row].split('[');
parsedHand.river = parsedHand.river[2].substring(0, 2);
parsedHand.table.push(parsedHand.river);
row++;
// advance to the next event
while( handHistory[row].indexOf(" ") !== 0 && handHistory[row].indexOf("*** ") !== 0){
parsedHand.riverActions.push(handHistory[row]);
row++;
}
// get the others if it was a run in twice
if(parsedHand.runTwice){
// check if there is a second flop
if(handHistory[row].indexOf("FLOP") !== -1){
parsedHand.secondFlop = handHistory[row].split('[');
parsedHand.secondFlop = parsedHand.secondFlop[1].substring(0, 8).split(" ");
row++;
}
// check if there is a second turn
if(handHistory[row].indexOf("TURN") !== -1){
//get the TURN card
parsedHand.secondTurn = handHistory[row].split('[');
parsedHand.secondTurn = parsedHand.secondTurn[2].substring(0, 2);
row++;
}
// doesnt need to check, there will always be a second river
parsedHand.secondRiver = handHistory[row].split('[');
parsedHand.secondRiver = parsedHand.secondRiver[2].substring(0, 2);
row++;
}
}
}
}
// commented portuguese code for now, so it reduces complexity while developing new features and refactoring code
} /* else {
// code for portuguese
// need to create a new function for this
while( handHistory[row].indexOf('Lugar ') === 0 ){
var endOfNumber = handHistory[row].indexOf(' em fichas');
var numIndex = endOfNumber;
while( handHistory[row][numIndex] !== '('){
numIndex--;
}
var chips = Number(handHistory[row].substring(numIndex +1, endOfNumber))
parsedHand.players.push(["player " + (parsedHand.players.length+1), chips]);
row++;
}
//advance to the hand row
while( handHistory[row].indexOf("*** ") !== 0 ){
row++;
}
// need to be prepared for hand history where no hand is known
// this part is not correct TODO
// var ownHand = handHistory[++row].split('[');
// ownHand = ownHand[ownHand.length-1].substring(0, 5).split(" ");
// parsedHand.hands.push(ownHand);
// filling in
row++;
// delete ^^
// todo: check if there are other hands available
//advance to the next event
while( handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.preFlopActions.push(handHistory[row]);
row++;
}
//check if there's a FLOP
if(handHistory[row].indexOf("FLOP") !== -1){
//get the FLOP cards
parsedHand.flop = handHistory[row].split('[');
parsedHand.flop = parsedHand.flop[1].substring(0, 8).split(" ");
parsedHand.table = parsedHand.flop.slice();
row++;
// advance to the next event
while( handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.flopActions.push(handHistory[row]);
row++;
}
// check if there's a TURN
if( handHistory[row].indexOf("TURN") === 4){
//get the TURN card
parsedHand.turn = handHistory[row].split('[');
parsedHand.turn = parsedHand.turn[2].substring(0, 2);
parsedHand.table.push(parsedHand.turn);
row++;
// advance to the next event
while( handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.turnActions.push(handHistory[row]);
row++;
}
// check if there's a RIVER
if( handHistory[row].indexOf("RIVER") === 4){
//get the RIVER card
parsedHand.river = handHistory[row].split('[');
parsedHand.river = parsedHand.river[2].substring(0, 2);
parsedHand.table.push(parsedHand.river);
row++;
// advance to the next event
while( handHistory[row].indexOf("*** ") !== 0 ){
parsedHand.riverActions.push(handHistory[row]);
row++;
}
}
}
}
} */
return parsedHand;
}
exports.Player = Player;
exports.createParsedHand = createParsedHand;
exports.checkRunTwice = checkRunTwice;
exports.prepareHandHistory = prepareHandHistory;
exports.getHandIdentifier = getHandIdentifier;
exports.getHandStyle = getHandStyle;
exports.handParser = handParser;