@@ -74,43 +74,24 @@ func (p *Publisher) Publish(state *State) {
74
74
return
75
75
}
76
76
77
- republish := updateMessage (& p .message , state )
77
+ p .setState (state )
78
+ p .sendCommands (state )
79
+ }
80
+
81
+ func (p * Publisher ) send () {
78
82
bytes , err := proto .Marshal (& p .message )
79
83
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 )
81
85
return
82
86
}
83
87
_ , err = p .conn .Write (bytes )
84
88
if err != nil {
85
89
log .Printf ("Could not write message: %v" , err )
86
90
}
87
-
88
- if republish {
89
- // immediately publish again to send another command
90
- p .Publish (state )
91
- }
92
91
}
93
92
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
114
95
115
96
r .GameEvents = mapGameEvents (state .GameEvents )
116
97
r .DesignatedPosition = mapLocation (state .PlacementPos )
@@ -123,6 +104,38 @@ func updateMessage(r *refproto.Referee, state *State) (republish bool) {
123
104
updateTeam (r .Blue , state .TeamState [TeamBlue ])
124
105
return
125
106
}
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
+
126
139
func mapGameEvents (events []* GameEvent ) []* refproto.GameEvent {
127
140
mappedEvents := make ([]* refproto.GameEvent , len (events ))
128
141
for i , e := range events {
@@ -131,10 +144,10 @@ func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {
131
144
return mappedEvents
132
145
}
133
146
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 )
138
151
}
139
152
140
153
func mapCommand (command RefCommand , team Team ) refproto.Referee_Command {
0 commit comments