Skip to content

Commit d57999e

Browse files
committed
[feature] Always send a STOP command before the ball placement
1 parent f8dcf46 commit d57999e

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

internal/app/controller/publisher.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (p *Publisher) Publish(state *State) {
7474
return
7575
}
7676

77-
updateMessage(&p.message, state)
77+
republish := updateMessage(&p.message, state)
7878
bytes, err := proto.Marshal(&p.message)
7979
if err != nil {
8080
log.Printf("Could not marshal referee message: %v\nError: %v", state, err)
@@ -84,18 +84,30 @@ func (p *Publisher) Publish(state *State) {
8484
if err != nil {
8585
log.Printf("Could not write message: %v", err)
8686
}
87+
88+
if republish {
89+
// immediately publish again to send another command
90+
p.Publish(state)
91+
}
8792
}
8893

89-
func updateMessage(r *refproto.Referee, state *State) {
94+
func updateMessage(r *refproto.Referee, state *State) (republish bool) {
95+
republish = false
9096

9197
newCommand := mapCommand(state.Command, state.CommandFor)
9298

9399
// send the GOAL command based on the team score
94100
// a STOP command will automatically be send in the next update cycle
95101
if state.TeamState[TeamYellow].Goals > int(*r.Yellow.Score) {
96102
updateCommand(r, refproto.Referee_GOAL_YELLOW)
103+
republish = true
97104
} else if state.TeamState[TeamBlue].Goals > int(*r.Blue.Score) {
98105
updateCommand(r, refproto.Referee_GOAL_BLUE)
106+
republish = true
107+
} else if state.Command == CommandBallPlacement && *r.Command != refproto.Referee_STOP {
108+
// send a STOP before the ball placement command to be compatible with earlier behavior
109+
updateCommand(r, refproto.Referee_STOP)
110+
republish = true
99111
} else if *r.Command != newCommand {
100112
updateCommand(r, newCommand)
101113
}
@@ -109,7 +121,7 @@ func updateMessage(r *refproto.Referee, state *State) {
109121
*r.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf
110122
updateTeam(r.Yellow, state.TeamState[TeamYellow])
111123
updateTeam(r.Blue, state.TeamState[TeamBlue])
112-
124+
return
113125
}
114126

115127
func updateCommand(r *refproto.Referee, newCommand refproto.Referee_Command) {

0 commit comments

Comments
 (0)