Skip to content

Commit 1a1e245

Browse files
committed
events: add PlayerSpottersChanged event (#114)
1 parent c95ea92 commit 1a1e245

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

datatables.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,15 @@ func (p *Parser) bindNewPlayer(playerEntity st.IEntity) {
306306
pl.IsDefusing = val.IntVal != 0
307307
})
308308

309+
spottedByMaskProp := playerEntity.FindPropertyI("m_bSpottedByMask.000")
310+
if spottedByMaskProp != nil {
311+
spottersChanged := func(val st.PropertyValue) {
312+
p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})
313+
}
314+
spottedByMaskProp.OnUpdate(spottersChanged)
315+
playerEntity.FindPropertyI("m_bSpottedByMask.001").OnUpdate(spottersChanged)
316+
}
317+
309318
if isNew && pl.SteamID != 0 {
310319
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
311320
}

datatables_test.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,50 @@ func TestParser_BindNewPlayer_Issue98_Reconnect(t *testing.T) {
6767
p.bindNewPlayer(player)
6868

6969
assert.Len(t, p.GameState().Participants().All(), 1)
70+
}
71+
72+
func TestParser_BindNewPlayer_PlayerSpotted_Under32(t *testing.T) {
73+
testPlayerSpotted(t, "m_bSpottedByMask.000")
74+
}
75+
func TestParser_BindNewPlayer_PlayerSpotted_Over32(t *testing.T) {
76+
testPlayerSpotted(t, "m_bSpottedByMask.001")
77+
}
78+
79+
func testPlayerSpotted(t *testing.T, propName string) {
80+
p := newParser()
81+
82+
p.rawPlayers = map[int]*playerInfo{
83+
0: {
84+
userID: 2,
85+
name: "Spotter",
86+
guid: "123",
87+
xuid: 1,
88+
},
89+
}
90+
91+
// TODO: Player interface so we don't have to mock all this
92+
spotted := new(fakest.Entity)
93+
spottedByProp0 := new(fakest.Property)
94+
var spottedByUpdateHandler st.PropertyUpdateHandler
95+
spottedByProp0.On("OnUpdate", mock.Anything).Run(func(args mock.Arguments) {
96+
spottedByUpdateHandler = args.Get(0).(st.PropertyUpdateHandler)
97+
})
98+
spotted.On("FindPropertyI", propName).Return(spottedByProp0)
99+
configurePlayerEntityMock(1, spotted)
100+
p.bindNewPlayer(spotted)
101+
102+
var actual events.PlayerSpottersChanged
103+
p.RegisterEventHandler(func(e events.PlayerSpottersChanged) {
104+
actual = e
105+
})
70106

107+
spottedByUpdateHandler(st.PropertyValue{IntVal: 1})
108+
109+
expected := events.PlayerSpottersChanged{
110+
Spotted: p.gameState.playersByEntityID[1],
111+
}
112+
assert.NotNil(t, expected.Spotted)
113+
assert.Equal(t, expected, actual)
71114
}
72115

73116
func newParser() *Parser {
@@ -76,18 +119,22 @@ func newParser() *Parser {
76119
return p
77120
}
78121

79-
func fakePlayerEntity(id int) st.IEntity {
122+
func fakePlayerEntity(id int) *fakest.Entity {
80123
entity := new(fakest.Entity)
124+
configurePlayerEntityMock(id, entity)
125+
return entity
126+
}
127+
128+
func configurePlayerEntityMock(id int, entity *fakest.Entity) {
81129
entity.On("ID").Return(id)
82130
var destroyCallback func()
83131
entity.On("OnDestroy", mock.Anything).Run(func(args mock.Arguments) {
84132
destroyCallback = args.Get(0).(func())
85133
})
86134
entity.On("OnPositionUpdate", mock.Anything).Return()
87135
entity.On("FindPropertyI", mock.Anything).Return(new(st.Property))
88-
entity.On("BindProperty", mock.Anything, mock.Anything, mock.Anything).Return(new(st.Property))
136+
entity.On("BindProperty", mock.Anything, mock.Anything, mock.Anything)
89137
entity.On("Destroy").Run(func(mock.Arguments) {
90138
destroyCallback()
91139
})
92-
return entity
93140
}

events/events.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,3 +501,8 @@ type IsWarmupPeriodChanged struct {
501501
OldIsWarmupPeriod bool
502502
NewIsWarmupPeriod bool
503503
}
504+
505+
// PlayerSpottersChanged signals that a player's spotters (other players that can se him) changed.
506+
type PlayerSpottersChanged struct {
507+
Spotted *common.Player
508+
}

sendtables/fake/property.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ func (p *Property) Value() st.PropertyValue {
2525

2626
// OnUpdate is a mock-implementation of IProperty.OnUpdate().
2727
func (p *Property) OnUpdate(handler st.PropertyUpdateHandler) {
28-
p.Called()
28+
p.Called(handler)
2929
}
3030

3131
// Bind is a mock-implementation of IProperty.Bind().
3232
func (p *Property) Bind(variable interface{}, valueType st.PropertyValueType) {
33-
p.Called()
33+
p.Called(variable, valueType)
3434
}

0 commit comments

Comments
 (0)