logstf-parser
TypeScript icon, indicating that this package has built-in type declarations

1.4.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i logstf-parser

Weekly Downloads

1

Version

1.4.0

License

AGPL

Unpacked Size

489 kB

Total Files

123

Last publish

Collaborators

  • thebv