Skip to content

Commit 3d0094a

Browse files
committed
common: add function TeamState.Team()
Useful for when we have a TeamState but don't know which team's it is from context. Fixes #76
1 parent 40df288 commit 3d0094a

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

common/common.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func (b Bomb) Position() r3.Vector {
105105

106106
// TeamState contains a team's ID, score, clan name & country flag.
107107
type TeamState struct {
108+
team Team
109+
108110
// ID stays the same even after switching sides.
109111
ID int
110112

@@ -116,3 +118,12 @@ type TeamState struct {
116118
// Watch out, in some demos this is upper-case and in some lower-case.
117119
Flag string
118120
}
121+
122+
// Team returns the team for which the TeamState contains data.
123+
func (ts TeamState) Team() Team {
124+
return ts.team
125+
}
126+
127+
func NewTeamState(team Team) TeamState {
128+
return TeamState{team: team}
129+
}

common/common_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ func TestDemoHeader(t *testing.T) {
3838
assert.Equal(t, float64(128.0), header.TickRate(), "TickRate should be 128")
3939
assert.Equal(t, time.Second/128, header.TickTime(), "TickTime should be 1/128")
4040
}
41+
42+
func TestTeamState(t *testing.T) {
43+
assert.Equal(t, TeamTerrorists, NewTeamState(TeamTerrorists).Team())
44+
assert.Equal(t, TeamCounterTerrorists, NewTeamState(TeamCounterTerrorists).Team())
45+
}

game_state.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ func newGameState() GameState {
110110
grenadeProjectiles: make(map[int]*common.GrenadeProjectile),
111111
infernos: make(map[int]*common.Inferno),
112112
entities: make(map[int]*st.Entity),
113+
tState: common.NewTeamState(common.TeamTerrorists),
114+
ctState: common.NewTeamState(common.TeamCounterTerrorists),
113115
}
114116
}
115117

game_state_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package demoinfocs
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/markus-wa/demoinfocs-golang/common"
9+
)
10+
11+
func TestNewGameState(t *testing.T) {
12+
gs := newGameState()
13+
14+
assert.Equal(t, common.TeamTerrorists, gs.tState.Team())
15+
assert.Equal(t, common.TeamCounterTerrorists, gs.ctState.Team())
16+
}

0 commit comments

Comments
 (0)