Documentation
¶
Overview ¶
Package cs2log provides utilities for parsing a counter-strike 2 server logfile. It exports types for cs2 logfiles, their regular expressions, a function for parsing and a function for converting to non-html-escaped JSON.
Look at the examples for Parse and ToJSON for usage instructions.
Index ¶
- Constants
- Variables
- func ToJSON(m Message) string
- type Equation
- type FreezTimeStart
- type GameOver
- type Message
- func NewFreezTimeStart(ti time.Time, r []string) Message
- func NewGameOver(ti time.Time, r []string) Message
- func NewPlayerAttack(ti time.Time, r []string) Message
- func NewPlayerBanned(ti time.Time, r []string) Message
- func NewPlayerBlinded(ti time.Time, r []string) Message
- func NewPlayerBombBeginDefuse(ti time.Time, r []string) Message
- func NewPlayerBombDefused(ti time.Time, r []string) Message
- func NewPlayerBombDropped(ti time.Time, r []string) Message
- func NewPlayerBombGot(ti time.Time, r []string) Message
- func NewPlayerBombPlanted(ti time.Time, r []string) Message
- func NewPlayerConnected(ti time.Time, r []string) Message
- func NewPlayerDisconnected(ti time.Time, r []string) Message
- func NewPlayerDropped(ti time.Time, r []string) Message
- func NewPlayerEntered(ti time.Time, r []string) Message
- func NewPlayerKill(ti time.Time, r []string) Message
- func NewPlayerKillAssist(ti time.Time, r []string) Message
- func NewPlayerKilledBomb(ti time.Time, r []string) Message
- func NewPlayerKilledSuicide(ti time.Time, r []string) Message
- func NewPlayerMoneyChange(ti time.Time, r []string) Message
- func NewPlayerPickedUp(ti time.Time, r []string) Message
- func NewPlayerPurchase(ti time.Time, r []string) Message
- func NewPlayerSay(ti time.Time, r []string) Message
- func NewPlayerSwitched(ti time.Time, r []string) Message
- func NewPlayerThrew(ti time.Time, r []string) Message
- func NewProjectileSpawned(ti time.Time, r []string) Message
- func NewServerMessage(ti time.Time, r []string) Message
- func NewTeamNotice(ti time.Time, r []string) Message
- func NewTeamScored(ti time.Time, r []string) Message
- func NewUnknown(ti time.Time, r []string) Message
- func NewWorldGameCommencing(ti time.Time, r []string) Message
- func NewWorldMatchStart(ti time.Time, r []string) Message
- func NewWorldRoundEnd(ti time.Time, r []string) Message
- func NewWorldRoundRestart(ti time.Time, r []string) Message
- func NewWorldRoundStart(ti time.Time, r []string) Message
- func Parse(line string) (Message, error)
- func ParseWithPatterns(line string, patterns map[*regexp.Regexp]MessageFunc) (Message, error)
- type MessageFunc
- type Meta
- type Player
- type PlayerAttack
- type PlayerBanned
- type PlayerBlinded
- type PlayerBombBeginDefuse
- type PlayerBombDefused
- type PlayerBombDropped
- type PlayerBombGot
- type PlayerBombPlanted
- type PlayerConnected
- type PlayerDisconnected
- type PlayerDropped
- type PlayerEntered
- type PlayerKill
- type PlayerKillAssist
- type PlayerKilledBomb
- type PlayerKilledSuicide
- type PlayerMoneyChange
- type PlayerPickedUp
- type PlayerPurchase
- type PlayerSay
- type PlayerSwitched
- type PlayerThrew
- type Position
- type PositionFloat
- type ProjectileSpawned
- type ServerMessage
- type TeamNotice
- type TeamScored
- type Unknown
- type Velocity
- type WorldGameCommencing
- type WorldMatchStart
- type WorldRoundEnd
- type WorldRoundRestart
- type WorldRoundStart
Examples ¶
Constants ¶
const ( // ServerMessagePattern regular expression ServerMessagePattern = `server_message: "(\w+)"` // FreezTimeStartPattern regular expression FreezTimeStartPattern = `Starting Freeze period` // WorldMatchStartPattern regular expression WorldMatchStartPattern = `World triggered "Match_Start" on "(\w+)"` // WorldRoundStartPattern regular expression WorldRoundStartPattern = `World triggered "Round_Start"` // WorldRoundRestartPattern regular expression WorldRoundRestartPattern = `World triggered "Restart_Round_\((\d+)_second\)` // WorldRoundEndPattern regular expression WorldRoundEndPattern = `World triggered "Round_End"` // WorldGameCommencingPattern regular expression WorldGameCommencingPattern = `World triggered "Game_Commencing"` // TeamScoredPattern regular expression TeamScoredPattern = `Team "(CT|TERRORIST)" scored "(\d+)" with "(\d+)" players` // TeamNoticePattern regular expression TeamNoticePattern = `Team "(CT|TERRORIST)" triggered "(\w+)" \(CT "(\d+)"\) \(T "(\d+)"\)` // PlayerConnectedPattern regular expression PlayerConnectedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><>" connected, address "(.*)"` // PlayerDisconnectedPattern regular expression PlayerDisconnectedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT|Unassigned|)>" disconnected \(reason "(.+)"\)` // PlayerEnteredPattern regular expression PlayerEnteredPattern = `"(.+)<(\d+)><([\[\]\w:]+)><>" entered the game` // PlayerBannedPattern regular expression PlayerBannedPattern = `Banid: "(.+)<(\d+)><([\[\]\w:]+)><\w*>" was banned "([\w. ]+)" by "(\w+)"` // PlayerSwitchedPattern regular expression PlayerSwitchedPattern = `"(.+)<(\d+)><([\[\]\w:]+)>" switched from team <(Unassigned|Spectator|TERRORIST|CT)> to <(Unassigned|Spectator|TERRORIST|CT)>` // PlayerSayPattern regular expression PlayerSayPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" say(_team)? "(.*)"` // PlayerPurchasePattern regular expression PlayerPurchasePattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" purchased "(\w+)"` // PlayerKillPattern regular expression PlayerKillPattern = `` /* 215-byte string literal not displayed */ // PlayerKillAssistPattern regular expression PlayerKillAssistPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" assisted killing "(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>"` // PlayerAttackPattern regular expression PlayerAttackPattern = `` /* 270-byte string literal not displayed */ // PlayerKilledBombPattern regular expression PlayerKilledBombPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" \[(-?\d+) (-?\d+) (-?\d+)\] was killed by the bomb\.` // PlayerKilledSuicidePattern regular expression PlayerKilledSuicidePattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" \[(-?\d+) (-?\d+) (-?\d+)\] committed suicide with "(.*)"` // PlayerPickedUpPattern regular expression PlayerPickedUpPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" picked up "(\w+)"` // PlayerDroppedPattern regular expression PlayerDroppedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT|Unassigned)>" dropped "(\w+)"` // PlayerMoneyChangePattern regular expression PlayerMoneyChangePattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" money change (\d+)\+?(-?\d+) = \$(\d+) \(tracked\)( \(purchase: (\w+)\))?` // PlayerBombGotPattern regular expression PlayerBombGotPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" triggered "Got_The_Bomb"` // PlayerBombPlantedPattern regular expression PlayerBombPlantedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" triggered "Planted_The_Bomb"` // PlayerBombDroppedPattern regular expression PlayerBombDroppedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" triggered "Dropped_The_Bomb"` // PlayerBombBeginDefusePattern regular expression PlayerBombBeginDefusePattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" triggered "Begin_Bomb_Defuse_With(out)?_Kit"` // PlayerBombDefusedPattern regular expression PlayerBombDefusedPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" triggered "Defused_The_Bomb"` // PlayerThrewPattern regular expression PlayerThrewPattern = `"(.+)<(\d+)><([\[\]\w:]+)><(TERRORIST|CT)>" threw (\w+) \[(-?\d+) (-?\d+) (-?\d+)\]( flashbang entindex (\d+))?\)?` // PlayerBlindedPattern regular expression PlayerBlindedPattern = `` /* 141-byte string literal not displayed */ // ProjectileSpawnedPattern regular expression ProjectileSpawnedPattern = `Molotov projectile spawned at (-?\d+\.\d+) (-?\d+\.\d+) (-?\d+\.\d+), velocity (-?\d+\.\d+) (-?\d+\.\d+) (-?\d+\.\d+)` // GameOverPattern regular expression GameOverPattern = `Game Over: (\w+) (\w+) (\w+) score (\d+):(\d+) after (\d+) min` )
Variables ¶
var DefaultPatterns = map[*regexp.Regexp]MessageFunc{ regexp.MustCompile(ServerMessagePattern): NewServerMessage, regexp.MustCompile(FreezTimeStartPattern): NewFreezTimeStart, regexp.MustCompile(WorldMatchStartPattern): NewWorldMatchStart, regexp.MustCompile(WorldRoundStartPattern): NewWorldRoundStart, regexp.MustCompile(WorldRoundRestartPattern): NewWorldRoundRestart, regexp.MustCompile(WorldRoundEndPattern): NewWorldRoundEnd, regexp.MustCompile(WorldGameCommencingPattern): NewWorldGameCommencing, regexp.MustCompile(TeamScoredPattern): NewTeamScored, regexp.MustCompile(TeamNoticePattern): NewTeamNotice, regexp.MustCompile(PlayerConnectedPattern): NewPlayerConnected, regexp.MustCompile(PlayerDisconnectedPattern): NewPlayerDisconnected, regexp.MustCompile(PlayerEnteredPattern): NewPlayerEntered, regexp.MustCompile(PlayerBannedPattern): NewPlayerBanned, regexp.MustCompile(PlayerSwitchedPattern): NewPlayerSwitched, regexp.MustCompile(PlayerSayPattern): NewPlayerSay, regexp.MustCompile(PlayerPurchasePattern): NewPlayerPurchase, regexp.MustCompile(PlayerKillPattern): NewPlayerKill, regexp.MustCompile(PlayerKillAssistPattern): NewPlayerKillAssist, regexp.MustCompile(PlayerAttackPattern): NewPlayerAttack, regexp.MustCompile(PlayerKilledBombPattern): NewPlayerKilledBomb, regexp.MustCompile(PlayerKilledSuicidePattern): NewPlayerKilledSuicide, regexp.MustCompile(PlayerPickedUpPattern): NewPlayerPickedUp, regexp.MustCompile(PlayerDroppedPattern): NewPlayerDropped, regexp.MustCompile(PlayerMoneyChangePattern): NewPlayerMoneyChange, regexp.MustCompile(PlayerBombGotPattern): NewPlayerBombGot, regexp.MustCompile(PlayerBombPlantedPattern): NewPlayerBombPlanted, regexp.MustCompile(PlayerBombDroppedPattern): NewPlayerBombDropped, regexp.MustCompile(PlayerBombBeginDefusePattern): NewPlayerBombBeginDefuse, regexp.MustCompile(PlayerBombDefusedPattern): NewPlayerBombDefused, regexp.MustCompile(PlayerThrewPattern): NewPlayerThrew, regexp.MustCompile(PlayerBlindedPattern): NewPlayerBlinded, regexp.MustCompile(ProjectileSpawnedPattern): NewProjectileSpawned, regexp.MustCompile(GameOverPattern): NewGameOver, }
var ErrorNoMatch = errors.New("no match")
ErrorNoMatch error when pattern is not matching
var LogLinePattern = regexp.MustCompile(`L (\d{2}\/\d{2}\/\d{4} - \d{2}:\d{2}:\d{2}): (.*)`)
LogLinePattern is the regular expression to capture a line of a logfile
Functions ¶
func ToJSON ¶
ToJSON marshals messages to JSON without escaping html
Example ¶
package main import ( "fmt" cs2log "github.com/janstuemmel/cs2-log" ) func main() { // parse Message msg, _ := cs2log.Parse(`L 11/05/2018 - 15:44:36: "Player-Name<12><[U:1:29384012]><CT>" purchased "m4a1"`) // cast Message interface type to PlayerPurchase type playerPurchase, _ := msg.(cs2log.PlayerPurchase) fmt.Println(playerPurchase.Player.SteamID) fmt.Println(playerPurchase.Item) // get json non-html-escaped jsn := cs2log.ToJSON(msg) fmt.Println(jsn) }
Output: [U:1:29384012] m4a1 {"time":"2018-11-05T15:44:36Z","type":"PlayerPurchase","player":{"name":"Player-Name","id":12,"steam_id":"[U:1:29384012]","side":"CT"},"item":"m4a1"}
Types ¶
type Equation ¶
Equation holds the parameters and result of a money change equation in the form A + B = Result
type FreezTimeStart ¶
type FreezTimeStart struct{ Meta }
FreezTimeStart is received before each round
type GameOver ¶
type GameOver struct { Meta Mode string `json:"mode"` MapGroup string `json:"map_group"` Map string `json:"map"` ScoreCT int `json:"score_ct"` ScoreT int `json:"score_t"` Duration int `json:"duration"` }
GameOver is received when a team won and the game ends
type Message ¶
Message is the interface for all messages
func Parse ¶
Parse parses a plain log message and returns message type or error if there's no match
Example ¶
package main import ( "fmt" cs2log "github.com/janstuemmel/cs2-log" ) func main() { var msg cs2log.Message // a line from a server logfile line := `L 11/05/2018 - 15:44:36: "Player-Name<12><[U:1:29384012]><CT>" purchased "m4a1"` // parse Message msg, _ = cs2log.Parse(line) fmt.Println(msg.GetType()) fmt.Println(msg.GetTime().String()) }
Output: PlayerPurchase 2018-11-05 15:44:36 +0000 UTC
func ParseWithPatterns ¶
Parse attempts to match a plain log message against the map of provided patterns, if the line matches a key from the map, the corresponding MessageFunc is called on the line to parse it into a Message
type Player ¶
type Player struct { Name string `json:"name"` ID int `json:"id"` SteamID string `json:"steam_id"` Side string `json:"side"` }
Player holds the information about a player known from log
type PlayerAttack ¶
type PlayerAttack struct { Meta Attacker Player `json:"attacker"` AttackerPosition Position `json:"attacker_pos"` Victim Player `json:"victim"` VictimPosition Position `json:"victim_pos"` Weapon string `json:"weapon"` Damage int `json:"damage"` DamageArmor int `json:"damage_armor"` Health int `json:"health"` Armor int `json:"armor"` Hitgroup string `json:"hitgroup"` }
PlayerAttack is recieved when a player attacks another
type PlayerBanned ¶
type PlayerBanned struct { Meta Player Player `json:"player"` Duration string `json:"duration"` By string `json:"by"` }
PlayerBanned is received when a player gots banned from the server
type PlayerBlinded ¶
type PlayerBlinded struct { Meta Attacker Player `json:"attacker"` Victim Player `json:"victim"` For float32 `json:"for"` Entindex int `json:"entindex"` }
PlayerBlinded is received when a player got blinded
type PlayerBombBeginDefuse ¶
PlayerBombBeginDefuse is received when a player begins defusing the bomb
type PlayerBombDefused ¶
PlayerBombDefused is received when a player defused the bomb
type PlayerBombDropped ¶
PlayerBombDropped is received when a player drops the bomb
type PlayerBombGot ¶
PlayerBombGot is received when a player picks up the bomb
type PlayerBombPlanted ¶
PlayerBombPlanted is received when a player plants the bomb
type PlayerConnected ¶
PlayerConnected message is received when a player connects and holds the address from where the player is connecting
type PlayerDisconnected ¶
PlayerDisconnected is received when a player disconnets and holds the reason why the player left
type PlayerDropped ¶
PlayerDropped is recieved when a player drops an item
type PlayerEntered ¶
PlayerEntered is received when a player enters the game
type PlayerKill ¶
type PlayerKill struct { Meta Attacker Player `json:"attacker"` AttackerPosition Position `json:"attacker_pos"` Victim Player `json:"victim"` VictimPosition Position `json:"victim_pos"` Weapon string `json:"weapon"` Headshot bool `json:"headshot"` Penetrated bool `json:"penetrated"` }
PlayerKill is received when a player kills another
type PlayerKillAssist ¶
type PlayerKillAssist struct { Meta Attacker Player `json:"attacker"` Victim Player `json:"victim"` }
PlayerKillAssist is received when a player assisted killing another
type PlayerKilledBomb ¶
PlayerKilledBomb is received when a player is killed by the bomb
type PlayerKilledSuicide ¶
type PlayerKilledSuicide struct { Meta Player Player `json:"player"` Position Position `json:"pos"` With string `json:"with"` }
PlayerKilledSuicide is received when a player commited suicide
type PlayerMoneyChange ¶
type PlayerMoneyChange struct { Meta Player Player `json:"player"` Equation Equation `json:"equation"` Purchase string `json:"purchase"` }
PlayerMoneyChange is received when a player loses or receives money TODO: add before +-money
type PlayerPickedUp ¶
PlayerPickedUp is received when a player picks up an item
type PlayerPurchase ¶
PlayerPurchase holds info about which player bought an item
type PlayerSay ¶
type PlayerSay struct { Meta Player Player `json:"player"` Text string `json:"text"` Team bool `json:"team"` }
PlayerSay is received when a player writes into chat
type PlayerSwitched ¶
type PlayerSwitched struct { Meta Player Player `json:"player"` From string `json:"from"` To string `json:"to"` }
PlayerSwitched is received when a player switches sides
type PlayerThrew ¶
type PlayerThrew struct { Meta Player Player `json:"player"` Position Position `json:"pos"` Entindex int `json:"entindex"` Grenade string `json:"grenade"` }
PlayerThrew is received when a player threw a grenade
type PositionFloat ¶
PositionFloat holds more exact coords
type ProjectileSpawned ¶
type ProjectileSpawned struct { Meta Position PositionFloat `json:"pos"` Velocity Velocity `json:"velocity"` }
ProjectileSpawned is received when a molotov spawned
type ServerMessage ¶
ServerMessage is received on a server event
type TeamNotice ¶
type TeamNotice struct { Meta Side string `json:"side"` Notice string `json:"notice"` ScoreCT int `json:"score_ct"` ScoreT int `json:"score_t"` }
TeamNotice message is received at the end of a round and holds information about which team won the round and the score
type TeamScored ¶
type TeamScored struct { Meta Side string `json:"side"` Score int `json:"score"` NumPlayers int `json:"num_players"` }
TeamScored is received at the end of each round and holds the scores for a team
type Unknown ¶
Unknown holds the raw log message of a message that is not defined in patterns but starts with time
type WorldGameCommencing ¶
type WorldGameCommencing struct{ Meta }
WorldGameCommencing message is received when a game is commencing
type WorldMatchStart ¶
WorldMatchStart holds the map wich will be played when match starts
type WorldRoundEnd ¶
type WorldRoundEnd struct{ Meta }
WorldRoundEnd message is received when a round ends
type WorldRoundRestart ¶
WorldRoundRestart is received when the server wants to restart a round
type WorldRoundStart ¶
type WorldRoundStart struct{ Meta }
WorldRoundStart message is received when a new round starts