Skip to content

Commit b3e6d60

Browse files
committed
simplified Parser.bindTeamStates()
1 parent c6a2e83 commit b3e6d60

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

datatables.go

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (p *Parser) mapEquipment() {
6161

6262
// Bind the attributes of the various entities to our structs on the parser
6363
func (p *Parser) bindEntities() {
64-
p.bindTeamScores()
64+
p.bindTeamStates()
6565
p.bindBombSites()
6666
p.bindPlayers()
6767
p.bindWeapons()
@@ -101,61 +101,43 @@ func (p *Parser) bindBomb() {
101101
})
102102
}
103103

104-
func (p *Parser) bindTeamScores() {
104+
func (p *Parser) bindTeamStates() {
105105
p.stParser.ServerClasses().FindByName("CCSTeam").OnEntityCreated(func(entity *st.Entity) {
106-
teamID := -1
107-
var clanName string
108-
var flagImage string
109-
score := 0
106+
team := entity.FindProperty("m_szTeamname").Value().StringVal
110107

111-
entity.BindProperty("m_iTeamNum", &teamID, st.ValTypeInt)
112-
entity.BindProperty("m_szClanTeamname", &clanName, st.ValTypeString)
113-
entity.BindProperty("m_szTeamFlagImage", &flagImage, st.ValTypeString)
114-
entity.BindProperty("m_scoreTotal", &score, st.ValTypeInt)
108+
var s *common.TeamState
115109

116-
entity.FindProperty("m_szTeamname").OnUpdate(func(val st.PropertyValue) {
117-
team := val.StringVal
110+
switch team {
111+
case "CT":
112+
s = &p.gameState.ctState
118113

119-
var s *common.TeamState
114+
case "TERRORIST":
115+
s = &p.gameState.tState
120116

121-
switch team {
122-
case "CT":
123-
s = &p.gameState.ctState
117+
case "Unassigned": // Ignore
118+
case "Spectator": // Ignore
124119

125-
case "TERRORIST":
126-
s = &p.gameState.tState
120+
default:
121+
panic(fmt.Sprintf("Unexpected team %q", team))
122+
}
127123

128-
case "Unassigned": // Ignore
129-
case "Spectator": // Ignore
124+
if s != nil {
125+
// Register updates
126+
entity.BindProperty("m_iTeamNum", &s.ID, st.ValTypeInt)
127+
entity.BindProperty("m_szClanTeamname", &s.ClanName, st.ValTypeString)
128+
entity.BindProperty("m_szTeamFlagImage", &s.Flag, st.ValTypeString)
130129

131-
default:
132-
panic(fmt.Sprintf("Unexpected team %q", team))
133-
}
130+
entity.FindProperty("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
131+
oldScore := s.Score
132+
s.Score = val.IntVal
134133

135-
if s != nil {
136-
// Set values that were already updated
137-
s.ID = teamID
138-
s.ClanName = clanName
139-
s.Flag = flagImage
140-
s.Score = score
141-
142-
// Register direct updates for the future
143-
// Except for teamId, it doesn't change; players swap teams instead
144-
entity.BindProperty("m_szClanTeamname", &s.ClanName, st.ValTypeString)
145-
entity.BindProperty("m_szTeamFlagImage", &s.Flag, st.ValTypeString)
146-
147-
entity.FindProperty("m_scoreTotal").OnUpdate(func(val st.PropertyValue) {
148-
oldScore := s.Score
149-
s.Score = val.IntVal
150-
151-
p.eventDispatcher.Dispatch(events.ScoreUpdated{
152-
OldScore: oldScore,
153-
NewScore: val.IntVal,
154-
TeamState: s,
155-
})
134+
p.eventDispatcher.Dispatch(events.ScoreUpdated{
135+
OldScore: oldScore,
136+
NewScore: val.IntVal,
137+
TeamState: s,
156138
})
157-
}
158-
})
139+
})
140+
}
159141
})
160142
}
161143

0 commit comments

Comments
 (0)