@@ -111,10 +111,10 @@ func (s *RemoteControlServer) handleClientConnection(conn net.Conn) {
111
111
continue
112
112
}
113
113
}
114
- if response, err := s .processRequest(*client.team, &req); err != nil {
114
+ if err := client .processRequest(&req); err != nil {
115
115
client.reply(client.Reject(err.Error()))
116
116
} else {
117
- client.replyWithPayload (client.Ok(), response )
117
+ client.replyWithState (client.Ok())
118
118
}
119
119
}
120
120
}
@@ -135,23 +135,38 @@ func (s *RemoteControlServer) SendRequest(teamName string, request *ControllerTo
135
135
return errors.Errorf("Remote control client '%v' not connected", teamName)
136
136
}
137
137
138
- func (c *RemoteControlClient) reply(reply *ControllerReply) {
139
- response := ControllerToRemoteControl{ControllerReply: reply}
138
+ func (c *RemoteControlClient) replyWithState(reply *ControllerReply) {
139
+ teamState := c.gcEngine.CurrentState().TeamState[c.team.String()]
140
+ emergencyStopDueIn := c.gcEngine.EmergencyStopDueIn(*c.team)
141
+ var emergencyStopIn *float32
142
+ if emergencyStopDueIn != nil {
143
+ emergencyStopIn = new(float32)
144
+ *emergencyStopIn = float32(emergencyStopDueIn.Seconds())
145
+ }
146
+ response := &ControllerToRemoteControl{
147
+ State: &RemoteControlTeamState{
148
+ KeeperId: teamState.Goalkeeper,
149
+ SubstituteBot: timeSet(teamState.RequestsBotSubstitutionSince),
150
+ EmergencyStop: timeSet(teamState.RequestsEmergencyStopSince),
151
+ EmergencyStopIn: emergencyStopIn,
152
+ Timeout: timeSet(teamState.RequestsTimeoutSince),
153
+ ChallengeFlag: gameEventPresent(c.gcEngine.CurrentState().GameEvents, state.GameEvent_CHALLENGE_FLAG, *c.team),
154
+ },
155
+ ControllerReply: reply,
156
+ }
140
157
141
- if err := sslconn.SendMessage(c.conn, & response); err != nil {
158
+ if err := sslconn.SendMessage(c.conn, response); err != nil {
142
159
log.Print("Failed to send reply: ", err)
143
160
}
144
161
}
145
162
146
- func (c *RemoteControlClient) replyWithPayload(reply *ControllerReply, response *ControllerToRemoteControl) {
147
- if response != nil {
148
- response.ControllerReply = reply
163
+ func (c *RemoteControlClient) reply(reply *ControllerReply) {
164
+ response := &ControllerToRemoteControl{
165
+ ControllerReply: reply,
166
+ }
149
167
150
- if err := sslconn.SendMessage(c.conn, response); err != nil {
151
- log.Print("Failed to send reply: ", err)
152
- }
153
- } else {
154
- c.reply(reply)
168
+ if err := sslconn.SendMessage(c.conn, response); err != nil {
169
+ log.Print("Failed to send reply: ", err)
155
170
}
156
171
}
157
172
@@ -171,106 +186,85 @@ func gameEventPresent(events []*state.GameEvent, eventType state.GameEvent_Type,
171
186
return
172
187
}
173
188
174
- func (s *RemoteControlServer ) processRequest(team state.Team, request *RemoteControlToController) (*ControllerToRemoteControl, error) {
189
+ func (c *RemoteControlClient ) processRequest(request *RemoteControlToController) error {
175
190
176
- if request.GetRequest() == RemoteControlToController_PING {
177
- return nil, nil
178
- }
179
- if request.GetRequest() == RemoteControlToController_GET_STATE {
180
- teamState := s.gcEngine.CurrentState().TeamState[team.String()]
181
- emergencyStopDueIn := s.gcEngine.EmergencyStopDueIn(team)
182
- var emergencyStopIn *float32
183
- if emergencyStopDueIn != nil {
184
- emergencyStopIn = new(float32)
185
- *emergencyStopIn = float32(emergencyStopDueIn.Seconds())
186
- }
187
- return &ControllerToRemoteControl{
188
- Keeper: teamState.Goalkeeper,
189
- SubstituteBot: timeSet(teamState.RequestsBotSubstitutionSince),
190
- EmergencyStop: timeSet(teamState.RequestsEmergencyStopSince),
191
- EmergencyStopIn: emergencyStopIn,
192
- Timeout: timeSet(teamState.RequestsTimeoutSince),
193
- ChallengeFlag: gameEventPresent(s.gcEngine.CurrentState().GameEvents, state.GameEvent_CHALLENGE_FLAG, team),
194
- }, nil
195
- }
196
-
197
- log.Print("Received request from remote-control: ", request)
198
-
199
- currentState := s.gcEngine.CurrentState()
200
- teamState := *currentState.TeamInfo(team)
191
+ currentState := c.gcEngine.CurrentState()
192
+ teamState := *currentState.TeamInfo(*c.team)
201
193
202
194
if x, ok := request.GetMsg().(*RemoteControlToController_DesiredKeeper); ok && *teamState.Goalkeeper != x.DesiredKeeper {
203
- if err := mayChangeKeeper(s .gcEngine.CurrentGcState(), &teamState); err != nil {
204
- return nil, errors.Wrap(err, "Remote control requests to change keeper, but: ")
195
+ if err := mayChangeKeeper(c .gcEngine.CurrentGcState(), &teamState); err != nil {
196
+ return errors.Wrap(err, "Remote control requests to change keeper, but: ")
205
197
}
206
- s .updateTeamConfig(team, &statemachine.UpdateTeamState{
198
+ c .updateTeamConfig(&statemachine.UpdateTeamState{
207
199
Goalkeeper: &x.DesiredKeeper,
208
200
})
209
201
}
210
202
211
203
if x, ok := request.GetMsg().(*RemoteControlToController_SubstituteBot); ok {
212
204
if *timeSet(teamState.RequestsBotSubstitutionSince) != x.SubstituteBot {
213
- s .updateTeamConfig(team, &statemachine.UpdateTeamState{
205
+ c .updateTeamConfig(&statemachine.UpdateTeamState{
214
206
RequestsBotSubstitution: &x.SubstituteBot,
215
207
})
216
208
}
217
- return nil, nil
209
+ return nil
218
210
}
219
211
220
212
if x, ok := request.GetMsg().(*RemoteControlToController_Timeout); ok {
221
- if *timeSet(teamState.RequestsTimeoutSince) != x.Timeout {
222
- s .updateTeamConfig(team, &statemachine.UpdateTeamState{
213
+ if ( *timeSet(teamState.RequestsTimeoutSince) ) != x.Timeout {
214
+ c .updateTeamConfig(&statemachine.UpdateTeamState{
223
215
RequestsTimeout: &x.Timeout,
224
216
})
225
217
}
226
- return nil, nil
218
+ return nil
227
219
}
228
220
229
221
if request.GetRequest() == RemoteControlToController_CHALLENGE_FLAG {
230
222
if *teamState.ChallengeFlags <= 0 {
231
- return nil, errors.New("No more challenge flags left")
223
+ return errors.New("No more challenge flags left")
232
224
} else if *teamState.TimeoutsLeft <= 0 {
233
- return nil, errors.New("No more timeouts left")
225
+ return errors.New("No more timeouts left")
234
226
}
235
227
eventType := state.GameEvent_CHALLENGE_FLAG
236
- s.gcEngine.EnqueueGameEvent(&state.GameEvent{
228
+ log.Println("Request challenge flag via remote control")
229
+ c.gcEngine.EnqueueGameEvent(&state.GameEvent{
237
230
Type: &eventType,
238
- Origin: []string{*origin(team)},
231
+ Origin: []string{*origin(c. team)},
239
232
Event: &state.GameEvent_ChallengeFlag_{
240
233
ChallengeFlag: &state.GameEvent_ChallengeFlag{
241
- ByTeam: & team,
234
+ ByTeam: c. team,
242
235
},
243
236
},
244
237
})
245
- return nil, nil
238
+ return nil
246
239
}
247
240
248
241
if x, ok := request.GetMsg().(*RemoteControlToController_EmergencyStop); ok {
249
242
if *currentState.GameState.Type != state.GameState_RUNNING {
250
- return nil, errors.New("Game is not running, can not request emergency stop")
243
+ return errors.New("Game is not running, can not request emergency stop")
251
244
}
252
245
if *timeSet(teamState.RequestsEmergencyStopSince) != x.EmergencyStop {
253
- s .updateTeamConfig(team, &statemachine.UpdateTeamState{
246
+ c .updateTeamConfig(&statemachine.UpdateTeamState{
254
247
RequestsEmergencyStop: &x.EmergencyStop,
255
248
})
256
249
}
257
- return nil, nil
250
+ return nil
258
251
}
259
252
260
- return nil, nil
253
+ return nil
261
254
}
262
255
263
- func (s *RemoteControlServer) updateTeamConfig(team state.Team, update *statemachine.UpdateTeamState) {
264
- update.ForTeam = &team
265
- s.gcEngine.Enqueue(&statemachine.Change{
266
- Origin: origin(team),
256
+ func (c *RemoteControlClient) updateTeamConfig(update *statemachine.UpdateTeamState) {
257
+ log.Println("Update team config via remote control: ", update.String())
258
+ update.ForTeam = c.team
259
+ c.gcEngine.Enqueue(&statemachine.Change{
260
+ Origin: origin(c.team),
267
261
Change: &statemachine.Change_UpdateTeamState{
268
262
UpdateTeamState: update,
269
263
},
270
264
})
271
265
}
272
266
273
- func origin(team state.Team) *string {
267
+ func origin(team * state.Team) *string {
274
268
origin := "Remote Control " + team.String()
275
269
return &origin
276
270
}
0 commit comments