forked from markus-wa/demoinfocs-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_state.go
104 lines (88 loc) · 2.6 KB
/
game_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package demoinfocs
import (
"github.com/markus-wa/demoinfocs-golang/common"
)
// GameState contains all game-state relevant information.
type GameState struct {
ingameTick int
tState TeamState
ctState TeamState
players map[int]*common.Player
}
type ingameTickNumber int
func (gs *GameState) handleIngameTickNumber(n ingameTickNumber) {
gs.ingameTick = int(n)
debugIngameTick(gs.ingameTick)
}
// IngameTick returns the latest actual tick number of the server during the game.
// Watch out, I've seen this return wonky negative numbers at the start of demos.
func (gs GameState) IngameTick() int {
return gs.ingameTick
}
// CTState returns the TeamState of the CT team.
// Make sure you handle swapping sides properly if you keep the reference.
func (gs *GameState) CTState() *TeamState {
return &gs.ctState
}
// TState returns the TeamState of the T team.
// Make sure you handle swapping sides properly if you keep the reference.
func (gs *GameState) TState() *TeamState {
return &gs.tState
}
// Participants returns all connected players.
// This includes spectators.
func (gs GameState) Participants() []*common.Player {
r := make([]*common.Player, 0, len(gs.players))
for _, ptcp := range gs.players {
r = append(r, ptcp)
}
return r
}
// PlayingParticipants returns all players that aren't spectating or unassigned.
func (gs GameState) PlayingParticipants() []*common.Player {
r := make([]*common.Player, 0, len(gs.players))
for _, ptcp := range gs.players {
if ptcp.Team != common.TeamSpectators && ptcp.Team != common.TeamUnassigned {
r = append(r, ptcp)
}
}
return r
}
// TeamMembers returns all players belonging to the requested team.
func (gs GameState) TeamMembers(team common.Team) []*common.Player {
r := make([]*common.Player, 0, len(gs.players))
for _, ptcp := range gs.players {
if ptcp.Team == team {
r = append(r, ptcp)
}
}
return r
}
func newGameState() GameState {
return GameState{players: make(map[int]*common.Player)}
}
// TeamState contains a team's ID, score, clan name & country flag.
type TeamState struct {
id int
score int
clanName string
flag string
}
// ID returns the team-ID.
// This stays the same even after switching sides.
func (ts TeamState) ID() int {
return ts.id
}
// Score returns the team's number of rounds won.
func (ts TeamState) Score() int {
return ts.score
}
// ClanName returns the team's clan name.
func (ts TeamState) ClanName() string {
return ts.clanName
}
// Flag returns the team's country flag.
// Watch out, in some demos this is upper-case and in some lower-case.
func (ts TeamState) Flag() string {
return ts.flag
}