Skip to content

Commit dbf083c

Browse files
committed
common: replaced more bindings with getters
1 parent ae1bd85 commit dbf083c

File tree

14 files changed

+320
-157
lines changed

14 files changed

+320
-157
lines changed

examples/heatmap/heatmap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
var points []r2.Point
4747
p.RegisterEventHandler(func(e events.WeaponFire) {
4848
// Translate positions from in-game coordinates to radar overview image pixels
49-
x, y := mapMetadata.TranslateScale(e.Shooter.Position.X, e.Shooter.Position.Y)
49+
x, y := mapMetadata.TranslateScale(e.Shooter.Position().X, e.Shooter.Position().Y)
5050

5151
points = append(points, r2.Point{X: x, Y: y})
5252
})

examples/print-events/print_events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313
// Run like this: go run print_events.go -demo /path/to/demo.dem
1414
func main() {
1515
f, err := os.Open(ex.DemoPathFromArgs())
16-
defer f.Close()
1716
checkError(err)
1817

18+
defer f.Close()
19+
1920
p := demoinfocs.NewParser(f)
2021

2122
// Parse header
@@ -42,9 +43,9 @@ func main() {
4243
switch e.Winner {
4344
case common.TeamTerrorists:
4445
// Winner's score + 1 because it hasn't actually been updated yet
45-
fmt.Printf("Round finished: winnerSide=T ; score=%d:%d\n", gs.TeamTerrorists().Score+1, gs.TeamCounterTerrorists().Score)
46+
fmt.Printf("Round finished: winnerSide=T ; score=%d:%d\n", gs.TeamTerrorists().Score()+1, gs.TeamCounterTerrorists().Score())
4647
case common.TeamCounterTerrorists:
47-
fmt.Printf("Round finished: winnerSide=CT ; score=%d:%d\n", gs.TeamCounterTerrorists().Score+1, gs.TeamTerrorists().Score)
48+
fmt.Printf("Round finished: winnerSide=CT ; score=%d:%d\n", gs.TeamCounterTerrorists().Score()+1, gs.TeamTerrorists().Score())
4849
default:
4950
// Probably match medic or something similar
5051
fmt.Println("Round finished: No winner (tie)")

pkg/demoinfocs/common/common.go

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type Bomb struct {
104104
// or LastOnGroundPosition if it's dropped or planted.
105105
func (b Bomb) Position() r3.Vector {
106106
if b.Carrier != nil {
107-
return b.Carrier.Position
107+
return b.Carrier.Position()
108108
}
109109

110110
return b.LastOnGroundPosition
@@ -115,16 +115,7 @@ type TeamState struct {
115115
team Team
116116
membersCallback func(Team) []*Player
117117

118-
// ID stays the same even after switching sides.
119-
ID int
120-
121-
Score int
122-
ClanName string
123-
124-
// Flag, e.g. DE, FR, etc.
125-
//
126-
// Watch out, in some demos this is upper-case and in some lower-case.
127-
Flag string
118+
Entity st.IEntity
128119

129120
// Terrorist TeamState for CTs, CT TeamState for Terrorists
130121
Opponent *TeamState
@@ -135,15 +126,37 @@ func (ts TeamState) Team() Team {
135126
return ts.team
136127
}
137128

138-
// Members returns the members of the team.
129+
// ID returns the team ID, this stays the same even after switching sides.
130+
func (ts TeamState) ID() int {
131+
return getInt(ts.Entity, "m_iTeamNum")
132+
}
133+
134+
// Score returns the current score of the team (usually 0-16 without overtime).
135+
func (ts TeamState) Score() int {
136+
return getInt(ts.Entity, "m_scoreTotal")
137+
}
138+
139+
// ClanName returns the team name (e.g. Fnatic).
140+
func (ts TeamState) ClanName() string {
141+
return getString(ts.Entity, "m_szClanTeamname")
142+
}
143+
144+
// Flag returns the flag code (e.g. DE, FR, etc.).
145+
//
146+
// Watch out, in some demos this is upper-case and in some lower-case.
147+
func (ts TeamState) Flag() string {
148+
return getString(ts.Entity, "m_szTeamFlagImage")
149+
}
150+
151+
// Members returns the players that are members of the team.
139152
func (ts TeamState) Members() []*Player {
140153
return ts.membersCallback(ts.team)
141154
}
142155

143156
// CurrentEquipmentValue returns the cumulative value of all equipment currently owned by the members of the team.
144157
func (ts TeamState) CurrentEquipmentValue() (value int) {
145158
for _, pl := range ts.Members() {
146-
value += pl.CurrentEquipmentValue
159+
value += pl.EquipmentValueCurrent()
147160
}
148161

149162
return
@@ -152,7 +165,7 @@ func (ts TeamState) CurrentEquipmentValue() (value int) {
152165
// RoundStartEquipmentValue returns the cumulative value of all equipment owned by the members of the team at the start of the current round.
153166
func (ts TeamState) RoundStartEquipmentValue() (value int) {
154167
for _, pl := range ts.Members() {
155-
value += pl.RoundStartEquipmentValue
168+
value += pl.EquipmentValueRoundStart()
156169
}
157170

158171
return
@@ -161,25 +174,25 @@ func (ts TeamState) RoundStartEquipmentValue() (value int) {
161174
// FreezeTimeEndEquipmentValue returns the cumulative value of all equipment owned by the members of the team at the end of the freeze-time of the current round.
162175
func (ts TeamState) FreezeTimeEndEquipmentValue() (value int) {
163176
for _, pl := range ts.Members() {
164-
value += pl.FreezetimeEndEquipmentValue
177+
value += pl.EquipmentValueFreezeTimeEnd()
165178
}
166179

167180
return
168181
}
169182

170-
// CashSpentThisRound returns the total amount of cash spent by the whole team in the current round.
171-
func (ts TeamState) CashSpentThisRound() (value int) {
183+
// MoneySpentThisRound returns the total amount of cash spent by the whole team in the current round.
184+
func (ts TeamState) MoneySpentThisRound() (value int) {
172185
for _, pl := range ts.Members() {
173-
value += pl.AdditionalInformation.CashSpentThisRound
186+
value += pl.AdditionalInformation.MoneySpentThisRound
174187
}
175188

176189
return
177190
}
178191

179-
// CashSpentThisRound returns the total amount of cash spent by the whole team during the whole game up to the current point.
180-
func (ts TeamState) CashSpentTotal() (value int) {
192+
// MoneySpentThisRound returns the total amount of cash spent by the whole team during the whole game up to the current point.
193+
func (ts TeamState) MoneySpentTotal() (value int) {
181194
for _, pl := range ts.Members() {
182-
value += pl.AdditionalInformation.CashSpentTotal
195+
value += pl.AdditionalInformation.MoneySpentTotal
183196
}
184197

185198
return

pkg/demoinfocs/common/common_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ func TestBombPosition(t *testing.T) {
2020
assert.Equal(t, groundPos, bomb.Position(), "Bomb position should be LastOnGroundPosition")
2121

2222
playerPos := r3.Vector{X: 4, Y: 5, Z: 6}
23-
bomb.Carrier = &Player{Position: playerPos}
23+
24+
plEntity := entityWithID(1)
25+
plEntity.On("Position").Return(playerPos)
26+
27+
bomb.Carrier = &Player{Entity: plEntity}
2428
assert.Equal(t, playerPos, bomb.Position(), "Bomb position should be Player.Position")
2529
}
2630

@@ -59,45 +63,54 @@ func TestTeamState_Members(t *testing.T) {
5963
assert.Equal(t, members, state.Members())
6064
}
6165

62-
func TestTeamState_CurrentEquipmentValue(t *testing.T) {
63-
members := []*Player{{CurrentEquipmentValue: 100}, {CurrentEquipmentValue: 200}}
66+
func TestTeamState_EquipmentValueCurrent(t *testing.T) {
67+
members := []*Player{
68+
playerWithProperty("m_unCurrentEquipmentValue", st.PropertyValue{IntVal: 100}),
69+
playerWithProperty("m_unCurrentEquipmentValue", st.PropertyValue{IntVal: 200}),
70+
}
6471
state := NewTeamState(TeamTerrorists, func(Team) []*Player { return members })
6572

6673
assert.Equal(t, 300, state.CurrentEquipmentValue())
6774
}
6875

69-
func TestTeamState_RoundStartEquipmentValue(t *testing.T) {
70-
members := []*Player{{RoundStartEquipmentValue: 100}, {RoundStartEquipmentValue: 200}}
76+
func TestTeamState_EquipmentValueRoundStart(t *testing.T) {
77+
members := []*Player{
78+
playerWithProperty("m_unRoundStartEquipmentValue", st.PropertyValue{IntVal: 100}),
79+
playerWithProperty("m_unRoundStartEquipmentValue", st.PropertyValue{IntVal: 200}),
80+
}
7181
state := NewTeamState(TeamTerrorists, func(Team) []*Player { return members })
7282

7383
assert.Equal(t, 300, state.RoundStartEquipmentValue())
7484
}
7585

76-
func TestTeamState_FreezeTimeEndEquipmentValue(t *testing.T) {
77-
members := []*Player{{FreezetimeEndEquipmentValue: 100}, {FreezetimeEndEquipmentValue: 200}}
86+
func TestTeamState_EquipmentValueFreezeTimeEnd(t *testing.T) {
87+
members := []*Player{
88+
playerWithProperty("m_unFreezetimeEndEquipmentValue", st.PropertyValue{IntVal: 100}),
89+
playerWithProperty("m_unFreezetimeEndEquipmentValue", st.PropertyValue{IntVal: 200}),
90+
}
7891
state := NewTeamState(TeamTerrorists, func(Team) []*Player { return members })
7992

8093
assert.Equal(t, 300, state.FreezeTimeEndEquipmentValue())
8194
}
8295

83-
func TestTeamState_CashSpentThisRound(t *testing.T) {
96+
func TestTeamState_MoneySpentThisRound(t *testing.T) {
8497
members := []*Player{
85-
{AdditionalInformation: &AdditionalPlayerInformation{CashSpentThisRound: 100}},
86-
{AdditionalInformation: &AdditionalPlayerInformation{CashSpentThisRound: 200}},
98+
{AdditionalInformation: &AdditionalPlayerInformation{MoneySpentThisRound: 100}},
99+
{AdditionalInformation: &AdditionalPlayerInformation{MoneySpentThisRound: 200}},
87100
}
88101
state := NewTeamState(TeamTerrorists, func(Team) []*Player { return members })
89102

90-
assert.Equal(t, 300, state.CashSpentThisRound())
103+
assert.Equal(t, 300, state.MoneySpentThisRound())
91104
}
92105

93-
func TestTeamState_CashSpentTotal(t *testing.T) {
106+
func TestTeamState_MoneySpentTotal(t *testing.T) {
94107
members := []*Player{
95-
{AdditionalInformation: &AdditionalPlayerInformation{CashSpentTotal: 100}},
96-
{AdditionalInformation: &AdditionalPlayerInformation{CashSpentTotal: 200}},
108+
{AdditionalInformation: &AdditionalPlayerInformation{MoneySpentTotal: 100}},
109+
{AdditionalInformation: &AdditionalPlayerInformation{MoneySpentTotal: 200}},
97110
}
98111
state := NewTeamState(TeamTerrorists, func(Team) []*Player { return members })
99112

100-
assert.Equal(t, 300, state.CashSpentTotal())
113+
assert.Equal(t, 300, state.MoneySpentTotal())
101114
}
102115

103116
type demoInfoProviderMock struct {
@@ -125,9 +138,15 @@ func mockDemoInfoProvider(tickRate float64, tick int) demoInfoProvider {
125138
}
126139
}
127140

128-
func entityWithProperty(propName string, value st.PropertyValue) st.IEntity {
141+
func entityWithID(id int) *stfake.Entity {
129142
entity := new(stfake.Entity)
130-
entity.On("ID").Return(1)
143+
entity.On("ID").Return(id)
144+
145+
return entity
146+
}
147+
148+
func entityWithProperty(propName string, value st.PropertyValue) *stfake.Entity {
149+
entity := entityWithID(1)
131150

132151
prop := new(stfake.Property)
133152
prop.On("Value").Return(value)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package common
2+
3+
import st "github.com/markus-wa/demoinfocs-golang/v2/pkg/demoinfocs/sendtables"
4+
5+
func getInt(entity st.IEntity, propName string) int {
6+
if entity == nil {
7+
return 0
8+
}
9+
10+
return entity.PropertyValueMust(propName).IntVal
11+
}
12+
13+
func getFloat(entity st.IEntity, propName string) float32 {
14+
if entity == nil {
15+
return 0
16+
}
17+
18+
return entity.PropertyValueMust(propName).FloatVal
19+
}
20+
21+
func getString(entity st.IEntity, propName string) string {
22+
if entity == nil {
23+
return ""
24+
}
25+
26+
return entity.PropertyValueMust(propName).StringVal
27+
}
28+
29+
func getBool(entity st.IEntity, propName string) bool {
30+
if entity == nil {
31+
return false
32+
}
33+
34+
return entity.PropertyValueMust(propName).BoolVal()
35+
}

pkg/demoinfocs/common/equipment.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,17 @@ func (e Equipment) AmmoInMagazine() int {
328328

329329
// AmmoType returns the weapon's ammo type, mostly (only?) relevant for grenades.
330330
func (e Equipment) AmmoType() int {
331-
return e.Entity.PropertyValueMust("LocalWeaponData.m_iPrimaryAmmoType").IntVal
331+
return getInt(e.Entity, "LocalWeaponData.m_iPrimaryAmmoType")
332332
}
333333

334334
// ZoomLevel returns how far the player has zoomed in on the weapon.
335335
// Only weapons with scopes have a valid zoom level.
336336
func (e Equipment) ZoomLevel() ZoomLevel {
337-
val, _ := e.Entity.PropertyValue("LocalWeaponData.m_iPrimaryAmmoType")
337+
if e.Entity == nil {
338+
return 0
339+
}
340+
341+
val, _ := e.Entity.PropertyValue("m_zoomLevel")
338342
return ZoomLevel(val.IntVal)
339343
}
340344

0 commit comments

Comments
 (0)