Skip to content

Commit a830fb3

Browse files
committed
events: add TeamState where applicable
1 parent 3d0094a commit a830fb3

File tree

7 files changed

+79
-25
lines changed

7 files changed

+79
-25
lines changed

common/common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ type TeamState struct {
117117
//
118118
// Watch out, in some demos this is upper-case and in some lower-case.
119119
Flag string
120+
121+
// Terrorist TeamState for CTs, CT TeamState for Terrorists
122+
Opponent *TeamState
120123
}
121124

122125
// Team returns the team for which the TeamState contains data.

events/events.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ const (
6161
// Attention: TeamState.Score() won't be up to date yet after this.
6262
// Add +1 to the winner's score as a workaround.
6363
type RoundEnd struct {
64-
Message string
65-
Reason RoundEndReason
66-
Winner common.Team
64+
Message string
65+
Reason RoundEndReason
66+
Winner common.Team
67+
WinnerState *common.TeamState // TeamState of the winner. May be nil if it's a draw (Winner == TeamSpectators)
68+
LoserState *common.TeamState // TeamState of the loser. May be nil if it's a draw (Winner == TeamSpectators)
6769
}
6870

6971
// RoundEndOfficial signals that the round has 'officially' ended.
@@ -107,11 +109,20 @@ type Footstep struct {
107109

108110
// PlayerTeamChange occurs when a player swaps teams.
109111
type PlayerTeamChange struct {
110-
Player *common.Player
112+
Player *common.Player
113+
111114
NewTeam common.Team
115+
// TeamState of the old team.
116+
// May be nil if player changed from spectators/unassigned (OldTeam == TeamSpectators || OldTeam == TeamUnassigned).
117+
NewTeamState *common.TeamState
118+
112119
OldTeam common.Team
113-
Silent bool
114-
IsBot bool
120+
// TeamState of the old team.
121+
// May be nil if player changed from spectators/unassigned (OldTeam == TeamSpectators || OldTeam == TeamUnassigned).
122+
OldTeamState *common.TeamState
123+
124+
Silent bool
125+
IsBot bool
115126
}
116127

117128
// PlayerJump signals that a player has jumped.

game_events.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ func (p *Parser) handleGameEvent(ge *msg.CSVCMsg_GameEvent) {
5959
case "round_end": // Round ended and the winner was announced
6060
data = mapGameEventData(d, ge)
6161

62-
t := common.TeamSpectators
63-
64-
switch data["winner"].GetValByte() {
65-
case int32(p.gameState.tState.ID):
66-
t = common.TeamTerrorists
67-
case int32(p.gameState.ctState.ID):
68-
t = common.TeamCounterTerrorists
62+
winner := common.Team(data["winner"].ValByte)
63+
winnerState := p.gameState.Team(winner)
64+
var loserState *common.TeamState
65+
if winnerState != nil {
66+
loserState = winnerState.Opponent
6967
}
7068

7169
p.eventDispatcher.Dispatch(events.RoundEnd{
72-
Message: data["message"].GetValString(),
73-
Reason: events.RoundEndReason(data["reason"].GetValByte()),
74-
Winner: t,
70+
Message: data["message"].GetValString(),
71+
Reason: events.RoundEndReason(data["reason"].GetValByte()),
72+
Winner: winner,
73+
WinnerState: winnerState,
74+
LoserState: loserState,
7575
})
7676

7777
case "round_officially_ended": // The event after which you get teleported to the spawn (=> You can still walk around between round_end and this event)
@@ -270,12 +270,15 @@ func (p *Parser) handleGameEvent(ge *msg.CSVCMsg_GameEvent) {
270270
if player.Team != newTeam {
271271
player.Team = newTeam
272272

273+
oldTeam := common.Team(data["oldteam"].GetValByte())
273274
p.eventDispatcher.Dispatch(events.PlayerTeamChange{
274-
Player: player,
275-
IsBot: data["isbot"].GetValBool(),
276-
Silent: data["silent"].GetValBool(),
277-
NewTeam: newTeam,
278-
OldTeam: common.Team(data["oldteam"].GetValByte()),
275+
Player: player,
276+
IsBot: data["isbot"].GetValBool(),
277+
Silent: data["silent"].GetValBool(),
278+
NewTeam: newTeam,
279+
NewTeamState: p.gameState.Team(newTeam),
280+
OldTeam: oldTeam,
281+
OldTeamState: p.gameState.Team(oldTeam),
279282
})
280283
} else {
281284
p.eventDispatcher.Dispatch(events.ParserWarn{

game_state.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ func (gs GameState) IngameTick() int {
4141
return gs.ingameTick
4242
}
4343

44+
// Team returns the TeamState corresponding to team.
45+
// Returns nil if team != TeamTerrorists && team != TeamCounterTerrorists.
46+
//
47+
// Make sure to handle swapping sides properly if you keep the reference.
48+
func (gs *GameState) Team(team common.Team) *common.TeamState {
49+
if team == common.TeamTerrorists {
50+
return &gs.tState
51+
} else if team == common.TeamCounterTerrorists {
52+
return &gs.ctState
53+
}
54+
return nil
55+
}
56+
4457
// TeamCounterTerrorists returns the TeamState of the CT team.
4558
//
4659
// Make sure to handle swapping sides properly if you keep the reference.
@@ -103,8 +116,8 @@ func (gs GameState) IsMatchStarted() bool {
103116
return gs.isMatchStarted
104117
}
105118

106-
func newGameState() GameState {
107-
return GameState{
119+
func newGameState() *GameState {
120+
gs := &GameState{
108121
playersByEntityID: make(map[int]*common.Player),
109122
playersByUserID: make(map[int]*common.Player),
110123
grenadeProjectiles: make(map[int]*common.GrenadeProjectile),
@@ -113,6 +126,11 @@ func newGameState() GameState {
113126
tState: common.NewTeamState(common.TeamTerrorists),
114127
ctState: common.NewTeamState(common.TeamCounterTerrorists),
115128
}
129+
130+
gs.tState.Opponent = &gs.ctState
131+
gs.ctState.Opponent = &gs.tState
132+
133+
return gs
116134
}
117135

118136
// Participants provides helper functions on top of the currently connected players.

game_state_interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type IGameState interface {
1414
//
1515
// Watch out, I've seen this return wonky negative numbers at the start of demos.
1616
IngameTick() int
17+
// Team returns the TeamState corresponding to team.
18+
// Returns nil if team != TeamTerrorists && team != TeamCounterTerrorists.
19+
//
20+
// Make sure to handle swapping sides properly if you keep the reference.
21+
Team(team common.Team) *common.TeamState
1722
// TeamCounterTerrorists returns the TeamState of the CT team.
1823
//
1924
// Make sure to handle swapping sides properly if you keep the reference.

game_state_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,23 @@ import (
88
"github.com/markus-wa/demoinfocs-golang/common"
99
)
1010

11-
func TestNewGameState(t *testing.T) {
11+
func TestNewGameState_TeamState_Team(t *testing.T) {
1212
gs := newGameState()
1313

1414
assert.Equal(t, common.TeamTerrorists, gs.tState.Team())
1515
assert.Equal(t, common.TeamCounterTerrorists, gs.ctState.Team())
1616
}
17+
18+
func TestNewGameState_TeamState_Pointers(t *testing.T) {
19+
gs := newGameState()
20+
21+
assert.True(t, gs.TeamCounterTerrorists() == &gs.ctState)
22+
assert.True(t, gs.TeamTerrorists() == &gs.tState)
23+
}
24+
25+
func TestNewGameState_TeamState_Opponent(t *testing.T) {
26+
gs := newGameState()
27+
28+
assert.True(t, &gs.ctState == gs.tState.Opponent)
29+
assert.True(t, &gs.tState == gs.ctState.Opponent)
30+
}

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func NewParserWithConfig(demostream io.Reader, config ParserConfig) *Parser {
232232
p.rawPlayers = make(map[int]*playerInfo)
233233
p.triggers = make(map[int]*boundingBoxInformation)
234234
p.cancelChan = make(chan struct{}, 1)
235-
p.gameState = newGameState()
235+
p.gameState = *newGameState()
236236
p.grenadeModelIndices = make(map[int]common.EquipmentElement)
237237

238238
// Attach proto msg handlers

0 commit comments

Comments
 (0)