demoinfocs

package module
v0.0.0-...-9ebba78 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2017 License: MIT Imports: 16 Imported by: 0

README

demoinfocs-golang

Is a CS:GO demo parser written in Go based on Valve's demoinfogo and SatsHelix's demoinfo.

GoDoc Build Status codecov License

Go Get

go get github.com/markus-wa/demoinfocs-golang

Example

This is a simple example on how to use the library. It prints out the winner of each round after the warm up is over.

Check out the godoc on the events package for some more information about the available events and their purpose.

import (
	"fmt"
	dem "github.com/markus-wa/demoinfocs-golang"
	"github.com/markus-wa/demoinfocs-golang/common"
	"github.com/markus-wa/demoinfocs-golang/events"
	"log"
	"os"
)

func main() {
	f, err := os.Open("path/to/demo.dem")
	defer f.Close()
	if err != nil {
		log.Fatal(err)
	}

	p := dem.NewParser(f)

	// Parse header
	p.ParseHeader()

	// Get T / CT team state references
	tState := p.TState()
	ctState := p.CTState()

	// We need this to skip restarts and team switches before the match start
	// Might not be necessary for all demos (especially MM)
	// But for pro matches / scrims it might be depending on how the server was set up
	// TODO: This might not always be correct, needs testing
	matchStarted := false
	p.RegisterEventHandler(func(events.MatchStartedEvent) {
		matchStarted = true
	})
	matchReallyStarted := false
	p.RegisterEventHandler(func(events.RoundStartedEvent) {
		if matchStarted {
			matchReallyStarted = true
		}
	})

	// Register handler on round end to figure out who won
	p.RegisterEventHandler(func(e events.RoundEndedEvent) {
		if matchReallyStarted {
			if e.Winner == common.Team_Terrorists {
				fmt.Println("T-side won the round - score:", tState.Score()+1) // Score + 1 because it hasn't actually been updated yet
			} else if e.Winner == common.Team_CounterTerrorists {
				fmt.Println("CT-side won the round - score:", ctState.Score()+1)
			} else {
				fmt.Println("Apparently neither the Ts nor CTs won the round, interesting")
			}
		} else {
			fmt.Println("Skipping warmup event")
		}
	})

	// Parse to end
	err = p.ParseToEnd()
	if err != nil {
		log.Fatal(err)
	}
}

Development

Running tests

To run tests Git LFS is required.

git submodule init
git submodule update
pushd test/cs-demos && git lfs pull && popd
go test
Generating protobuf code

Should you need to re-generate the protobuf generated code in the msg package, you will need the following tools:

Make sure both are inside your PATH variable.

After installing these use go generate ./msg to generate the protobuf code.

Documentation

Overview

Package demoinfocs provides demo parser for the game Counter-Strike: Global Offensive. It is based on the official demoinfogo tool by Valve as well as Stats Helix's demoinfo. A good entry point to using the library is the Parser type. Demo events are documented in the events package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser can parse a CS:GO demo. Creating a Parser is done via NewParser(). To start off use Parser.ParseHeader() to parse the demo header. After parsing the header Parser.ParseNextFrame() and Parser.ParseToEnd() can be used to parse the demo. Use Parser.RegisterEventHandler() to receive notifications about events.

func NewParser

func NewParser(demostream io.Reader) *Parser

NewParser creates a new Parser on the basis of an io.Reader - like os.File or bytes.Reader - that reads demo data.

func (*Parser) CTState

func (p *Parser) CTState() *TeamState

CTState returns the TeamState of the CT team. Make sure you handle swapping sides properly if you keep the reference.

func (*Parser) Cancel

func (p *Parser) Cancel()

Cancel aborts ParseToEnd() on the upcoming tick.

func (*Parser) CurrentFrame

func (p *Parser) CurrentFrame() int

CurrentFrame return the number of the current frame, aka. 'demo-tick' (Since demos often have a different tick-rate than the game). Starts with frame 0, should go up to DemoHeader.PlaybackFrames but might not be the case (usually it's just close to it).

func (*Parser) CurrentTime

func (p *Parser) CurrentTime() float32

CurrentTime returns the ingame time in seconds since the start of the demo.

func (*Parser) FrameRate

func (p *Parser) FrameRate() float32

FrameRate returns the frame rate of the demo (frames / demo-ticks per second). Not necessarily the tick-rate the server ran on during the game. VolvoPlx128BitKTnxBye

func (*Parser) FrameTime

func (p *Parser) FrameTime() float32

FrameTime returns the time a frame / demo-tick takes in seconds.

func (*Parser) IngameTick

func (p *Parser) IngameTick() int

IngameTick returns the latest actual tick number of the server during the game. Watch out, I've seen this return wonky negative numbers at the start of demos.

func (*Parser) Map

func (p *Parser) Map() string

Map returns the map name. E.g. de_dust2 or de_inferno.

func (*Parser) ParseHeader

func (p *Parser) ParseHeader() error

ParseHeader attempts to parse the header of the demo. Returns error if the filestamp (first 8 bytes) doesn't match HL2DEMO.

func (*Parser) ParseNextFrame

func (p *Parser) ParseNextFrame() bool

ParseNextFrame attempts to parse the next frame / demo-tick (not ingame tick). Returns true unless the demo command 'stop' was encountered. Panics if header hasn't been parsed yet - see Parser.ParseHeader().

func (*Parser) ParseToEnd

func (p *Parser) ParseToEnd() error

ParseToEnd attempts to parse the demo until the end. Aborts and returns an error if Cancel() is called before the end. May panic if the demo is corrupt in some way.

func (*Parser) Participants

func (p *Parser) Participants() []*common.Player

Participants returns all connected players. This includes spectators.

func (*Parser) PlayingParticipants

func (p *Parser) PlayingParticipants() []*common.Player

PlayingParticipants returns all players that aren't spectating.

func (*Parser) Progress

func (p *Parser) Progress() float32

Progress returns the parsing progress from 0 to 1. Where 0 means nothing has been parsed yet and 1 means the demo has been parsed to the end. Might not actually be reliable since it's just based on the reported tick count of the header.

func (*Parser) RegisterEventHandler

func (p *Parser) RegisterEventHandler(handler interface{})

RegisterEventHandler registers a handler for game events. Must be of type func(<EventType>) where EventType is the kind of event that is handled. To catch all events func(interface{}) can be used. Parameter handler has to be of type interface{} because lolnogenerics.

func (*Parser) TState

func (p *Parser) TState() *TeamState

TState returns the TeamState of the T team. Make sure you handle swapping sides properly if you keep the reference.

type TeamState

type TeamState struct {
	// contains filtered or unexported fields
}

TeamState contains a team's ID, score, clan name & country flag.

func (TeamState) ClanName

func (ts TeamState) ClanName() string

ClanName returns the team's clan name.

func (TeamState) Flag

func (ts TeamState) Flag() string

Flag returns the team's country flag.

func (TeamState) ID

func (ts TeamState) ID() int

ID returns the team-ID. This stays the same even after switching sides.

func (TeamState) Score

func (ts TeamState) Score() int

Score returns the team's number of rounds won.

Directories

Path Synopsis
Package bitread provides a wrapper for github.com/markus-wa/gobitread with CS:GO demo parsing specific helpers.
Package bitread provides a wrapper for github.com/markus-wa/gobitread with CS:GO demo parsing specific helpers.
Package common contains common types, constants and functions used over different demoinfocs packages.
Package common contains common types, constants and functions used over different demoinfocs packages.
Package events contains all events that can be sent out from demoinfocs.Parser.
Package events contains all events that can be sent out from demoinfocs.Parser.
Package msg is a generated protocol buffer package.
Package msg is a generated protocol buffer package.
Package sendtables contains sendtable specific magic and should really be better documented (TODO).
Package sendtables contains sendtable specific magic and should really be better documented (TODO).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL