Skip to content

Commit 5341742

Browse files
committed
[feature] Show next command in UI and publish it in referee message
1 parent 53c2683 commit 5341742

File tree

7 files changed

+149
-100
lines changed

7 files changed

+149
-100
lines changed

internal/app/controller/engine.go

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func (e *Engine) SendCommand(command RefCommand, forTeam Team) {
6262
e.State.GameEvents = []*GameEvent{}
6363
}
6464
e.State.PlacementPos = nil
65+
e.State.NextCommand = CommandUnknown
66+
e.State.NextCommandFor = TeamUnknown
6567
}
6668
}
6769

@@ -80,32 +82,53 @@ func (e *Engine) UndoLastAction() {
8082
}
8183
}
8284

83-
func (e *Engine) Continue() error {
85+
func (e *Engine) Continue() {
86+
if e.State.NextCommand != CommandUnknown {
87+
e.SendCommand(e.State.NextCommand, e.State.NextCommandFor)
88+
}
89+
}
90+
91+
func (e *Engine) updateNextCommand() {
8492
if len(e.State.GameEvents) == 0 {
85-
return errors.New("No game events available to determine next action")
93+
return
8694
}
8795
primaryEvent := e.State.GameEvents[0]
88-
teamInFavor := primaryEvent.ByTeam().Opposite()
96+
command, forTeam, err := e.CommandForEvent(primaryEvent)
97+
if err != nil {
98+
log.Print("Warn: ", err)
99+
return
100+
}
101+
e.State.NextCommand = command
102+
e.State.NextCommandFor = forTeam
103+
}
104+
105+
func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam Team, err error) {
106+
if event.IsSecondary() {
107+
return
108+
}
109+
110+
forTeam = event.ByTeam().Opposite()
89111

90112
if e.State.Division == DivA {
91113
for _, event := range e.State.GameEvents {
92114
if event.Type == GameEventPlacementFailedByTeamInFavor {
93-
switch primaryEvent.Type {
115+
switch event.Type {
94116
case
95117
GameEventGoal,
96118
GameEventDefenderInDefenseArea,
97119
GameEventMultipleCards:
98120
default:
99121
// the placement failed by team in favor
100122
// the game is continued by the other team
101-
e.SendCommand(CommandIndirect, teamInFavor.Opposite())
123+
command = CommandIndirect
124+
forTeam = forTeam.Opposite()
102125
}
103-
return nil
126+
return
104127
}
105128
}
106129
}
107130

108-
switch primaryEvent.Type {
131+
switch event.Type {
109132
case
110133
GameEventBallLeftFieldTouchLine,
111134
GameEventIcing,
@@ -118,7 +141,7 @@ func (e *Engine) Continue() error {
118141
GameEventDefenderInDefenseAreaPartially,
119142
GameEventKickTimeout,
120143
GameEventKeeperHeldBall:
121-
e.SendCommand(CommandIndirect, teamInFavor)
144+
command = CommandIndirect
122145
case
123146
GameEventBallLeftFieldGoalLine,
124147
GameEventIndirectGoal,
@@ -127,40 +150,26 @@ func (e *Engine) Continue() error {
127150
GameEventBotCrashUnique,
128151
GameEventBotPushedBot,
129152
GameEventBotHeldBallDeliberately:
130-
e.SendCommand(CommandDirect, teamInFavor)
153+
command = CommandDirect
131154
case
132155
GameEventGoal:
133-
e.SendCommand(CommandKickoff, teamInFavor)
156+
command = CommandKickoff
134157
case
135158
GameEventBotCrashDrawn,
136159
GameEventNoProgressInGame:
137-
e.SendCommand(CommandForceStart, TeamUnknown)
160+
command = CommandForceStart
138161
case
139162
GameEventDefenderInDefenseArea,
140163
GameEventMultipleCards:
141-
e.SendCommand(CommandPenalty, teamInFavor)
164+
command = CommandPenalty
142165
case
143166
GameEventBotInterferedPlacement,
144167
GameEventDefenderTooCloseToKickPoint:
145-
if cmd, err := e.LastGameStartCommand(); err != nil {
146-
log.Print("Warn: ", err)
147-
} else {
148-
e.SendCommand(cmd, teamInFavor)
149-
}
150-
case
151-
GameEventBotTooFastInStop,
152-
GameEventUnsportiveBehaviorMinor,
153-
GameEventUnsportiveBehaviorMajor,
154-
GameEventBotCrashUniqueContinue,
155-
GameEventBotPushedBotContinue,
156-
GameEventMultipleFouls,
157-
GameEventPlacementFailedByTeamInFavor,
158-
GameEventPlacementFailedByOpponent:
159-
// no command
168+
command, err = e.LastGameStartCommand()
160169
default:
161-
return errors.Errorf("Unknown game event: %v", e.State.GameEvents)
170+
err = errors.Errorf("Unhandled game event: %v", e.State.GameEvents)
162171
}
163-
return nil
172+
return
164173
}
165174

166175
func (e *Engine) LastGameStartCommand() (RefCommand, error) {
@@ -183,6 +192,7 @@ func (e *Engine) Process(event Event) error {
183192
return err
184193
}
185194
e.updateMaxBots()
195+
e.updateNextCommand()
186196
e.appendHistory()
187197
return nil
188198
}
@@ -555,7 +565,7 @@ func (e *Engine) processTrigger(t *EventTrigger) (err error) {
555565
} else if t.Type == TriggerUndo {
556566
e.UndoLastAction()
557567
} else if t.Type == TriggerContinue {
558-
err = e.Continue()
568+
e.Continue()
559569
} else {
560570
return errors.Errorf("Unknown trigger: %v", t.Type)
561571
}

internal/app/controller/publisher.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func initRefereeMessage(m *refproto.Referee) {
5858
m.Blue = new(refproto.Referee_TeamInfo)
5959
initTeamInfo(m.Blue)
6060
m.BlueTeamOnPositiveHalf = new(bool)
61+
m.NextCommand = new(refproto.Referee_Command)
6162
}
6263

6364
func initTeamInfo(t *refproto.Referee_TeamInfo) {
@@ -109,6 +110,8 @@ func (p *RefMessage) setState(state *State) (republish bool) {
109110
*p.referee.Stage = mapStage(state.Stage)
110111
*p.referee.StageTimeLeft = int32(state.StageTimeLeft.Nanoseconds() / 1000)
111112
*p.referee.BlueTeamOnPositiveHalf = state.TeamState[TeamBlue].OnPositiveHalf
113+
*p.referee.NextCommand = mapCommand(state.NextCommand, state.NextCommandFor)
114+
112115
updateTeam(p.referee.Yellow, state.TeamState[TeamYellow])
113116
updateTeam(p.referee.Blue, state.TeamState[TeamBlue])
114117
return

internal/app/controller/state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func (s Stage) IsPausedStage() bool {
171171
type RefCommand string
172172

173173
const (
174+
// CommandUnknown not set
175+
CommandUnknown RefCommand = ""
174176
// CommandHalt HALT
175177
CommandHalt RefCommand = "halt"
176178
// CommandStop STOP
@@ -258,6 +260,8 @@ type State struct {
258260
Division Division `json:"division"`
259261
PlacementPos *Location `json:"placementPos"`
260262
AutoContinue bool `json:"autoContinue"`
263+
NextCommand RefCommand `json:"nextCommand"`
264+
NextCommandFor Team `json:"nextCommandFor"`
261265
}
262266

263267
// NewState creates a new state, initialized for the start of a new game

pkg/refproto/ssl_referee.pb.go

Lines changed: 76 additions & 65 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)