diff --git a/main.go b/main.go index 9c31ad2..02e102e 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "fmt" "math" "os" - "time" demoinfocs "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs" "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/common" @@ -16,10 +15,13 @@ import ( type PlayerStats struct { Name string `json:"name"` SteamID string `json:"steamId"` + Team string `json:"team"` Kills int `json:"kills"` Deaths int `json:"deaths"` Assists int `json:"assists"` FlashAssists int `json:"flashAssists"` + TotalDamage int `json:"totalDamage"` + UtilityDamage int `json:"utilityDamage"` MVPs int `json:"mvps"` MVPReason1 int `json:"mvpEliminations"` MVPReason2 int `json:"mvpDefuse"` @@ -56,7 +58,6 @@ type DemoMeta struct { WinnerTeam string `json:"winnerTeam"` RoundCount int `json:"roundCount"` RoundHistory []RoundResult `json:"roundHistory"` - DemoDate string `json:"demoDate"` } func sanitizeFloat(value float64) float64 { @@ -66,14 +67,6 @@ func sanitizeFloat(value float64) float64 { return value } -func getDemoTimestamp(path string) string { - info, err := os.Stat(path) - if err != nil { - return "" - } - return info.ModTime().UTC().Format("2006-01-02T15:04:05Z") -} - func reasonToString(reason events.RoundEndReason) string { switch reason { case events.RoundEndReasonTargetBombed: @@ -146,22 +139,37 @@ func main() { } playerStats := make(map[uint64]*PlayerStats) + playerInitialTeams := make(map[uint64]string) - getOrCreate := func(sid uint64, name string) *PlayerStats { + getOrCreate := func(player common.Player) *PlayerStats { + sid := player.SteamID64 stat := playerStats[sid] if stat == nil { stat = &PlayerStats{ - Name: name, + Name: player.Name, SteamID: fmt.Sprintf("%d", sid), + Team: "", // später gesetzt } playerStats[sid] = stat } return stat } + p.RegisterEventHandler(func(e events.MatchStart) { + for _, player := range p.GameState().Participants().Playing() { + switch player.Team { + case common.TeamTerrorists: + playerInitialTeams[player.SteamID64] = "T" + case common.TeamCounterTerrorists: + playerInitialTeams[player.SteamID64] = "CT" + } + } + }) + + // Eventhandler (Kill, Hurt, etc.) p.RegisterEventHandler(func(e events.Kill) { if e.Killer != nil && e.Killer.SteamID64 != e.Victim.SteamID64 { - stat := getOrCreate(e.Killer.SteamID64, e.Killer.Name) + stat := getOrCreate(*e.Killer) stat.Kills++ if e.IsHeadshot { stat.Headshots++ @@ -188,25 +196,38 @@ func main() { } } if e.Victim != nil { - stat := getOrCreate(e.Victim.SteamID64, e.Victim.Name) + stat := getOrCreate(*e.Victim) stat.Deaths++ } if e.Assister != nil { - stat := getOrCreate(e.Assister.SteamID64, e.Assister.Name) + stat := getOrCreate(*e.Assister) stat.Assists++ } }) p.RegisterEventHandler(func(e events.PlayerFlashed) { if e.Attacker != nil && e.Attacker.SteamID64 != e.Player.SteamID64 { - stat := getOrCreate(e.Attacker.SteamID64, e.Attacker.Name) + stat := getOrCreate(*e.Attacker) stat.FlashAssists++ } }) + p.RegisterEventHandler(func(e events.PlayerHurt) { + if e.Attacker != nil && e.Attacker != e.Player { + stat := getOrCreate(*e.Attacker) + stat.TotalDamage += e.HealthDamage + if e.Weapon != nil { + switch e.Weapon.Type { + case common.EqHE, common.EqIncendiary, common.EqMolotov: + stat.UtilityDamage += e.HealthDamage + } + } + } + }) + p.RegisterEventHandler(func(e events.RoundMVPAnnouncement) { if e.Player != nil { - stat := getOrCreate(e.Player.SteamID64, e.Player.Name) + stat := getOrCreate(*e.Player) stat.MVPs++ switch e.Reason { case events.MVPReasonMostEliminations: @@ -243,7 +264,7 @@ func main() { p.RegisterEventHandler(func(e events.RankUpdate) { if e.Player != nil { - stat := getOrCreate(e.Player.SteamID64, e.Player.Name) + stat := getOrCreate(*e.Player) stat.RankOld = e.RankOld stat.RankNew = e.RankNew stat.RankChange = int(e.RankChange) @@ -258,9 +279,13 @@ func main() { } teamCT := p.GameState().TeamCounterTerrorists().ClanName() + if teamCT == "" { + teamCT = "CT" + } teamT := p.GameState().TeamTerrorists().ClanName() - scoreCT = p.GameState().TeamCounterTerrorists().Score() - scoreT = p.GameState().TeamTerrorists().Score() + if teamT == "" { + teamT = "T" + } winnerTeam := "Draw" if scoreCT > scoreT { @@ -269,11 +294,6 @@ func main() { winnerTeam = teamT } - demoDate := getDemoTimestamp(filePath) - if demoDate == "" { - demoDate = time.Now().UTC().Format("2006-01-02T15:04:05Z") - } - duration := sanitizeFloat(header.PlaybackTime.Seconds()) tickRate := 0.0 if duration > 0 { @@ -296,17 +316,23 @@ func main() { WinnerTeam: winnerTeam, RoundCount: roundCount, RoundHistory: roundHistory, - DemoDate: demoDate, } - for _, stat := range playerStats { + // Final: Teamzuweisung korrekt, keine Überschreibung + for sid, stat := range playerStats { + if stat.Team == "" || stat.Team == "Unknown" { + if team, ok := playerInitialTeams[sid]; ok { + stat.Team = team + } else { + stat.Team = "Unknown" + } + } result.Players = append(result.Players, *stat) } jsonData, err := json.Marshal(result) if err != nil { fmt.Printf("❌ Fehler beim JSON-Export: %v\n", err) - fmt.Printf("⛔ Dump vor Fehler: %+v\n", result) os.Exit(1) } diff --git a/parser_cs2-linux b/parser_cs2-linux index e8c99a2..d4c164c 100644 Binary files a/parser_cs2-linux and b/parser_cs2-linux differ diff --git a/parser_cs2-win.exe b/parser_cs2-win.exe index 82109bf..c75b200 100644 Binary files a/parser_cs2-win.exe and b/parser_cs2-win.exe differ