@@ -13,7 +13,12 @@ const maxDatagramSize = 8192
13
13
// Publisher can publish state and commands to the teams
14
14
type Publisher struct {
15
15
conn * net.UDPConn
16
- message refproto.Referee
16
+ message RefMessage
17
+ }
18
+
19
+ type RefMessage struct {
20
+ referee refproto.Referee
21
+ send func ()
17
22
}
18
23
19
24
// NewPublisher creates a new publisher that publishes referee messages via UDP to the teams
@@ -34,8 +39,9 @@ func NewPublisher(address string) (publisher Publisher, err error) {
34
39
log .Println ("Publishing to" , address )
35
40
36
41
publisher .conn = conn
42
+ publisher .message = RefMessage {send : publisher .send }
37
43
38
- initRefereeMessage (& publisher .message )
44
+ initRefereeMessage (& publisher .message . referee )
39
45
40
46
return
41
47
}
@@ -69,17 +75,21 @@ func initTeamInfo(t *refproto.Referee_TeamInfo) {
69
75
70
76
// Publish the state and command
71
77
func (p * Publisher ) Publish (state * State ) {
78
+ p .message .Publish (state )
79
+ }
72
80
73
- if p .conn == nil {
74
- return
75
- }
76
-
81
+ // Publish the state and command
82
+ func (p * RefMessage ) Publish (state * State ) {
77
83
p .setState (state )
78
84
p .sendCommands (state )
79
85
}
80
86
81
87
func (p * Publisher ) send () {
82
- bytes , err := proto .Marshal (& p .message )
88
+ if p .conn == nil {
89
+ return
90
+ }
91
+
92
+ bytes , err := proto .Marshal (& p .message .referee )
83
93
if err != nil {
84
94
log .Printf ("Could not marshal referee message: %v\n Error: %v" , p .message , err )
85
95
return
@@ -90,8 +100,8 @@ func (p *Publisher) send() {
90
100
}
91
101
}
92
102
93
- func (p * Publisher ) setState (state * State ) (republish bool ) {
94
- r := & p .message
103
+ func (p * RefMessage ) setState (state * State ) (republish bool ) {
104
+ r := & p .referee
95
105
96
106
r .GameEvents = mapGameEvents (state .GameEvents )
97
107
r .DesignatedPosition = mapLocation (state .PlacementPos )
@@ -105,26 +115,26 @@ func (p *Publisher) setState(state *State) (republish bool) {
105
115
return
106
116
}
107
117
108
- func (p * Publisher ) sendCommands (state * State ) {
118
+ func (p * RefMessage ) sendCommands (state * State ) {
109
119
newCommand := mapCommand (state .Command , state .CommandFor )
110
120
111
121
// 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 ) {
122
+ if state .TeamState [TeamYellow ].Goals > int (* p .referee .Yellow .Score ) {
113
123
p .updateCommand (refproto .Referee_GOAL_YELLOW )
114
124
p .send ()
115
125
p .updateCommand (newCommand )
116
- } else if state .TeamState [TeamBlue ].Goals > int (* p .message .Blue .Score ) {
126
+ } else if state .TeamState [TeamBlue ].Goals > int (* p .referee .Blue .Score ) {
117
127
p .updateCommand (refproto .Referee_GOAL_BLUE )
118
128
p .send ()
119
129
p .updateCommand (newCommand )
120
- } else if * p .message .Command != newCommand {
130
+ } else if * p .referee .Command != newCommand {
121
131
switch state .Command {
122
132
case CommandBallPlacement ,
123
133
CommandDirect ,
124
134
CommandIndirect ,
125
135
CommandKickoff ,
126
136
CommandPenalty :
127
- if * p .message .Command != refproto .Referee_STOP {
137
+ if * p .referee .Command != refproto .Referee_STOP {
128
138
// send a STOP right before the actual command to be compatible with old behavior
129
139
p .updateCommand (refproto .Referee_STOP )
130
140
p .send ()
@@ -136,6 +146,12 @@ func (p *Publisher) sendCommands(state *State) {
136
146
p .send ()
137
147
}
138
148
149
+ func (p * RefMessage ) updateCommand (newCommand refproto.Referee_Command ) {
150
+ * p .referee .Command = newCommand
151
+ * p .referee .CommandCounter ++
152
+ * p .referee .CommandTimestamp = uint64 (time .Now ().UnixNano () / 1000 )
153
+ }
154
+
139
155
func mapGameEvents (events []* GameEvent ) []* refproto.GameEvent {
140
156
mappedEvents := make ([]* refproto.GameEvent , len (events ))
141
157
for i , e := range events {
@@ -144,12 +160,6 @@ func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {
144
160
return mappedEvents
145
161
}
146
162
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 )
151
- }
152
-
153
163
func mapCommand (command RefCommand , team Team ) refproto.Referee_Command {
154
164
switch command {
155
165
case CommandHalt :
0 commit comments