Skip to content

Instantly share code, notes, and snippets.

@shoffing
Created September 19, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoffing/8415340600231eeb9c80ee98d2384eaa to your computer and use it in GitHub Desktop.
Save shoffing/8415340600231eeb9c80ee98d2384eaa to your computer and use it in GitHub Desktop.
reads csgo demos and extracts initial spawns, bomb plants, and attacks
const demofile = require('demofile');
const fs = require('fs');
const _ = require('lodash');
const DEMO_FILE = 'astralis-vs-liquid-inferno.dem';
let distance3 = (p1, p2) => {
return Math.sqrt( Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2) + Math.pow(p2.z - p1.z, 2) );
};
fs.readFile(DEMO_FILE, function (err, buffer) {
let demoFile = new demofile.DemoFile();
let getTeam = (player) => {
if (_.includes(demoFile.teams[demofile.TEAM_CTS].members.map(p => p.userId), player.userId)) {
return "CT";
}
return "T";
};
let attacks = [];
let spawns = [];
let plants = [];
let gameStarted = false;
demoFile.gameEvents.on("round_start", e => {
console.log("=== NEW ROUND ===");
if (!gameStarted) {
spawns = demoFile.players.filter(p => p.isAlive).map(p => p.position);
}
gameStarted = true;
});
demoFile.gameEvents.on("bomb_planted", e => {
console.log("~~~ BOMB PLANTED ~~~")
if (gameStarted) {
plants.push(demoFile.entities.getByUserId(e.userid).position);
}
});
demoFile.userMessages.on("message", e => {
if (gameStarted && e.name === 'CS_UM_Damage') {
const victim = _.find(demoFile.players, p => p.index === e.msg.victimEntindex);
const attacker = _.minBy(demoFile.players, p => {
return distance3(p.position, e.msg.inflictorWorldPos);
});
if (victim && attacker && victim.index !== attacker.index
&& attacker.isAlive && distance3(attacker.position, e.msg.inflictorWorldPos) < 50) {
const victimTeam = getTeam(victim);
const attackerTeam = getTeam(attacker);
const amount = e.msg.amount;
const weapon = attacker.weapon.className;
console.log(`${attacker.name} (${attackerTeam}) attacked ${victim.name} (${victimTeam}) for ${amount} damage with ${weapon}`);
attacks.push({
attackerPos: attacker.position,
attackerTeam: attackerTeam,
victimPos: victim.position,
victimTeam: victimTeam,
amount: amount,
weapon: weapon,
});
}
}
});
demoFile.parse(buffer);
demoFile.on('end', (e) => {
if (e.error) {
console.log(e.error);
} else {
const data = {
attacks: attacks,
spawns: spawns,
plants: plants,
};
fs.writeFile(`${DEMO_FILE}.data.json`, JSON.stringify(data), 'utf8', (err) => {
if (err) throw err;
console.log('The data file has been saved!');
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment