@@ -33,6 +33,7 @@ func (e *Engine) ResetGame() {
33
33
e .State .TeamState [TeamYellow ].TimeoutTimeLeft = e .config .Normal .TimeoutDuration
34
34
e .State .TeamState [TeamBlue ].TimeoutsLeft = e .config .Normal .Timeouts
35
35
e .State .TeamState [TeamYellow ].TimeoutsLeft = e .config .Normal .Timeouts
36
+ e .RefereeEvents = []RefereeEvent {}
36
37
e .History = History {}
37
38
}
38
39
@@ -45,6 +46,18 @@ func (e *Engine) Tick(delta time.Duration) {
45
46
}
46
47
}
47
48
49
+ func (e * Engine ) SendCommand (command RefCommand , forTeam Team ) {
50
+ e .State .Command = command
51
+ e .State .CommandFor = forTeam
52
+ e .LogCommand ()
53
+ }
54
+
55
+ func (e * Engine ) SendGameEvent (gameEvent GameEventType , forTeam Team ) {
56
+ e .State .GameEvent = gameEvent
57
+ e .State .GameEventFor = forTeam
58
+ e .LogGameEvent ()
59
+ }
60
+
48
61
// UndoLastAction restores the last state from internal history
49
62
func (e * Engine ) UndoLastAction () {
50
63
lastIndex := len (e .History ) - 2
@@ -55,39 +68,41 @@ func (e *Engine) UndoLastAction() {
55
68
}
56
69
57
70
func (e * Engine ) Process (event Event ) error {
58
- cmd , err := e .processEvent (event )
71
+ err := e .processEvent (event )
59
72
if err != nil {
60
73
return err
61
74
}
62
- if cmd != nil {
63
- e .LogCommand (cmd )
64
- }
75
+ e .appendHistory ()
76
+ return nil
77
+ }
78
+
79
+ func (e * Engine ) appendHistory () {
65
80
e .History = append (e .History , HistoryEntry {* e .State , e .RefereeEvents })
66
81
if len (e .History ) > maxHistorySize {
67
82
e .History = e .History [1 :]
68
83
}
69
- return nil
70
84
}
71
85
72
- func (e * Engine ) LogGameEvent (eventType GameEventType ) {
86
+ func (e * Engine ) LogGameEvent () {
73
87
gameEvent := RefereeEvent {
74
- Timestamp : e .TimeProvider (),
75
- StageTime : e .State .StageTimeElapsed ,
76
- Type : RefereeEventGameEvent ,
77
- GameEventType : & eventType ,
88
+ Timestamp : e .TimeProvider (),
89
+ StageTime : e .State .StageTimeElapsed ,
90
+ Type : RefereeEventGameEvent ,
91
+ Name : string (e .State .GameEvent ),
92
+ Team : e .State .GameEventFor ,
78
93
}
79
94
e .RefereeEvents = append (e .RefereeEvents , gameEvent )
80
95
}
81
96
82
- func (e * Engine ) LogCommand (command * EventCommand ) {
83
- gameEvent := RefereeEvent {
97
+ func (e * Engine ) LogCommand () {
98
+ refereeEvent := RefereeEvent {
84
99
Timestamp : e .TimeProvider (),
85
100
StageTime : e .State .StageTimeElapsed ,
86
101
Type : RefereeEventCommand ,
87
- Command : & command . Type ,
88
- Team : command . ForTeam ,
102
+ Name : string ( e . State . Command ) ,
103
+ Team : e . State . CommandFor ,
89
104
}
90
- e .RefereeEvents = append (e .RefereeEvents , gameEvent )
105
+ e .RefereeEvents = append (e .RefereeEvents , refereeEvent )
91
106
}
92
107
93
108
func (e * Engine ) loadStages () {
@@ -116,12 +131,12 @@ func (e *Engine) updateTimes(delta time.Duration) {
116
131
}
117
132
}
118
133
119
- if e .State .GameState () == GameStateTimeout && e .State .CommandFor != nil {
120
- e .State .TeamState [* e .State .CommandFor ].TimeoutTimeLeft -= delta
134
+ if e .State .GameState () == GameStateTimeout && e .State .CommandFor . Known () {
135
+ e .State .TeamState [e .State .CommandFor ].TimeoutTimeLeft -= delta
121
136
}
122
137
}
123
138
124
- func (e * Engine ) processEvent (event Event ) ( * EventCommand , error ) {
139
+ func (e * Engine ) processEvent (event Event ) error {
125
140
if event .Command != nil {
126
141
return e .processCommand (event .Command )
127
142
} else if event .Modify != nil {
@@ -135,37 +150,35 @@ func (e *Engine) processEvent(event Event) (*EventCommand, error) {
135
150
} else if event .GameEvent != nil {
136
151
return e .processGameEvent (event .GameEvent )
137
152
}
138
- return nil , errors .New ("unknown event" )
153
+ return errors .New ("unknown event" )
139
154
}
140
155
141
- func (e * Engine ) processCommand (c * EventCommand ) (* EventCommand , error ) {
156
+ func (e * Engine ) processCommand (c * EventCommand ) error {
157
+
158
+ if c .Type == CommandTimeout {
159
+ e .State .TeamState [* c .ForTeam ].TimeoutsLeft --
160
+ } else if c .Type == CommandNormalStart {
161
+ e .updatePreStages ()
162
+ }
142
163
switch c .Type {
143
164
case CommandDirect , CommandIndirect , CommandKickoff , CommandPenalty , CommandTimeout , CommandBallPlacement :
144
165
if c .ForTeam == nil {
145
- return nil , errors .Errorf ("Team required for %v" , c .Type )
166
+ return errors .Errorf ("Team required for %v" , c .Type )
146
167
}
168
+ e .SendCommand (c .Type , * c .ForTeam )
147
169
case CommandHalt , CommandStop , CommandForceStart , CommandNormalStart :
170
+ e .SendCommand (c .Type , "" )
148
171
default :
149
- return nil , errors .Errorf ("Unknown command: %v" , c )
172
+ return errors .Errorf ("Unknown command: %v" , c )
150
173
}
151
174
152
- if c .Type == CommandTimeout {
153
- e .State .TeamState [* c .ForTeam ].TimeoutsLeft --
154
- e .State .CommandFor = c .ForTeam
155
- } else if c .Type == CommandNormalStart {
156
- e .updatePreStages ()
157
- }
158
-
159
- e .State .Command = c .Type
160
- e .State .CommandFor = c .ForTeam
161
-
162
175
log .Printf ("Processed command %v" , * c )
163
- return c , nil
176
+ return nil
164
177
}
165
178
166
- func (e * Engine ) processModify (m * EventModifyValue ) ( * EventCommand , error ) {
179
+ func (e * Engine ) processModify (m * EventModifyValue ) error {
167
180
if m .ForTeam .Unknown () {
168
- return nil , errors .Errorf ("Unknown team: %v" , m .ForTeam )
181
+ return errors .Errorf ("Unknown team: %v" , m .ForTeam )
169
182
}
170
183
teamState := e .State .TeamState [m .ForTeam ]
171
184
if m .Goals != nil {
@@ -192,7 +205,7 @@ func (e *Engine) processModify(m *EventModifyValue) (*EventCommand, error) {
192
205
} else if m .YellowCardTime != nil {
193
206
cardId := m .YellowCardTime .CardID
194
207
if cardId < 0 || cardId >= len (teamState .YellowCardTimes ) {
195
- return nil , errors .Errorf ("Invalid card index: %v" , cardId )
208
+ return errors .Errorf ("Invalid card index: %v" , cardId )
196
209
}
197
210
if duration , err := strToDuration (m .YellowCardTime .Duration ); err == nil {
198
211
teamState .YellowCardTimes [cardId ] = duration
@@ -201,43 +214,40 @@ func (e *Engine) processModify(m *EventModifyValue) (*EventCommand, error) {
201
214
if duration , err := strToDuration (* m .TimeoutTimeLeft ); err == nil {
202
215
teamState .TimeoutTimeLeft = duration
203
216
} else {
204
- return nil , err
217
+ return err
205
218
}
206
219
} else {
207
- return nil , errors .Errorf ("Unknown modify: %v" , m )
220
+ return errors .Errorf ("Unknown modify: %v" , m )
208
221
}
209
222
log .Printf ("Processed modification %v" , m )
210
- return nil , nil
223
+ return nil
211
224
}
212
225
213
- func (e * Engine ) processStage (s * EventStage ) ( * EventCommand , error ) {
226
+ func (e * Engine ) processStage (s * EventStage ) error {
214
227
if e .State .GameState () != GameStateHalted && e .State .GameState () != GameStateStopped {
215
- return nil , errors .New ("The game state must be halted or stopped to change the stage" )
228
+ return errors .New ("The game state must be halted or stopped to change the stage" )
216
229
}
217
230
218
- var cmd * EventCommand
219
231
if s .StageOperation == StageNext {
220
- cmd = e .updateStage (e .State .Stage .Next ())
232
+ e .updateStage (e .State .Stage .Next ())
221
233
} else if s .StageOperation == StagePrevious {
222
- cmd = e .updateStage (e .State .Stage .Previous ())
234
+ e .updateStage (e .State .Stage .Previous ())
223
235
} else {
224
- return nil , errors .Errorf ("Unknown stage operation: %v" , s .StageOperation )
236
+ return errors .Errorf ("Unknown stage operation: %v" , s .StageOperation )
225
237
}
226
238
227
239
log .Printf ("Processed stage %v" , s .StageOperation )
228
240
229
- return cmd , nil
241
+ return nil
230
242
}
231
243
232
- func (e * Engine ) updateStage (stage Stage ) ( cmd * EventCommand ) {
244
+ func (e * Engine ) updateStage (stage Stage ) {
233
245
234
246
e .State .StageTimeLeft = e .StageTimes [stage ]
235
247
e .State .StageTimeElapsed = 0
236
248
237
249
if ! e .State .Stage .IsPreStage () {
238
- e .State .Command = CommandHalt
239
- e .State .CommandFor = nil
240
- cmd = & EventCommand {nil , CommandHalt }
250
+ e .SendCommand (CommandHalt , "" )
241
251
}
242
252
243
253
if stage == StageFirstHalf {
@@ -269,22 +279,22 @@ func (e *Engine) updatePreStages() {
269
279
}
270
280
}
271
281
272
- func (e * Engine ) processCard (card * EventCard ) ( * EventCommand , error ) {
282
+ func (e * Engine ) processCard (card * EventCard ) error {
273
283
if card .ForTeam != TeamYellow && card .ForTeam != TeamBlue {
274
- return nil , errors .Errorf ("Unknown team: %v" , card .ForTeam )
284
+ return errors .Errorf ("Unknown team: %v" , card .ForTeam )
275
285
}
276
286
if card .Type != CardTypeYellow && card .Type != CardTypeRed {
277
- return nil , errors .Errorf ("Unknown card type: %v" , card .Type )
287
+ return errors .Errorf ("Unknown card type: %v" , card .Type )
278
288
}
279
289
teamState := e .State .TeamState [card .ForTeam ]
280
290
if card .Operation == CardOperationAdd {
281
- return nil , addCard (card , teamState , e .config .YellowCardDuration )
291
+ return addCard (card , teamState , e .config .YellowCardDuration )
282
292
} else if card .Operation == CardOperationRevoke {
283
- return nil , revokeCard (card , teamState )
293
+ return revokeCard (card , teamState )
284
294
} else if card .Operation == CardOperationModify {
285
- return nil , modifyCard (card , teamState )
295
+ return modifyCard (card , teamState )
286
296
}
287
- return nil , errors .Errorf ("Unknown operation: %v" , card .Operation )
297
+ return errors .Errorf ("Unknown operation: %v" , card .Operation )
288
298
}
289
299
290
300
func modifyCard (card * EventCard , teamState * TeamInfo ) error {
@@ -311,7 +321,7 @@ func addCard(card *EventCard, teamState *TeamInfo, duration time.Duration) error
311
321
return nil
312
322
}
313
323
314
- func (e * Engine ) processTrigger (t * EventTrigger ) ( * EventCommand , error ) {
324
+ func (e * Engine ) processTrigger (t * EventTrigger ) error {
315
325
if t .Type == TriggerResetMatch {
316
326
e .ResetGame ()
317
327
} else if t .Type == TriggerSwitchColor {
@@ -325,16 +335,24 @@ func (e *Engine) processTrigger(t *EventTrigger) (*EventCommand, error) {
325
335
} else if t .Type == TriggerUndo {
326
336
e .UndoLastAction ()
327
337
} else {
328
- return nil , errors .Errorf ("Unknown trigger: %v" , t .Type )
338
+ return errors .Errorf ("Unknown trigger: %v" , t .Type )
329
339
}
330
340
log .Printf ("Processed trigger %v" , t .Type )
331
- return nil , nil
341
+ return nil
332
342
}
333
343
334
- func (e * Engine ) processGameEvent (t * EventGameEvent ) (* EventCommand , error ) {
344
+ func (e * Engine ) processGameEvent (t * EventGameEvent ) error {
345
+
346
+ if t .BallLeftFieldTouchLine != nil {
347
+ e .SendGameEvent (GameEventBallLeftFieldTouchLine , t .BallLeftFieldTouchLine .Team )
348
+ } else if t .BallLeftFieldGoalLine != nil {
349
+ e .SendGameEvent (GameEventBallLeftFieldGoalLine , t .BallLeftFieldGoalLine .Team )
350
+ } else {
351
+ return errors .Errorf ("Unknown game event: %v" , * t )
352
+ }
335
353
336
354
log .Printf ("Processed game event %v" , t )
337
- return nil , nil
355
+ return nil
338
356
}
339
357
340
358
func revokeCard (card * EventCard , teamState * TeamInfo ) error {
0 commit comments