@@ -8,9 +8,14 @@ import (
88 "github.com/markus-wa/demoinfocs-golang/common"
99)
1010
11- func TestNewGameState_TeamState_Team (t * testing.T ) {
11+ func TestNewGameState (t * testing.T ) {
1212 gs := newGameState ()
1313
14+ assert .NotNil (t , gs .playersByEntityID )
15+ assert .NotNil (t , gs .playersByUserID )
16+ assert .NotNil (t , gs .grenadeProjectiles )
17+ assert .NotNil (t , gs .infernos )
18+ assert .NotNil (t , gs .entities )
1419 assert .Equal (t , common .TeamTerrorists , gs .tState .Team ())
1520 assert .Equal (t , common .TeamCounterTerrorists , gs .ctState .Team ())
1621}
@@ -28,3 +33,105 @@ func TestNewGameState_TeamState_Opponent(t *testing.T) {
2833 assert .True (t , & gs .ctState == gs .tState .Opponent )
2934 assert .True (t , & gs .tState == gs .ctState .Opponent )
3035}
36+
37+ func TestGameState_Participants (t * testing.T ) {
38+ gs := newGameState ()
39+ ptcp := gs .Participants ()
40+ byEntity := ptcp .ByEntityID ()
41+ byUserID := ptcp .ByUserID ()
42+
43+ // Should update ptcp as well since it uses the same map
44+ gs .playersByEntityID [0 ] = common .NewPlayer ()
45+ gs .playersByUserID [0 ] = common .NewPlayer ()
46+
47+ assert .Equal (t , gs .playersByEntityID , ptcp .ByEntityID ())
48+ assert .Equal (t , gs .playersByUserID , ptcp .ByUserID ())
49+
50+ // But should not update byEntity or byUserID since they're copies
51+ assert .NotEqual (t , byEntity , ptcp .ByEntityID ())
52+ assert .NotEqual (t , byUserID , ptcp .ByUserID ())
53+ }
54+
55+ func TestParticipants_All (t * testing.T ) {
56+ gs := newGameState ()
57+ pl := common .NewPlayer ()
58+ gs .playersByUserID [0 ] = pl
59+
60+ allPlayers := gs .Participants ().All ()
61+
62+ assert .Len (t , allPlayers , 1 )
63+ assert .Equal (t , pl , allPlayers [0 ])
64+ }
65+
66+ func TestParticipants_Playing (t * testing.T ) {
67+ gs := newGameState ()
68+
69+ terrorist := common .NewPlayer ()
70+ terrorist .Team = common .TeamTerrorists
71+ gs .playersByUserID [0 ] = terrorist
72+ ct := common .NewPlayer ()
73+ ct .Team = common .TeamCounterTerrorists
74+ gs .playersByUserID [1 ] = ct
75+ unassigned := common .NewPlayer ()
76+ unassigned .Team = common .TeamUnassigned
77+ gs .playersByUserID [2 ] = unassigned
78+ spectator := common .NewPlayer ()
79+ spectator .Team = common .TeamSpectators
80+ gs .playersByUserID [3 ] = spectator
81+ def := common .NewPlayer ()
82+ gs .playersByUserID [4 ] = def
83+
84+ playing := gs .Participants ().Playing ()
85+
86+ assert .Len (t , playing , 2 )
87+ assert .Equal (t , terrorist , playing [0 ])
88+ assert .Equal (t , ct , playing [1 ])
89+ }
90+
91+ func TestParticipants_TeamMembers (t * testing.T ) {
92+ gs := newGameState ()
93+
94+ terrorist := common .NewPlayer ()
95+ terrorist .Team = common .TeamTerrorists
96+ gs .playersByUserID [0 ] = terrorist
97+ ct := common .NewPlayer ()
98+ ct .Team = common .TeamCounterTerrorists
99+ gs .playersByUserID [1 ] = ct
100+ unassigned := common .NewPlayer ()
101+ unassigned .Team = common .TeamUnassigned
102+ gs .playersByUserID [2 ] = unassigned
103+ spectator := common .NewPlayer ()
104+ spectator .Team = common .TeamSpectators
105+ gs .playersByUserID [3 ] = spectator
106+ def := common .NewPlayer ()
107+ gs .playersByUserID [4 ] = def
108+
109+ playing := gs .Participants ().TeamMembers (common .TeamCounterTerrorists )
110+
111+ assert .Len (t , playing , 1 )
112+ assert .Equal (t , ct , playing [0 ])
113+ }
114+
115+ func TestParticipants_FindByHandle (t * testing.T ) {
116+ gs := newGameState ()
117+
118+ pl := common .NewPlayer ()
119+ pl .Team = common .TeamTerrorists
120+ gs .playersByEntityID [3000 & entityHandleIndexMask ] = pl
121+
122+ found := gs .Participants ().FindByHandle (3000 )
123+
124+ assert .Equal (t , pl , found )
125+ }
126+
127+ func TestParticipants_FindByHandle_InvalidEntityHandle (t * testing.T ) {
128+ gs := newGameState ()
129+
130+ pl := common .NewPlayer ()
131+ pl .Team = common .TeamTerrorists
132+ gs .playersByEntityID [invalidEntityHandle & entityHandleIndexMask ] = pl
133+
134+ found := gs .Participants ().FindByHandle (invalidEntityHandle )
135+
136+ assert .Nil (t , found )
137+ }
0 commit comments