-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLogParser.ts
453 lines (413 loc) · 18.2 KB
/
LogParser.ts
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
/// <reference path="./node_modules/@types/xregexp/index.d.ts" />
/// <reference path="./node_modules/@types/steamid/index.d.ts" />
import XRegExp = require('xregexp')
import * as events from './events'
import PlayerStatsModule from './modules/PlayerStatsModule'
import GameStateModule from './modules/GameStateModule'
import TeamStatsModule from './modules/TeamStatsModule'
import PvPModule from './modules/PvPModule'
import PvCModule from './modules/PvCModule'
import ChatModule from './modules/ChatModule'
import RealDamageModule from './modules/RealDamageModule'
import PlayerClassStatsModule from './modules/PlayerClassStatsModule'
// TODO: MedicStats
// TODO: HighlightsModule
// TODO: CP/CTF support
// TODO: Class support without plugin
// TODO: Feign death
// TODO: Captures
const PLAYER_EXPRESSION: RegExp = XRegExp('^(?<name>.{1,80}?)<\\d{1,4}><(?<steamid>.{1,40})><(?<team>(Red|Blue|Spectator|Console))>')
const TIMESTAMP_EXPRESSION: RegExp = /^L (\1\d{2})\/(\2\d{2})\/(\3\d{4}) - (\4\d{2}):(\5\d{2}):(\6\d{2})/
const PROPERTIES_EXPRESSION: RegExp = /\((\w{1,60}) "([^"]{1,60})"\)/
export function parseLines(lines: string[]): Game {
const game = new Game()
lines.forEach(line => game.processLine(line))
game.finish()
return game.toJSON()
}
export interface PlayerInfo {
id: string,
name: string,
team: string
}
function getFromPlayerString(playerString: string): PlayerInfo | null {
if (!playerString) throw new Error("Empty playerString")
const matches: any = XRegExp.exec(playerString, PLAYER_EXPRESSION)
if (!matches) return null
return {
id: matches.steamid,
name: matches.name,
team: matches.team
}
}
interface IEventDefinition {
createEvent: IEventCreator | null;
regexp: RegExp;
}
interface IEventCreator {
(regexpMatches: any, props: Map<string, string>, time: number): events.IEvent | null;
}
interface IEventCallback {
(event: events.IEvent): void
}
export interface IGameState {
isLive: boolean
mapName: string | null
}
export class Game {
events: Map<string, IEventDefinition>
modules: events.IStats[]
gameState: IGameState
constructor() {
this.gameState = {
isLive: false,
mapName: null
}
this.modules = [
new GameStateModule(this.gameState),
new TeamStatsModule(this.gameState),
new PlayerStatsModule(this.gameState),
new PlayerClassStatsModule(this.gameState),
new PvPModule(this.gameState),
new PvCModule(this.gameState),
new RealDamageModule(this.gameState),
new ChatModule(this.gameState),
]
this.events = new Map<string, IEventDefinition>()
this.events.set("onDamage", {
regexp: XRegExp('^"(?P<attacker>.+?)" triggered "damage"( against "(?P<victim>.+?)")?'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IDamageEvent | null {
const attacker = getFromPlayerString(regexpMatches.attacker)
const victim = getFromPlayerString(regexpMatches.victim)
const damage = parseInt(props.get('damage') || '0')
const weapon = props.get('weapon')
const headshot = parseInt(props.get('headshot') || '0') ? true : false
if (!attacker) return null
return {
timestamp: time,
attacker: attacker,
victim: victim,
damage: damage,
weapon: weapon,
headshot: headshot
}
}
});
this.events.set("onHeal", {
regexp: XRegExp('^"(?P<player>.+?)" triggered "healed" against "(?P<target>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IHealEvent | null {
const healer = getFromPlayerString(regexpMatches.player)
const target = getFromPlayerString(regexpMatches.target)
const healing = parseInt(props.get('healing') || '0')
if (!healer || !target || healing < 1 || healing > 450) return null
return {
timestamp: time,
healer: healer,
target: target,
healing: healing
}
}
});
// L 08/26/2018 - 23:06:46: "arekk<78><[U:1:93699014]><Red>" triggered "shot_fired" (weapon "gloves")
this.events.set("onShot", {
regexp: XRegExp('^"(?P<player>.+?)" triggered "shot_fired"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IShotEvent | null {
const player = getFromPlayerString(regexpMatches.player)
const weapon = props.get('weapon')
if (!player ||!weapon) return null
return {
timestamp: time,
player: player,
weapon: weapon,
}
}
});
this.events.set("onShotHit", {
regexp: XRegExp('^"(?P<player>.+?)" triggered "shot_hit"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IShotEvent | null {
const player = getFromPlayerString(regexpMatches.player)
const weapon = props.get('weapon')
if (!player ||!weapon) return null
return {
timestamp: time,
player: player,
weapon: weapon,
}
}
});
this.events.set("onKill", {
regexp: XRegExp('^"(?P<attacker>.+?)" killed "(?P<victim>.+?)" with "(?P<weapon>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IKillEvent | null {
const attacker = getFromPlayerString(regexpMatches.attacker)
const victim = getFromPlayerString(regexpMatches.victim)
const weapon = regexpMatches.weapon
const isHeadshot = props.get("headshot") === '1' ? true : false
const isBackstab = props.get("ubercharge") === '1' ? true : false
if (!attacker || !victim) return null
return {
timestamp: time,
attacker: attacker,
victim: victim,
weapon: weapon,
headshot: isHeadshot,
backstab: isBackstab,
}
}
});
this.events.set("onAssist", {
regexp: XRegExp('^"(?P<player>.+?)" triggered "kill assist" against "(?P<victim>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IAssistEvent | null {
const assister = getFromPlayerString(regexpMatches.player)
const victim = getFromPlayerString(regexpMatches.victim)
if (!assister || !victim) return null
const attackerPosition = props.get("attacker_position") || null
const assisterPosition = props.get("assister_position") || null
const victimPosition = props.get("victim_position") || null
return {
timestamp: time,
assister: assister,
victim: victim,
attackerPosition: attackerPosition,
assisterPosition: assisterPosition,
victimPosition: victimPosition
}
}
});
this.events.set("onPickup", {
regexp: XRegExp('^"(?P<player>.+?)" picked up item "(?P<item>.{1,40}?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IPickupEvent | null {
const item = regexpMatches.item
return {
timestamp: time,
item: item
}
}
});
this.events.set("onSuicide", {
regexp: XRegExp('^"(?P<player>.+?)" committed suicide'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.ISuicideEvent | null {
const player = getFromPlayerString(regexpMatches.player)
if (!player) return null
return {
timestamp: time,
player: player
}
}
});
this.events.set("onSpawn", {
regexp: XRegExp('^"(?P<player>.+?)" spawned as "(?P<role>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.ISpawnEvent | null {
const player = getFromPlayerString(regexpMatches.player)
let role = regexpMatches.role.toLowerCase()
if (role === 'heavy') role = 'heavyweapons'
if (!player) return null
return {
timestamp: time,
player: player,
role: role
}
}
});
this.events.set("onRole", {
regexp: XRegExp('^"(?P<player>.+?)" changed role to "(?P<role>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IRoleEvent | null {
const player = getFromPlayerString(regexpMatches.player)
let role = regexpMatches.role.toLowerCase()
if (role === 'heavy') role = 'heavyweapons'
if (!player) return null
return {
timestamp: time,
player: player,
role: role
}
}
});
// (cp "0") (cpname "Blue Final Point") (numcappers "4") (player1 "yomps<76><[U:1:84024852]><Red>") (position1 "-3530 -1220 583") (player2 "b4nny<77><[U:1:10403381]><Red>") (position2 "-3570 -1311 583") (player3 "arekk<78><[U:1:93699014]><Red>") (position3 "-3509 -1157 576") (player4 "cookiejake<81><[U:1:84193779]><Red>") (position4 "-3521 -1306 583")
this.events.set("onCapture", {
regexp: XRegExp('^Team "(?P<team>(Red|Blue)?)" triggered "pointcaptured'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.ICaptureEvent | null {
const pointId = parseInt(props.get('cp') || '-1') + 1
return {
timestamp: time,
team: regexpMatches.team,
pointId: pointId,
pointName: '',
numCappers: 0,
playerIds: [],
}
}
});
this.events.set("onMedicDeath", {
regexp: XRegExp('^"(?P<attacker>.+?)" triggered "medic_death" against "(?P<victim>.+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IMedicDeathEvent | null {
const attacker = getFromPlayerString(regexpMatches.attacker)
const victim = getFromPlayerString(regexpMatches.victim)
if (!attacker || !victim) return null
const isDrop = props.get("ubercharge") === '1' ? true : false
return {
timestamp: time,
attacker: attacker,
victim: victim,
isDrop: isDrop,
}
}
});
this.events.set("onRoundStart", {
regexp: XRegExp('^World triggered "Round_Start"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IRoundStartEvent | null {
return {
timestamp: time
}
}
});
this.events.set("onRoundEnd", {
regexp: XRegExp('^World triggered "Round_(?P<type>Win|Stalemate)'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IRoundEndEvent | null {
const winner = props.get('winner') || null
return {
timestamp: time,
type: regexpMatches.type,
winner: <events.Team>winner
}
}
});
this.events.set("onGameOver", {
createEvent: null,
regexp: XRegExp('^World triggered "Game_Over"'),
});
this.events.set("onJoinTeam", {
createEvent: null,
regexp: XRegExp('^"(?P<player>.+?)" joined team "(?P<newteam>.+?)"'),
});
this.events.set("onDisconnect", {
createEvent: null,
regexp: XRegExp('^"(?P<player>.+?)" disconnected'),
});
this.events.set("onCharge", {
regexp: XRegExp('^"(?P<player>.+?)" triggered "chargedeployed"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IChargeEvent | null {
const player = getFromPlayerString(regexpMatches.player)
if (!player) return null
const medigunType = props.get("medigun") || "medigun"
return {
player: player,
timestamp: time,
medigunType: medigunType
}
}
});
this.events.set("onChat", {
regexp: XRegExp('^"(?P<player>.+?)" say "(?P<message>.{1,160}?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IChatEvent | null {
const player = getFromPlayerString(regexpMatches.player)
if (!player) return null
const message = regexpMatches.message
return {
timestamp: time,
player: player,
message: message,
}
}
});
this.events.set("onBuild", {
createEvent: null,
regexp: XRegExp('^"(?P<player>.+?)" triggered "builtobject"'),
});
this.events.set("onFlag", {
createEvent: null,
regexp: XRegExp('^"(?P<player>.+?)" triggered "flagevent"'),
});
this.events.set("onScore", {
regexp: XRegExp('^Team "(?P<team>(Red|Blue))" (current|final) score "(?P<score>\\d+?)"'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IRoundScoreEvent | null {
return {
timestamp: time,
team: regexpMatches.team,
score: regexpMatches.score,
}
}
});
this.events.set("onPause", {
regexp: XRegExp('^World triggered "Game_Paused'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IPauseEvent | null {
return {
timestamp: time,
}
}
});
this.events.set("onUnpause", {
regexp: XRegExp('^World triggered "Game_Unpaused'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IUnpauseEvent | null {
return {
timestamp: time,
}
}
});
this.events.set("onMapLoad", {
regexp: XRegExp('^Started map "(?P<mapname>.+?)'),
createEvent: function (regexpMatches: any, props: Map<string, string>, time: number): events.IMapLoadEvent | null {
return {
timestamp: time,
mapName: regexpMatches.mapname
}
}
});
// medic_firstheal: XRegExp('""" triggered "first_heal_after_spawn")
// medic_chargeready: XRegExp('"" triggered "chargeready"')
// medic_death_ex: XRegExp('"" triggered "medic_death_ex"')
// medic_chargeended: XRegExp('"" triggered "chargeended"')
// medic_emptyuber: XRegExp('"" triggered "empty_uber"')
// medic_lostadvantage: XRegExp('"" triggered "lost_uber_advantage"')
}
createEvent(eventType: string, regexpMatches: object, props: Map<string, string>, time: number): events.IEvent | null {
const eventDefinition = this.events.get(eventType)
if (!eventDefinition || !eventDefinition.createEvent) return null
const event = eventDefinition.createEvent(regexpMatches, props, time)
return event
}
processLine(line: string) {
const eventLine = line.slice(25)
for (let [eventName, eventProps] of this.events.entries()) {
const matches = XRegExp.exec(eventLine, eventProps.regexp);
if (!matches) continue
const time = this.makeTimestamp(line)
if (!time) return
const props = new Map<string, string>()
XRegExp.forEach(eventLine, PROPERTIES_EXPRESSION, function (match, i) {
const key = match[1]
const value = match[2]
props.set(key, value)
})
const event: events.IEvent | null = this.createEvent(eventName, matches, props, time);
if (!event) return
for (const m of this.modules) {
const callback: IEventCallback = m[eventName]
if (callback) callback.call(m, event)
}
}
}
private makeTimestamp(line: string): number | null {
const t = TIMESTAMP_EXPRESSION.exec(line);
if (!t) return null
const year = parseInt(t[3])
const month = parseInt(t[1]) - 1
const day = parseInt(t[2])
const hours = parseInt(t[4])
const minutes = parseInt(t[5])
const seconds = parseInt(t[6])
const time = new Date(year, month, day, hours, minutes, seconds).getTime() / 1000;
return time
}
finish(): void {
for (const m of this.modules) {
if (m.finish) m.finish()
}
}
toJSON() {
let output: any = {}
for (const m of this.modules) {
output[m.identifier] = m.toJSON()
}
return output
}
}