Skip to content

Commit 847d318

Browse files
committed
[refactoring] Cleanup state
1 parent fe3f18c commit 847d318

File tree

4 files changed

+98
-87
lines changed

4 files changed

+98
-87
lines changed

internal/app/controller/publisher.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func initTeamInfo(t *sslproto.SSL_Referee_TeamInfo) {
6262
}
6363

6464
// Publish the state and command
65-
func (p *Publisher) Publish(state *RefBoxState, command *RefBoxEventCommand) {
65+
func (p *Publisher) Publish(state *State, command *RefBoxEventCommand) {
6666

6767
if p.conn == nil {
6868
return
@@ -80,11 +80,11 @@ func (p *Publisher) Publish(state *RefBoxState, command *RefBoxEventCommand) {
8080
}
8181
}
8282

83-
func updateMessage(r *sslproto.SSL_Referee, state *RefBoxState, command *RefBoxEventCommand) {
83+
func updateMessage(r *sslproto.SSL_Referee, state *State, command *RefBoxEventCommand) {
8484

8585
*r.PacketTimestamp = uint64(time.Now().UnixNano() / 1000)
8686
*r.Stage = mapStage(state.Stage)
87-
*r.StageTimeLeft = int32(state.GameTimeLeft.Nanoseconds() / 1000)
87+
*r.StageTimeLeft = int32(state.StageTimeLeft.Nanoseconds() / 1000)
8888
*r.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf
8989
updateTeam(r.Yellow, state.TeamState[TeamYellow])
9090
updateTeam(r.Blue, state.TeamState[TeamBlue])
@@ -131,7 +131,7 @@ func commandByTeam(command *RefBoxEventCommand, blueCommand sslproto.SSL_Referee
131131
return yellowCommand
132132
}
133133

134-
func updateTeam(team *sslproto.SSL_Referee_TeamInfo, state *RefBoxTeamState) {
134+
func updateTeam(team *sslproto.SSL_Referee_TeamInfo, state *TeamInfo) {
135135
*team.Name = state.Name
136136
*team.Score = uint32(state.Goals)
137137
*team.RedCards = uint32(state.RedCards)
@@ -150,7 +150,7 @@ func mapTimes(durations []time.Duration) []uint32 {
150150
return times
151151
}
152152

153-
func mapStage(stage RefBoxStage) sslproto.SSL_Referee_Stage {
153+
func mapStage(stage Stage) sslproto.SSL_Referee_Stage {
154154
switch stage {
155155
case StagePreGame:
156156
return sslproto.SSL_Referee_NORMAL_FIRST_HALF_PRE

internal/app/controller/refBox.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ const configFileName = "config/ssl-game-controller.yaml"
1616
var refBox = NewRefBox()
1717

1818
type RefBox struct {
19-
State *RefBoxState
19+
State *State
2020
timer timer.Timer
2121
MatchTimeStart time.Time
2222
notifyUpdateState chan struct{}
23-
StateHistory []RefBoxState
23+
StateHistory []State
2424
Config Config
2525
stateHistoryFile *os.File
2626
lastStateFile *os.File
27-
StageTimes map[RefBoxStage]time.Duration
27+
StageTimes map[Stage]time.Duration
2828
Publisher Publisher
2929
}
3030

@@ -177,7 +177,7 @@ func (r *RefBox) UndoLastAction() {
177177
}
178178
}
179179
func (r *RefBox) loadStages() {
180-
r.StageTimes = map[RefBoxStage]time.Duration{}
180+
r.StageTimes = map[Stage]time.Duration{}
181181
for _, stage := range Stages {
182182
r.StageTimes[stage] = 0
183183
}
@@ -193,8 +193,8 @@ func (r *RefBox) loadStages() {
193193

194194
func updateTimes(r *RefBox, delta time.Duration) {
195195
if r.State.GameState == GameStateRunning {
196-
r.State.GameTimeElapsed += delta
197-
r.State.GameTimeLeft -= delta
196+
r.State.StageTimeElapsed += delta
197+
r.State.StageTimeLeft -= delta
198198

199199
for _, teamState := range r.State.TeamState {
200200
reduceYellowCardTimes(teamState, delta)
@@ -207,13 +207,13 @@ func updateTimes(r *RefBox, delta time.Duration) {
207207
}
208208
}
209209

210-
func reduceYellowCardTimes(teamState *RefBoxTeamState, delta time.Duration) {
210+
func reduceYellowCardTimes(teamState *TeamInfo, delta time.Duration) {
211211
for i := range teamState.YellowCardTimes {
212212
teamState.YellowCardTimes[i] -= delta
213213
}
214214
}
215215

216-
func removeElapsedYellowCards(teamState *RefBoxTeamState) {
216+
func removeElapsedYellowCards(teamState *TeamInfo) {
217217
b := teamState.YellowCardTimes[:0]
218218
for _, x := range teamState.YellowCardTimes {
219219
if x > 0 {

internal/app/controller/refBoxEvents.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ func processStage(s *RefBoxEventStage) error {
154154
return errors.Errorf("Unknown stage operation: %v", s.StageOperation)
155155
}
156156

157-
refBox.State.GameTimeLeft = refBox.StageTimes[refBox.State.Stage]
158-
refBox.State.GameTimeElapsed = 0
157+
refBox.State.StageTimeLeft = refBox.StageTimes[refBox.State.Stage]
158+
refBox.State.StageTimeElapsed = 0
159159

160160
if refBox.State.Stage == StageFirstHalf {
161161
refBox.MatchTimeStart = time.Now()
@@ -172,7 +172,7 @@ func processStage(s *RefBoxEventStage) error {
172172
return nil
173173
}
174174

175-
func indexOfStage(stage RefBoxStage) (int, error) {
175+
func indexOfStage(stage Stage) (int, error) {
176176
for i, v := range Stages {
177177
if v == stage {
178178
return i, nil
@@ -307,7 +307,7 @@ func processCard(card *RefBoxEventCard) error {
307307
return errors.Errorf("Unknown operation: %v", card.Operation)
308308
}
309309

310-
func modifyCard(card *RefBoxEventCard, teamState *RefBoxTeamState) error {
310+
func modifyCard(card *RefBoxEventCard, teamState *TeamInfo) error {
311311
if card.Type == CardTypeRed {
312312
return errors.New("Red cards can not be modified")
313313
}
@@ -319,7 +319,7 @@ func modifyCard(card *RefBoxEventCard, teamState *RefBoxTeamState) error {
319319
return nil
320320
}
321321

322-
func addCard(card *RefBoxEventCard, teamState *RefBoxTeamState) error {
322+
func addCard(card *RefBoxEventCard, teamState *TeamInfo) error {
323323
if card.Type == CardTypeYellow {
324324
log.Printf("Add yellow card for team %v", card.ForTeam)
325325
teamState.YellowCards++
@@ -331,7 +331,7 @@ func addCard(card *RefBoxEventCard, teamState *RefBoxTeamState) error {
331331
return nil
332332
}
333333

334-
func revokeCard(card *RefBoxEventCard, teamState *RefBoxTeamState) error {
334+
func revokeCard(card *RefBoxEventCard, teamState *TeamInfo) error {
335335
if card.Type == CardTypeYellow {
336336
if teamState.YellowCards > 0 {
337337
log.Printf("Revoke yellow card for team %v", card.ForTeam)

internal/app/controller/state.go

Lines changed: 79 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,47 @@ import (
44
"time"
55
)
66

7+
// a team, one of Yellow or Blue
78
type Team string
89

9-
type RefBoxStage string
10-
type RefBoxGameState string
11-
1210
const (
1311
TeamYellow Team = "Yellow"
1412
TeamBlue Team = "Blue"
13+
)
1514

16-
StagePreGame RefBoxStage = "Pre-Game"
17-
StageFirstHalf RefBoxStage = "First Half"
18-
StageHalfTime RefBoxStage = "Half Time"
19-
StageSecondHalfPre RefBoxStage = "Pre-Second Half"
20-
StageSecondHalf RefBoxStage = "Second Half"
21-
StageOvertimeBreak RefBoxStage = "Overtime Break"
22-
StageOvertimeFirstHalfPre RefBoxStage = "Pre-Overtime First Half"
23-
StageOvertimeFirstHalf RefBoxStage = "Overtime First Half"
24-
StageOvertimeHalfTime RefBoxStage = "Overtime Half Time"
25-
StageOvertimeSecondHalfPre RefBoxStage = "Pre-Overtime Second Half"
26-
StageOvertimeSecondHalf RefBoxStage = "Overtime Second Half"
27-
StageShootoutBreak RefBoxStage = "Shootout Half Time"
28-
StageShootout RefBoxStage = "Shootout"
29-
StagePostGame RefBoxStage = "End of Game"
30-
31-
GameStateHalted RefBoxGameState = "Halted"
32-
GameStateStopped RefBoxGameState = "Stopped"
33-
GameStateRunning RefBoxGameState = "Running"
34-
GameStatePreKickoff RefBoxGameState = "Prepare Kickoff"
35-
GameStatePrePenalty RefBoxGameState = "Prepare Penalty"
36-
GameStateTimeout RefBoxGameState = "Timeout"
37-
GameStateBallPlacement RefBoxGameState = "Ball Placement"
15+
// return the other team
16+
// if the team is not Yellow or Blue, return the same team
17+
func (t Team) Other() Team {
18+
if t == TeamYellow {
19+
return TeamBlue
20+
} else if t == TeamBlue {
21+
return TeamYellow
22+
}
23+
return t
24+
}
25+
26+
// a stage of a match
27+
type Stage string
28+
29+
const (
30+
StagePreGame Stage = "Pre-Game"
31+
StageFirstHalf Stage = "First Half"
32+
StageHalfTime Stage = "Half Time"
33+
StageSecondHalfPre Stage = "Pre-Second Half"
34+
StageSecondHalf Stage = "Second Half"
35+
StageOvertimeBreak Stage = "Overtime Break"
36+
StageOvertimeFirstHalfPre Stage = "Pre-Overtime First Half"
37+
StageOvertimeFirstHalf Stage = "Overtime First Half"
38+
StageOvertimeHalfTime Stage = "Overtime Half Time"
39+
StageOvertimeSecondHalfPre Stage = "Pre-Overtime Second Half"
40+
StageOvertimeSecondHalf Stage = "Overtime Second Half"
41+
StageShootoutBreak Stage = "Shootout Half Time"
42+
StageShootout Stage = "Shootout"
43+
StagePostGame Stage = "End of Game"
3844
)
3945

40-
var Stages = []RefBoxStage{
46+
// all available stages, ordered
47+
var Stages = []Stage{
4148
StagePreGame,
4249
StageFirstHalf,
4350
StageHalfTime,
@@ -54,7 +61,21 @@ var Stages = []RefBoxStage{
5461
StagePostGame,
5562
}
5663

57-
type RefBoxTeamState struct {
64+
// a game state of a game
65+
type GameState string
66+
67+
const (
68+
GameStateHalted GameState = "Halted"
69+
GameStateStopped GameState = "Stopped"
70+
GameStateRunning GameState = "Running"
71+
GameStatePreKickoff GameState = "Prepare Kickoff"
72+
GameStatePrePenalty GameState = "Prepare Penalty"
73+
GameStateTimeout GameState = "Timeout"
74+
GameStateBallPlacement GameState = "Ball Placement"
75+
)
76+
77+
// team information
78+
type TeamInfo struct {
5879
Name string `json:"name"`
5980
Goals int `json:"goals"`
6081
Goalie int `json:"goalie"`
@@ -66,57 +87,47 @@ type RefBoxTeamState struct {
6687
OnPositiveHalf bool `json:"onPositiveHalf"`
6788
}
6889

69-
type RefBoxState struct {
70-
Stage RefBoxStage `json:"stage"`
71-
GameState RefBoxGameState `json:"gameState"`
72-
GameStateFor *Team `json:"gameStateForTeam"`
73-
GameTimeElapsed time.Duration `json:"gameTimeElapsed"`
74-
GameTimeLeft time.Duration `json:"gameTimeLeft"`
75-
MatchDuration time.Duration `json:"matchDuration"`
76-
TeamState map[Team]*RefBoxTeamState `json:"teamState"`
90+
// the state of the game
91+
type State struct {
92+
Stage Stage `json:"stage"`
93+
GameState GameState `json:"gameState"`
94+
GameStateFor *Team `json:"gameStateForTeam"`
95+
StageTimeElapsed time.Duration `json:"gameTimeElapsed"`
96+
StageTimeLeft time.Duration `json:"gameTimeLeft"`
97+
MatchDuration time.Duration `json:"matchDuration"`
98+
TeamState map[Team]*TeamInfo `json:"teamState"`
7799
}
78100

79-
func NewRefBoxState(config Config) (refBoxState *RefBoxState) {
80-
refBoxState = new(RefBoxState)
101+
// create a new state, initialized for the start of a new game
102+
func NewRefBoxState(config Config) (refBoxState *State) {
103+
refBoxState = new(State)
81104
refBoxState.Stage = StagePreGame
82105
refBoxState.GameState = GameStateHalted
83106

84107
// for some reason, the UI does not reset times correctly if duration is zero, so set it to 1ns
85-
refBoxState.GameTimeLeft = 1
86-
refBoxState.GameTimeElapsed = 1
108+
refBoxState.StageTimeLeft = 1
109+
refBoxState.StageTimeElapsed = 1
87110
refBoxState.MatchDuration = 1
88111

89-
refBoxState.TeamState = map[Team]*RefBoxTeamState{}
90-
refBoxState.TeamState[TeamYellow] = new(RefBoxTeamState)
91-
refBoxState.TeamState[TeamYellow].Name = ""
92-
refBoxState.TeamState[TeamYellow].Goals = 0
93-
refBoxState.TeamState[TeamYellow].Goalie = 0
94-
refBoxState.TeamState[TeamYellow].YellowCards = 0
95-
refBoxState.TeamState[TeamYellow].YellowCardTimes = []time.Duration{}
96-
refBoxState.TeamState[TeamYellow].RedCards = 0
97-
refBoxState.TeamState[TeamYellow].TimeoutsLeft = config.Normal.Timeouts
98-
refBoxState.TeamState[TeamYellow].TimeoutTimeLeft = config.Normal.TimeoutDuration
99-
refBoxState.TeamState[TeamYellow].OnPositiveHalf = true
100-
101-
refBoxState.TeamState[TeamBlue] = new(RefBoxTeamState)
102-
refBoxState.TeamState[TeamBlue].Name = ""
103-
refBoxState.TeamState[TeamBlue].Goals = 0
104-
refBoxState.TeamState[TeamBlue].Goalie = 0
105-
refBoxState.TeamState[TeamBlue].YellowCards = 0
106-
refBoxState.TeamState[TeamBlue].YellowCardTimes = []time.Duration{}
107-
refBoxState.TeamState[TeamBlue].RedCards = 0
108-
refBoxState.TeamState[TeamBlue].TimeoutsLeft = config.Normal.Timeouts
109-
refBoxState.TeamState[TeamBlue].TimeoutTimeLeft = config.Normal.TimeoutDuration
110-
refBoxState.TeamState[TeamBlue].OnPositiveHalf = false
112+
refBoxState.TeamState = map[Team]*TeamInfo{}
113+
refBoxState.TeamState[TeamYellow] = new(TeamInfo)
114+
refBoxState.TeamState[TeamBlue] = new(TeamInfo)
115+
*refBoxState.TeamState[TeamYellow] = NewTeamInfo(config)
116+
*refBoxState.TeamState[TeamBlue] = NewTeamInfo(config)
117+
refBoxState.TeamState[TeamBlue].OnPositiveHalf = !refBoxState.TeamState[TeamYellow].OnPositiveHalf
111118

112119
return
113120
}
114121

115-
func (t Team) Other() Team {
116-
if t == TeamYellow {
117-
return TeamBlue
118-
} else if t == TeamBlue {
119-
return TeamYellow
120-
}
121-
return t
122+
func NewTeamInfo(config Config) (t TeamInfo) {
123+
t.Name = ""
124+
t.Goals = 0
125+
t.Goalie = 0
126+
t.YellowCards = 0
127+
t.YellowCardTimes = []time.Duration{}
128+
t.RedCards = 0
129+
t.TimeoutsLeft = config.Normal.Timeouts
130+
t.TimeoutTimeLeft = config.Normal.TimeoutDuration
131+
t.OnPositiveHalf = true
132+
return
122133
}

0 commit comments

Comments
 (0)