Skip to content

TheBv/logstf-parser

 
 

Repository files navigation

Logs.tf Parser

Build

This is an updated version of the logs.tf parser which aims to recreate the parser currently used by logs.tf as closely as possible while still fixing/improving upon various issues.

This parser is currently being used for demoticks.tf

Installation

Install it from npm:

$ npm install logstf-parser

Brief Example

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:

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:

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
    }

}

List of events

About

Updated version of the logs.tf parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.0%
  • JavaScript 2.0%