Skip to content

Commit e4ab22e

Browse files
authored
Merge pull request #499 from markus-wa/fix-team
fix: possible wrong players team
2 parents a01466f + 22d4801 commit e4ab22e

File tree

2 files changed

+44
-24
lines changed

2 files changed

+44
-24
lines changed

pkg/demoinfocs/datatables.go

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,32 @@ func (p *parser) getOrCreatePlayerFromControllerEntity(controllerEntity st.Entit
514514
func (p *parser) bindNewPlayerControllerS2(controllerEntity st.Entity) {
515515
pl := p.getOrCreatePlayerFromControllerEntity(controllerEntity)
516516

517-
controllerEntity.Property("m_hPawn").OnUpdate(func(val st.PropertyValue) {
518-
if val.Handle() == constants.InvalidEntityHandleSource2 {
519-
pl.IsConnected = false
517+
controllerEntity.Property("m_iConnected").OnUpdate(func(val st.PropertyValue) {
518+
state := val.S2UInt32()
519+
wasConnected := pl.IsConnected
520+
pl.IsConnected = state == 0
521+
522+
isDisconnection := state == 8
523+
if isDisconnection {
524+
for k, v := range p.rawPlayers {
525+
if v.XUID == pl.SteamID64 {
526+
delete(p.rawPlayers, k)
527+
}
528+
}
529+
p.gameEventHandler.dispatch(events.PlayerDisconnected{
530+
Player: pl,
531+
})
532+
533+
return
534+
}
535+
536+
isConnection := !wasConnected && pl.IsConnected
537+
if isConnection {
538+
if pl.SteamID64 != 0 {
539+
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
540+
} else {
541+
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
542+
}
520543
}
521544
})
522545

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

559582
p.bindPlayerWeaponsS2(pawnEntity, pl)
560-
561-
if !pl.IsConnected {
562-
pl.IsConnected = true
563-
if pl.SteamID64 != 0 {
564-
p.eventDispatcher.Dispatch(events.PlayerConnect{Player: pl})
565-
} else {
566-
p.eventDispatcher.Dispatch(events.BotConnect{Player: pl})
567-
}
568-
}
569583
})
570584

571585
// Position
@@ -632,14 +646,6 @@ func (p *parser) bindNewPlayerPawnS2(pawnEntity st.Entity) {
632646
spottedByMaskProp.OnUpdate(spottersChanged)
633647
pawnEntity.Property("m_bSpottedByMask.0001").OnUpdate(spottersChanged)
634648
}
635-
636-
pawnEntity.OnDestroy(func() {
637-
pl := getPlayerFromPawnEntity(pawnEntity)
638-
if pl == nil {
639-
return
640-
}
641-
pl.IsConnected = false
642-
})
643649
}
644650

645651
const maxWeapons = 64

pkg/demoinfocs/game_events.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ func (geh gameEventHandler) HostageRescuedAll(map[string]*msg.CSVCMsg_GameEventK
639639
}
640640

641641
func (geh gameEventHandler) playerConnect(data map[string]*msg.CSVCMsg_GameEventKeyT) {
642+
if geh.parser.isSource2() {
643+
return
644+
}
645+
642646
pl := common.PlayerInfo{
643647
UserID: int(data["userid"].GetValShort()),
644648
Name: data["name"].GetValString(),
@@ -656,12 +660,14 @@ func (geh gameEventHandler) playerConnect(data map[string]*msg.CSVCMsg_GameEvent
656660
}
657661
}
658662

659-
if !geh.parser.isSource2() {
660-
geh.parser.setRawPlayer(int(data["index"].GetValByte()), pl)
661-
}
663+
geh.parser.setRawPlayer(int(data["index"].GetValByte()), pl)
662664
}
663665

664666
func (geh gameEventHandler) playerDisconnect(data map[string]*msg.CSVCMsg_GameEventKeyT) {
667+
if geh.parser.isSource2() {
668+
return
669+
}
670+
665671
uid := int(data["userid"].GetValShort())
666672

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

680-
geh.playerByUserID(uid).IsConnected = false
686+
pl.IsConnected = false
681687
}
682688
}
683689

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

688694
if player != nil {
689695
if player.Team != newTeam {
696+
if geh.parser.isSource2() {
697+
// The "team" field may be incorrect with CS2 demos.
698+
// As the prop m_iTeamNum (bound to player.Team) is updated before the game-event is fired we can force
699+
// the correct team here.
700+
// https://github.com/markus-wa/demoinfocs-golang/issues/494
701+
newTeam = player.Team
702+
}
703+
690704
player.Team = newTeam
691705
}
692706

0 commit comments

Comments
 (0)