-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
dumpfile.ts
195 lines (160 loc) · 5.86 KB
/
dumpfile.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
/* eslint-disable no-console */
// This file is an advanced example of how to log player kills,
// team scores, chat text and server cvar changes from a demo file.
import * as ansiStyles from "ansi-styles";
import * as fs from "fs";
import * as util from "util";
import {
DemoFile,
extractPublicEncryptionKey,
Player,
TeamNumber
} from "demofile";
const colourReplacements = [
{ pattern: /\x01/g, ansi: ansiStyles.whiteBright.open }, // Default
{ pattern: /\x02/g, ansi: ansiStyles.red.open }, // Dark Red
{ pattern: /\x03/g, ansi: ansiStyles.magenta.open }, // Light purple
{ pattern: /\x04/g, ansi: ansiStyles.greenBright.open }, // Bright Green
{ pattern: /\x05/g, ansi: ansiStyles.green.open }, // Pale Green
{ pattern: /\x06/g, ansi: ansiStyles.greenBright.open }, // Green
{ pattern: /\x07/g, ansi: ansiStyles.redBright.open }, // Pale Red
{ pattern: /\x08/g, ansi: ansiStyles.gray.open }, // Grey
{ pattern: /\x09/g, ansi: ansiStyles.yellowBright.open }, // Yellow
{ pattern: /\x0A/g, ansi: ansiStyles.white.open }, // Silver
{ pattern: /\x0B/g, ansi: ansiStyles.blueBright.open }, // Blue
{ pattern: /\x0C/g, ansi: ansiStyles.blue.open }, // Dark Blue
{ pattern: /\x0D/g, ansi: ansiStyles.magenta.open }, // Blue Grey for SayText2, Purple for SayText
{ pattern: /\x0E/g, ansi: ansiStyles.magentaBright.open }, // Magenta
{ pattern: /\x0F/g, ansi: ansiStyles.red.open }, // Dull Red
{ pattern: /\x10/g, ansi: ansiStyles.yellow.open } // Orange
];
const standardMessages: { [message: string]: string | undefined } = {
Cstrike_Chat_All: "\x03%s\x01 : %s",
Cstrike_Chat_AllDead: "*DEAD* \x03%s\x01 : %s",
Game_connected: "%s connected."
};
function teamNumberToAnsi(teamNum: TeamNumber) {
if (teamNum === TeamNumber.Terrorists) {
return ansiStyles.redBright.open;
}
if (teamNum === TeamNumber.CounterTerrorists) {
return ansiStyles.blueBright.open;
}
return ansiStyles.gray.open;
}
const filePath = process.argv[2]!;
const stream = fs.createReadStream(filePath);
const demoFile = new DemoFile();
// Read the match info from a '.info' file if one exists alongside the demo.
if (fs.existsSync(filePath + ".info")) {
const encryptionKey = extractPublicEncryptionKey(
fs.readFileSync(filePath + ".info")
);
demoFile.setEncryptionKey(encryptionKey);
}
function logTeamScores() {
const teams = demoFile.teams;
const terrorists = teams[TeamNumber.Terrorists]!;
const cts = teams[TeamNumber.CounterTerrorists]!;
console.log(
"\t%s: %s score %d\n\t%s: %s score %d",
terrorists.teamName,
terrorists.clanName,
terrorists.score,
cts.teamName,
cts.clanName,
cts.score
);
}
function formatSayText(entityIndex: number, text: string) {
text = "\x01" + text;
// If we have an entity index set, colour 0x03 in that entity's team colour
if (entityIndex > 0) {
const ent = demoFile.entities.entities.get(entityIndex);
if (ent instanceof Player) {
text = text.replace(/\x03/g, teamNumberToAnsi(ent.teamNumber));
}
}
// Replace each colour code with its corresponding ANSI escape sequence
for (const r of colourReplacements) {
text = text.replace(r.pattern, ansiStyles.reset.open + r.ansi);
}
return text + ansiStyles.reset.open;
}
demoFile.on("start", () => {
console.log("Demo header:", demoFile.header);
});
demoFile.on("end", e => {
if (e.error) {
console.error("Error during parsing:", e.error);
} else {
logTeamScores();
}
console.log("Finished.");
});
demoFile.conVars.on("change", e => {
console.log("%s: %s -> %s", e.name, e.oldValue, e.value);
});
demoFile.gameEvents.on("player_death", e => {
const victim = demoFile.entities.getByUserId(e.userid);
const victimColour = teamNumberToAnsi(
victim ? victim.teamNumber : TeamNumber.Spectator
);
const victimName = victim ? victim.name : "unnamed";
const attacker = demoFile.entities.getByUserId(e.attacker);
const attackerColour = teamNumberToAnsi(
attacker ? attacker.teamNumber : TeamNumber.Spectator
);
const attackerName = attacker ? attacker.name : "unnamed";
const headshotText = e.headshot ? " HS" : "";
console.log(
`${attackerColour}${attackerName}${ansiStyles.reset.open} [${e.weapon}${headshotText}] ${victimColour}${victimName}${ansiStyles.reset.open}`
);
});
demoFile.userMessages.on("TextMsg", e => {
const params = e.params
.map(param =>
param[0] === "#" ? standardMessages[param.substring(1)] || param : param
)
.filter(s => s.length) as [string, ...string[]];
const formatted = util.format.apply(null, params);
console.log(formatSayText(0, formatted));
});
demoFile.userMessages.on("SayText", e => {
console.log(formatSayText(0, e.text));
});
demoFile.userMessages.on("SayText2", e => {
const nonEmptyParams = e.params.filter(s => s.length);
const msgText = standardMessages[e.msgName];
const formatted = msgText
? util.format.apply(null, [msgText, ...nonEmptyParams])
: `${e.msgName} ${nonEmptyParams.join(" ")}`;
console.log(formatSayText(e.entIdx, formatted));
});
demoFile.gameEvents.on("round_end", e => {
console.log(
"*** Round ended '%s' (reason: %s, tick: %d, time: %d secs)",
demoFile.gameRules.phase,
e.reason,
demoFile.currentTick,
demoFile.currentTime | 0
);
// We can't print the team scores here as they haven't been updated yet.
// See round_officially_ended below.
});
demoFile.gameEvents.on("round_officially_ended", logTeamScores);
demoFile.entities.on("create", e => {
// We're only interested in player entities being created.
if (!(e.entity instanceof Player)) {
return;
}
console.log("%s (%s) joined the game", e.entity.name, e.entity.steamId);
});
demoFile.entities.on("beforeremove", e => {
if (!(e.entity instanceof Player)) {
return;
}
console.log("%s left the game", e.entity.name);
});
// Start parsing the stream now that we've added our event listeners
demoFile.parseStream(stream);