Skip to content

Commit afe697a

Browse files
authored
Merge pull request #385 from markus-wa/s2-entities-part2
WIP: s2 entities part 2
2 parents d3c8779 + 23ee21a commit afe697a

33 files changed

+926
-206
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ linters:
1313
disable-all: true
1414
enable:
1515
- bodyclose
16-
- depguard
1716
- dogsled
1817
- dupl
1918
- exportloopref

pkg/demoinfocs/common/common.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ func NewTeamState(team Team, membersCallback func(Team) []*Player) TeamState {
217217
// ConvertSteamIDTxtTo32 converts a Steam-ID in text format to a 32-bit variant.
218218
// See https://developer.valvesoftware.com/wiki/SteamID
219219
func ConvertSteamIDTxtTo32(steamID string) (uint32, error) {
220-
if strings.HasSuffix(steamID, "]") {
221-
steamID = strings.TrimSuffix(steamID, "]") // Source 2 has [U:1:397560266] instead of STEAM_0:1:198780133
222-
}
220+
steamID = strings.TrimSuffix(steamID, "]") // Source 2 has [U:1:397560266] instead of STEAM_0:1:198780133
223221

224222
arr := strings.Split(steamID, ":")
225223

pkg/demoinfocs/common/common_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/golang/geo/r3"
88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
910

1011
st "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/sendtables"
1112
stfake "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/sendtables/fake"
@@ -179,6 +180,10 @@ type demoInfoProviderMock struct {
179180
equipment *Equipment
180181
}
181182

183+
func (p demoInfoProviderMock) FindEntityByHandle(handle uint64) st.Entity {
184+
panic("implement me")
185+
}
186+
182187
func (p demoInfoProviderMock) TickRate() float64 {
183188
return p.tickRate
184189
}
@@ -229,6 +234,8 @@ func entityWithProperty(propName string, value st.PropertyValue) *stfake.Entity
229234
func entityWithProperties(properties []fakeProp) *stfake.Entity {
230235
entity := entityWithID(1)
231236

237+
entity.On("Property", mock.Anything).Return(nil)
238+
232239
for _, prop := range properties {
233240
property := new(stfake.Property)
234241
property.On("Value").Return(prop.value)

pkg/demoinfocs/common/entity_util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ func getInt(entity st.Entity, propName string) int {
77
return 0
88
}
99

10-
return entity.PropertyValueMust(propName).IntVal
10+
return entity.PropertyValueMust(propName).Int()
1111
}
1212

1313
func getFloat(entity st.Entity, propName string) float32 {
1414
if entity == nil {
1515
return 0
1616
}
1717

18-
return entity.PropertyValueMust(propName).FloatVal
18+
return entity.PropertyValueMust(propName).Float()
1919
}
2020

2121
func getString(entity st.Entity, propName string) string {
2222
if entity == nil {
2323
return ""
2424
}
2525

26-
return entity.PropertyValueMust(propName).StringVal
26+
return entity.PropertyValueMust(propName).String()
2727
}
2828

2929
func getBool(entity st.Entity, propName string) bool {

pkg/demoinfocs/common/player.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ type Player struct {
3535
IsUnknown bool // Used to identify unknown/broken players. see https://github.com/markus-wa/demoinfocs-golang/issues/162
3636
}
3737

38+
func (p *Player) PlayerPawnEntity() st.Entity {
39+
return p.demoInfoProvider.FindEntityByHandle(p.Entity.PropertyValueMust("m_hPlayerPawn").Handle())
40+
}
41+
42+
func (p *Player) GetTeam() Team {
43+
return Team(p.PlayerPawnEntity().PropertyValueMust("m_iTeamNum").S2UInt64())
44+
}
45+
46+
func (p *Player) GetFlashDuration() float32 {
47+
return p.PlayerPawnEntity().PropertyValueMust("m_flFlashDuration").Float()
48+
}
49+
3850
// String returns the player's name.
3951
// Implements fmt.Stringer.
4052
func (p *Player) String() string {
@@ -53,6 +65,12 @@ func (p *Player) SteamID32() uint32 {
5365

5466
// IsAlive returns true if the player is alive.
5567
func (p *Player) IsAlive() bool {
68+
s2IsAlive := p.Entity.Property("m_bPawnIsAlive")
69+
70+
if s2IsAlive != nil {
71+
return s2IsAlive.Value().BoolVal()
72+
}
73+
5674
return p.Health() > 0 || getInt(p.Entity, "m_lifeState") == 0
5775
}
5876

@@ -245,6 +263,12 @@ func (p *Player) ControlledBot() *Player {
245263

246264
// Health returns the player's health points, normally 0-100.
247265
func (p *Player) Health() int {
266+
s2Prop := p.Entity.Property("m_iPawnHealth")
267+
268+
if s2Prop != nil {
269+
return int(s2Prop.Value().S2UInt64())
270+
}
271+
248272
return getInt(p.Entity, "m_iHealth")
249273
}
250274

@@ -501,6 +525,7 @@ type demoInfoProvider interface {
501525
FindPlayerByHandle(handle int) *Player
502526
PlayerResourceEntity() st.Entity
503527
FindWeaponByEntityID(id int) *Equipment
528+
FindEntityByHandle(handle uint64) st.Entity
504529
}
505530

506531
// NewPlayer creates a *Player with an initialized equipment map.

0 commit comments

Comments
 (0)