Skip to content

Commit

Permalink
Merge pull request #499 from markus-wa/fix-team
Browse files Browse the repository at this point in the history
fix: possible wrong players team
  • Loading branch information
markus-wa authored Feb 8, 2024
2 parents a01466f + 22d4801 commit e4ab22e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
46 changes: 26 additions & 20 deletions pkg/demoinfocs/datatables.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,32 @@ func (p *parser) getOrCreatePlayerFromControllerEntity(controllerEntity st.Entit
func (p *parser) bindNewPlayerControllerS2(controllerEntity st.Entity) {
pl := p.getOrCreatePlayerFromControllerEntity(controllerEntity)

controllerEntity.Property("m_hPawn").OnUpdate(func(val st.PropertyValue) {
if val.Handle() == constants.InvalidEntityHandleSource2 {
pl.IsConnected = false
controllerEntity.Property("m_iConnected").OnUpdate(func(val st.PropertyValue) {
state := val.S2UInt32()
wasConnected := pl.IsConnected
pl.IsConnected = state == 0

isDisconnection := state == 8
if isDisconnection {
for k, v := range p.rawPlayers {
if v.XUID == pl.SteamID64 {
delete(p.rawPlayers, k)
}
}
p.gameEventHandler.dispatch(events.PlayerDisconnected{
Player: pl,
})

return
}

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

Expand Down Expand Up @@ -557,15 +580,6 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
pl := p.getOrCreatePlayerFromControllerEntity(controllerEntity)

p.bindPlayerWeaponsS2(pawnEntity, pl)

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

// Position
Expand Down Expand Up @@ -632,14 +646,6 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
spottedByMaskProp.OnUpdate(spottersChanged)
pawnEntity.Property("m_bSpottedByMask.0001").OnUpdate(spottersChanged)
}

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

const maxWeapons = 64
Expand Down
22 changes: 18 additions & 4 deletions pkg/demoinfocs/game_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,10 @@ func (geh gameEventHandler) HostageRescuedAll(map[string]*msg.CSVCMsg_GameEventK
}

func (geh gameEventHandler) playerConnect(data map[string]*msg.CSVCMsg_GameEventKeyT) {
if geh.parser.isSource2() {
return
}

pl := common.PlayerInfo{
UserID: int(data["userid"].GetValShort()),
Name: data["name"].GetValString(),
Expand All @@ -656,12 +660,14 @@ func (geh gameEventHandler) playerConnect(data map[string]*msg.CSVCMsg_GameEvent
}
}

if !geh.parser.isSource2() {
geh.parser.setRawPlayer(int(data["index"].GetValByte()), pl)
}
geh.parser.setRawPlayer(int(data["index"].GetValByte()), pl)
}

func (geh gameEventHandler) playerDisconnect(data map[string]*msg.CSVCMsg_GameEventKeyT) {
if geh.parser.isSource2() {
return
}

uid := int(data["userid"].GetValShort())

for k, v := range geh.parser.rawPlayers {
Expand All @@ -677,7 +683,7 @@ func (geh gameEventHandler) playerDisconnect(data map[string]*msg.CSVCMsg_GameEv
Player: pl,
})

geh.playerByUserID(uid).IsConnected = false
pl.IsConnected = false
}
}

Expand All @@ -687,6 +693,14 @@ func (geh gameEventHandler) playerTeam(data map[string]*msg.CSVCMsg_GameEventKey

if player != nil {
if player.Team != newTeam {
if geh.parser.isSource2() {
// The "team" field may be incorrect with CS2 demos.
// As the prop m_iTeamNum (bound to player.Team) is updated before the game-event is fired we can force
// the correct team here.
// https://github.com/markus-wa/demoinfocs-golang/issues/494
newTeam = player.Team
}

player.Team = newTeam
}

Expand Down

0 comments on commit e4ab22e

Please sign in to comment.