Skip to content

Commit 89faff3

Browse files
committed
feat: add player rank properties
1 parent a5adbe2 commit 89faff3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

pkg/demoinfocs/common/player.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,51 @@ func (p *Player) Armor() int {
368368
return getInt(p.Entity, "m_ArmorValue")
369369
}
370370

371+
// RankType returns the current rank type that the player is playing for.
372+
// CS:GO values:
373+
// -1 -> Information not present, the demo is too old
374+
// 0 -> None/not available
375+
// 6 -> Classic Competitive
376+
// 7 -> Wingman 2v2
377+
// 10 -> Danger zone
378+
//
379+
// CS2 values:
380+
// -1 -> Not available, demo probably not coming from a Valve server
381+
// 0 -> None?
382+
// 11 -> Classic Competitive
383+
func (p *Player) RankType() int {
384+
if p.demoInfoProvider.IsSource2() {
385+
return getInt(p.Entity, "m_iCompetitiveRankType")
386+
}
387+
388+
// This prop is not available in old demos
389+
if prop, exists := p.resourceEntity().PropertyValue("m_iCompetitiveRankType." + p.entityIDStr()); exists {
390+
return prop.Int()
391+
}
392+
393+
return -1
394+
}
395+
396+
// Rank returns the current rank of the player for the current RankType.
397+
// CS:GO demos -> from 0 to 18 (0 = unranked/unknown, 18 = Global Elite)
398+
// CS2 demos -> Number representation of the player's rank.
399+
func (p *Player) Rank() int {
400+
if p.demoInfoProvider.IsSource2() {
401+
return getInt(p.Entity, "m_iCompetitiveRanking")
402+
}
403+
404+
return getInt(p.resourceEntity(), "m_iCompetitiveRanking."+p.entityIDStr())
405+
}
406+
407+
// CompetitiveWins returns the amount of competitive wins the player has for the current RankType.
408+
func (p *Player) CompetitiveWins() int {
409+
if p.demoInfoProvider.IsSource2() {
410+
return getInt(p.Entity, "m_iCompetitiveWins")
411+
}
412+
413+
return getInt(p.resourceEntity(), "m_iCompetitiveWins."+p.entityIDStr())
414+
}
415+
371416
// Money returns the amount of money in the player's bank.
372417
func (p *Player) Money() int {
373418
if p.demoInfoProvider.IsSource2() {

pkg/demoinfocs/common/player_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,24 @@ func TestPlayer_LastPlaceName(t *testing.T) {
561561
assert.Equal(t, "TopofMid", pl.LastPlaceName())
562562
}
563563

564+
func TestPlayer_RankType(t *testing.T) {
565+
pl := playerWithResourceProperty("m_iCompetitiveRankType", st.PropertyValue{IntVal: 6})
566+
567+
assert.Equal(t, 6, pl.RankType())
568+
}
569+
570+
func TestPlayer_Rank(t *testing.T) {
571+
pl := playerWithResourceProperty("m_iCompetitiveRanking", st.PropertyValue{IntVal: 10})
572+
573+
assert.Equal(t, 10, pl.Rank())
574+
}
575+
576+
func TestPlayer_CompetitiveWins(t *testing.T) {
577+
pl := playerWithResourceProperty("m_iCompetitiveWins", st.PropertyValue{IntVal: 190})
578+
579+
assert.Equal(t, 190, pl.CompetitiveWins())
580+
}
581+
564582
func newPlayer(tick int) *Player {
565583
return NewPlayer(mockDemoInfoProvider(128, tick))
566584
}

0 commit comments

Comments
 (0)