@@ -15,6 +15,7 @@ const configFileName = "config/ssl-game-controller.yaml"
15
15
16
16
var refBox = NewRefBox ()
17
17
18
+ // RefBox controls a game
18
19
type RefBox struct {
19
20
State * State
20
21
timer timer.Timer
@@ -28,6 +29,7 @@ type RefBox struct {
28
29
Publisher Publisher
29
30
}
30
31
32
+ // NewRefBox creates a new refBox
31
33
func NewRefBox () (refBox * RefBox ) {
32
34
33
35
refBox = new (RefBox )
@@ -41,26 +43,12 @@ func NewRefBox() (refBox *RefBox) {
41
43
return
42
44
}
43
45
44
- func loadPublisher (config Config ) Publisher {
45
- publisher , err := NewPublisher (config .Publish .Address )
46
- if err != nil {
47
- log .Printf ("Could not start publisher on %v. %v" , config .Publish .Address , err )
48
- }
49
- return publisher
50
- }
51
-
52
- func loadConfig () Config {
53
- config , err := LoadConfig (configFileName )
54
- if err != nil {
55
- log .Printf ("Warning: Could not load config: %v" , err )
56
- }
57
- return config
58
- }
59
-
46
+ // RunRefBox starts the global refBox
60
47
func RunRefBox () {
61
48
refBox .Run ()
62
49
}
63
50
51
+ // Run the refBox by loading configs, states, timer, etc.
64
52
func (r * RefBox ) Run () (err error ) {
65
53
66
54
os .MkdirAll (logDir , os .ModePerm )
@@ -85,6 +73,22 @@ func (r *RefBox) Run() (err error) {
85
73
return nil
86
74
}
87
75
76
+ func loadPublisher (config Config ) Publisher {
77
+ publisher , err := NewPublisher (config .Publish .Address )
78
+ if err != nil {
79
+ log .Printf ("Could not start publisher on %v. %v" , config .Publish .Address , err )
80
+ }
81
+ return publisher
82
+ }
83
+
84
+ func loadConfig () Config {
85
+ config , err := LoadConfig (configFileName )
86
+ if err != nil {
87
+ log .Printf ("Warning: Could not load config: %v" , err )
88
+ }
89
+ return config
90
+ }
91
+
88
92
func (r * RefBox ) openStateFiles () {
89
93
stateHistoryLogFileName := logDir + "/state-history_" + time .Now ().Format ("2006-01-02_15-04-05" ) + ".log"
90
94
f , err := os .OpenFile (stateHistoryLogFileName , os .O_APPEND | os .O_WRONLY | os .O_CREATE , 0600 )
@@ -117,6 +121,7 @@ func (r *RefBox) readLastState() {
117
121
}
118
122
}
119
123
124
+ // Tick updates the times of the state and removes cards, if necessary
120
125
func (r * RefBox ) Tick () {
121
126
delta := r .timer .Delta ()
122
127
updateTimes (r , delta )
@@ -126,16 +131,19 @@ func (r *RefBox) Tick() {
126
131
}
127
132
}
128
133
134
+ // Update publishes the state to the UI and the teams
129
135
func (r * RefBox ) Update (command * RefBoxEventCommand ) {
130
136
r .notifyUpdateState <- struct {}{}
131
137
refBox .Publisher .Publish (refBox .State , command )
132
138
}
133
139
140
+ // SaveState writes the latest state out and logs the state history
134
141
func (r * RefBox ) SaveState () {
135
142
r .SaveLatestState ()
136
143
r .SaveStateHistory ()
137
144
}
138
145
146
+ // SaveLatestState writes the current state into a file
139
147
func (r * RefBox ) SaveLatestState () {
140
148
jsonState , err := json .MarshalIndent (r .State , "" , " " )
141
149
if err != nil {
@@ -154,6 +162,7 @@ func (r *RefBox) SaveLatestState() {
154
162
r .lastStateFile .Sync ()
155
163
}
156
164
165
+ // SaveStateHistory writes the current state to the history file
157
166
func (r * RefBox ) SaveStateHistory () {
158
167
159
168
r .StateHistory = append (r .StateHistory , * r .State )
@@ -169,13 +178,15 @@ func (r *RefBox) SaveStateHistory() {
169
178
r .stateHistoryFile .Sync ()
170
179
}
171
180
181
+ // UndoLastAction restores the last state from internal history
172
182
func (r * RefBox ) UndoLastAction () {
173
183
lastIndex := len (r .StateHistory ) - 2
174
184
if lastIndex >= 0 {
175
185
* r .State = r .StateHistory [lastIndex ]
176
186
r .StateHistory = r .StateHistory [0 :lastIndex ]
177
187
}
178
188
}
189
+
179
190
func (r * RefBox ) loadStages () {
180
191
r .StageTimes = map [Stage ]time.Duration {}
181
192
for _ , stage := range Stages {
0 commit comments