TF2 log parser
npm install logstf-parserts
const parser = require("logstf-parser");
const LogsParser = new parser.LogParser();
const lines = fs.readFileSync(filePath, "UTF-8").split("\n");
const game = LogsParser.parseLines(lines)
console.log(game.toJson())
console.log(game.toLogstf())
// Returns a format like the one logs.tf json provides this however requires one to have some
// of the default modules loaded
`
Adding modules
By default only the GameStateModule will be loaded other modules can be included like so:
`ts
const LogsParser = new parser.LogParser();
//Note that we're passing the class and not an instance!
LogsParser.addModule(parser.defaultModules.KillstreakModule);
//To load all modules one can iterate through the object e.g.:
for (const module of Object.values(parser.defaultModules)){
LogsParser.addModule(module);
}
//If you want to define your own GameStateModule you should disable the provided one like this:
LogsParser.useCustomGameState();
`
Similar to this you can create and load custom modules.
Custom modules
One can also define custom modules to extract other events from the logfiles.
Each module must be a class which should contain an identifier as well as a finish() and toJson() method.
Example:
`ts
import {events} from "logstf-parser";
import {IGameState} from "logstf-parser";
class MyModule implements events.IStats {
public identifier: string
private killEvents: events.IKillEvent[]
private gameStartTime: number | null
constructor(gameState: IGameState) {
this.identifier = 'myModule'
this.killEvents = []
this.gameStartTime = null
}
onRoundStart(event: events.IRoundStartEvent) {
if (!this.gameStartTime) this.gameStartTime = event.timestamp
}
onKill(event: events.IKillEvent) {
if (!this.gameStartTime) return;
killEvents.push(event)
}
finish(){
//Get's called after every line has been processed
}
toJSON(): events.IKillEvent[] {
return this.killEvents
}
}
``