Skip to content

Commit b56361b

Browse files
BestAwperEvermarkus-wa
authored andcommitted
add IParticipants.AllByUserID() method (#167) (#168)
1 parent 3643290 commit b56361b

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

datatables_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@ func testPlayerSpotted(t *testing.T, propName string) {
116116
func newParser() *Parser {
117117
p := NewParser(new(DevNullReader))
118118
p.header = &common.DemoHeader{}
119+
119120
return p
120121
}
121122

122123
func fakePlayerEntity(id int) *fakest.Entity {
123124
entity := new(fakest.Entity)
124125
configurePlayerEntityMock(id, entity)
126+
125127
return entity
126128
}
127129

demoinfocs_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ func BenchmarkConcurrent(b *testing.B) {
361361
func openFile(tb testing.TB, file string) *os.File {
362362
f, err := os.Open(file)
363363
assert.NoError(tb, err, "failed to open file %q", file)
364+
364365
return f
365366
}
366367

@@ -369,6 +370,7 @@ func assertGolden(tb testing.TB, assertions *assert.Assertions, testCase string,
369370
if ver := runtime.Version(); strings.Compare(ver, goldenVerificationGoVersionMin) < 0 {
370371
tb.Logf("old go version %q detected, skipping golden file verification", ver)
371372
tb.Logf("need at least version %q to compare against golden file", goldenVerificationGoVersionMin)
373+
372374
return
373375
}
374376

fake/participants.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ func (ptcp *Participants) ByEntityID() map[int]*common.Player {
2424
return ptcp.Called().Get(0).(map[int]*common.Player)
2525
}
2626

27+
// AllByUserID is a mock-implementation of IParticipants.AllByUserID().
28+
func (ptcp *Participants) AllByUserID() map[int]*common.Player {
29+
return ptcp.Called().Get(0).(map[int]*common.Player)
30+
}
31+
2732
// All is a mock-implementation of IParticipants.All().
2833
func (ptcp *Participants) All() []*common.Player {
2934
return ptcp.Called().Get(0).([]*common.Player)

game_state.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (gs *GameState) Team(team common.Team) *common.TeamState {
5959
} else if team == common.TeamCounterTerrorists {
6060
return &gs.ctState
6161
}
62+
6263
return nil
6364
}
6465

@@ -179,6 +180,7 @@ func (ptcp Participants) ByUserID() map[int]*common.Player {
179180
res[k] = v
180181
}
181182
}
183+
182184
return res
183185
}
184186

@@ -190,6 +192,20 @@ func (ptcp Participants) ByEntityID() map[int]*common.Player {
190192
for k, v := range ptcp.playersByEntityID {
191193
res[k] = v
192194
}
195+
196+
return res
197+
}
198+
199+
// AllByUserID returns all currently known players & spectators, including disconnected ones,
200+
// in a map where the key is the user-ID.
201+
// The returned map is a snapshot and is not updated on changes (not a reference to the actual, underlying map).
202+
// Includes spectators.
203+
func (ptcp Participants) AllByUserID() map[int]*common.Player {
204+
res := make(map[int]*common.Player)
205+
for k, v := range ptcp.playersByUserID {
206+
res[k] = v
207+
}
208+
193209
return res
194210
}
195211

@@ -200,6 +216,7 @@ func (ptcp Participants) All() []*common.Player {
200216
for _, p := range ptcp.playersByUserID {
201217
res = append(res, p)
202218
}
219+
203220
return res
204221
}
205222

@@ -210,6 +227,7 @@ func (ptcp Participants) Connected() []*common.Player {
210227
for _, p := range original {
211228
res = append(res, p)
212229
}
230+
213231
return res
214232
}
215233

@@ -222,6 +240,7 @@ func (ptcp Participants) Playing() []*common.Player {
222240
res = append(res, p)
223241
}
224242
}
243+
225244
return res
226245
}
227246

@@ -234,6 +253,7 @@ func (ptcp Participants) TeamMembers(team common.Team) []*common.Player {
234253
res = append(res, p)
235254
}
236255
}
256+
237257
return res
238258
}
239259

@@ -261,6 +281,7 @@ func (ptcp Participants) SpottersOf(spotted *common.Player) (spotters []*common.
261281
spotters = append(spotters, other)
262282
}
263283
}
284+
264285
return
265286
}
266287

@@ -271,5 +292,6 @@ func (ptcp Participants) SpottedBy(spotter *common.Player) (spotted []*common.Pl
271292
spotted = append(spotted, other)
272293
}
273294
}
295+
274296
return
275297
}

game_state_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,31 @@ func TestGameState_Participants(t *testing.T) {
4141
ptcp := gs.Participants()
4242
byEntity := ptcp.ByEntityID()
4343
byUserID := ptcp.ByUserID()
44+
allByUserID := ptcp.AllByUserID()
4445

4546
// Should update ptcp as well since it uses the same map
4647
gs.playersByEntityID[0] = newPlayer()
4748
gs.playersByUserID[0] = newPlayer()
4849

4950
assert.Equal(t, gs.playersByEntityID, ptcp.ByEntityID())
5051
assert.Equal(t, gs.playersByUserID, ptcp.ByUserID())
52+
assert.Equal(t, gs.playersByUserID, ptcp.AllByUserID())
5153

5254
// But should not update byEntity or byUserID since they're copies
5355
assert.NotEqual(t, byEntity, ptcp.ByEntityID())
54-
assert.NotEqual(t, byUserID, ptcp.ByUserID())
56+
byUserID2 := ptcp.ByUserID()
57+
assert.NotEqual(t, byUserID, byUserID2)
58+
assert.Equal(t, gs.playersByUserID, ptcp.AllByUserID())
59+
60+
gs.playersByEntityID[1] = newDisconnectedPlayer()
61+
gs.playersByUserID[1] = newDisconnectedPlayer()
62+
63+
assert.Equal(t, gs.playersByUserID, ptcp.AllByUserID())
64+
65+
assert.NotEqual(t, byEntity, ptcp.ByEntityID())
66+
// Should be equal since ByUserID() do not return disconnected users
67+
assert.Equal(t, byUserID2, ptcp.ByUserID())
68+
assert.NotEqual(t, allByUserID, ptcp.ByUserID())
5569
}
5670

5771
func TestGameState_ConVars(t *testing.T) {
@@ -264,5 +278,13 @@ func newPlayer() *common.Player {
264278
pl := common.NewPlayer(nil)
265279
pl.Entity = new(st.Entity)
266280
pl.IsConnected = true
281+
282+
return pl
283+
}
284+
285+
func newDisconnectedPlayer() *common.Player {
286+
pl := common.NewPlayer(nil)
287+
pl.Entity = new(st.Entity)
288+
267289
return pl
268290
}

participants_interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ 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+
// AllByUserID returns all currently known players & spectators, including disconnected ones,
24+
// in a map where the key is the user-ID.
25+
// The returned map is a snapshot and is not updated on changes (not a reference to the actual, underlying map).
26+
// Includes spectators.
27+
AllByUserID() map[int]*common.Player
2328
// All returns all currently known players & spectators, including disconnected ones, of the demo.
2429
// The returned slice is a snapshot and is not updated on changes.
2530
All() []*common.Player

0 commit comments

Comments
 (0)