-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogParserV4.js
More file actions
302 lines (249 loc) · 8.27 KB
/
logParserV4.js
File metadata and controls
302 lines (249 loc) · 8.27 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
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
/**
* MTG Arena Log Parser v4
* Based on actual MTG Arena log format with NodeStates
*/
class LogParserV4 {
constructor() {
this.currentMatch = null;
this.pendingResult = null;
this.processedMatches = new Set();
}
parse(data) {
const events = [];
const lines = data.split('\n');
for (const line of lines) {
if (!line.trim()) continue;
const event = this.parseLine(line);
if (event) {
// Avoid duplicate events
const eventKey = `${event.type}_${event.data.matchId}_${event.data.timestamp}`;
if (!this.processedMatches.has(eventKey)) {
this.processedMatches.add(eventKey);
events.push(event);
}
}
}
return events;
}
parseLine(line) {
// Only parse JSON lines
if (!line.trim().startsWith('{')) return null;
let data;
try {
data = JSON.parse(line);
} catch (e) {
return null;
}
// Check for NodeStates - indicates active/completed game nodes
if (data.NodeStates || data.nodeStates) {
return this.parseNodeStates(data);
}
// Check for match result events
if (data.matchResult !== undefined || data.MatchResult !== undefined) {
return this.handleMatchResult(data);
}
// Check for explicit match end
if (data.eventName === 'MatchEnd' || data.EventName === 'MatchEnd') {
return this.handleMatchEnd(data);
}
// Check for winning team ID (game result)
if (data.winningTeamId !== undefined || data.WinningTeamId !== undefined) {
return this.handleGameResult(data);
}
// Check for match start
if (data.eventName === 'MatchStart' || data.EventName === 'MatchStart') {
return this.handleMatchStart(data);
}
return null;
}
parseNodeStates(data) {
const nodes = data.NodeStates || data.nodeStates;
const milestones = data.MilestoneStates || data.milestoneStates;
// Look for PlayMatch node to detect match activity
for (const [nodeName, nodeState] of Object.entries(nodes)) {
// Match started
if ((nodeName.includes('PlayMatch') || nodeName.includes('Match')) &&
(nodeState.Status === 'Active' || nodeState.Status === 'Started')) {
if (!this.currentMatch) {
this.currentMatch = {
matchId: `match_${Date.now()}`,
startTime: Date.now(),
format: this.detectFormatFromNodes(nodes),
timestamp: new Date().toISOString()
};
return {
type: 'MATCH_START',
data: { ...this.currentMatch }
};
}
}
// Match ended
if ((nodeName.includes('PlayMatch') || nodeName.includes('Match')) &&
nodeState.Status === 'Completed') {
if (this.currentMatch) {
// Try to determine result from milestone rewards
let result = 'unknown';
if (milestones) {
result = this.determineResultFromMilestones(milestones);
}
const matchData = {
matchId: this.currentMatch.matchId,
result: result,
format: this.currentMatch.format,
timestamp: new Date().toISOString()
};
this.currentMatch = null;
return {
type: 'MATCH_END',
data: matchData
};
}
}
}
return null;
}
determineResultFromMilestones(milestones) {
// Check milestone rewards to determine win/loss
for (const [name, state] of Object.entries(milestones)) {
if (name.includes('Match') || name.includes('Game')) {
const rewards = state.RewardItems || state.rewards;
if (rewards && rewards.length > 0) {
// Has rewards = likely a win
return 'win';
}
// Check if claimed/completed without rewards = likely a loss
if (state.Status === 'Claimed' || state.Status === 'Completed') {
return 'loss';
}
}
}
return 'unknown';
}
detectFormatFromNodes(nodes) {
// Try to detect format from node names
for (const nodeName of Object.keys(nodes)) {
if (nodeName.includes('Standard')) return 'Standard';
if (nodeName.includes('Alchemy')) return 'Alchemy';
if (nodeName.includes('Historic')) return 'Historic';
if (nodeName.includes('Explorer')) return 'Explorer';
if (nodeName.includes('Brawl')) return 'Brawl';
if (nodeName.includes('Draft')) return 'Draft';
if (nodeName.includes('Sealed')) return 'Sealed';
}
return 'Unknown';
}
handleMatchStart(data) {
this.currentMatch = {
matchId: data.matchId || data.MatchId || `match_${Date.now()}`,
eventId: data.eventId || data.EventId || 'unknown',
format: this.detectFormat(data),
timestamp: new Date().toISOString()
};
return {
type: 'MATCH_START',
data: { ...this.currentMatch }
};
}
handleMatchEnd(data) {
const result = this.extractResult(data);
const matchData = {
matchId: this.currentMatch?.matchId || data.matchId || 'unknown',
result: result,
format: this.currentMatch?.format || this.detectFormat(data),
timestamp: new Date().toISOString()
};
this.currentMatch = null;
return {
type: 'MATCH_END',
data: matchData
};
}
handleMatchResult(data) {
const result = this.extractResult(data);
return {
type: 'MATCH_END',
data: {
matchId: this.currentMatch?.matchId || data.matchId || 'unknown',
result: result,
format: this.currentMatch?.format || 'Unknown',
timestamp: new Date().toISOString()
}
};
}
handleGameResult(data) {
const winningTeamId = data.winningTeamId || data.WinningTeamId;
const playerTeamId = 1; // Assume player is team 1
const result = (winningTeamId === playerTeamId) ? 'win' : 'loss';
// Store pending result - might be updated by match end
this.pendingResult = result;
return {
type: 'GAME_END',
data: {
result: result,
winningTeamId: winningTeamId,
timestamp: new Date().toISOString()
}
};
}
extractResult(data) {
if (data.result !== undefined) return this.normalizeResult(data.result);
if (data.Result !== undefined) return this.normalizeResult(data.Result);
if (data.matchResult !== undefined) return this.normalizeResult(data.matchResult);
if (data.MatchResult !== undefined) return this.normalizeResult(data.MatchResult);
return this.pendingResult || 'unknown';
}
detectFormat(data) {
const formatType = data.formatType || data.FormatType || data.format || data.Format;
if (formatType) return this.normalizeFormat(formatType);
const eventId = data.eventId || data.EventId || data.event_id;
if (eventId) return this.formatFromEventId(eventId);
return 'Unknown';
}
normalizeFormat(format) {
const map = {
'Standard': 'Standard',
'Alchemy': 'Alchemy',
'Historic': 'Historic',
'Explorer': 'Explorer',
'Pioneer': 'Pioneer',
'Timeless': 'Timeless',
'Brawl': 'Brawl',
'HistoricBrawl': 'Historic Brawl',
'Draft': 'Draft',
'Sealed': 'Sealed'
};
return map[format] || format;
}
formatFromEventId(eventId) {
if (!eventId) return 'Unknown';
const id = eventId.toLowerCase();
if (id.includes('standard')) return 'Standard';
if (id.includes('alchemy')) return 'Alchemy';
if (id.includes('historic')) {
if (id.includes('brawl')) return 'Historic Brawl';
return 'Historic';
}
if (id.includes('explorer')) return 'Explorer';
if (id.includes('pioneer')) return 'Pioneer';
if (id.includes('timeless')) return 'Timeless';
if (id.includes('brawl')) return 'Brawl';
if (id.includes('draft')) return 'Draft';
if (id.includes('sealed')) return 'Sealed';
return 'Unknown';
}
normalizeResult(result) {
if (typeof result === 'number') {
if (result === 1) return 'win';
if (result === 2) return 'loss';
if (result === 0) return 'draw';
}
if (typeof result === 'string') {
const lower = result.toLowerCase();
if (lower === 'victory' || lower === 'win') return 'win';
if (lower === 'defeat' || lower === 'loss' || lower === 'defeated') return 'loss';
if (lower === 'draw') return 'draw';
}
return 'unknown';
}
}
module.exports = LogParserV4;