-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandReader.js
More file actions
executable file
·207 lines (185 loc) · 6.13 KB
/
HandReader.js
File metadata and controls
executable file
·207 lines (185 loc) · 6.13 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
var fs = require('fs'),
_ = require('underscore'),
S = require('string'),
mongoose = require('mongoose'),
Hand = mongoose.model('Hand'),
GAME_TYPES = {
'Pot Limit Omaha Hi': 'PLO',
'Omaha Pot Limit': 'PLO',
'PL Omaha Hi': 'PLO',
'CAP PL Omaha Hi': 'CAP PLO',
'Hold\'em No Limit': 'NL'
};
function addGameSequence(sequenceStr) {
if (sequenceStr) {
if (games[sequenceStr]) games[sequenceStr]++;
else games[sequenceStr] = 1;
}
}
function readFileSync(file) {
return fs.readFileSync(file, 'utf8');
}
function getSiteIdentifier(str) {
//console.log(str.split(' ')[0]);
if (str.split(' ')[0].trim() === 'PokerStars') return 'ps';
if (str.split(' ')[0].trim() === 'Full') return 'ft';
return '';
}
function getGameType(str, site) {
var gameType = '';
if (site === 'ps') {
//the game type string is located between the first ':' and the first '('
//in a pokerstars file
gameType = str.substring(str.indexOf(':') + 1, str.indexOf('(')).trim();
}
if (site === 'ft') {
var strAfterFirstDash = str.substr(str.indexOf('-') + 1);
gameType = strAfterFirstDash.match(/[\bA-z][A-z\s]{0,25}\b/).shift();
}
//only use one name for game type
return _.has(GAME_TYPES, gameType) ? GAME_TYPES[gameType] : gameType;
}
function getStakes(str) {
return str.match(/[\b$][\w/$]{0,25}/).shift();
}
function getDate(str) {
return str.match(/[\b0-9]{4}\/[0-9]{2}\/[0-9]{2}/).shift();
}
function getHandNumber(str) {
var words = str.split(' '),
num;
for (var i = 0; i < words.length; i++) {
var word = words[i];
if (word.charAt(0) === '#') {
num = word.substr(1, words.length - 1);
break;
}
}
return num;
}
function isStartOfNewHand(line) {
return line.search('Game #') !== -1 ||
(line.search('Hand #') !== -1 && !S(line).startsWith('Dealer:'));
}
exports.processFile = function(data) {
var lines = data.split('\n'),
flopped = false,
turned = false,
rivered = false,
hand = {},
site = getSiteIdentifier(lines[0]),
n = 0,
HERO,
VILLAIN,
sequence = '',
handText = '';
console.log('Lines in file:', lines.length);
for (var i = 0; i <= lines.length - 1; i++) {
//use the string wrapper library to get more functions
var line = S(lines[i]),
startOfHand = isStartOfNewHand(line.s),
isLastHand = i === lines.length - 1;
//we will store the text for each hand
if (!startOfHand && !line.isEmpty()) handText += line.s.concat('\n');
//console.log(line);
if (startOfHand || isLastHand) {
//if it's not the first iteration, let's capture the previous game
//before starting the new one
if (sequence) {
//console.log(sequence);
hand.position = sequence.substr(0,2);
hand.sequence = sequence.substr(3);
//console.log(hand.sequence)
hand.hero = HERO;
hand.villain = VILLAIN;
hand.story = handText;
//console.log(handText);
Hand.update(
{ id: hand.id },
hand,
{ upsert: true },
function(err) {
if (err) {
console.log(err);
}
}
);
//increment the game counter
n++;
//if last hand we are done
if (isLastHand) continue;
}
//clear out the old hand text and start the next hand
handText = '';
handText = line.concat('\n');
//clear out the old sequence before starting the new one
sequence = '';
//reset the flags that prevent multiple flop/turn/river (run it twice)
flopped = false;
turned = false;
rivered = false;
//capture the unique game #
hand.id = site + getHandNumber(line.s);
//capture other game info
hand.game = getGameType(line.s, site);
hand.stakes = getStakes(line.s);
hand.date = getDate(line.s);
//continue to next iteration to prevent unnecessary code execution
continue;
}
if (line.contains('FLOP') && !flopped) {
step = 'flop';
sequence += '-' + step;
flopped = true;
continue;
}
if (line.contains('TURN') && !turned) {
step = 'turn';
sequence += '-' + step;
turned = true;
continue;
}
if (line.contains('RIVER') && !rivered) {
step = 'river';
sequence += '-' + step;
rivered = true;
continue;
}
//figure out who the players are if we don't know yet
if (!VILLAIN || !HERO) {
if (line.startsWith('Seat') && !line.contains('is sitting out')) {
var player = line
.between(':', '(')
.trim().s;
if (player === '@elonmusk') HERO = player;
else VILLAIN = player;
}
}
if (line.startsWith(HERO + ' posts the small')) sequence += 'sb';
else if (line.startsWith(HERO + ' posts the big')) sequence += 'bb';
else if (line.startsWith(HERO + ': posts small')) sequence += 'sb';
else if (line.startsWith(HERO + ': posts big')) sequence += 'bb';
else if (line.startsWith(HERO + ' bets')) sequence += '-hr';
else if (line.startsWith(HERO + ' raises')) sequence += '-hr';
else if (line.startsWith(HERO + ' folds')) sequence += '-hf';
else if (line.startsWith(HERO + ' checks')) sequence += '-hc';
else if (line.startsWith(HERO + ' calls')) sequence += '-hc';
else if (line.startsWith(HERO + ': bets')) sequence += '-hr';
else if (line.startsWith(HERO + ': raises')) sequence += '-hr';
else if (line.startsWith(HERO + ': folds')) sequence += '-hf';
else if (line.startsWith(HERO + ': checks')) sequence += '-hc';
else if (line.startsWith(HERO + ': calls')) sequence += '-hc';
else if (line.startsWith(VILLAIN + ' bets')) sequence += '-vr';
else if (line.startsWith(VILLAIN + ' raises')) sequence += '-vr';
else if (line.startsWith(VILLAIN + ' folds')) sequence += '-vf';
else if (line.startsWith(VILLAIN + ' checks')) sequence += '-vc';
else if (line.startsWith(VILLAIN + ' calls')) sequence += '-vc';
else if (line.startsWith(VILLAIN + ': bets')) sequence += '-vr';
else if (line.startsWith(VILLAIN + ': raises')) sequence += '-vr';
else if (line.startsWith(VILLAIN + ': folds')) sequence += '-vf';
else if (line.startsWith(VILLAIN + ': checks')) sequence += '-vc';
else if (line.startsWith(VILLAIN + ': calls')) sequence += '-vc';
}
//return the number of games processed
return n;
}