Skip to content

Commit a0e62dd

Browse files
authored
move Player.ResourceEntity() to GameState.PlayerResourceEntity() (#266)
1 parent d98d320 commit a0e62dd

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -352,67 +352,72 @@ func (p *Player) Flags() PlayerFlags {
352352
return PlayerFlags(getInt(p.Entity, "m_fFlags"))
353353
}
354354

355-
/////////////////////////////
355+
// //////////////////////////
356356
// CCSPlayerResource stuff //
357-
/////////////////////////////
357+
// //////////////////////////
358358

359359
func (p *Player) entityIDStr() string {
360360
return fmt.Sprintf("%03d", p.EntityID)
361361
}
362362

363-
// ResourceEntity returns the player's CCSPlayerResource entity.
363+
// ResourceEntity returns the game's CCSPlayerResource entity.
364+
// Deprecated: use GameState.PlayerResourceEntity() instead.
364365
func (p *Player) ResourceEntity() st.Entity {
366+
return p.resourceEntity()
367+
}
368+
369+
func (p *Player) resourceEntity() st.Entity {
365370
return p.demoInfoProvider.PlayerResourceEntity()
366371
}
367372

368373
// ClanTag returns the player's individual clan tag (Steam Groups etc.).
369374
func (p *Player) ClanTag() string {
370-
return getString(p.ResourceEntity(), "m_szClan."+p.entityIDStr())
375+
return getString(p.resourceEntity(), "m_szClan."+p.entityIDStr())
371376
}
372377

373378
// Ping returns the players latency to the game server.
374379
func (p *Player) Ping() int {
375-
return getInt(p.ResourceEntity(), "m_iPing."+p.entityIDStr())
380+
return getInt(p.resourceEntity(), "m_iPing."+p.entityIDStr())
376381
}
377382

378383
// Score returns the players score as shown on the scoreboard.
379384
func (p *Player) Score() int {
380-
return getInt(p.ResourceEntity(), "m_iScore."+p.entityIDStr())
385+
return getInt(p.resourceEntity(), "m_iScore."+p.entityIDStr())
381386
}
382387

383388
// Color returns the players color as shown on the match.
384389
func (p *Player) Color() Color {
385-
return Color(getInt(p.ResourceEntity(), "m_iCompTeammateColor."+p.entityIDStr()))
390+
return Color(getInt(p.resourceEntity(), "m_iCompTeammateColor."+p.entityIDStr()))
386391
}
387392

388393
// Kills returns the amount of kills the player has as shown on the scoreboard.
389394
func (p *Player) Kills() int {
390-
return getInt(p.ResourceEntity(), "m_iKills."+p.entityIDStr())
395+
return getInt(p.resourceEntity(), "m_iKills."+p.entityIDStr())
391396
}
392397

393398
// Deaths returns the amount of deaths the player has as shown on the scoreboard.
394399
func (p *Player) Deaths() int {
395-
return getInt(p.ResourceEntity(), "m_iDeaths."+p.entityIDStr())
400+
return getInt(p.resourceEntity(), "m_iDeaths."+p.entityIDStr())
396401
}
397402

398403
// Assists returns the amount of assists the player has as shown on the scoreboard.
399404
func (p *Player) Assists() int {
400-
return getInt(p.ResourceEntity(), "m_iAssists."+p.entityIDStr())
405+
return getInt(p.resourceEntity(), "m_iAssists."+p.entityIDStr())
401406
}
402407

403408
// MVPs returns the amount of Most-Valuable-Player awards the player has as shown on the scoreboard.
404409
func (p *Player) MVPs() int {
405-
return getInt(p.ResourceEntity(), "m_iMVPs."+p.entityIDStr())
410+
return getInt(p.resourceEntity(), "m_iMVPs."+p.entityIDStr())
406411
}
407412

408413
// MoneySpentTotal returns the total amount of money the player has spent in the current match.
409414
func (p *Player) MoneySpentTotal() int {
410-
return getInt(p.ResourceEntity(), "m_iTotalCashSpent."+p.entityIDStr())
415+
return getInt(p.resourceEntity(), "m_iTotalCashSpent."+p.entityIDStr())
411416
}
412417

413418
// MoneySpentThisRound returns the amount of money the player has spent in the current round.
414419
func (p *Player) MoneySpentThisRound() int {
415-
return getInt(p.ResourceEntity(), "m_iCashSpentThisRound."+p.entityIDStr())
420+
return getInt(p.resourceEntity(), "m_iCashSpentThisRound."+p.entityIDStr())
416421
}
417422

418423
type demoInfoProvider interface {

pkg/demoinfocs/datatables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (p *parser) bindPlayers() {
165165
})
166166

167167
p.stParser.ServerClasses().FindByName("CCSPlayerResource").OnEntityCreated(func(entity st.Entity) {
168-
p.playerResourceEntity = entity
168+
p.gameState.playerResourceEntity = entity
169169
})
170170
}
171171

pkg/demoinfocs/fake/game_state.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ func (gs *GameState) ConVars() map[string]string {
9494
func (gs *GameState) Rules() demoinfocs.GameRules {
9595
return gs.Called().Get(0).(demoinfocs.GameRules)
9696
}
97+
98+
// PlayerResorceEntity is a mock-implementation of GameState.PlayerResorceEntity().
99+
func (gs *GameState) PlayerResourceEntity() st.Entity {
100+
return gs.Called().Get(0).(st.Entity)
101+
}

pkg/demoinfocs/game_state.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,27 @@ import (
1616

1717
// gameState contains all game-state relevant information.
1818
type gameState struct {
19-
ingameTick int
20-
tState common.TeamState
21-
ctState common.TeamState
22-
playersByUserID map[int]*common.Player // Maps user-IDs to players
23-
playersByEntityID map[int]*common.Player // Maps entity-IDs to players
24-
grenadeProjectiles map[int]*common.GrenadeProjectile // Maps entity-IDs to active nade-projectiles. That's grenades that have been thrown, but have not yet detonated.
25-
infernos map[int]*common.Inferno // Maps entity-IDs to active infernos.
26-
weapons map[int]*common.Equipment // Maps entity IDs to weapons. Used to remember what a weapon is (p250 / cz etc.)
27-
entities map[int]st.Entity // Maps entity IDs to entities
28-
bomb common.Bomb
29-
totalRoundsPlayed int
30-
gamePhase common.GamePhase
31-
isWarmupPeriod bool
32-
isMatchStarted bool
33-
lastFlash lastFlash // Information about the last flash that exploded, used to find the attacker and projectile for player_blind events
34-
currentDefuser *common.Player // Player currently defusing the bomb, if any
35-
currentPlanter *common.Player // Player currently planting the bomb, if any
36-
thrownGrenades map[*common.Player][]*common.Equipment // Information about every player's thrown grenades (from the moment they are thrown to the moment their effect is ended)
37-
rules gameRules
38-
demoInfo demoInfoProvider
19+
ingameTick int
20+
tState common.TeamState
21+
ctState common.TeamState
22+
playersByUserID map[int]*common.Player // Maps user-IDs to players
23+
playersByEntityID map[int]*common.Player // Maps entity-IDs to players
24+
playerResourceEntity st.Entity // CCSPlayerResource entity instance, contains scoreboard info and more
25+
grenadeProjectiles map[int]*common.GrenadeProjectile // Maps entity-IDs to active nade-projectiles. That's grenades that have been thrown, but have not yet detonated.
26+
infernos map[int]*common.Inferno // Maps entity-IDs to active infernos.
27+
weapons map[int]*common.Equipment // Maps entity IDs to weapons. Used to remember what a weapon is (p250 / cz etc.)
28+
entities map[int]st.Entity // Maps entity IDs to entities
29+
bomb common.Bomb
30+
totalRoundsPlayed int
31+
gamePhase common.GamePhase
32+
isWarmupPeriod bool
33+
isMatchStarted bool
34+
lastFlash lastFlash // Information about the last flash that exploded, used to find the attacker and projectile for player_blind events
35+
currentDefuser *common.Player // Player currently defusing the bomb, if any
36+
currentPlanter *common.Player // Player currently planting the bomb, if any
37+
thrownGrenades map[*common.Player][]*common.Equipment // Information about every player's thrown grenades (from the moment they are thrown to the moment their effect is ended)
38+
rules gameRules
39+
demoInfo demoInfoProvider
3940
}
4041

4142
type lastFlash struct {
@@ -157,6 +158,12 @@ func (gs *gameState) ConVars() map[string]string {
157158
return gs.rules.ConVars()
158159
}
159160

161+
// PlayerResourceEntity returns the game's CCSPlayerResource entity.
162+
// Contains scoreboard information and more.
163+
func (gs *gameState) PlayerResourceEntity() st.Entity {
164+
return gs.playerResourceEntity
165+
}
166+
160167
func newGameState(demoInfo demoInfoProvider) *gameState {
161168
gs := &gameState{
162169
playersByEntityID: make(map[int]*common.Player),

pkg/demoinfocs/game_state_interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ type GameState interface {
6060
// See also: https://developer.valvesoftware.com/wiki/List_of_CS:GO_Cvars.
6161
// Deprecated: see GameRules().ConVars()
6262
ConVars() map[string]string
63+
// PlayerResourceEntity returns the game's CCSPlayerResource entity.
64+
// Contains scoreboard information and more.
65+
PlayerResourceEntity() st.Entity
6366
}

pkg/demoinfocs/parser.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ type parser struct {
7575
grenadeModelIndices map[int]common.EquipmentType // Used to map model indices to grenades (used for grenade projectiles)
7676
stringTables []*msg.CSVCMsg_CreateStringTable // Contains all created sendtables, needed when updating them
7777
delayedEventHandlers []func() // Contains event handlers that need to be executed at the end of a tick (e.g. flash events because FlashDuration isn't updated before that)
78-
playerResourceEntity st.Entity // CCSPlayerResource entity instance, contains scoreboard info and more
7978
}
8079

8180
// NetMessageCreator creates additional net-messages to be dispatched to net-message handlers.
@@ -362,5 +361,5 @@ func (p demoInfoProvider) FindPlayerByHandle(handle int) *common.Player {
362361
}
363362

364363
func (p demoInfoProvider) PlayerResourceEntity() st.Entity {
365-
return p.parser.playerResourceEntity
364+
return p.parser.gameState.playerResourceEntity
366365
}

0 commit comments

Comments
 (0)