-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
250 lines (219 loc) · 7.77 KB
/
Copy pathparser.js
File metadata and controls
250 lines (219 loc) · 7.77 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
/**
* HandReplayer Parser — GG/PokerStars 手牌历史 → JSON 中间格式
*
* 用法: const data = parseHandHistory(text)
* 输出: JSON 中间格式(符合 SPEC 4.2 数据模型)
*/
function parseHandHistory(text) {
const data = {
hand_id: '',
game: 'NL Hold\'em',
blinds: [0, 0],
table: '',
max_players: 6,
button_seat: 0,
players: [],
streets: [],
result: { winner: '', winning_hand: '', pot: 0, showdown: false }
};
// ── 手牌 ID ──
const idMatch = text.match(/(?:Poker\s*)?(?:Hand|HAND)\s*#?(\d+)/i);
if (idMatch) data.hand_id = idMatch[1];
// ── 盲注级别 ──
const blindMatch = text.match(/\$(\d+)\/\$(\d+)/);
if (blindMatch) data.blinds = [parseInt(blindMatch[1]), parseInt(blindMatch[2])];
// ── 桌型 ──
if (/heads.?up|2.?max/i.test(text)) data.max_players = 2;
else if (/9.?max|full.?ring/i.test(text)) data.max_players = 9;
else if (/6.?max/i.test(text)) data.max_players = 6;
// ── 按钮位 ──
const btnMatch = text.match(/Seat\s*#?(\d+)\s+is\s+the\s+button/i);
if (btnMatch) data.button_seat = parseInt(btnMatch[1]);
// ── 玩家 ──
const seatRe = /Seat\s+(\d+):\s+([^(]+?)\s+\(\$?([\d.,]+)/gi;
let seatMatch;
while ((seatMatch = seatRe.exec(text)) !== null) {
if (seatMatch.index < text.indexOf('*** HOLE CARDS ***')) {
data.players.push({
seat: parseInt(seatMatch[1]),
name: seatMatch[2].trim(),
stack: parseFloat(seatMatch[3].replace(/,/g, '')),
cards: null
});
}
}
// 根据实际玩家数修正 max_players(如果没有显式声明)
if (!/(heads.?up|2.?max|6.?max|9.?max|full.?ring)/i.test(text)) {
const count = data.players.length;
if (count <= 2) data.max_players = 2;
else if (count <= 6) data.max_players = 6;
else data.max_players = 9;
}
// ── 解析阶段 ──
const lines = text.split('\n');
// 找到玩家列表结束位置
let startIdx = 0;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('*** HOLE CARDS ***') || lines[i].includes('*** HOLE CARDS ***')) {
startIdx = i;
break;
}
}
// ── 洞牌 ──
const heroMatch = text.match(/Dealt to (\S+)\s*\[([^\]]+)\]/i);
let heroName = '';
if (heroMatch) {
heroName = heroMatch[1].trim();
const cards = heroMatch[2].trim().split(/\s+/).map(c => c.trim());
const hero = data.players.find(p => p.name.toLowerCase() === heroName.toLowerCase());
if (hero) hero.cards = cards;
}
// ── 街道解析 ──
const streetNames = ['HOLE CARDS', 'FLOP', 'TURN', 'RIVER', 'SHOWDOWN', 'SUMMARY'];
const streetLabels = ['preflop', 'flop', 'turn', 'river'];
let currentStreet = null;
let streetIndex = -1;
let potTotal = 0;
for (let i = startIdx; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
// 检测街道标记
let foundStreet = false;
for (let s = 0; s < streetNames.length; s++) {
const re = new RegExp(`\\*{3}\\s*${streetNames[s]}\\s*\\*{3}`, 'i');
if (re.test(line)) {
if (s >= 0 && s <= 3) {
// preflop/flop/turn/river
currentStreet = {
name: streetLabels[s],
cards: [],
actions: []
};
// 提取公共牌
if (s >= 1) {
const boardMatch = line.match(/\[([^\]]+)\]/);
if (boardMatch) {
currentStreet.cards = boardMatch[1].trim().split(/\s+/).map(c => c.trim());
}
}
data.streets.push(currentStreet);
} else if (s === 4) {
// SHOWDOWN
data.result.showdown = true;
}
foundStreet = true;
streetIndex = s;
break;
}
}
if (foundStreet) continue;
// ── 如果还在 HOLE CARDS 之后但还没到 FLOP,就是 preflop actions ──
if (streetIndex === 0 && currentStreet) {
const action = parseAction(line, data.players);
if (action) currentStreet.actions.push(action);
continue;
}
// ── FLOP/TURN/RIVER actions ──
if (streetIndex >= 1 && streetIndex <= 3 && currentStreet) {
const action = parseAction(line, data.players);
if (action) currentStreet.actions.push(action);
continue;
}
// ── SHOWDOWN 解析 ──
if (streetIndex === 4) {
const showMatch = line.match(/^(\S+):\s*shows?\s*\[([^\]]+)\]/i);
if (showMatch) {
const name = showMatch[1].trim();
const cards = showMatch[2].trim().split(/\s+/).map(c => c.trim());
const player = data.players.find(p => p.name.toLowerCase() === name.toLowerCase());
if (player) player.cards = cards;
// 记录胜者
const handMatch = line.match(/\(([^)]+)\)/);
if (handMatch) {
data.result.winner = name;
data.result.winning_hand = handMatch[1].trim();
}
}
// 收集底池
const collectedMatch = line.match(/(\S+)\s+collected\s+\$?([\d.,]+)/i);
if (collectedMatch) {
data.result.pot = parseFloat(collectedMatch[2].replace(/,/g, ''));
}
}
// ── SUMMARY 解析 ──
if (streetIndex === 5) {
const potMatch = line.match(/Total pot\s+\$?([\d.,]+)/i);
if (potMatch) {
data.result.pot = parseFloat(potMatch[1].replace(/,/g, ''));
}
// 如果之前没抓到胜者
const wonMatch = line.match(/Seat \d+: (\S+).*won\s+\$?([\d.,]+)/i);
if (wonMatch && !data.result.winner) {
data.result.winner = wonMatch[1].trim();
}
}
}
// 如果没有 river street 但牌局有摊牌,补一个空 river
const hasRiver = data.streets.some(s => s.name === 'river');
if (!hasRiver && data.streets.length >= 3) {
// 尝试从文本提取 river 牌
const riverMatch = text.match(/\*{3}\s*RIVER\s*\*{3}\s*\[([^\]]+)\]/i);
data.streets.push({
name: 'river',
cards: riverMatch ? riverMatch[1].trim().split(/\s+/) : [],
actions: []
});
}
return data;
}
/**
* 解析单条 action
*/
function parseAction(line, players) {
// fold
const foldMatch = line.match(/^(\S+):\s*folds?\s*$/i);
if (foldMatch) return { player: foldMatch[1].trim(), action: 'fold' };
// check
const checkMatch = line.match(/^(\S+):\s*checks?\s*$/i);
if (checkMatch) return { player: checkMatch[1].trim(), action: 'check' };
// call
const callMatch = line.match(/^(\S+):\s*calls?\s+\$?([\d.,]+)/i);
if (callMatch) return { player: callMatch[1].trim(), action: 'call', amount: parseFloat(callMatch[2].replace(/,/g, '')) };
// bet
const betMatch = line.match(/^(\S+):\s*bets?\s+\$?([\d.,]+)/i);
if (betMatch) return { player: betMatch[1].trim(), action: 'bet', amount: parseFloat(betMatch[2].replace(/,/g, '')) };
// raise (all-in)
const raiseAllInMatch = line.match(/^(\S+):\s*raises?\s+\$?([\d.,]+)\s+to\s+\$?([\d.,]+)\s+and\s+is\s+all-in/i);
if (raiseAllInMatch) {
return {
player: raiseAllInMatch[1].trim(),
action: 'raise',
amount: parseFloat(raiseAllInMatch[3].replace(/,/g, '')),
all_in: true
};
}
// raise (regular)
const raiseMatch = line.match(/^(\S+):\s*raises?\s+\$?([\d.,]+)\s+to\s+\$?([\d.,]+)/i);
if (raiseMatch) {
return {
player: raiseMatch[1].trim(),
action: 'raise',
amount: parseFloat(raiseMatch[3].replace(/,/g, ''))
};
}
// all-in (shove)
const allInMatch = line.match(/^(\S+):\s*(?:bets?\s+\$?([\d.,]+)\s+and\s+is\s+)?all-in\s*\$?([\d.,]*)/i);
if (allInMatch) {
const amt = allInMatch[2] || allInMatch[3];
return {
player: allInMatch[1].trim(),
action: 'all_in',
amount: amt ? parseFloat(amt.replace(/,/g, '')) : undefined
};
}
return null;
}
// ES Module export
if (typeof module !== 'undefined' && module.exports) {
module.exports = { parseHandHistory };
}