Skip to content

Commit 0515f18

Browse files
committed
refactor GameState.playersByUserID population
1 parent 194fcfa commit 0515f18

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

datatables.go

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,26 +179,42 @@ func (p *Parser) bindPlayers() {
179179
}
180180

181181
func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
182-
var pl *common.Player
183-
playerIndex := playerEntity.ID()
184-
if p.gameState.playersByEntityID[playerIndex] != nil {
185-
pl = p.gameState.playersByEntityID[playerIndex]
186-
} else {
187-
pl = common.NewPlayer()
188-
p.gameState.playersByEntityID[playerIndex] = pl
189-
pl.SteamID = -1
190-
pl.Name = "unconnected"
182+
entityID := playerEntity.ID()
183+
rp := p.rawPlayers[entityID-1]
184+
185+
isNew := false
186+
pl := p.gameState.playersByEntityID[entityID]
187+
if pl == nil {
188+
pl = p.gameState.playersByUserID[rp.userID]
189+
190+
if pl == nil {
191+
isNew = true
192+
193+
pl = common.NewPlayer()
194+
pl.Name = rp.name
195+
pl.SteamID = rp.xuid
196+
pl.IsBot = rp.isFakePlayer
197+
pl.UserID = rp.userID
198+
}
191199
}
200+
p.gameState.playersByEntityID[entityID] = pl
201+
p.gameState.playersByUserID[rp.userID] = pl
192202

193-
pl.EntityID = playerEntity.ID()
203+
pl.EntityID = entityID
194204
pl.Entity = playerEntity
205+
pl.AdditionalPlayerInformation = &p.additionalPlayerInfo[entityID]
195206

196207
playerEntity.OnDestroy(func() {
197-
delete(p.gameState.playersByEntityID, pl.EntityID)
208+
delete(p.gameState.playersByEntityID, entityID)
198209
})
199210

200211
// Position
201-
playerEntity.BindPosition(&pl.Position)
212+
playerEntity.OnPositionUpdate(func(pos r3.Vector) {
213+
pl.Position = pos
214+
if pl.IsAlive() {
215+
pl.LastAlivePosition = pos
216+
}
217+
})
202218

203219
// General info
204220
playerEntity.FindProperty("m_iTeamNum").OnUpdate(func(val st.PropertyValue) {
@@ -279,6 +295,10 @@ func (p *Parser) bindNewPlayer(playerEntity *st.Entity) {
279295
}
280296
pl.IsDefusing = val.IntVal != 0
281297
})
298+
299+
if isNew && pl.SteamID != 0 {
300+
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
301+
}
282302
}
283303

284304
func (p *Parser) bindWeapons() {

parsing.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -245,31 +245,6 @@ type frameParsedTokenType struct{}
245245
var frameParsedToken = new(frameParsedTokenType)
246246

247247
func (p *Parser) handleFrameParsed(*frameParsedTokenType) {
248-
for k, rp := range p.rawPlayers {
249-
// We need to re-map the players from their entityID to their UID.
250-
// This is necessary because we don't always have the UID when the player connects (or something like that, not really sure tbh).
251-
// k+1 for index -> ID
252-
if pl := p.gameState.playersByEntityID[k+1]; pl != nil {
253-
pl.Name = rp.name
254-
pl.SteamID = rp.xuid
255-
pl.IsBot = rp.isFakePlayer
256-
pl.AdditionalPlayerInformation = &p.additionalPlayerInfo[pl.EntityID]
257-
258-
if pl.IsAlive() {
259-
pl.LastAlivePosition = pl.Position
260-
}
261-
262-
if p.gameState.playersByUserID[rp.userID] == nil {
263-
p.gameState.playersByUserID[rp.userID] = pl
264-
pl.UserID = rp.userID
265-
266-
if pl.SteamID != 0 {
267-
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
268-
}
269-
}
270-
}
271-
}
272-
273248
// PlayerFlashed events need to be dispatched at the end of the tick
274249
// because Player.FlashDuration is updated after the game-events are parsed.
275250
for _, e := range p.currentFlashEvents {

0 commit comments

Comments
 (0)