Skip to content

Commit e89ab3b

Browse files
committed
common: changed some receiver types from struct to pointer
1 parent ba9c09c commit e89ab3b

File tree

4 files changed

+38
-35
lines changed

4 files changed

+38
-35
lines changed

pkg/demoinfocs/common/common.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type DemoHeader struct {
4242
// Not necessarily the tick-rate the server ran on during the game.
4343
//
4444
// Returns 0 if PlaybackTime or PlaybackFrames are 0 (corrupt demo headers).
45-
func (h DemoHeader) FrameRate() float64 {
45+
func (h *DemoHeader) FrameRate() float64 {
4646
if h.PlaybackTime == 0 {
4747
return 0
4848
}
@@ -53,7 +53,7 @@ func (h DemoHeader) FrameRate() float64 {
5353
// FrameTime returns the time a frame / demo-tick takes in seconds.
5454
//
5555
// Returns 0 if PlaybackTime or PlaybackFrames are 0 (corrupt demo headers).
56-
func (h DemoHeader) FrameTime() time.Duration {
56+
func (h *DemoHeader) FrameTime() time.Duration {
5757
if h.PlaybackFrames == 0 {
5858
return 0
5959
}
@@ -75,14 +75,14 @@ type GrenadeProjectile struct {
7575
}
7676

7777
// Position returns the current position of the grenade projectile in world coordinates.
78-
func (g GrenadeProjectile) Position() r3.Vector {
78+
func (g *GrenadeProjectile) Position() r3.Vector {
7979
return g.Entity.Position()
8080
}
8181

8282
// UniqueID returns the unique id of the grenade.
8383
// The unique id is a random int generated internally by this library and can be used to differentiate
8484
// grenades from each other. This is needed because demo-files reuse entity ids.
85-
func (g GrenadeProjectile) UniqueID() int64 {
85+
func (g *GrenadeProjectile) UniqueID() int64 {
8686
return g.uniqueID
8787
}
8888

@@ -104,7 +104,7 @@ type Bomb struct {
104104
// Position returns the current position of the bomb.
105105
// This is either the position of the player holding it
106106
// or LastOnGroundPosition if it's dropped or planted.
107-
func (b Bomb) Position() r3.Vector {
107+
func (b *Bomb) Position() r3.Vector {
108108
if b.Carrier != nil {
109109
return b.Carrier.Position()
110110
}
@@ -124,39 +124,39 @@ type TeamState struct {
124124
}
125125

126126
// Team returns the team for which the TeamState contains data.
127-
func (ts TeamState) Team() Team {
127+
func (ts *TeamState) Team() Team {
128128
return ts.team
129129
}
130130

131131
// ID returns the team ID, this stays the same even after switching sides.
132-
func (ts TeamState) ID() int {
132+
func (ts *TeamState) ID() int {
133133
return getInt(ts.Entity, "m_iTeamNum")
134134
}
135135

136136
// Score returns the current score of the team (usually 0-16 without overtime).
137-
func (ts TeamState) Score() int {
137+
func (ts *TeamState) Score() int {
138138
return getInt(ts.Entity, "m_scoreTotal")
139139
}
140140

141141
// ClanName returns the team name (e.g. Fnatic).
142-
func (ts TeamState) ClanName() string {
142+
func (ts *TeamState) ClanName() string {
143143
return getString(ts.Entity, "m_szClanTeamname")
144144
}
145145

146146
// Flag returns the flag code (e.g. DE, FR, etc.).
147147
//
148148
// Watch out, in some demos this is upper-case and in some lower-case.
149-
func (ts TeamState) Flag() string {
149+
func (ts *TeamState) Flag() string {
150150
return getString(ts.Entity, "m_szTeamFlagImage")
151151
}
152152

153153
// Members returns the players that are members of the team.
154-
func (ts TeamState) Members() []*Player {
154+
func (ts *TeamState) Members() []*Player {
155155
return ts.membersCallback(ts.team)
156156
}
157157

158158
// CurrentEquipmentValue returns the cumulative value of all equipment currently owned by the members of the team.
159-
func (ts TeamState) CurrentEquipmentValue() (value int) {
159+
func (ts *TeamState) CurrentEquipmentValue() (value int) {
160160
for _, pl := range ts.Members() {
161161
value += pl.EquipmentValueCurrent()
162162
}
@@ -165,7 +165,7 @@ func (ts TeamState) CurrentEquipmentValue() (value int) {
165165
}
166166

167167
// RoundStartEquipmentValue returns the cumulative value of all equipment owned by the members of the team at the start of the current round.
168-
func (ts TeamState) RoundStartEquipmentValue() (value int) {
168+
func (ts *TeamState) RoundStartEquipmentValue() (value int) {
169169
for _, pl := range ts.Members() {
170170
value += pl.EquipmentValueRoundStart()
171171
}
@@ -174,7 +174,7 @@ func (ts TeamState) RoundStartEquipmentValue() (value int) {
174174
}
175175

176176
// 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.
177-
func (ts TeamState) FreezeTimeEndEquipmentValue() (value int) {
177+
func (ts *TeamState) FreezeTimeEndEquipmentValue() (value int) {
178178
for _, pl := range ts.Members() {
179179
value += pl.EquipmentValueFreezeTimeEnd()
180180
}
@@ -183,7 +183,7 @@ func (ts TeamState) FreezeTimeEndEquipmentValue() (value int) {
183183
}
184184

185185
// MoneySpentThisRound returns the total amount of cash spent by the whole team in the current round.
186-
func (ts TeamState) MoneySpentThisRound() (value int) {
186+
func (ts *TeamState) MoneySpentThisRound() (value int) {
187187
for _, pl := range ts.Members() {
188188
value += pl.MoneySpentThisRound()
189189
}
@@ -192,7 +192,7 @@ func (ts TeamState) MoneySpentThisRound() (value int) {
192192
}
193193

194194
// MoneySpentThisRound returns the total amount of cash spent by the whole team during the whole game up to the current point.
195-
func (ts TeamState) MoneySpentTotal() (value int) {
195+
func (ts *TeamState) MoneySpentTotal() (value int) {
196196
for _, pl := range ts.Members() {
197197
value += pl.MoneySpentTotal()
198198
}

pkg/demoinfocs/common/common_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ func TestDemoHeader(t *testing.T) {
4444
}
4545

4646
func TestDemoHeader_FrameRate_PlaybackTime_Zero(t *testing.T) {
47-
assert.Zero(t, DemoHeader{}.FrameRate())
47+
assert.Zero(t, new(DemoHeader).FrameRate())
4848
}
4949

5050
func TestDemoHeader_FrameTime_PlaybackFrames_Zero(t *testing.T) {
51-
assert.Zero(t, DemoHeader{}.FrameTime())
51+
assert.Zero(t, new(DemoHeader).FrameTime())
5252
}
5353

5454
func TestTeamState_Team(t *testing.T) {
55-
assert.Equal(t, TeamTerrorists, NewTeamState(TeamTerrorists, nil).Team())
56-
assert.Equal(t, TeamCounterTerrorists, NewTeamState(TeamCounterTerrorists, nil).Team())
55+
tState := NewTeamState(TeamTerrorists, nil)
56+
ctState := NewTeamState(TeamCounterTerrorists, nil)
57+
58+
assert.Equal(t, TeamTerrorists, tState.Team())
59+
assert.Equal(t, TeamCounterTerrorists, ctState.Team())
5760
}
5861

5962
func TestTeamState_Members(t *testing.T) {

pkg/demoinfocs/common/equipment.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,26 @@ type Equipment struct {
291291

292292
// String returns a human readable name for the equipment.
293293
// E.g. 'AK-47', 'UMP-45', 'Smoke Grenade' etc.
294-
func (e Equipment) String() string {
294+
func (e *Equipment) String() string {
295295
return e.Type.String()
296296
}
297297

298298
// Class returns the class of the equipment.
299299
// E.g. pistol, smg, heavy etc.
300-
func (e Equipment) Class() EquipmentClass {
300+
func (e *Equipment) Class() EquipmentClass {
301301
return e.Type.Class()
302302
}
303303

304304
// UniqueID returns the unique id of the equipment element.
305305
// The unique id is a random int generated internally by this library and can be used to differentiate
306306
// equipment from each other. This is needed because demo-files reuse entity ids.
307-
func (e Equipment) UniqueID() int64 {
307+
func (e *Equipment) UniqueID() int64 {
308308
return e.uniqueID
309309
}
310310

311311
// AmmoInMagazine returns the ammo left in the magazine.
312312
// Returns CWeaponCSBase.m_iClip1 for most weapons and 1 for grenades.
313-
func (e Equipment) AmmoInMagazine() int {
313+
func (e *Equipment) AmmoInMagazine() int {
314314
if e.Class() == EqClassGrenade {
315315
return 1
316316
}
@@ -329,13 +329,13 @@ func (e Equipment) AmmoInMagazine() int {
329329
}
330330

331331
// AmmoType returns the weapon's ammo type, mostly (only?) relevant for grenades.
332-
func (e Equipment) AmmoType() int {
332+
func (e *Equipment) AmmoType() int {
333333
return getInt(e.Entity, "LocalWeaponData.m_iPrimaryAmmoType")
334334
}
335335

336336
// ZoomLevel returns how far the player has zoomed in on the weapon.
337337
// Only weapons with scopes have a valid zoom level.
338-
func (e Equipment) ZoomLevel() ZoomLevel {
338+
func (e *Equipment) ZoomLevel() ZoomLevel {
339339
if e.Entity == nil {
340340
return 0
341341
}
@@ -349,7 +349,7 @@ func (e Equipment) ZoomLevel() ZoomLevel {
349349
// AmmoReserve returns the ammo left available for reloading.
350350
// Returns CWeaponCSBase.m_iPrimaryReserveAmmoCount for most weapons and 'Owner.AmmoLeft[AmmoType] - 1' for grenades.
351351
// Use AmmoInMagazine() + AmmoReserve() to quickly get the amount of grenades a player owns.
352-
func (e Equipment) AmmoReserve() int {
352+
func (e *Equipment) AmmoReserve() int {
353353
if e.Class() == EqClassGrenade {
354354
if e.Owner != nil {
355355
// minus one for 'InMagazine'

pkg/demoinfocs/common/inferno.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,19 @@ type Fires struct {
4040
// UniqueID returns the unique id of the inferno.
4141
// The unique id is a random int generated internally by this library and can be used to differentiate
4242
// infernos from each other. This is needed because demo-files reuse entity ids.
43-
func (inf Inferno) UniqueID() int64 {
43+
func (inf *Inferno) UniqueID() int64 {
4444
return inf.uniqueID
4545
}
4646

47+
// Thrower returns the player who threw the fire grenade.
48+
// Could be nil if the player disconnected after throwing it.
49+
func (inf *Inferno) Thrower() *Player {
50+
return inf.demoInfoProvider.FindPlayerByHandle(inf.Entity.Property("m_hOwnerEntity").Value().IntVal)
51+
}
52+
4753
// Fires returns all fires (past + present).
4854
// Some are currently active and some have extinguished (see Fire.IsBurning).
49-
func (inf Inferno) Fires() Fires {
55+
func (inf *Inferno) Fires() Fires {
5056
entity := inf.Entity
5157
origin := entity.Position()
5258
nFires := entity.PropertyValueMust("m_fireCount").IntVal
@@ -173,12 +179,6 @@ func (f Fires) ConvexHull3D() quickhull.ConvexHull {
173179
return convexHull(pointCloud)
174180
}
175181

176-
// Thrower returns the player who threw the fire grenade.
177-
// Could be nil if the player disconnected after throwing it.
178-
func (inf Inferno) Thrower() *Player {
179-
return inf.demoInfoProvider.FindPlayerByHandle(inf.Entity.Property("m_hOwnerEntity").Value().IntVal)
180-
}
181-
182182
func convexHull(pointCloud []r3.Vector) quickhull.ConvexHull {
183183
return new(quickhull.QuickHull).ConvexHull(pointCloud, false, false, 0)
184184
}

0 commit comments

Comments
 (0)