Skip to content

Commit 0f0c474

Browse files
committed
[refactoring] Make publisher fields accessible from outside, rename
They need to be accessed in upcoming changes
1 parent 4c9696c commit 0f0c474

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

internal/app/controller/publisher.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ const maxDatagramSize = 8192
1414
type Publisher struct {
1515
address string
1616
conn *net.UDPConn
17-
message RefMessage
17+
Message RefMessage
1818
}
1919

2020
type RefMessage struct {
21-
referee *refproto.Referee
22-
send func()
21+
ProtoMsg *refproto.Referee
22+
Send func()
2323
}
2424

2525
// NewPublisher creates a new publisher that publishes referee messages via UDP to the teams
@@ -28,8 +28,8 @@ func NewPublisher(address string) (publisher Publisher) {
2828
publisher.address = address
2929

3030
// initialize default referee message
31-
publisher.message = RefMessage{send: publisher.send, referee: new(refproto.Referee)}
32-
initRefereeMessage(publisher.message.referee)
31+
publisher.Message = RefMessage{Send: publisher.send, ProtoMsg: new(refproto.Referee)}
32+
initRefereeMessage(publisher.Message.ProtoMsg)
3333

3434
publisher.connect()
3535

@@ -96,7 +96,7 @@ func initTeamInfo(t *refproto.Referee_TeamInfo) {
9696

9797
// Publish the state and command
9898
func (p *Publisher) Publish(state *State) {
99-
p.message.Publish(state)
99+
p.Message.Publish(state)
100100
}
101101

102102
// Publish the state and command
@@ -111,9 +111,9 @@ func (p *Publisher) send() {
111111
return
112112
}
113113

114-
bytes, err := proto.Marshal(p.message.referee)
114+
bytes, err := proto.Marshal(p.Message.ProtoMsg)
115115
if err != nil {
116-
log.Printf("Could not marshal referee message: %v\nError: %v", p.message, err)
116+
log.Printf("Could not marshal referee message: %v\nError: %v", p.Message, err)
117117
return
118118
}
119119
_, err = p.conn.Write(bytes)
@@ -124,19 +124,19 @@ func (p *Publisher) send() {
124124
}
125125

126126
func (p *RefMessage) setState(state *State) (republish bool) {
127-
p.referee.GameEvents = mapGameEvents(state.GameEvents)
128-
p.referee.DesignatedPosition = mapLocation(state.PlacementPos)
129-
p.referee.ProposedGameEvents = mapProposals(state.GameEventProposals)
130-
131-
*p.referee.PacketTimestamp = uint64(time.Now().UnixNano() / 1000)
132-
*p.referee.Stage = mapStage(state.Stage)
133-
*p.referee.StageTimeLeft = int32(state.StageTimeLeft.Nanoseconds() / 1000)
134-
*p.referee.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf
135-
*p.referee.NextCommand = mapCommand(state.NextCommand, state.NextCommandFor)
136-
*p.referee.CurrentActionTimeRemaining = int32(state.CurrentActionTimeRemaining.Nanoseconds() / 1000)
137-
138-
updateTeam(p.referee.Yellow, state.TeamState[TeamYellow])
139-
updateTeam(p.referee.Blue, state.TeamState[TeamBlue])
127+
p.ProtoMsg.GameEvents = mapGameEvents(state.GameEvents)
128+
p.ProtoMsg.DesignatedPosition = mapLocation(state.PlacementPos)
129+
p.ProtoMsg.ProposedGameEvents = mapProposals(state.GameEventProposals)
130+
131+
*p.ProtoMsg.PacketTimestamp = uint64(time.Now().UnixNano() / 1000)
132+
*p.ProtoMsg.Stage = mapStage(state.Stage)
133+
*p.ProtoMsg.StageTimeLeft = int32(state.StageTimeLeft.Nanoseconds() / 1000)
134+
*p.ProtoMsg.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf
135+
*p.ProtoMsg.NextCommand = mapCommand(state.NextCommand, state.NextCommandFor)
136+
*p.ProtoMsg.CurrentActionTimeRemaining = int32(state.CurrentActionTimeRemaining.Nanoseconds() / 1000)
137+
138+
updateTeam(p.ProtoMsg.Yellow, state.TeamState[TeamYellow])
139+
updateTeam(p.ProtoMsg.Blue, state.TeamState[TeamBlue])
140140
return
141141
}
142142

@@ -156,37 +156,37 @@ func (p *RefMessage) sendCommands(state *State) {
156156
newCommand := mapCommand(state.Command, state.CommandFor)
157157

158158
// send the GOAL command based on the team score for compatibility with old behavior
159-
if state.TeamState[TeamYellow].Goals > int(*p.referee.Yellow.Score) {
159+
if state.TeamState[TeamYellow].Goals > int(*p.ProtoMsg.Yellow.Score) {
160160
p.updateCommand(refproto.Referee_GOAL_YELLOW)
161-
p.send()
161+
p.Send()
162162
p.updateCommand(newCommand)
163-
} else if state.TeamState[TeamBlue].Goals > int(*p.referee.Blue.Score) {
163+
} else if state.TeamState[TeamBlue].Goals > int(*p.ProtoMsg.Blue.Score) {
164164
p.updateCommand(refproto.Referee_GOAL_BLUE)
165-
p.send()
165+
p.Send()
166166
p.updateCommand(newCommand)
167-
} else if *p.referee.Command != newCommand {
167+
} else if *p.ProtoMsg.Command != newCommand {
168168
switch state.Command {
169169
case CommandBallPlacement,
170170
CommandDirect,
171171
CommandIndirect,
172172
CommandKickoff,
173173
CommandPenalty:
174-
if *p.referee.Command != refproto.Referee_STOP {
174+
if *p.ProtoMsg.Command != refproto.Referee_STOP {
175175
// send a STOP right before the actual command to be compatible with old behavior
176176
p.updateCommand(refproto.Referee_STOP)
177-
p.send()
177+
p.Send()
178178
}
179179
}
180180
p.updateCommand(newCommand)
181181
}
182182

183-
p.send()
183+
p.Send()
184184
}
185185

186186
func (p *RefMessage) updateCommand(newCommand refproto.Referee_Command) {
187-
*p.referee.Command = newCommand
188-
*p.referee.CommandCounter++
189-
*p.referee.CommandTimestamp = uint64(time.Now().UnixNano() / 1000)
187+
*p.ProtoMsg.Command = newCommand
188+
*p.ProtoMsg.CommandCounter++
189+
*p.ProtoMsg.CommandTimestamp = uint64(time.Now().UnixNano() / 1000)
190190
}
191191

192192
func mapGameEvents(events []*GameEvent) []*refproto.GameEvent {

internal/app/controller/publisher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
func Test_updateMessage(t *testing.T) {
1212
state := NewState()
1313

14-
r := &RefMessage{send: func() {}, referee: new(refproto.Referee)}
15-
referee := r.referee
14+
r := &RefMessage{Send: func() {}, ProtoMsg: new(refproto.Referee)}
15+
referee := r.ProtoMsg
1616
initRefereeMessage(referee)
1717
r.Publish(state)
1818

0 commit comments

Comments
 (0)