@@ -74,43 +74,24 @@ func (p *Publisher) Publish(state *State) {
7474 return
7575 }
7676
77- republish := updateMessage (& p .message , state )
77+ p .setState (state )
78+ p .sendCommands (state )
79+ }
80+
81+ func (p * Publisher ) send () {
7882 bytes , err := proto .Marshal (& p .message )
7983 if err != nil {
80- log .Printf ("Could not marshal referee message: %v\n Error: %v" , state , err )
84+ log .Printf ("Could not marshal referee message: %v\n Error: %v" , p . message , err )
8185 return
8286 }
8387 _ , err = p .conn .Write (bytes )
8488 if err != nil {
8589 log .Printf ("Could not write message: %v" , err )
8690 }
87-
88- if republish {
89- // immediately publish again to send another command
90- p .Publish (state )
91- }
9291}
9392
94- func updateMessage (r * refproto.Referee , state * State ) (republish bool ) {
95- republish = false
96-
97- newCommand := mapCommand (state .Command , state .CommandFor )
98-
99- // send the GOAL command based on the team score
100- // a STOP command will automatically be send in the next update cycle
101- if state .TeamState [TeamYellow ].Goals > int (* r .Yellow .Score ) {
102- updateCommand (r , refproto .Referee_GOAL_YELLOW )
103- republish = true
104- } else if state .TeamState [TeamBlue ].Goals > int (* r .Blue .Score ) {
105- updateCommand (r , refproto .Referee_GOAL_BLUE )
106- republish = true
107- } else if state .Command == CommandBallPlacement && * r .Command != refproto .Referee_STOP && * r .Command != refproto .Referee_BALL_PLACEMENT_BLUE && * r .Command != refproto .Referee_BALL_PLACEMENT_YELLOW {
108- // send a STOP before the ball placement command to be compatible with earlier behavior
109- updateCommand (r , refproto .Referee_STOP )
110- republish = true
111- } else if * r .Command != newCommand {
112- updateCommand (r , newCommand )
113- }
93+ func (p * Publisher ) setState (state * State ) (republish bool ) {
94+ r := & p .message
11495
11596 r .GameEvents = mapGameEvents (state .GameEvents )
11697 r .DesignatedPosition = mapLocation (state .PlacementPos )
@@ -123,6 +104,38 @@ func updateMessage(r *refproto.Referee, state *State) (republish bool) {
123104 updateTeam (r .Blue , state .TeamState [TeamBlue ])
124105 return
125106}
107+
108+ func (p * Publisher ) sendCommands (state * State ) {
109+ newCommand := mapCommand (state .Command , state .CommandFor )
110+
111+ // send the GOAL command based on the team score for compatibility with old behavior
112+ if state .TeamState [TeamYellow ].Goals > int (* p .message .Yellow .Score ) {
113+ p .updateCommand (refproto .Referee_GOAL_YELLOW )
114+ p .send ()
115+ p .updateCommand (newCommand )
116+ } else if state .TeamState [TeamBlue ].Goals > int (* p .message .Blue .Score ) {
117+ p .updateCommand (refproto .Referee_GOAL_BLUE )
118+ p .send ()
119+ p .updateCommand (newCommand )
120+ } else if * p .message .Command != newCommand {
121+ switch state .Command {
122+ case CommandBallPlacement ,
123+ CommandDirect ,
124+ CommandIndirect ,
125+ CommandKickoff ,
126+ CommandPenalty :
127+ if * p .message .Command != refproto .Referee_STOP {
128+ // send a STOP right before the actual command to be compatible with old behavior
129+ p .updateCommand (refproto .Referee_STOP )
130+ p .send ()
131+ }
132+ }
133+ p .updateCommand (newCommand )
134+ }
135+
136+ p .send ()
137+ }
138+
126139func mapGameEvents (events []* GameEvent ) []* refproto.GameEvent {
127140 mappedEvents := make ([]* refproto.GameEvent , len (events ))
128141 for i , e := range events {
@@ -131,10 +144,10 @@ func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {
131144 return mappedEvents
132145}
133146
134- func updateCommand ( r * refproto. Referee , newCommand refproto.Referee_Command ) {
135- * r .Command = newCommand
136- * r .CommandCounter ++
137- * r .CommandTimestamp = uint64 (time .Now ().UnixNano () / 1000 )
147+ func ( p * Publisher ) updateCommand ( newCommand refproto.Referee_Command ) {
148+ * p . message .Command = newCommand
149+ * p . message .CommandCounter ++
150+ * p . message .CommandTimestamp = uint64 (time .Now ().UnixNano () / 1000 )
138151}
139152
140153func mapCommand (command RefCommand , team Team ) refproto.Referee_Command {
0 commit comments