Skip to content

Commit 703ddf5

Browse files
committed
Participants: add Connected() function
- makes All() return disconnected players - Connected() now behaves as All() did before
1 parent 86bebf8 commit 703ddf5

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

datatables_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestParser_BindNewPlayer_Issue98(t *testing.T) {
4242
player := fakePlayerEntity(2)
4343
p.bindNewPlayer(player)
4444

45-
assert.Len(t, p.GameState().Participants().All(), 1)
45+
assert.Len(t, p.GameState().Participants().Connected(), 1)
4646
}
4747

4848
func TestParser_BindNewPlayer_Issue98_Reconnect(t *testing.T) {

fake/participants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ func (ptcp *Participants) All() []*common.Player {
2626
return ptcp.Called().Get(0).([]*common.Player)
2727
}
2828

29+
// Connected is a mock-implementation of IParticipants.Connected().
30+
func (ptcp *Participants) Connected() []*common.Player {
31+
return ptcp.Called().Get(0).([]*common.Player)
32+
}
33+
2934
// Playing is a mock-implementation of IParticipants.Playing().
3035
func (ptcp *Participants) Playing() []*common.Player {
3136
return ptcp.Called().Get(0).([]*common.Player)

game_state.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,19 @@ func (ptcp Participants) ByEntityID() map[int]*common.Player {
168168
return res
169169
}
170170

171-
// All returns all currently connected players & spectators.
171+
// All returns all currently known players & spectators, including disconnected ones, of the demo.
172172
// The returned slice is a snapshot and is not updated on changes.
173173
func (ptcp Participants) All() []*common.Player {
174+
res := make([]*common.Player, 0, len(ptcp.playersByUserID))
175+
for _, p := range ptcp.playersByUserID {
176+
res = append(res, p)
177+
}
178+
return res
179+
}
180+
181+
// Connected returns all currently connected players & spectators.
182+
// The returned slice is a snapshot and is not updated on changes.
183+
func (ptcp Participants) Connected() []*common.Player {
174184
res, original := ptcp.initalizeSliceFromByUserID()
175185
for _, p := range original {
176186
res = append(res, p)

game_state_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,28 +134,28 @@ func TestParticipants_FindByHandle_InvalidEntityHandle(t *testing.T) {
134134
assert.Nil(t, found)
135135
}
136136

137-
func TestParticipants_SuppressNoEntity(t *testing.T) {
137+
func TestParticipants_Connected_SuppressNoEntity(t *testing.T) {
138138
gs := newGameState()
139139
pl := newPlayer()
140140
gs.playersByUserID[0] = pl
141141
pl2 := common.NewPlayer(0, func() int { return 0 })
142142
pl2.IsConnected = true
143143
gs.playersByUserID[1] = pl2
144144

145-
allPlayers := gs.Participants().All()
145+
allPlayers := gs.Participants().Connected()
146146

147147
assert.ElementsMatch(t, []*common.Player{pl}, allPlayers)
148148
}
149149

150-
func TestParticipants_SuppressNotConnected(t *testing.T) {
150+
func TestParticipants_Connected_SuppressNotConnected(t *testing.T) {
151151
gs := newGameState()
152152
pl := newPlayer()
153153
gs.playersByUserID[0] = pl
154154
pl2 := newPlayer()
155155
pl2.IsConnected = false
156156
gs.playersByUserID[1] = pl2
157157

158-
allPlayers := gs.Participants().All()
158+
allPlayers := gs.Participants().Connected()
159159

160160
assert.ElementsMatch(t, []*common.Player{pl}, allPlayers)
161161
}

participants_interface.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ type IParticipants interface {
2020
// The returned map is a snapshot and is not updated on changes (not a reference to the actual, underlying map).
2121
// Includes spectators.
2222
ByEntityID() map[int]*common.Player
23-
// All returns all currently connected players & spectators.
23+
// All returns all currently known players & spectators, including disconnected ones, of the demo.
2424
// The returned slice is a snapshot and is not updated on changes.
2525
All() []*common.Player
26+
// Connected returns all currently connected players & spectators.
27+
// The returned slice is a snapshot and is not updated on changes.
28+
Connected() []*common.Player
2629
// Playing returns all players that aren't spectating or unassigned.
2730
// The returned slice is a snapshot and is not updated on changes.
2831
Playing() []*common.Player

0 commit comments

Comments
 (0)