Skip to content

Commit

Permalink
fix: retrieve and update correct player after disconnection/bot take …
Browse files Browse the repository at this point in the history
…over

Use the current m_hController value to retrieve the player and move the
logic out of m_hController updates.
  • Loading branch information
akiver committed Nov 11, 2023
1 parent 60ef60a commit 4c2597b
Showing 1 changed file with 75 additions and 45 deletions.
120 changes: 75 additions & 45 deletions pkg/demoinfocs/datatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,15 @@ func (p *parser) bindNewPlayerControllerS2(controllerEntity st.Entity) {
func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
var prevControllerHandle uint64

getPlayerFromPawnEntity := func(pawnEntity st.Entity) *common.Player {
controllerProp, hasProp := pawnEntity.PropertyValue("m_hController")
if !hasProp {
return nil
}

return p.gameState.Participants().FindByHandle64(controllerProp.Handle())
}

pawnEntity.Property("m_hController").OnUpdate(func(controllerHandleVal st.PropertyValue) {
controllerHandle := controllerHandleVal.Handle()
if controllerHandle == constants.InvalidEntityHandleSource2 || controllerHandle == prevControllerHandle {
Expand All @@ -540,65 +549,86 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {

pl := p.getOrCreatePlayerFromControllerEntity(controllerEntity)

if !pl.IsConnected {
pl.IsConnected = true
p.bindPlayerWeaponsS2(pawnEntity, pl)

if pl.SteamID64 != 0 {
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
} else {
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
}
pl.IsConnected = true
if pl.SteamID64 != 0 {
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
} else {
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
}
})

// Position
pawnEntity.OnPositionUpdate(func(pos r3.Vector) {
if pl.IsAlive() {
pl.LastAlivePosition = pos
}
})
// Position
pawnEntity.OnPositionUpdate(func(pos r3.Vector) {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}
if pl.IsAlive() {
pl.LastAlivePosition = pos
}
})

pawnEntity.Property("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
if val.Float() == 0 {
pl.FlashTick = 0
} else {
pl.FlashTick = p.gameState.ingameTick
}
pawnEntity.Property("m_flFlashDuration").OnUpdate(func(val st.PropertyValue) {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}
if val.Float() == 0 {
pl.FlashTick = 0
} else {
pl.FlashTick = p.gameState.ingameTick
}

pl.FlashDuration = val.Float()
pl.FlashDuration = val.Float()

if pl.FlashDuration > 0 {
if len(p.gameState.flyingFlashbangs) == 0 {
return
}

flashbang := p.gameState.flyingFlashbangs[0]
flashbang.flashedEntityIDs = append(flashbang.flashedEntityIDs, pl.EntityID)
if pl.FlashDuration > 0 {
if len(p.gameState.flyingFlashbangs) == 0 {
return
}
})

p.bindPlayerWeaponsS2(pawnEntity, pl)
flashbang := p.gameState.flyingFlashbangs[0]
flashbang.flashedEntityIDs = append(flashbang.flashedEntityIDs, pl.EntityID)
}
})

pawnEntity.Property("m_pWeaponServices.m_hActiveWeapon").OnUpdate(func(val st.PropertyValue) {
pl.IsReloading = false
})
pawnEntity.Property("m_pWeaponServices.m_hActiveWeapon").OnUpdate(func(val st.PropertyValue) {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}
pl.IsReloading = false
})

pawnEntity.Property("m_bIsDefusing").OnUpdate(func(val st.PropertyValue) {
pl.IsDefusing = val.BoolVal()
})
pawnEntity.Property("m_bIsDefusing").OnUpdate(func(val st.PropertyValue) {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}
pl.IsDefusing = val.BoolVal()
})

spottedByMaskProp := pawnEntity.Property("m_bSpottedByMask.0000")
if spottedByMaskProp != nil {
spottersChanged := func(val st.PropertyValue) {
p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})
spottedByMaskProp := pawnEntity.Property("m_bSpottedByMask.0000")
if spottedByMaskProp != nil {
spottersChanged := func(val st.PropertyValue) {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}

spottedByMaskProp.OnUpdate(spottersChanged)
pawnEntity.Property("m_bSpottedByMask.0001").OnUpdate(spottersChanged)
p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})

Check failure on line 619 in pkg/demoinfocs/datatables.go

View workflow job for this annotation

GitHub Actions / golangci-lint

[golangci-lint] pkg/demoinfocs/datatables.go#L619

expressions should not be cuddled with blocks (wsl)
Raw output
pkg/demoinfocs/datatables.go:619:4: expressions should not be cuddled with blocks (wsl)
			p.eventDispatcher.Dispatch(events.PlayerSpottersChanged{Spotted: pl})
			^
}

pawnEntity.OnDestroy(func() {
pl.IsConnected = false
})
spottedByMaskProp.OnUpdate(spottersChanged)
pawnEntity.Property("m_bSpottedByMask.0001").OnUpdate(spottersChanged)
}

pawnEntity.OnDestroy(func() {
pl := getPlayerFromPawnEntity(pawnEntity)
if pl == nil {
return
}
pl.IsConnected = false
})
}

Expand Down

0 comments on commit 4c2597b

Please sign in to comment.