Skip to content

Commit 100b0fc

Browse files
committed
[refactoring] Refactored several structures
* replace gameState with last command * remove goal command from internal structure (does not fit well here) * publish protobuf goal command based on score, followed by stop command * renamed gameTime to stage time * moved protobuf files to a non-conflicting page name
1 parent a3870be commit 100b0fc

20 files changed

+270
-267
lines changed

internal/app/controller/controller.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (r *GameController) Run() (err error) {
5555
for {
5656
r.timer.WaitTillNextFullSecond()
5757
r.Engine.Tick(r.timer.Delta())
58+
r.saveLatestState()
5859
r.publish(nil)
5960
}
6061
}()
@@ -66,10 +67,10 @@ func (r *GameController) OnNewEvent(event Event) {
6667
if err != nil {
6768
log.Println("Could not process event:", event, err)
6869
} else {
70+
r.saveLatestState()
71+
r.saveStateHistory()
6972
r.publish(cmd)
70-
if cmd != nil {
71-
r.publishGameEvents()
72-
}
73+
r.publishGameEvents()
7374
}
7475
}
7576

@@ -126,17 +127,13 @@ func (r *GameController) readLastState() {
126127

127128
// publish publishes the state to the UI and the teams
128129
func (r *GameController) publish(command *EventCommand) {
129-
r.saveLatestState()
130-
if command != nil {
131-
r.saveStateHistory()
132-
}
133130
r.ApiServer.PublishState(*r.Engine.State)
134-
r.Publisher.Publish(r.Engine.State, command)
131+
r.Publisher.Publish(r.Engine.State)
135132
}
136133

137134
// publishGameEvents publishes the current list of game events
138135
func (r *GameController) publishGameEvents() {
139-
r.ApiServer.PublishGameEvents(r.Engine.GameEvents)
136+
r.ApiServer.PublishGameEvents(r.Engine.RefereeEvent)
140137
}
141138

142139
// saveLatestState writes the current state into a file

internal/app/controller/engine.go

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Engine struct {
1414
config ConfigGame
1515
StateHistory []State
1616
TimeProvider func() time.Time
17-
GameEvents []RefereeEvent
17+
RefereeEvent []RefereeEvent
1818
}
1919

2020
func NewEngine(config ConfigGame) (e Engine) {
@@ -31,6 +31,7 @@ func (e *Engine) ResetGame() {
3131
e.State.TeamState[TeamYellow].TimeoutTimeLeft = e.config.Normal.TimeoutDuration
3232
e.State.TeamState[TeamBlue].TimeoutsLeft = e.config.Normal.Timeouts
3333
e.State.TeamState[TeamYellow].TimeoutsLeft = e.config.Normal.Timeouts
34+
e.RefereeEvent = []RefereeEvent{}
3435
}
3536

3637
// Tick updates the times of the state and removes cards, if necessary
@@ -69,7 +70,7 @@ func (e *Engine) LogGameEvent(eventType GameEventType) {
6970
Type: RefereeEventGameEvent,
7071
GameEventType: &eventType,
7172
}
72-
e.GameEvents = append(e.GameEvents, gameEvent)
73+
e.RefereeEvent = append(e.RefereeEvent, gameEvent)
7374
}
7475

7576
func (e *Engine) LogCommand(command *EventCommand) {
@@ -80,7 +81,7 @@ func (e *Engine) LogCommand(command *EventCommand) {
8081
Command: &command.Type,
8182
Team: command.ForTeam,
8283
}
83-
e.GameEvents = append(e.GameEvents, gameEvent)
84+
e.RefereeEvent = append(e.RefereeEvent, gameEvent)
8485
}
8586

8687
func (e *Engine) loadStages() {
@@ -99,7 +100,7 @@ func (e *Engine) loadStages() {
99100
}
100101

101102
func (e *Engine) updateTimes(delta time.Duration) {
102-
if e.State.GameState == GameStateRunning {
103+
if e.State.GameState() == GameStateRunning {
103104
e.State.StageTimeElapsed += delta
104105
e.State.StageTimeLeft -= delta
105106

@@ -109,8 +110,8 @@ func (e *Engine) updateTimes(delta time.Duration) {
109110
}
110111
}
111112

112-
if e.State.GameState == GameStateTimeout && e.State.GameStateFor != nil {
113-
e.State.TeamState[*e.State.GameStateFor].TimeoutTimeLeft -= delta
113+
if e.State.GameState() == GameStateTimeout && e.State.CommandFor != nil {
114+
e.State.TeamState[*e.State.CommandFor].TimeoutTimeLeft -= delta
114115
}
115116
}
116117

@@ -131,53 +132,25 @@ func (e *Engine) processEvent(event Event) (*EventCommand, error) {
131132

132133
func (e *Engine) processCommand(c *EventCommand) (*EventCommand, error) {
133134
switch c.Type {
134-
case CommandHalt:
135-
e.State.GameState = GameStateHalted
136-
e.State.GameStateFor = nil
137-
case CommandStop:
138-
e.State.GameState = GameStateStopped
139-
e.State.GameStateFor = nil
140-
case CommandNormalStart:
141-
e.State.GameState = GameStateRunning
142-
e.State.GameStateFor = nil
143-
e.updatePreStages()
144-
case CommandForceStart, CommandDirect, CommandIndirect:
145-
e.State.GameState = GameStateRunning
146-
e.State.GameStateFor = nil
147-
case CommandKickoff:
148-
if c.ForTeam == nil {
149-
return nil, errors.New("Team required for kickoff")
150-
}
151-
e.State.GameState = GameStatePreKickoff
152-
e.State.GameStateFor = c.ForTeam
153-
case CommandPenalty:
154-
if c.ForTeam == nil {
155-
return nil, errors.New("Team required for penalty")
156-
}
157-
e.State.GameState = GameStatePrePenalty
158-
e.State.GameStateFor = c.ForTeam
159-
case CommandBallPlacement:
160-
if c.ForTeam == nil {
161-
return nil, errors.New("Team required for ball placement")
162-
}
163-
e.State.GameState = GameStateBallPlacement
164-
e.State.GameStateFor = c.ForTeam
165-
case CommandGoal:
135+
case CommandDirect, CommandIndirect, CommandKickoff, CommandPenalty, CommandTimeout, CommandBallPlacement:
166136
if c.ForTeam == nil {
167-
return nil, errors.New("Team required for goal")
137+
return nil, errors.Errorf("Team required for %v", c.Type)
168138
}
169-
e.State.TeamState[*c.ForTeam].Goals++
170-
case CommandTimeout:
171-
if c.ForTeam == nil {
172-
return nil, errors.New("Team required for timeout")
173-
}
174-
e.State.TeamState[*c.ForTeam].TimeoutsLeft--
175-
e.State.GameState = GameStateTimeout
176-
e.State.GameStateFor = c.ForTeam
139+
case CommandHalt, CommandStop, CommandForceStart, CommandNormalStart:
177140
default:
178141
return nil, errors.Errorf("Unknown command: %v", c)
179142
}
180143

144+
if c.Type == CommandTimeout {
145+
e.State.TeamState[*c.ForTeam].TimeoutsLeft--
146+
e.State.CommandFor = c.ForTeam
147+
} else if c.Type == CommandNormalStart {
148+
e.updatePreStages()
149+
}
150+
151+
e.State.Command = c.Type
152+
e.State.CommandFor = c.ForTeam
153+
181154
log.Printf("Processed command %v", *c)
182155
return c, nil
183156
}
@@ -230,7 +203,7 @@ func (e *Engine) processModify(m *EventModifyValue) (*EventCommand, error) {
230203
}
231204

232205
func (e *Engine) processStage(s *EventStage) (*EventCommand, error) {
233-
if e.State.GameState != GameStateHalted && e.State.GameState != GameStateStopped {
206+
if e.State.GameState() != GameStateHalted && e.State.GameState() != GameStateStopped {
234207
return nil, errors.New("The game state must be halted or stopped to change the stage")
235208
}
236209

@@ -254,8 +227,8 @@ func (e *Engine) updateStage(stage Stage) (cmd *EventCommand) {
254227
e.State.StageTimeElapsed = 0
255228

256229
if !e.State.Stage.IsPreStage() {
257-
e.State.GameState = GameStateHalted
258-
e.State.GameStateFor = nil
230+
e.State.Command = CommandHalt
231+
e.State.CommandFor = nil
259232
cmd = &EventCommand{nil, CommandHalt}
260233
}
261234

internal/app/controller/events.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,6 @@ const (
2727
CardOperationModify CardOperation = "modify"
2828
)
2929

30-
// RefCommand is a command to be send to the teams
31-
type RefCommand string
32-
33-
const (
34-
// CommandHalt HALT
35-
CommandHalt RefCommand = "halt"
36-
// CommandStop STOP
37-
CommandStop RefCommand = "stop"
38-
// CommandNormalStart NORMAL_START
39-
CommandNormalStart RefCommand = "normalStart"
40-
// CommandForceStart FORCE_START
41-
CommandForceStart RefCommand = "forceStart"
42-
// CommandDirect DIRECT
43-
CommandDirect RefCommand = "direct"
44-
// CommandIndirect INDIRECT
45-
CommandIndirect RefCommand = "indirect"
46-
// CommandKickoff KICKOFF
47-
CommandKickoff RefCommand = "kickoff"
48-
// CommandPenalty PENALTY
49-
CommandPenalty RefCommand = "penalty"
50-
// CommandTimeout TIMEOUT
51-
CommandTimeout RefCommand = "timeout"
52-
// CommandBallPlacement BALL_PLACEMENT
53-
CommandBallPlacement RefCommand = "ballPlacement"
54-
// CommandGoal GOAL
55-
CommandGoal RefCommand = "goal"
56-
)
57-
5830
// StageOperation to apply on the current stage
5931
type StageOperation string
6032

0 commit comments

Comments
 (0)