-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.go
708 lines (636 loc) · 22.4 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
package main
import (
"errors"
"log"
"os"
demoinfocs "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs"
"github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/common"
events "github.com/markus-wa/demoinfocs-golang/v4/pkg/demoinfocs/events"
)
func demoParsing(path string) ([]PlayerStats, error) {
f, err := os.Open(path)
checkError(err)
defer f.Close()
var live = 0
p := demoinfocs.NewParser(f)
defer p.Close()
startingOutput := []PlayerStats{}
output := []PlayerStats{}
roundKills := []KillState{}
var allPlayers []*common.Player
//Detects start of game
p.RegisterEventHandler(func(e events.RoundStart) {
pistolRound := true
for _, player := range p.GameState().Participants().Playing() {
if player.Money() != 800 {
pistolRound = false
}
}
if pistolRound {
live++
if live == 1 {
// Match starts
startingOutput = []PlayerStats{}
for _, player := range p.GameState().Participants().Playing() {
allPlayers = append(allPlayers, player)
startingOutput = append(startingOutput, PlayerStats{ID: player.SteamID64, Name: player.Name, Rounds: 0, DetailedStats: DetailedPlayerStats{CTRounds: 0, TRounds: 0}})
}
}
}
})
p.RegisterEventHandler(func(e events.RoundEnd) {
//Counting T and CT Rounds per player (Not working on challengerMode but fuck em)
for i := 0; i < len(startingOutput); i++ {
for j := 0; j < len(startingOutput); j++ {
if startingOutput[i].ID == allPlayers[j].SteamID64 {
if allPlayers[j].Team == 2 {
startingOutput[i].DetailedStats.TRounds++
} else if allPlayers[j].Team == 3 {
startingOutput[i].DetailedStats.CTRounds++
}
break
}
}
}
// VALID ROUND
if live > 0 && len(roundKills) > 0 {
rounds := p.GameState().TeamCounterTerrorists().Score() + p.GameState().TeamTerrorists().Score()
// Initalise round
for i := 0; i < len(startingOutput); i++ {
playerRef := findPlayer(startingOutput[i].ID, p.GameState())
var roundOutcome bool
if playerRef.Team == e.Winner {
roundOutcome = true
} else {
roundOutcome = false
}
startingOutput[i].RoundEvent = append(startingOutput[i].RoundEvent, RoundEvent{RoundNumber: int8(rounds), Died: false, RoundWon: roundOutcome, Side: playerRef.Team})
}
// Clutches
err := determineClutchOutcome(roundKills, p.GameState(), e.Winner, startingOutput)
if err != nil {
log.Printf("Failed to parse clutches for round %d\n", rounds)
}
// Trade kills, Kills, Assists
err = determineTradeKills(roundKills, rounds, startingOutput)
if err != nil {
log.Printf("Failed to parse Trades for round %d\n", rounds)
log.Println(err)
}
// TODO
err = determinePlayerSurvival(roundKills, rounds, startingOutput)
if err != nil {
log.Printf("Failed to parse surviver checks %d\n", rounds)
}
err = determinePlayerKillOrAssist(roundKills, rounds, startingOutput)
if err != nil {
log.Printf("Failed to parse Kill and Assist checks %d\n", rounds)
}
err = determineEntryKill(roundKills, rounds, startingOutput)
if err != nil {
log.Printf("Failed to parse Entry Checks %d\n", rounds)
}
}
roundKills = roundKills[:0]
})
p.RegisterEventHandler(func(e events.Kill) {
// kills = append(kills, e)
if live > 0 && e.Victim != nil {
var targetPlayer uint64
kills = append(kills, e)
victimTeam := e.Victim.TeamState.Members()
if e.Killer != nil {
targetPlayer = e.Killer.SteamID64
TSide := p.GameState().TeamTerrorists().Members()
CTSide := p.GameState().TeamCounterTerrorists().Members()
// Time in seconds since demo started
time := p.CurrentFrame() / int(p.TickRate())
roundKills = append(roundKills, KillState{Killer: e.Killer, Victim: e.Victim, Assister: e.Assister, TSide: TSide, CTSide: CTSide, Time: time})
}
for i, player := range startingOutput {
//Player found in group
if player.ID == targetPlayer {
//Wallbang Kills
if e.IsWallBang() {
startingOutput[i].DetailedStats.KillStats.Wallbangs++
}
//Headshot Kills
if e.IsHeadshot {
startingOutput[i].DetailedStats.KillStats.Headshots++
}
//Blind kills
if e.AttackerBlind {
startingOutput[i].DetailedStats.KillStats.Blind++
}
if e.ThroughSmoke {
startingOutput[i].DetailedStats.KillStats.ThroughSmoke++
}
if e.Killer.IsAirborne() {
startingOutput[i].DetailedStats.KillStats.AirborneKills++
}
//Entry Kill Stats
isEntry := true
for _, player := range victimTeam {
if !player.IsAlive() {
isEntry = false
}
}
if isEntry {
startingOutput[i].DetailedStats.KillStats.Entry.TotalAttempts++
//2 = T side
if e.Killer.Team == 2 {
startingOutput[i].DetailedStats.KillStats.Entry.TEntryKills++
} else {
startingOutput[i].DetailedStats.KillStats.Entry.CTEntryKills++
}
}
}
}
var deadPlayer uint64
if e.Victim != nil {
deadPlayer = e.Victim.SteamID64
}
// Die after getting Kill
deadPlayerIsEntry := true
for _, player := range victimTeam {
if !player.IsAlive() {
deadPlayerIsEntry = false
}
}
// Entry kills Lost
if deadPlayerIsEntry {
for i, player := range startingOutput {
//Player found in group
if player.ID == deadPlayer {
startingOutput[i].DetailedStats.KillStats.Entry.TotalAttempts++
//2 = T side
if e.Killer.Team == 2 {
// T First Deaths
startingOutput[i].DetailedStats.KillStats.Entry.TEntryDeaths++
} else {
// CT First Deaths
startingOutput[i].DetailedStats.KillStats.Entry.CTEntryDeaths++
}
}
}
}
}
})
//CT and T Rounds
//Objectives / Round
//Bomb Plants
p.RegisterEventHandler(func(e events.BombPlanted) {
targetPlayer := e.Player.SteamID64
for i, player := range startingOutput {
if player.ID == targetPlayer {
startingOutput[i].DetailedStats.BombStats.BombsPlanted++
startingOutput[i].DetailedStats.BombStats.BombPlantAttempts++
}
}
})
//Bomb Defused
p.RegisterEventHandler(func(e events.BombDefused) {
targetPlayer := e.Player.SteamID64
for i, player := range startingOutput {
if player.ID == targetPlayer {
startingOutput[i].DetailedStats.BombStats.BombsDefused++
startingOutput[i].DetailedStats.BombStats.BombDefuseAttempts++
}
}
})
//Bomb Plant attempts
p.RegisterEventHandler(func(e events.BombPlantAborted) {
targetPlayer := e.Player.SteamID64
for i, player := range startingOutput {
if player.ID == targetPlayer {
startingOutput[i].DetailedStats.BombStats.BombPlantAttempts++
}
}
})
//Bomb Defused attempts
p.RegisterEventHandler(func(e events.BombDefuseAborted) {
targetPlayer := e.Player.SteamID64
for i, player := range startingOutput {
if player.ID == targetPlayer {
startingOutput[i].DetailedStats.BombStats.BombDefuseAttempts++
}
}
})
//TODO
// Trade Kills
//TODO
// Trade Deaths
p.RegisterEventHandler(func(e events.ItemRefund) {
player := e.Player.SteamID64
for i := 0; i < len(startingOutput); i++ {
if player == startingOutput[i].ID {
startingOutput[i].DetailedStats.RefundTotal++
}
}
})
// //Game ended
p.RegisterEventHandler(func(e events.AnnouncementWinPanelMatch) {
// allPlayers := p.GameState().Participants().Playing()
score := FinalScore{p.GameState().TeamCounterTerrorists().Score(), p.GameState().TeamTerrorists().Score()}
totalRounds := score.CT + score.T
if score.CT < 12 && score.T < 12 {
//Not 1 half has been completed so match is likely void
failed = append(failed, path)
log.Printf(Red+"Demo %s not valid. Too few rounds player\n"+Reset, path)
return
}
for _, player := range allPlayers {
stats := playerStatsCalc(player, totalRounds)
for _, oldPlayer := range startingOutput {
if stats.ID == oldPlayer.ID {
KAST := playerKASTCalc(oldPlayer)
// Create new PlayerStats object
newPlayerStats := PlayerStats{
Name: stats.Name,
TeamName: player.TeamState.ClanName(),
ID: stats.ID,
Rounds: stats.Rounds,
Matches: 1,
Kills: stats.Kills,
Deaths: stats.Deaths,
Damage: stats.Damage,
DetailedStats: oldPlayer.DetailedStats,
RoundEvent: oldPlayer.RoundEvent,
KASTRounds: KAST,
}
// Copy oldPlayer's DetailedStats
// Append newPlayerStats to output
output = append(output, newPlayerStats)
}
}
}
})
// Parse the whole demo
err = p.ParseToEnd()
if err != nil {
log.Println(Red + "Demo ended abruptly! Likely was not a completed game." + Reset)
log.Println(err)
failed = append(failed, path)
return nil, err
}
return output, nil
}
func findPlayerIndex(arr1 []PlayerStats, target *common.Player) (int, error) {
for i, player := range arr1 {
//Player found in group
if player.Name == target.Name {
return i, nil
}
}
return 0, errors.New("ID not found in the slice")
}
func determineClutchOutcome(killEvents []KillState, gameState demoinfocs.GameState, winner common.Team, output []PlayerStats) error {
alivePlayersT := make(map[*common.Player]bool)
alivePlayersCT := make(map[*common.Player]bool)
// Initialize alive players for both teams
for _, player := range gameState.Team(common.TeamTerrorists).Members() {
alivePlayersT[player] = true
}
for _, player := range gameState.Team(common.TeamCounterTerrorists).Members() {
alivePlayersCT[player] = true
}
potentialClutchWinner := &common.Player{}
clutchSituation := 0
oneOnOneSituation := false
for _, kill := range killEvents {
if kill.Victim != nil {
if kill.Victim.Team == common.TeamTerrorists {
delete(alivePlayersT, kill.Victim)
} else {
delete(alivePlayersCT, kill.Victim)
}
}
// Check if either team is in a clutch situation
if len(alivePlayersT) == 1 && len(alivePlayersCT) > 1 {
for player := range alivePlayersT {
potentialClutchWinner = player
}
if clutchSituation == 0 {
clutchSituation = len(alivePlayersCT)
}
// fmt.Printf("1v%d - %s\n", clutchSituation, potentialClutchWinner.Name)
} else if len(alivePlayersCT) == 1 && len(alivePlayersT) > 1 {
for player := range alivePlayersCT {
potentialClutchWinner = player
}
if clutchSituation == 0 {
clutchSituation = len(alivePlayersT)
}
// fmt.Printf("1v%d - %s\n", clutchSituation, potentialClutchWinner.Name)
}
// Detect if it's a 1v1 situation
if len(alivePlayersT) == 1 && len(alivePlayersCT) == 1 {
oneOnOneSituation = true
}
}
// Check the final state of the clutch
if clutchSituation > 0 || oneOnOneSituation {
lastKill := killEvents[len(killEvents)-1]
potentiali, err := findPlayerIndex(output, potentialClutchWinner)
round := gameState.TeamCounterTerrorists().Score() + gameState.TeamTerrorists().Score()
if lastKill.Killer != nil && potentialClutchWinner.Team == winner && err == nil {
if !oneOnOneSituation {
//Apply win on main clutcher
if potentiali != -1 {
// 1.0
output[potentiali].DetailedStats.ClutchStats = append(output[potentiali].DetailedStats.ClutchStats, Clutch{Type: int8(clutchSituation), Won: true, Round: round, Kills: clutchSituation})
// 2.0
target := currentRoundIdx(round, output[potentiali].RoundEvent)
if target != -1 {
output[potentiali].RoundEvent[target].Clutch.Kills = clutchSituation
output[potentiali].RoundEvent[target].Clutch.Won = true
output[potentiali].RoundEvent[target].Clutch.Type = int8(clutchSituation)
}
// fmt.Printf("%s won 1v%d\n", potentialClutchWinner.Name, clutchSituation)
}
return nil
} else {
if potentialClutchWinner.IsAlive() {
//Apply win on main clutcher
// 1.0
output[potentiali].DetailedStats.ClutchStats = append(output[potentiali].DetailedStats.ClutchStats, Clutch{Type: int8(clutchSituation), Won: true, Round: round, Kills: clutchSituation})
// 2.0
target := currentRoundIdx(round, output[potentiali].RoundEvent)
if target != -1 {
output[potentiali].RoundEvent[target].Clutch.Kills = clutchSituation
output[potentiali].RoundEvent[target].Clutch.Won = true
output[potentiali].RoundEvent[target].Clutch.Type = int8(clutchSituation)
}
//Set victim to losing a 1v1
loser, err := findPlayerIndex(output, lastKill.Victim)
if err == nil {
// 1.0
output[loser].DetailedStats.ClutchStats = append(output[loser].DetailedStats.ClutchStats, Clutch{Type: 1, Won: false, Round: round, Kills: 0})
// fmt.Printf("%s won 1v%d -- %s lost 1v1\n", potentialClutchWinner.Name, clutchSituation, lastKill.Victim.Name)
// 2.0
target := currentRoundIdx(round, output[loser].RoundEvent)
if target != -1 {
output[loser].RoundEvent[target].Clutch.Kills = 0
output[loser].RoundEvent[target].Clutch.Won = false
output[loser].RoundEvent[target].Clutch.Type = 1
}
}
} else {
// 1.0
output[potentiali].DetailedStats.ClutchStats = append(output[potentiali].DetailedStats.ClutchStats, Clutch{Type: int8(clutchSituation), Won: true, Round: round, Kills: clutchSituation})
// 2.0
winnerTarget := currentRoundIdx(round, output[potentiali].RoundEvent)
if winnerTarget != -1 {
output[potentiali].RoundEvent[winnerTarget].Clutch.Kills = 0
output[potentiali].RoundEvent[winnerTarget].Clutch.Won = true
output[potentiali].RoundEvent[winnerTarget].Clutch.Type = int8(clutchSituation)
}
loser, err := findPlayerIndex(output, lastKill.Killer)
if err == nil {
// 1.0
output[loser].DetailedStats.ClutchStats = append(output[loser].DetailedStats.ClutchStats, Clutch{Type: 1, Won: false, Round: round, Kills: 1})
// fmt.Printf("%s won 1v%d -- %s lost 1v1\n", potentialClutchWinner.Name, clutchSituation, lastKill.Killer.Name)
// 2.0
loserTarget := currentRoundIdx(round, output[loser].RoundEvent)
if loserTarget != -1 {
output[loser].RoundEvent[loserTarget].Clutch.Kills = 1
output[loser].RoundEvent[loserTarget].Clutch.Won = false
output[loser].RoundEvent[loserTarget].Clutch.Type = 1
}
}
}
return nil
}
} else {
//Lost the clutch
if oneOnOneSituation {
loser, err := findPlayerIndex(output, potentialClutchWinner)
if err == nil {
// 1.0
output[loser].DetailedStats.ClutchStats = append(output[loser].DetailedStats.ClutchStats, Clutch{Type: int8(clutchSituation), Won: false, Round: round, Kills: clutchSituation - 1})
// 2.0
loserTarget := currentRoundIdx(round, output[loser].RoundEvent)
if loserTarget != -1 {
output[loser].RoundEvent[loserTarget].Clutch.Kills = clutchSituation - 1
output[loser].RoundEvent[loserTarget].Clutch.Won = false
output[loser].RoundEvent[loserTarget].Clutch.Type = int8(clutchSituation)
}
}
winner, err := findPlayerIndex(output, lastKill.Killer)
if err == nil {
// 1.0
output[winner].DetailedStats.ClutchStats = append(output[winner].DetailedStats.ClutchStats, Clutch{Type: 1, Won: true, Round: round, Kills: 1})
// fmt.Printf("%s lost 1v%d -- %s won 1v1\n", potentialClutchWinner.Name, clutchSituation, lastKill.Killer.Name)
// 2.0
winnerTarget := currentRoundIdx(round, output[winner].RoundEvent)
if winnerTarget != -1 {
output[winner].RoundEvent[winnerTarget].Clutch.Kills = 1
output[winner].RoundEvent[winnerTarget].Clutch.Won = true
output[winner].RoundEvent[winnerTarget].Clutch.Type = 1
}
}
return nil
} else {
leftAlive := CountAliveOnWinningTeam(killEvents, winner, gameState)
// 1.0
if potentiali != -1 {
output[potentiali].DetailedStats.ClutchStats = append(output[potentiali].DetailedStats.ClutchStats, Clutch{Type: int8(clutchSituation), Won: false, Round: round, Kills: clutchSituation - leftAlive})
}
// fmt.Printf("%s lost 1v%d\n", potentialClutchWinner.Name, clutchSituation)
// 2.0
target := currentRoundIdx(round, output[potentiali].RoundEvent)
if target != -1 {
output[potentiali].RoundEvent[target].Clutch.Kills = clutchSituation
output[potentiali].RoundEvent[target].Clutch.Won = true
output[potentiali].RoundEvent[target].Clutch.Type = int8(clutchSituation)
}
return nil
}
}
}
return nil
}
func CountAliveOnWinningTeam(killEvents []KillState, winningTeam common.Team, gameState demoinfocs.GameState) int {
alivePlayers := make(map[*common.Player]bool)
// Initialize all players on the winning team as alive
for _, player := range gameState.Team(winningTeam).Members() {
alivePlayers[player] = true
}
aliveCount := 5
// Process each kill event
for _, kill := range killEvents {
if kill.Victim != nil && kill.Victim.Team == winningTeam {
aliveCount--
}
}
return aliveCount
}
func playerStatsCalc(player *common.Player, totalRounds int) PlayerStats {
var output = PlayerStats{ID: player.SteamID64, Name: player.Name, Rounds: totalRounds, Kills: player.Kills(), Deaths: player.Deaths(), Assists: player.Assists(), Damage: player.TotalDamage(), Matches: 1}
return output
}
func determineTradeKills(killEvents []KillState, round int, output []PlayerStats) error {
outer:
for i, kill := range killEvents {
// If a player has killed another player
// We want to track...
// If a 2nd player has died to the same person (this is an attempted trade) -
// Also want to track if a player has been traded after they died (traded deaths)
if kill.Killer != nil && kill.Victim != nil {
// Loop through the rest of the kills and see if they have been traded
for j := i + 1; j < len(killEvents); j++ {
potentialTrade := killEvents[j]
if potentialTrade.Killer != nil && potentialTrade.Victim != nil {
// If kills are more than 6 seconds old they arent relevant
if potentialTrade.Time-kill.Time > 6 {
continue outer
}
// Player who got a kill was killed
if potentialTrade.Victim.SteamID64 == kill.Killer.SteamID64 {
traderIdx, err := findPlayerIndex(output, potentialTrade.Killer)
if err != nil {
// log.Printf("player %s not found", kill.Killer.Name)
continue
}
playerTraded, err := findPlayerIndex(output, kill.Victim)
if err != nil {
// log.Printf("player %s not found", kill.Victim.Name)
continue
}
// 1.0
output[traderIdx].DetailedStats.KillStats.TradeStats.TotalAttempts++
// 2.0
traderRoundIdx := currentRoundIdx(round, output[traderIdx].RoundEvent)
tradedPlayerIdx := currentRoundIdx(round, output[playerTraded].RoundEvent)
// Increments players trade kills. Also increments original victims traded deaths.
if traderRoundIdx != -1 {
output[traderIdx].RoundEvent[traderRoundIdx].TradeAttempts++
output[traderIdx].RoundEvent[traderRoundIdx].TradeKills++
}
if tradedPlayerIdx != -1 {
output[playerTraded].RoundEvent[tradedPlayerIdx].DeathTraded = true
}
if potentialTrade.Killer.Team == common.TeamCounterTerrorists {
// 1.0
output[traderIdx].DetailedStats.KillStats.TradeStats.CTTradeKills++
output[playerTraded].DetailedStats.KillStats.TradeStats.CTTradedDeaths++
continue outer
} else {
output[traderIdx].DetailedStats.KillStats.TradeStats.TTradeKills++
output[playerTraded].DetailedStats.KillStats.TradeStats.TTradedDeaths++
continue outer
}
}
// Player who got a kill denied a trade attempt
if potentialTrade.Killer.SteamID64 == kill.Killer.SteamID64 {
failedTrader, err := findPlayerIndex(output, potentialTrade.Victim)
if err != nil {
// log.Printf("player %s not found", kill.Killer.Name)
continue
}
// 2.0
failedIdx := currentRoundIdx(round, output[failedTrader].RoundEvent)
if failedIdx != -1 {
output[failedTrader].RoundEvent[failedIdx].TradeAttempts++
}
// 1.0
output[failedTrader].DetailedStats.KillStats.TradeStats.TotalAttempts++
if potentialTrade.Killer.Team == common.TeamCounterTerrorists {
output[failedTrader].DetailedStats.KillStats.TradeStats.CTFailedTrades++
continue outer
} else {
output[failedTrader].DetailedStats.KillStats.TradeStats.TFailedTrades++
continue outer
}
}
}
}
}
}
return nil
}
func determinePlayerSurvival(killEvents []KillState, roundNumber int, output []PlayerStats) error {
for _, kill := range killEvents {
if kill.Victim != nil {
idx, err := findPlayerIndex(output, kill.Victim)
if err != nil {
continue
}
roundIdx := currentRoundIdx(roundNumber, output[idx].RoundEvent)
if roundIdx != -1 {
output[idx].RoundEvent[roundIdx].Died = true
}
}
}
return nil
}
func determinePlayerKillOrAssist(killEvents []KillState, roundNumber int, output []PlayerStats) error {
for _, kill := range killEvents {
if kill.Killer != nil {
idx, err := findPlayerIndex(output, kill.Killer)
if err != nil {
continue
}
roundIdx := currentRoundIdx(roundNumber, output[idx].RoundEvent)
if roundIdx != -1 {
output[idx].RoundEvent[roundIdx].Kills++
}
}
if kill.Assister != nil {
idx, err := findPlayerIndex(output, kill.Assister)
if err != nil {
continue
}
roundIdx := currentRoundIdx(roundNumber, output[idx].RoundEvent)
if roundIdx != -1 {
output[idx].RoundEvent[roundIdx].Assists++
}
}
}
return nil
}
func determineEntryKill(killEvents []KillState, roundNumber int, output []PlayerStats) error {
for _, entry := range killEvents {
killer, err := findPlayerIndex(output, entry.Killer)
if err != nil {
return err
}
victim, err := findPlayerIndex(output, entry.Victim)
if err != nil {
return err
}
killerRoundIdx := currentRoundIdx(roundNumber, output[killer].RoundEvent)
victimRoundIdx := currentRoundIdx(roundNumber, output[victim].RoundEvent)
if killerRoundIdx != -1 && victimRoundIdx != -1 && entry.Victim != nil {
output[killer].RoundEvent[killerRoundIdx].EntryKill = true
output[victim].RoundEvent[victimRoundIdx].EntryDeath = true
return nil
}
}
return nil
}
func currentRoundIdx(target int, haystack []RoundEvent) int {
for i, curr := range haystack {
if curr.RoundNumber == int8(target) {
return i
}
}
return -1
}
func playerKASTCalc(player PlayerStats) int {
KASTRounds := 0
for _, round := range player.RoundEvent {
if round.Kills > 0 || round.Assists > 0 || !round.Died || round.TradeKills > 0 {
KASTRounds++
}
}
return KASTRounds
}
func findPlayer(target uint64, game demoinfocs.GameState) *common.Player {
for _, player := range game.Participants().All() {
if player.SteamID64 == target {
return player
}
}
return nil
}