@@ -62,6 +62,8 @@ func (e *Engine) SendCommand(command RefCommand, forTeam Team) {
62
62
e .State .GameEvents = []* GameEvent {}
63
63
}
64
64
e .State .PlacementPos = nil
65
+ e .State .NextCommand = CommandUnknown
66
+ e .State .NextCommandFor = TeamUnknown
65
67
}
66
68
}
67
69
@@ -80,32 +82,53 @@ func (e *Engine) UndoLastAction() {
80
82
}
81
83
}
82
84
83
- func (e * Engine ) Continue () error {
85
+ func (e * Engine ) Continue () {
86
+ if e .State .NextCommand != CommandUnknown {
87
+ e .SendCommand (e .State .NextCommand , e .State .NextCommandFor )
88
+ }
89
+ }
90
+
91
+ func (e * Engine ) updateNextCommand () {
84
92
if len (e .State .GameEvents ) == 0 {
85
- return errors . New ( "No game events available to determine next action" )
93
+ return
86
94
}
87
95
primaryEvent := e .State .GameEvents [0 ]
88
- teamInFavor := primaryEvent .ByTeam ().Opposite ()
96
+ command , forTeam , err := e .CommandForEvent (primaryEvent )
97
+ if err != nil {
98
+ log .Print ("Warn: " , err )
99
+ return
100
+ }
101
+ e .State .NextCommand = command
102
+ e .State .NextCommandFor = forTeam
103
+ }
104
+
105
+ func (e * Engine ) CommandForEvent (event * GameEvent ) (command RefCommand , forTeam Team , err error ) {
106
+ if event .IsSecondary () {
107
+ return
108
+ }
109
+
110
+ forTeam = event .ByTeam ().Opposite ()
89
111
90
112
if e .State .Division == DivA {
91
113
for _ , event := range e .State .GameEvents {
92
114
if event .Type == GameEventPlacementFailedByTeamInFavor {
93
- switch primaryEvent .Type {
115
+ switch event .Type {
94
116
case
95
117
GameEventGoal ,
96
118
GameEventDefenderInDefenseArea ,
97
119
GameEventMultipleCards :
98
120
default :
99
121
// the placement failed by team in favor
100
122
// the game is continued by the other team
101
- e .SendCommand (CommandIndirect , teamInFavor .Opposite ())
123
+ command = CommandIndirect
124
+ forTeam = forTeam .Opposite ()
102
125
}
103
- return nil
126
+ return
104
127
}
105
128
}
106
129
}
107
130
108
- switch primaryEvent .Type {
131
+ switch event .Type {
109
132
case
110
133
GameEventBallLeftFieldTouchLine ,
111
134
GameEventIcing ,
@@ -118,7 +141,7 @@ func (e *Engine) Continue() error {
118
141
GameEventDefenderInDefenseAreaPartially ,
119
142
GameEventKickTimeout ,
120
143
GameEventKeeperHeldBall :
121
- e . SendCommand ( CommandIndirect , teamInFavor )
144
+ command = CommandIndirect
122
145
case
123
146
GameEventBallLeftFieldGoalLine ,
124
147
GameEventIndirectGoal ,
@@ -127,40 +150,26 @@ func (e *Engine) Continue() error {
127
150
GameEventBotCrashUnique ,
128
151
GameEventBotPushedBot ,
129
152
GameEventBotHeldBallDeliberately :
130
- e . SendCommand ( CommandDirect , teamInFavor )
153
+ command = CommandDirect
131
154
case
132
155
GameEventGoal :
133
- e . SendCommand ( CommandKickoff , teamInFavor )
156
+ command = CommandKickoff
134
157
case
135
158
GameEventBotCrashDrawn ,
136
159
GameEventNoProgressInGame :
137
- e . SendCommand ( CommandForceStart , TeamUnknown )
160
+ command = CommandForceStart
138
161
case
139
162
GameEventDefenderInDefenseArea ,
140
163
GameEventMultipleCards :
141
- e . SendCommand ( CommandPenalty , teamInFavor )
164
+ command = CommandPenalty
142
165
case
143
166
GameEventBotInterferedPlacement ,
144
167
GameEventDefenderTooCloseToKickPoint :
145
- if cmd , err := e .LastGameStartCommand (); err != nil {
146
- log .Print ("Warn: " , err )
147
- } else {
148
- e .SendCommand (cmd , teamInFavor )
149
- }
150
- case
151
- GameEventBotTooFastInStop ,
152
- GameEventUnsportiveBehaviorMinor ,
153
- GameEventUnsportiveBehaviorMajor ,
154
- GameEventBotCrashUniqueContinue ,
155
- GameEventBotPushedBotContinue ,
156
- GameEventMultipleFouls ,
157
- GameEventPlacementFailedByTeamInFavor ,
158
- GameEventPlacementFailedByOpponent :
159
- // no command
168
+ command , err = e .LastGameStartCommand ()
160
169
default :
161
- return errors .Errorf ("Unknown game event: %v" , e .State .GameEvents )
170
+ err = errors .Errorf ("Unhandled game event: %v" , e .State .GameEvents )
162
171
}
163
- return nil
172
+ return
164
173
}
165
174
166
175
func (e * Engine ) LastGameStartCommand () (RefCommand , error ) {
@@ -183,6 +192,7 @@ func (e *Engine) Process(event Event) error {
183
192
return err
184
193
}
185
194
e .updateMaxBots ()
195
+ e .updateNextCommand ()
186
196
e .appendHistory ()
187
197
return nil
188
198
}
@@ -555,7 +565,7 @@ func (e *Engine) processTrigger(t *EventTrigger) (err error) {
555
565
} else if t .Type == TriggerUndo {
556
566
e .UndoLastAction ()
557
567
} else if t .Type == TriggerContinue {
558
- err = e .Continue ()
568
+ e .Continue ()
559
569
} else {
560
570
return errors .Errorf ("Unknown trigger: %v" , t .Type )
561
571
}
0 commit comments