@@ -111,10 +111,10 @@ func (s *RemoteControlServer) handleClientConnection(conn net.Conn) {
111111 continue
112112 }
113113 }
114- if response , err := s .processRequest (* client . team , & req ); err != nil {
114+ if err := client .processRequest (& req ); err != nil {
115115 client .reply (client .Reject (err .Error ()))
116116 } else {
117- client .replyWithPayload (client .Ok (), response )
117+ client .replyWithState (client .Ok ())
118118 }
119119 }
120120}
@@ -135,23 +135,38 @@ func (s *RemoteControlServer) SendRequest(teamName string, request *ControllerTo
135135 return errors .Errorf ("Remote control client '%v' not connected" , teamName )
136136}
137137
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+ }
140157
141- if err := sslconn .SendMessage (c .conn , & response ); err != nil {
158+ if err := sslconn .SendMessage (c .conn , response ); err != nil {
142159 log .Print ("Failed to send reply: " , err )
143160 }
144161}
145162
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+ }
149167
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 )
155170 }
156171}
157172
@@ -171,106 +186,85 @@ func gameEventPresent(events []*state.GameEvent, eventType state.GameEvent_Type,
171186 return
172187}
173188
174- func (s * RemoteControlServer ) processRequest (team state. Team , request * RemoteControlToController ) ( * ControllerToRemoteControl , error ) {
189+ func (c * RemoteControlClient ) processRequest (request * RemoteControlToController ) error {
175190
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 )
201193
202194 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: " )
205197 }
206- s .updateTeamConfig (team , & statemachine.UpdateTeamState {
198+ c .updateTeamConfig (& statemachine.UpdateTeamState {
207199 Goalkeeper : & x .DesiredKeeper ,
208200 })
209201 }
210202
211203 if x , ok := request .GetMsg ().(* RemoteControlToController_SubstituteBot ); ok {
212204 if * timeSet (teamState .RequestsBotSubstitutionSince ) != x .SubstituteBot {
213- s .updateTeamConfig (team , & statemachine.UpdateTeamState {
205+ c .updateTeamConfig (& statemachine.UpdateTeamState {
214206 RequestsBotSubstitution : & x .SubstituteBot ,
215207 })
216208 }
217- return nil , nil
209+ return nil
218210 }
219211
220212 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 {
223215 RequestsTimeout : & x .Timeout ,
224216 })
225217 }
226- return nil , nil
218+ return nil
227219 }
228220
229221 if request .GetRequest () == RemoteControlToController_CHALLENGE_FLAG {
230222 if * teamState .ChallengeFlags <= 0 {
231- return nil , errors .New ("No more challenge flags left" )
223+ return errors .New ("No more challenge flags left" )
232224 } else if * teamState .TimeoutsLeft <= 0 {
233- return nil , errors .New ("No more timeouts left" )
225+ return errors .New ("No more timeouts left" )
234226 }
235227 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 {
237230 Type : & eventType ,
238- Origin : []string {* origin (team )},
231+ Origin : []string {* origin (c . team )},
239232 Event : & state.GameEvent_ChallengeFlag_ {
240233 ChallengeFlag : & state.GameEvent_ChallengeFlag {
241- ByTeam : & team ,
234+ ByTeam : c . team ,
242235 },
243236 },
244237 })
245- return nil , nil
238+ return nil
246239 }
247240
248241 if x , ok := request .GetMsg ().(* RemoteControlToController_EmergencyStop ); ok {
249242 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" )
251244 }
252245 if * timeSet (teamState .RequestsEmergencyStopSince ) != x .EmergencyStop {
253- s .updateTeamConfig (team , & statemachine.UpdateTeamState {
246+ c .updateTeamConfig (& statemachine.UpdateTeamState {
254247 RequestsEmergencyStop : & x .EmergencyStop ,
255248 })
256249 }
257- return nil , nil
250+ return nil
258251 }
259252
260- return nil , nil
253+ return nil
261254}
262255
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 ),
267261 Change : & statemachine.Change_UpdateTeamState {
268262 UpdateTeamState : update ,
269263 },
270264 })
271265}
272266
273- func origin (team state.Team ) * string {
267+ func origin (team * state.Team ) * string {
274268 origin := "Remote Control " + team .String ()
275269 return & origin
276270}
0 commit comments