@@ -52,12 +52,12 @@ func (e *Engine) UndoLastAction() {
52
52
}
53
53
}
54
54
55
- func (e * Engine ) Process (event Event ) error {
56
- err := e .processEvent (event )
55
+ func (e * Engine ) Process (event Event ) ( * EventCommand , error ) {
56
+ cmd , err := e .processEvent (event )
57
57
if err == nil {
58
58
e .StateHistory = append (e .StateHistory , * e .State )
59
59
}
60
- return err
60
+ return cmd , err
61
61
}
62
62
63
63
func (e * Engine ) loadStages () {
@@ -91,7 +91,7 @@ func (e *Engine) updateTimes(delta time.Duration) {
91
91
}
92
92
}
93
93
94
- func (e * Engine ) processEvent (event Event ) error {
94
+ func (e * Engine ) processEvent (event Event ) ( * EventCommand , error ) {
95
95
if event .Command != nil {
96
96
return e .processCommand (event .Command )
97
97
} else if event .Modify != nil {
@@ -103,62 +103,65 @@ func (e *Engine) processEvent(event Event) error {
103
103
} else if event .Trigger != nil {
104
104
return e .processTrigger (event .Trigger )
105
105
}
106
- return errors .New ("unknown event" )
106
+ return nil , errors .New ("unknown event" )
107
107
}
108
108
109
- func (e * Engine ) processCommand (c * EventCommand ) error {
109
+ func (e * Engine ) processCommand (c * EventCommand ) ( * EventCommand , error ) {
110
110
switch c .Type {
111
111
case CommandHalt :
112
112
e .State .GameState = GameStateHalted
113
113
e .State .GameStateFor = nil
114
114
case CommandStop :
115
115
e .State .GameState = GameStateStopped
116
116
e .State .GameStateFor = nil
117
- case CommandForceStart , CommandNormalStart , CommandDirect , CommandIndirect :
117
+ case CommandNormalStart :
118
+ e .State .GameState = GameStateRunning
119
+ e .State .GameStateFor = nil
120
+ e .updatePreStages ()
121
+ case CommandForceStart , CommandDirect , CommandIndirect :
118
122
e .State .GameState = GameStateRunning
119
123
e .State .GameStateFor = nil
120
124
case CommandKickoff :
121
125
if c .ForTeam == nil {
122
- return errors .New ("Team required for kickoff" )
126
+ return nil , errors .New ("Team required for kickoff" )
123
127
}
124
128
e .State .GameState = GameStatePreKickoff
125
129
e .State .GameStateFor = c .ForTeam
126
130
case CommandPenalty :
127
131
if c .ForTeam == nil {
128
- return errors .New ("Team required for penalty" )
132
+ return nil , errors .New ("Team required for penalty" )
129
133
}
130
134
e .State .GameState = GameStatePrePenalty
131
135
e .State .GameStateFor = c .ForTeam
132
136
case CommandBallPlacement :
133
137
if c .ForTeam == nil {
134
- return errors .New ("Team required for ball placement" )
138
+ return nil , errors .New ("Team required for ball placement" )
135
139
}
136
140
e .State .GameState = GameStateBallPlacement
137
141
e .State .GameStateFor = c .ForTeam
138
142
case CommandGoal :
139
143
if c .ForTeam == nil {
140
- return errors .New ("Team required for goal" )
144
+ return nil , errors .New ("Team required for goal" )
141
145
}
142
146
e .State .TeamState [* c .ForTeam ].Goals ++
143
147
case CommandTimeout :
144
148
if c .ForTeam == nil {
145
- return errors .New ("Team required for timeout" )
149
+ return nil , errors .New ("Team required for timeout" )
146
150
}
147
151
e .State .TeamState [* c .ForTeam ].TimeoutsLeft --
148
152
e .State .GameState = GameStateTimeout
149
153
e .State .GameStateFor = c .ForTeam
150
154
default :
151
- return errors .Errorf ("Unknown command: %v" , c )
155
+ return nil , errors .Errorf ("Unknown command: %v" , c )
152
156
}
153
157
154
- e .updatePreStages ()
155
158
log .Printf ("Processed command %v" , * c )
156
- return nil
159
+ return c , nil
157
160
}
158
161
159
- func (e * Engine ) processModify (m * EventModifyValue ) error {
162
+ func (e * Engine ) processModify (m * EventModifyValue ) ( * EventCommand , error ) {
160
163
if m .ForTeam .Unknown () {
161
- return errors .Errorf ("Unknown team: %v" , m .ForTeam )
164
+ return nil , errors .Errorf ("Unknown team: %v" , m .ForTeam )
162
165
}
163
166
teamState := e .State .TeamState [m .ForTeam ]
164
167
if m .Goals != nil {
@@ -179,7 +182,7 @@ func (e *Engine) processModify(m *EventModifyValue) error {
179
182
} else if m .YellowCardTime != nil {
180
183
cardId := m .YellowCardTime .CardID
181
184
if cardId < 0 || cardId >= len (teamState .YellowCardTimes ) {
182
- return errors .Errorf ("Invalid card index: %v" , cardId )
185
+ return nil , errors .Errorf ("Invalid card index: %v" , cardId )
183
186
}
184
187
if duration , err := strToDuration (m .YellowCardTime .Duration ); err == nil {
185
188
teamState .YellowCardTimes [cardId ] = duration
@@ -188,78 +191,90 @@ func (e *Engine) processModify(m *EventModifyValue) error {
188
191
if duration , err := strToDuration (* m .TimeoutTimeLeft ); err == nil {
189
192
teamState .TimeoutTimeLeft = duration
190
193
} else {
191
- return err
194
+ return nil , err
192
195
}
193
196
} else {
194
- return errors .Errorf ("Unknown modify: %v" , m )
197
+ return nil , errors .Errorf ("Unknown modify: %v" , m )
195
198
}
196
199
log .Printf ("Processed modification %v" , m )
197
- return nil
200
+ return nil , nil
198
201
}
199
202
200
- func (e * Engine ) processStage (s * EventStage ) error {
203
+ func (e * Engine ) processStage (s * EventStage ) ( * EventCommand , error ) {
201
204
if e .State .GameState != GameStateHalted && e .State .GameState != GameStateStopped {
202
- return errors .New ("The game state must be halted or stopped to change the stage" )
205
+ return nil , errors .New ("The game state must be halted or stopped to change the stage" )
203
206
}
204
207
205
- index , err := e .State .Stage .index ()
206
- if err != nil {
207
- return err
208
- }
208
+ var cmd * EventCommand
209
209
if s .StageOperation == StageNext {
210
- nextIndex := index + 1
211
- if nextIndex >= len (Stages ) {
212
- return errors .New ("No next stage" )
213
- }
214
- e .updateStage (Stages [nextIndex ])
210
+ cmd = e .updateStage (e .State .Stage .Next ())
215
211
} else if s .StageOperation == StagePrevious {
216
- nextIndex := index - 1
217
- if nextIndex < 0 {
218
- return errors .New ("No previous stage" )
219
- }
220
- e .updateStage (Stages [nextIndex ])
212
+ cmd = e .updateStage (e .State .Stage .Previous ())
221
213
} else {
222
- return errors .Errorf ("Unknown stage operation: %v" , s .StageOperation )
214
+ return nil , errors .Errorf ("Unknown stage operation: %v" , s .StageOperation )
223
215
}
224
216
225
217
log .Printf ("Processed stage %v" , s .StageOperation )
226
218
227
- return nil
219
+ return cmd , nil
228
220
}
229
221
230
- func (e * Engine ) updateStage (stage Stage ) {
231
- e .State .Stage = stage
222
+ func (e * Engine ) updateStage (stage Stage ) (cmd * EventCommand ) {
232
223
233
- e .State .StageTimeLeft = e .StageTimes [e . State . Stage ]
224
+ e .State .StageTimeLeft = e .StageTimes [stage ]
234
225
e .State .StageTimeElapsed = 0
235
226
236
- if e .State .Stage == StageFirstHalf {
227
+ if ! e .State .Stage .IsPreStage () {
228
+ e .State .GameState = GameStateHalted
229
+ e .State .GameStateFor = nil
230
+ cmd = & EventCommand {nil , CommandHalt }
231
+ }
232
+
233
+ if stage == StageFirstHalf {
237
234
e .MatchTimeStart = time .Now ()
238
235
}
239
- if e .State .Stage == StageOvertimeFirstHalfPre {
236
+
237
+ if stage == StageOvertimeFirstHalfPre {
240
238
e .State .TeamState [TeamYellow ].TimeoutsLeft = e .config .Overtime .Timeouts
241
239
e .State .TeamState [TeamYellow ].TimeoutTimeLeft = e .config .Overtime .TimeoutDuration
242
240
e .State .TeamState [TeamBlue ].TimeoutsLeft = e .config .Overtime .Timeouts
243
241
e .State .TeamState [TeamBlue ].TimeoutTimeLeft = e .config .Overtime .TimeoutDuration
244
242
}
243
+
244
+ e .State .Stage = stage
245
+ return
246
+ }
247
+
248
+ func (e * Engine ) updatePreStages () {
249
+
250
+ switch e .State .Stage {
251
+ case StagePreGame :
252
+ e .updateStage (StageFirstHalf )
253
+ case StageSecondHalfPre :
254
+ e .updateStage (StageSecondHalf )
255
+ case StageOvertimeFirstHalfPre :
256
+ e .updateStage (StageOvertimeFirstHalf )
257
+ case StageOvertimeSecondHalfPre :
258
+ e .updateStage (StageOvertimeSecondHalf )
259
+ }
245
260
}
246
261
247
- func (e * Engine ) processCard (card * EventCard ) error {
262
+ func (e * Engine ) processCard (card * EventCard ) ( * EventCommand , error ) {
248
263
if card .ForTeam != TeamYellow && card .ForTeam != TeamBlue {
249
- return errors .Errorf ("Unknown team: %v" , card .ForTeam )
264
+ return nil , errors .Errorf ("Unknown team: %v" , card .ForTeam )
250
265
}
251
266
if card .Type != CardTypeYellow && card .Type != CardTypeRed {
252
- return errors .Errorf ("Unknown card type: %v" , card .Type )
267
+ return nil , errors .Errorf ("Unknown card type: %v" , card .Type )
253
268
}
254
269
teamState := e .State .TeamState [card .ForTeam ]
255
270
if card .Operation == CardOperationAdd {
256
- return addCard (card , teamState , e .config .YellowCardDuration )
271
+ return nil , addCard (card , teamState , e .config .YellowCardDuration )
257
272
} else if card .Operation == CardOperationRevoke {
258
- return revokeCard (card , teamState )
273
+ return nil , revokeCard (card , teamState )
259
274
} else if card .Operation == CardOperationModify {
260
- return modifyCard (card , teamState )
275
+ return nil , modifyCard (card , teamState )
261
276
}
262
- return errors .Errorf ("Unknown operation: %v" , card .Operation )
277
+ return nil , errors .Errorf ("Unknown operation: %v" , card .Operation )
263
278
}
264
279
265
280
func modifyCard (card * EventCard , teamState * TeamInfo ) error {
@@ -286,7 +301,7 @@ func addCard(card *EventCard, teamState *TeamInfo, duration time.Duration) error
286
301
return nil
287
302
}
288
303
289
- func (e * Engine ) processTrigger (t * EventTrigger ) error {
304
+ func (e * Engine ) processTrigger (t * EventTrigger ) ( * EventCommand , error ) {
290
305
if t .Type == TriggerResetMatch {
291
306
e .ResetGame ()
292
307
@@ -297,10 +312,10 @@ func (e *Engine) processTrigger(t *EventTrigger) error {
297
312
} else if t .Type == TriggerUndo {
298
313
e .UndoLastAction ()
299
314
} else {
300
- return errors .Errorf ("Unknown trigger: %v" , t .Type )
315
+ return nil , errors .Errorf ("Unknown trigger: %v" , t .Type )
301
316
}
302
317
log .Printf ("Processed trigger %v" , t .Type )
303
- return nil
318
+ return nil , nil
304
319
}
305
320
306
321
func revokeCard (card * EventCard , teamState * TeamInfo ) error {
@@ -327,22 +342,6 @@ func revokeCard(card *EventCard, teamState *TeamInfo) error {
327
342
return nil
328
343
}
329
344
330
- func (e * Engine ) updatePreStages () {
331
- proceedPreStages := e .State .GameState != GameStateHalted
332
- if proceedPreStages {
333
- switch e .State .Stage {
334
- case StagePreGame :
335
- e .updateStage (StageFirstHalf )
336
- case StageSecondHalfPre :
337
- e .updateStage (StageSecondHalf )
338
- case StageOvertimeFirstHalfPre :
339
- e .updateStage (StageOvertimeFirstHalf )
340
- case StageOvertimeSecondHalfPre :
341
- e .updateStage (StageOvertimeSecondHalf )
342
- }
343
- }
344
- }
345
-
346
345
func strToDuration (s string ) (duration time.Duration , err error ) {
347
346
duration = 0
348
347
err = nil
0 commit comments