|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/RoboCup-SSL/ssl-go-tools/sslproto" |
| 5 | + "github.com/golang/protobuf/proto" |
| 6 | + "log" |
| 7 | + "net" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +const maxDatagramSize = 8192 |
| 12 | + |
| 13 | +type RefBoxPublisher struct { |
| 14 | + Conn *net.UDPConn |
| 15 | + Message sslproto.SSL_Referee |
| 16 | +} |
| 17 | + |
| 18 | +// NewBroadcaster creates a new UDP multicast connection on which to broadcast |
| 19 | +func NewRefBoxPublisher(address string) RefBoxPublisher { |
| 20 | + addr, err := net.ResolveUDPAddr("udp", address) |
| 21 | + if err != nil { |
| 22 | + log.Fatalln(err) |
| 23 | + } |
| 24 | + |
| 25 | + conn, err := net.DialUDP("udp", nil, addr) |
| 26 | + if err != nil { |
| 27 | + log.Fatalln(err) |
| 28 | + } |
| 29 | + conn.SetReadBuffer(maxDatagramSize) |
| 30 | + log.Println("Connected to", address) |
| 31 | + |
| 32 | + publisher := RefBoxPublisher{} |
| 33 | + publisher.Conn = conn |
| 34 | + |
| 35 | + initRefereeMessage(&publisher.Message) |
| 36 | + |
| 37 | + return publisher |
| 38 | +} |
| 39 | + |
| 40 | +func initRefereeMessage(m *sslproto.SSL_Referee) { |
| 41 | + m.PacketTimestamp = new(uint64) |
| 42 | + m.Stage = new(sslproto.SSL_Referee_Stage) |
| 43 | + m.StageTimeLeft = new(int32) |
| 44 | + m.Command = new(sslproto.SSL_Referee_Command) |
| 45 | + m.CommandCounter = new(uint32) |
| 46 | + m.CommandTimestamp = new(uint64) |
| 47 | + m.Yellow = new(sslproto.SSL_Referee_TeamInfo) |
| 48 | + initTeamInfo(m.Yellow) |
| 49 | + m.Blue = new(sslproto.SSL_Referee_TeamInfo) |
| 50 | + initTeamInfo(m.Blue) |
| 51 | + m.BlueTeamOnPositiveHalf = new(bool) |
| 52 | +} |
| 53 | + |
| 54 | +func initTeamInfo(t *sslproto.SSL_Referee_TeamInfo) { |
| 55 | + t.Name = new(string) |
| 56 | + t.Score = new(uint32) |
| 57 | + t.RedCards = new(uint32) |
| 58 | + t.YellowCards = new(uint32) |
| 59 | + t.Timeouts = new(uint32) |
| 60 | + t.TimeoutTime = new(uint32) |
| 61 | + t.Goalie = new(uint32) |
| 62 | +} |
| 63 | + |
| 64 | +func (p *RefBoxPublisher) Publish(state *RefBoxState, command *RefBoxEventCommand) { |
| 65 | + |
| 66 | + updateMessage(&p.Message, state, command) |
| 67 | + bytes, err := proto.Marshal(&p.Message) |
| 68 | + if err != nil { |
| 69 | + log.Printf("Could not marshal referee message: %v\nError: %v", state, err) |
| 70 | + return |
| 71 | + } |
| 72 | + _, err = p.Conn.Write(bytes) |
| 73 | + if err != nil { |
| 74 | + log.Printf("Could not write message: %v", err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func updateMessage(r *sslproto.SSL_Referee, state *RefBoxState, command *RefBoxEventCommand) { |
| 79 | + |
| 80 | + *r.PacketTimestamp = uint64(time.Now().UnixNano() / 1000) |
| 81 | + *r.Stage = mapStage(state.Stage) |
| 82 | + *r.StageTimeLeft = int32(state.GameTimeLeft.Nanoseconds() / 1000) |
| 83 | + *r.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf |
| 84 | + updateTeam(r.Yellow, state.TeamState[TeamYellow]) |
| 85 | + updateTeam(r.Blue, state.TeamState[TeamBlue]) |
| 86 | + |
| 87 | + if command != nil { |
| 88 | + *r.Command = mapCommand(command) |
| 89 | + *r.CommandCounter++ |
| 90 | + *r.CommandTimestamp = uint64(time.Now().UnixNano() / 1000) |
| 91 | + } |
| 92 | +} |
| 93 | +func mapCommand(c *RefBoxEventCommand) sslproto.SSL_Referee_Command { |
| 94 | + switch c.Type { |
| 95 | + case CommandHalt: |
| 96 | + return sslproto.SSL_Referee_HALT |
| 97 | + case CommandStop: |
| 98 | + return sslproto.SSL_Referee_STOP |
| 99 | + case CommandNormalStart: |
| 100 | + return sslproto.SSL_Referee_NORMAL_START |
| 101 | + case CommandForceStart: |
| 102 | + return sslproto.SSL_Referee_FORCE_START |
| 103 | + case CommandDirect: |
| 104 | + return commandByTeam(c, sslproto.SSL_Referee_DIRECT_FREE_BLUE, sslproto.SSL_Referee_DIRECT_FREE_YELLOW) |
| 105 | + case CommandIndirect: |
| 106 | + return commandByTeam(c, sslproto.SSL_Referee_INDIRECT_FREE_BLUE, sslproto.SSL_Referee_INDIRECT_FREE_YELLOW) |
| 107 | + case CommandKickoff: |
| 108 | + return commandByTeam(c, sslproto.SSL_Referee_PREPARE_KICKOFF_BLUE, sslproto.SSL_Referee_PREPARE_KICKOFF_YELLOW) |
| 109 | + case CommandPenalty: |
| 110 | + return commandByTeam(c, sslproto.SSL_Referee_PREPARE_PENALTY_BLUE, sslproto.SSL_Referee_PREPARE_PENALTY_YELLOW) |
| 111 | + case CommandTimeout: |
| 112 | + return commandByTeam(c, sslproto.SSL_Referee_TIMEOUT_BLUE, sslproto.SSL_Referee_TIMEOUT_YELLOW) |
| 113 | + case CommandBallPlacement: |
| 114 | + return commandByTeam(c, sslproto.SSL_Referee_BALL_PLACEMENT_BLUE, sslproto.SSL_Referee_BALL_PLACEMENT_YELLOW) |
| 115 | + case CommandGoal: |
| 116 | + return commandByTeam(c, sslproto.SSL_Referee_GOAL_BLUE, sslproto.SSL_Referee_GOAL_YELLOW) |
| 117 | + } |
| 118 | + return -1 |
| 119 | +} |
| 120 | + |
| 121 | +func commandByTeam(command *RefBoxEventCommand, blueCommand sslproto.SSL_Referee_Command, yellowCommand sslproto.SSL_Referee_Command) sslproto.SSL_Referee_Command { |
| 122 | + if *command.ForTeam == TeamBlue { |
| 123 | + return blueCommand |
| 124 | + } |
| 125 | + return yellowCommand |
| 126 | +} |
| 127 | + |
| 128 | +func updateTeam(team *sslproto.SSL_Referee_TeamInfo, state *RefBoxTeamState) { |
| 129 | + *team.Name = state.Name |
| 130 | + *team.Score = uint32(state.Goals) |
| 131 | + *team.RedCards = uint32(state.RedCards) |
| 132 | + team.YellowCardTimes = mapTimes(state.YellowCardTimes) |
| 133 | + *team.YellowCards = uint32(state.YellowCards) |
| 134 | + *team.Timeouts = uint32(state.TimeoutsLeft) |
| 135 | + *team.TimeoutTime = uint32(state.TimeoutTimeLeft.Nanoseconds() / 1000) |
| 136 | + *team.Goalie = uint32(state.Goalie) |
| 137 | +} |
| 138 | + |
| 139 | +func mapTimes(durations []time.Duration) []uint32 { |
| 140 | + times := make([]uint32, len(durations)) |
| 141 | + for i, d := range durations { |
| 142 | + times[i] = uint32(d.Nanoseconds() / 1000) |
| 143 | + } |
| 144 | + return times |
| 145 | +} |
| 146 | + |
| 147 | +func mapStage(stage RefBoxStage) sslproto.SSL_Referee_Stage { |
| 148 | + switch stage { |
| 149 | + case StagePreGame: |
| 150 | + return sslproto.SSL_Referee_NORMAL_FIRST_HALF_PRE |
| 151 | + case StageFirstHalf: |
| 152 | + return sslproto.SSL_Referee_NORMAL_FIRST_HALF |
| 153 | + case StageHalfTime: |
| 154 | + return sslproto.SSL_Referee_NORMAL_HALF_TIME |
| 155 | + case StageSecondHalfPre: |
| 156 | + return sslproto.SSL_Referee_NORMAL_SECOND_HALF_PRE |
| 157 | + case StageSecondHalf: |
| 158 | + return sslproto.SSL_Referee_NORMAL_SECOND_HALF |
| 159 | + case StageOvertimeBreak: |
| 160 | + return sslproto.SSL_Referee_EXTRA_TIME_BREAK |
| 161 | + case StageOvertimeFirstHalfPre: |
| 162 | + return sslproto.SSL_Referee_EXTRA_FIRST_HALF_PRE |
| 163 | + case StageOvertimeFirstHalf: |
| 164 | + return sslproto.SSL_Referee_EXTRA_FIRST_HALF |
| 165 | + case StageOvertimeHalfTime: |
| 166 | + return sslproto.SSL_Referee_EXTRA_HALF_TIME |
| 167 | + case StageOvertimeSecondHalfPre: |
| 168 | + return sslproto.SSL_Referee_EXTRA_SECOND_HALF_PRE |
| 169 | + case StageOvertimeSecondHalf: |
| 170 | + return sslproto.SSL_Referee_EXTRA_SECOND_HALF |
| 171 | + case StageShootoutBreak: |
| 172 | + return sslproto.SSL_Referee_PENALTY_SHOOTOUT_BREAK |
| 173 | + case StageShootout: |
| 174 | + return sslproto.SSL_Referee_PENALTY_SHOOTOUT |
| 175 | + case StagePostGame: |
| 176 | + return sslproto.SSL_Referee_POST_GAME |
| 177 | + } |
| 178 | + return -1 |
| 179 | +} |
0 commit comments