Skip to content

Commit 3a5e977

Browse files
committed
[refactoring] Rename refereeEvents to uiProtocol
1 parent 941efe0 commit 3a5e977

File tree

6 files changed

+149
-151
lines changed

6 files changed

+149
-151
lines changed

internal/app/controller/controller.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ const configFileName = "config/ssl-game-controller.yaml"
1818

1919
// GameController controls a game
2020
type GameController struct {
21-
Config config.Controller
22-
Publisher Publisher
23-
ApiServer ApiServer
24-
AutoRefServer *rcon.AutoRefServer
25-
TeamServer *rcon.TeamServer
26-
Engine Engine
27-
timer timer.Timer
28-
historyPreserver HistoryPreserver
29-
numRefereeEventsLastPublish int
30-
outstandingTeamChoice *TeamChoice
31-
Mutex sync.Mutex
32-
VisionReceiver *vision.Receiver
21+
Config config.Controller
22+
Publisher Publisher
23+
ApiServer ApiServer
24+
AutoRefServer *rcon.AutoRefServer
25+
TeamServer *rcon.TeamServer
26+
Engine Engine
27+
timer timer.Timer
28+
historyPreserver HistoryPreserver
29+
numUiProtocolsLastPublish int
30+
outstandingTeamChoice *TeamChoice
31+
Mutex sync.Mutex
32+
VisionReceiver *vision.Receiver
3333
}
3434

3535
type TeamChoice struct {
@@ -76,12 +76,12 @@ func (c *GameController) Run() {
7676
} else if len(*history) > 0 {
7777
c.Engine.History = *history
7878
*c.Engine.State = c.Engine.History[len(c.Engine.History)-1].State
79-
c.Engine.RefereeEvents = c.Engine.History[len(c.Engine.History)-1].RefereeEvents
79+
c.Engine.UiProtocol = c.Engine.History[len(c.Engine.History)-1].UiProtocol
8080
}
8181
}
8282

8383
c.ApiServer.PublishState(*c.Engine.State)
84-
c.ApiServer.PublishRefereeEvents(c.Engine.RefereeEvents)
84+
c.ApiServer.PublishUiProtocol(c.Engine.UiProtocol)
8585
c.TeamServer.AllowedTeamNames = []string{c.Engine.State.TeamState[TeamYellow].Name,
8686
c.Engine.State.TeamState[TeamBlue].Name}
8787

@@ -330,9 +330,9 @@ func loadConfig() config.Controller {
330330

331331
// publish publishes the state to the UI and the teams
332332
func (c *GameController) publish() {
333-
if len(c.Engine.RefereeEvents) != c.numRefereeEventsLastPublish {
334-
c.ApiServer.PublishRefereeEvents(c.Engine.RefereeEvents)
335-
c.numRefereeEventsLastPublish = len(c.Engine.RefereeEvents)
333+
if len(c.Engine.UiProtocol) != c.numUiProtocolsLastPublish {
334+
c.ApiServer.PublishUiProtocol(c.Engine.UiProtocol)
335+
c.numUiProtocolsLastPublish = len(c.Engine.UiProtocol)
336336
}
337337

338338
c.TeamServer.AllowedTeamNames = []string{c.Engine.State.TeamState[TeamYellow].Name,

internal/app/controller/engine.go

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package controller
22

33
import (
4-
"fmt"
54
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
65
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
76
"github.com/pkg/errors"
@@ -14,13 +13,13 @@ import (
1413
const maxHistorySize = 10
1514

1615
type Engine struct {
17-
State *State
18-
RefereeEvents []RefereeEvent
19-
StageTimes map[Stage]time.Duration
20-
config config.Game
21-
TimeProvider func() time.Time
22-
History History
23-
Geometry config.Geometry
16+
State *State
17+
UiProtocol []UiProtocolEntry
18+
StageTimes map[Stage]time.Duration
19+
config config.Game
20+
TimeProvider func() time.Time
21+
History History
22+
Geometry config.Geometry
2423
}
2524

2625
func NewEngine(config config.Game) (e Engine) {
@@ -39,7 +38,7 @@ func (e *Engine) ResetGame() {
3938
e.State.TeamState[team].MaxAllowedBots = e.config.MaxBots[e.State.Division]
4039
}
4140

42-
e.RefereeEvents = []RefereeEvent{}
41+
e.UiProtocol = []UiProtocolEntry{}
4342
e.State.Division = e.config.DefaultDivision
4443
e.Geometry = *e.config.DefaultGeometry[e.State.Division]
4544
}
@@ -87,16 +86,16 @@ func (e *Engine) UndoLastAction() {
8786
lastIndex := len(e.History) - 2
8887
if lastIndex >= 0 {
8988
*e.State = e.History[lastIndex].State.DeepCopy()
90-
e.RefereeEvents = append(e.History[lastIndex].RefereeEvents[:0:0],
91-
e.History[lastIndex].RefereeEvents...)
89+
e.UiProtocol = append(e.History[lastIndex].UiProtocol[:0:0],
90+
e.History[lastIndex].UiProtocol...)
9291
e.History = e.History[0:lastIndex]
9392
}
9493
}
9594

9695
func (e *Engine) appendHistory() {
9796
var entry HistoryEntry
9897
entry.State = e.State.DeepCopy()
99-
entry.RefereeEvents = append(e.RefereeEvents[:0:0], e.RefereeEvents...)
98+
entry.UiProtocol = append(e.UiProtocol[:0:0], e.UiProtocol...)
10099
e.History = append(e.History, entry)
101100
if len(e.History) > maxHistorySize {
102101
e.History = e.History[1:]
@@ -208,9 +207,9 @@ func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam
208207
}
209208

210209
func (e *Engine) LastGameStartCommand() (RefCommand, error) {
211-
for i := len(e.RefereeEvents) - 1; i >= 0; i-- {
212-
event := e.RefereeEvents[i]
213-
if event.Type == RefereeEventCommand {
210+
for i := len(e.UiProtocol) - 1; i >= 0; i-- {
211+
event := e.UiProtocol[i]
212+
if event.Type == UiProtocolCommand {
214213
cmd := RefCommand(event.Name)
215214
switch cmd {
216215
case CommandPenalty, CommandKickoff, CommandIndirect, CommandDirect:
@@ -241,87 +240,6 @@ func (e *Engine) updateMaxBots() {
241240
}
242241
}
243242

244-
func (e *Engine) LogGameEvent(event GameEvent) {
245-
gameEvent := RefereeEvent{
246-
Timestamp: e.TimeProvider().UnixNano(),
247-
StageTime: e.State.StageTimeElapsed,
248-
Type: RefereeEventGameEvent,
249-
Name: string(event.Type),
250-
Team: event.ByTeam(),
251-
Description: event.Details.String(),
252-
}
253-
e.RefereeEvents = append(e.RefereeEvents, gameEvent)
254-
}
255-
256-
func (e *Engine) LogIgnoredGameEvent(event GameEvent) {
257-
gameEvent := RefereeEvent{
258-
Timestamp: e.TimeProvider().UnixNano(),
259-
StageTime: e.State.StageTimeElapsed,
260-
Type: RefereeEventGameEventIgnored,
261-
Name: string(event.Type),
262-
Team: event.ByTeam(),
263-
Description: event.Details.String(),
264-
}
265-
e.RefereeEvents = append(e.RefereeEvents, gameEvent)
266-
}
267-
268-
func (e *Engine) LogCommand() {
269-
refereeEvent := RefereeEvent{
270-
Timestamp: e.TimeProvider().UnixNano(),
271-
StageTime: e.State.StageTimeElapsed,
272-
Type: RefereeEventCommand,
273-
Name: string(e.State.Command),
274-
Team: e.State.CommandFor,
275-
}
276-
e.RefereeEvents = append(e.RefereeEvents, refereeEvent)
277-
}
278-
279-
func (e *Engine) LogCard(card *EventCard) {
280-
refereeEvent := RefereeEvent{
281-
Timestamp: e.TimeProvider().UnixNano(),
282-
StageTime: e.State.StageTimeElapsed,
283-
Type: RefereeEventCard,
284-
Name: fmt.Sprintf("%v %v card", card.Operation, card.Type),
285-
Team: card.ForTeam,
286-
}
287-
e.RefereeEvents = append(e.RefereeEvents, refereeEvent)
288-
}
289-
290-
func (e *Engine) LogTime(description string, forTeam Team) {
291-
refereeEvent := RefereeEvent{
292-
Timestamp: e.TimeProvider().UnixNano(),
293-
StageTime: e.State.StageTimeElapsed,
294-
Type: RefereeEventTime,
295-
Name: description,
296-
Team: forTeam,
297-
}
298-
e.RefereeEvents = append(e.RefereeEvents, refereeEvent)
299-
}
300-
301-
func (e *Engine) LogStage(stage Stage) {
302-
refereeEvent := RefereeEvent{
303-
Timestamp: e.TimeProvider().UnixNano(),
304-
StageTime: e.State.StageTimeElapsed,
305-
Type: RefereeEventStage,
306-
Name: string(stage),
307-
}
308-
e.RefereeEvents = append(e.RefereeEvents, refereeEvent)
309-
}
310-
311-
func (e *Engine) LogModify(m EventModifyValue) {
312-
team := m.ForTeam
313-
m.ForTeam = TeamUnknown
314-
refereeEvent := RefereeEvent{
315-
Timestamp: e.TimeProvider().UnixNano(),
316-
StageTime: e.State.StageTimeElapsed,
317-
Type: RefereeEventModify,
318-
Name: m.Type(),
319-
Team: team,
320-
Description: m.Value(),
321-
}
322-
e.RefereeEvents = append(e.RefereeEvents, refereeEvent)
323-
}
324-
325243
func (e *Engine) loadStages() {
326244
e.StageTimes = map[Stage]time.Duration{}
327245
for _, stage := range Stages {

internal/app/controller/history.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type HistoryPreserver struct {
1515
}
1616

1717
type HistoryEntry struct {
18-
State State
19-
RefereeEvents []RefereeEvent
18+
State State
19+
UiProtocol []UiProtocolEntry
2020
}
2121

2222
type History []HistoryEntry

internal/app/controller/refereeEvents.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

internal/app/controller/uiProtocol.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package controller
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
type UiProtocolType string
9+
10+
const (
11+
UiProtocolCommand UiProtocolType = "command"
12+
UiProtocolStage UiProtocolType = "stage"
13+
UiProtocolCard UiProtocolType = "card"
14+
UiProtocolTime UiProtocolType = "time"
15+
UiProtocolGameEvent UiProtocolType = "gameEvent"
16+
UiProtocolGameEventIgnored UiProtocolType = "ignoredGameEvent"
17+
UiProtocolModify UiProtocolType = "modify"
18+
)
19+
20+
type UiProtocolEntry struct {
21+
Timestamp int64 `json:"timestamp"`
22+
StageTime time.Duration `json:"stageTime"`
23+
Type UiProtocolType `json:"type"`
24+
Name string `json:"name"`
25+
Team Team `json:"team"`
26+
Description string `json:"description"`
27+
}
28+
29+
func (e *Engine) LogGameEvent(event GameEvent) {
30+
entry := UiProtocolEntry{
31+
Timestamp: e.TimeProvider().UnixNano(),
32+
StageTime: e.State.StageTimeElapsed,
33+
Type: UiProtocolGameEvent,
34+
Name: string(event.Type),
35+
Team: event.ByTeam(),
36+
Description: event.Details.String(),
37+
}
38+
e.UiProtocol = append(e.UiProtocol, entry)
39+
}
40+
41+
func (e *Engine) LogIgnoredGameEvent(event GameEvent) {
42+
entry := UiProtocolEntry{
43+
Timestamp: e.TimeProvider().UnixNano(),
44+
StageTime: e.State.StageTimeElapsed,
45+
Type: UiProtocolGameEventIgnored,
46+
Name: string(event.Type),
47+
Team: event.ByTeam(),
48+
Description: event.Details.String(),
49+
}
50+
e.UiProtocol = append(e.UiProtocol, entry)
51+
}
52+
53+
func (e *Engine) LogCommand() {
54+
entry := UiProtocolEntry{
55+
Timestamp: e.TimeProvider().UnixNano(),
56+
StageTime: e.State.StageTimeElapsed,
57+
Type: UiProtocolCommand,
58+
Name: string(e.State.Command),
59+
Team: e.State.CommandFor,
60+
}
61+
e.UiProtocol = append(e.UiProtocol, entry)
62+
}
63+
64+
func (e *Engine) LogCard(card *EventCard) {
65+
entry := UiProtocolEntry{
66+
Timestamp: e.TimeProvider().UnixNano(),
67+
StageTime: e.State.StageTimeElapsed,
68+
Type: UiProtocolCard,
69+
Name: fmt.Sprintf("%v %v card", card.Operation, card.Type),
70+
Team: card.ForTeam,
71+
}
72+
e.UiProtocol = append(e.UiProtocol, entry)
73+
}
74+
75+
func (e *Engine) LogTime(description string, forTeam Team) {
76+
entry := UiProtocolEntry{
77+
Timestamp: e.TimeProvider().UnixNano(),
78+
StageTime: e.State.StageTimeElapsed,
79+
Type: UiProtocolTime,
80+
Name: description,
81+
Team: forTeam,
82+
}
83+
e.UiProtocol = append(e.UiProtocol, entry)
84+
}
85+
86+
func (e *Engine) LogStage(stage Stage) {
87+
entry := UiProtocolEntry{
88+
Timestamp: e.TimeProvider().UnixNano(),
89+
StageTime: e.State.StageTimeElapsed,
90+
Type: UiProtocolStage,
91+
Name: string(stage),
92+
}
93+
e.UiProtocol = append(e.UiProtocol, entry)
94+
}
95+
96+
func (e *Engine) LogModify(m EventModifyValue) {
97+
entry := UiProtocolEntry{
98+
Timestamp: e.TimeProvider().UnixNano(),
99+
StageTime: e.State.StageTimeElapsed,
100+
Type: UiProtocolModify,
101+
Name: m.Type(),
102+
Team: m.ForTeam,
103+
Description: m.Value(),
104+
}
105+
e.UiProtocol = append(e.UiProtocol, entry)
106+
}

0 commit comments

Comments
 (0)