Repository: markus-wa/demoinfocs-golang
Pull request #250 (2020-11-08 10:03:07)

add color function for common.Player
(#248)

Worked perfectly on a demo I tested =>

> CT Black Fart Yellow
> CT KMA Purple
> T Ryba Orange
> CT Xelf Blue
> CT Cheuveulu Orange
> T Nozyyy🦁 Yellow
> T Slink1019 Blue
> T Kamik Green
> T Stog CS.DEALS Purple
> CT socio* linux <3 Green

Let me know how should I test that ?

codecov[bot]: # [Codecov](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250?src=pr&el=h1) Report
> Merging [#250](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250?src=pr&el=desc) (b5ee041) into [master](https://codecov.io/gh/markus-wa/demoinfocs-golang/commit/a6c8bb9ba201bd924a5a22e937733bd7460951e0?el=desc) (a6c8bb9) will **decrease** coverage by `0.15%`.
> The diff coverage is `0.00%`.

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

```diff
@@            Coverage Diff             @@
##           master     #250      +/-   ##
==========================================
- Coverage   92.31%   92.15%   -0.16%     
==========================================
  Files          28       28              
  Lines        2368     2372       +4     
==========================================
  Hits         2186     2186              
- Misses        129      133       +4     
  Partials       53       53              
```


| [Impacted Files](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/demoinfocs/common/common.go](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250/diff?src=pr&el=tree#diff-cGtnL2RlbW9pbmZvY3MvY29tbW9uL2NvbW1vbi5nbw==) | `90.90% <0.00%> (-6.66%)` | :arrow_down: |
| [pkg/demoinfocs/common/player.go](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250/diff?src=pr&el=tree#diff-cGtnL2RlbW9pbmZvY3MvY29tbW9uL3BsYXllci5nbw==) | `96.38% <0.00%> (-1.18%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250?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/250?src=pr&el=footer). Last update [a6c8bb9...b5ee041](https://codecov.io/gh/markus-wa/demoinfocs-golang/pull/250?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Julien2313: I'll check to do the tests later today ;)

No problem for the delay.
Julien2313: ![image](https://user-images.githubusercontent.com/8778771/99180602-e094fb80-2727-11eb-9be8-9fbfdaaf8b70.png)

Finally here ! ha ha
Julien2313: `VectorVal, ArrayVal, StringVal, FloatVal are missing in PropertyValue (exhaustivestruct)` Mmmh, is that normal ?
markus-wa: @Julien2313 - I think I disabled that check on master, just merged that back in. Let's see what the new build says 🤞
Julien2313: RIP ha ha
markus-wa: oops that was just on a branch (#249)

let's try that again

Patch:
diff --git a/pkg/demoinfocs/common/common.go b/pkg/demoinfocs/common/common.go
index 680178b..ca3b970 100644
--- a/pkg/demoinfocs/common/common.go
+++ b/pkg/demoinfocs/common/common.go
@@ -239,3 +239,33 @@ func ConvertSteamID32To64(steamID32 uint32) uint64 {
 func ConvertSteamID64To32(steamID64 uint64) uint32 {
 	return uint32(steamID64 - steamID64IndividualIdentifier)
 }
+
+// Color is the type for the various colors constants.
+type Color int
+
+// Color constants give information about which color a player has.
+const (
+	Grey Color = iota - 1
+	Yellow
+	Purple
+	Green
+	Blue
+	Orange
+)
+
+var strColors = map[Color]string{
+	Grey:   "Grey",
+	Yellow: "Yellow",
+	Purple: "Purple",
+	Green:  "Green",
+	Blue:   "Blue",
+	Orange: "Orange",
+}
+
+func (c Color) String() string {
+	if _, exists := strColors[c]; !exists {
+		return "Unknown-Color"
+	}
+
+	return strColors[c]
+}
diff --git a/pkg/demoinfocs/common/player.go b/pkg/demoinfocs/common/player.go
index 202cc6c..010e8d7 100644
--- a/pkg/demoinfocs/common/player.go
+++ b/pkg/demoinfocs/common/player.go
@@ -376,6 +376,11 @@ func (p *Player) Score() int {
 	return getInt(p.ResourceEntity(), "m_iScore."+p.entityIDStr())
 }
 
+// Color returns the players color as shown on the match.
+func (p *Player) Color() Color {
+	return Color(getInt(p.ResourceEntity(), "m_iCompTeammateColor."+p.entityIDStr()))
+}
+
 // Kills returns the amount of kills the player has as shown on the scoreboard.
 func (p *Player) Kills() int {
 	return getInt(p.ResourceEntity(), "m_iKills."+p.entityIDStr())


Test patch:
diff --git a/pkg/demoinfocs/common/player_test.go b/pkg/demoinfocs/common/player_test.go
index 1d4ad02..02ab231 100644
--- a/pkg/demoinfocs/common/player_test.go
+++ b/pkg/demoinfocs/common/player_test.go
@@ -389,6 +389,12 @@ func TestPlayer_Score(t *testing.T) {
 	assert.Equal(t, 10, pl.Score())
 }
 
+func TestPlayer_Color(t *testing.T) {
+	pl := playerWithResourceProperty("m_iCompTeammateColor", st.PropertyValue{IntVal: int(Yellow)})
+
+	assert.Equal(t, Yellow, pl.Color())
+}
+
 func TestPlayer_Kills(t *testing.T) {
 	pl := playerWithResourceProperty("m_iKills", st.PropertyValue{IntVal: 5})