@@ -14,7 +14,7 @@ type Engine struct {
14
14
config ConfigGame
15
15
StateHistory []State
16
16
TimeProvider func () time.Time
17
- GameEvents []RefereeEvent
17
+ RefereeEvent []RefereeEvent
18
18
}
19
19
20
20
func NewEngine (config ConfigGame ) (e Engine ) {
@@ -31,6 +31,7 @@ func (e *Engine) ResetGame() {
31
31
e .State .TeamState [TeamYellow ].TimeoutTimeLeft = e .config .Normal .TimeoutDuration
32
32
e .State .TeamState [TeamBlue ].TimeoutsLeft = e .config .Normal .Timeouts
33
33
e .State .TeamState [TeamYellow ].TimeoutsLeft = e .config .Normal .Timeouts
34
+ e .RefereeEvent = []RefereeEvent {}
34
35
}
35
36
36
37
// Tick updates the times of the state and removes cards, if necessary
@@ -69,7 +70,7 @@ func (e *Engine) LogGameEvent(eventType GameEventType) {
69
70
Type : RefereeEventGameEvent ,
70
71
GameEventType : & eventType ,
71
72
}
72
- e .GameEvents = append (e .GameEvents , gameEvent )
73
+ e .RefereeEvent = append (e .RefereeEvent , gameEvent )
73
74
}
74
75
75
76
func (e * Engine ) LogCommand (command * EventCommand ) {
@@ -80,7 +81,7 @@ func (e *Engine) LogCommand(command *EventCommand) {
80
81
Command : & command .Type ,
81
82
Team : command .ForTeam ,
82
83
}
83
- e .GameEvents = append (e .GameEvents , gameEvent )
84
+ e .RefereeEvent = append (e .RefereeEvent , gameEvent )
84
85
}
85
86
86
87
func (e * Engine ) loadStages () {
@@ -99,7 +100,7 @@ func (e *Engine) loadStages() {
99
100
}
100
101
101
102
func (e * Engine ) updateTimes (delta time.Duration ) {
102
- if e .State .GameState == GameStateRunning {
103
+ if e .State .GameState () == GameStateRunning {
103
104
e .State .StageTimeElapsed += delta
104
105
e .State .StageTimeLeft -= delta
105
106
@@ -109,8 +110,8 @@ func (e *Engine) updateTimes(delta time.Duration) {
109
110
}
110
111
}
111
112
112
- if e .State .GameState == GameStateTimeout && e .State .GameStateFor != nil {
113
- e .State .TeamState [* e .State .GameStateFor ].TimeoutTimeLeft -= delta
113
+ if e .State .GameState () == GameStateTimeout && e .State .CommandFor != nil {
114
+ e .State .TeamState [* e .State .CommandFor ].TimeoutTimeLeft -= delta
114
115
}
115
116
}
116
117
@@ -131,53 +132,25 @@ func (e *Engine) processEvent(event Event) (*EventCommand, error) {
131
132
132
133
func (e * Engine ) processCommand (c * EventCommand ) (* EventCommand , error ) {
133
134
switch c .Type {
134
- case CommandHalt :
135
- e .State .GameState = GameStateHalted
136
- e .State .GameStateFor = nil
137
- case CommandStop :
138
- e .State .GameState = GameStateStopped
139
- e .State .GameStateFor = nil
140
- case CommandNormalStart :
141
- e .State .GameState = GameStateRunning
142
- e .State .GameStateFor = nil
143
- e .updatePreStages ()
144
- case CommandForceStart , CommandDirect , CommandIndirect :
145
- e .State .GameState = GameStateRunning
146
- e .State .GameStateFor = nil
147
- case CommandKickoff :
148
- if c .ForTeam == nil {
149
- return nil , errors .New ("Team required for kickoff" )
150
- }
151
- e .State .GameState = GameStatePreKickoff
152
- e .State .GameStateFor = c .ForTeam
153
- case CommandPenalty :
154
- if c .ForTeam == nil {
155
- return nil , errors .New ("Team required for penalty" )
156
- }
157
- e .State .GameState = GameStatePrePenalty
158
- e .State .GameStateFor = c .ForTeam
159
- case CommandBallPlacement :
160
- if c .ForTeam == nil {
161
- return nil , errors .New ("Team required for ball placement" )
162
- }
163
- e .State .GameState = GameStateBallPlacement
164
- e .State .GameStateFor = c .ForTeam
165
- case CommandGoal :
135
+ case CommandDirect , CommandIndirect , CommandKickoff , CommandPenalty , CommandTimeout , CommandBallPlacement :
166
136
if c .ForTeam == nil {
167
- return nil , errors .New ("Team required for goal" )
137
+ return nil , errors .Errorf ("Team required for %v" , c . Type )
168
138
}
169
- e .State .TeamState [* c .ForTeam ].Goals ++
170
- case CommandTimeout :
171
- if c .ForTeam == nil {
172
- return nil , errors .New ("Team required for timeout" )
173
- }
174
- e .State .TeamState [* c .ForTeam ].TimeoutsLeft --
175
- e .State .GameState = GameStateTimeout
176
- e .State .GameStateFor = c .ForTeam
139
+ case CommandHalt , CommandStop , CommandForceStart , CommandNormalStart :
177
140
default :
178
141
return nil , errors .Errorf ("Unknown command: %v" , c )
179
142
}
180
143
144
+ if c .Type == CommandTimeout {
145
+ e .State .TeamState [* c .ForTeam ].TimeoutsLeft --
146
+ e .State .CommandFor = c .ForTeam
147
+ } else if c .Type == CommandNormalStart {
148
+ e .updatePreStages ()
149
+ }
150
+
151
+ e .State .Command = c .Type
152
+ e .State .CommandFor = c .ForTeam
153
+
181
154
log .Printf ("Processed command %v" , * c )
182
155
return c , nil
183
156
}
@@ -230,7 +203,7 @@ func (e *Engine) processModify(m *EventModifyValue) (*EventCommand, error) {
230
203
}
231
204
232
205
func (e * Engine ) processStage (s * EventStage ) (* EventCommand , error ) {
233
- if e .State .GameState != GameStateHalted && e .State .GameState != GameStateStopped {
206
+ if e .State .GameState () != GameStateHalted && e .State .GameState () != GameStateStopped {
234
207
return nil , errors .New ("The game state must be halted or stopped to change the stage" )
235
208
}
236
209
@@ -254,8 +227,8 @@ func (e *Engine) updateStage(stage Stage) (cmd *EventCommand) {
254
227
e .State .StageTimeElapsed = 0
255
228
256
229
if ! e .State .Stage .IsPreStage () {
257
- e .State .GameState = GameStateHalted
258
- e .State .GameStateFor = nil
230
+ e .State .Command = CommandHalt
231
+ e .State .CommandFor = nil
259
232
cmd = & EventCommand {nil , CommandHalt }
260
233
}
261
234
0 commit comments