Repository: markus-wa/demoinfocs-golang
Pull request #274 (2021-04-04 11:42:12)

Add Player.CrosshairCode() to pull players current crosshair code
Adds the ability to get a player's crosshair code. 
If the player doesn't have a crosshair (legacy demo) an empty string is returned

codecov[bot]: # [Codecov](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=h1) Report
> Merging [#274](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=desc) (ea5d761) into [master](https://codecov.io/gh/markus-wa/demoinfocs-golang/commit/51e70260f3bba0e6dc2a3952a4174de57fcbe1b0?el=desc) (51e7026) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274/graphs/tree.svg?width=650&height=150&src=pr&token=20emCaZRHz)](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #274      +/-   ##
==========================================
+ Coverage   92.06%   92.08%   +0.01%     
==========================================
  Files          29       29              
  Lines        2459     2463       +4     
==========================================
+ Hits         2264     2268       +4     
  Misses        142      142              
  Partials       53       53              
```


| [Impacted Files](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/demoinfocs/common/player.go](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274/diff?src=pr&el=tree#diff-cGtnL2RlbW9pbmZvY3MvY29tbW9uL3BsYXllci5nbw==) | `97.91% <100.00%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=footer). Last update [51e7026...ea5d761](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/274?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/pkg/demoinfocs/common/player.go b/pkg/demoinfocs/common/player.go
index cb3e738..70ebc66 100644
--- a/pkg/demoinfocs/common/player.go
+++ b/pkg/demoinfocs/common/player.go
@@ -375,6 +375,18 @@ func (p *Player) ClanTag() string {
 	return getString(p.resourceEntity(), "m_szClan."+p.entityIDStr())
 }
 
+// CrosshairCode returns the player's crosshair code or an empty string if there isn't one.
+func (p *Player) CrosshairCode() string {
+	if p.resourceEntity() == nil {
+		return ""
+	}
+
+	// if the property doesn't exist we return empty string by default
+	val, _ := p.resourceEntity().PropertyValue("m_szCrosshairCodes." + p.entityIDStr())
+
+	return val.StringVal
+}
+
 // Ping returns the players latency to the game server.
 func (p *Player) Ping() int {
 	return getInt(p.resourceEntity(), "m_iPing."+p.entityIDStr())


Test patch:
diff --git a/pkg/demoinfocs/common/player_test.go b/pkg/demoinfocs/common/player_test.go
index 6a6ae7d..f758789 100644
--- a/pkg/demoinfocs/common/player_test.go
+++ b/pkg/demoinfocs/common/player_test.go
@@ -377,6 +377,18 @@ func TestPlayer_ClanTag(t *testing.T) {
 	assert.Equal(t, "SuperClan", pl.ClanTag())
 }
 
+func TestPlayer_CrosshairCode(t *testing.T) {
+	pl := playerWithResourceProperty("m_szCrosshairCodes", st.PropertyValue{StringVal: "CSGO-jvnbx-S3xFK-iEJXD-Y27Nd-AO6FP"})
+
+	assert.Equal(t, "CSGO-jvnbx-S3xFK-iEJXD-Y27Nd-AO6FP", pl.CrosshairCode())
+}
+
+func TestPlayer_WithoutCrosshairCode(t *testing.T) {
+	pl := newPlayer(0)
+
+	assert.Equal(t, pl.CrosshairCode(), "")
+}
+
 func TestPlayer_Ping(t *testing.T) {
 	pl := playerWithResourceProperty("m_iPing", st.PropertyValue{IntVal: 45})