Skip to content

Commit bfb182e

Browse files
committed
[refactoring] Rearrange code
1 parent df78d90 commit bfb182e

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

internal/app/controller/engine.go

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ func NewEngine(config config.Game) (e Engine) {
2828
return
2929
}
3030

31+
func (e *Engine) loadStages() {
32+
e.StageTimes = map[Stage]time.Duration{}
33+
for _, stage := range Stages {
34+
e.StageTimes[stage] = 0
35+
}
36+
e.StageTimes[StageFirstHalf] = e.config.Normal.HalfDuration
37+
e.StageTimes[StageHalfTime] = e.config.Normal.HalfTimeDuration
38+
e.StageTimes[StageSecondHalf] = e.config.Normal.HalfDuration
39+
e.StageTimes[StageOvertimeBreak] = e.config.Normal.BreakAfter
40+
e.StageTimes[StageOvertimeFirstHalf] = e.config.Overtime.HalfDuration
41+
e.StageTimes[StageOvertimeHalfTime] = e.config.Overtime.HalfTimeDuration
42+
e.StageTimes[StageOvertimeSecondHalf] = e.config.Overtime.HalfDuration
43+
e.StageTimes[StageShootoutBreak] = e.config.Overtime.BreakAfter
44+
}
45+
3146
func (e *Engine) ResetGame() {
3247
e.State = NewState()
3348
for _, team := range []Team{TeamBlue, TeamYellow} {
@@ -50,6 +65,36 @@ func (e *Engine) Tick(delta time.Duration) {
5065
}
5166
}
5267

68+
func (e *Engine) updateTimes(delta time.Duration) {
69+
if e.countStageTime() {
70+
e.State.StageTimeElapsed += delta
71+
e.State.StageTimeLeft -= delta
72+
73+
if e.State.StageTimeLeft+delta > 0 && e.State.StageTimeLeft <= 0 {
74+
e.LogTime("Stage time elapsed", "")
75+
}
76+
77+
for team, teamState := range e.State.TeamState {
78+
reduceYellowCardTimes(teamState, delta)
79+
e.removeElapsedYellowCards(team, teamState)
80+
e.updateMaxBots()
81+
}
82+
}
83+
84+
if e.State.GameState() == GameStateTimeout && e.State.CommandFor.Known() {
85+
e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft -= delta
86+
87+
timeLeft := e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft
88+
if timeLeft+delta > 0 && timeLeft <= 0 {
89+
e.LogTime("Timeout time elapsed", e.State.CommandFor)
90+
}
91+
}
92+
}
93+
94+
func (e *Engine) countStageTime() bool {
95+
return e.State.Stage.IsPausedStage() || e.State.GameState() == GameStateRunning
96+
}
97+
5398
func (e *Engine) SendCommand(command RefCommand, forTeam Team) {
5499
e.State.Command = command
55100
e.State.CommandFor = forTeam
@@ -95,25 +140,6 @@ func (e *Engine) Continue() {
95140
}
96141
}
97142

98-
func (e *Engine) updateNextCommand() {
99-
if e.State.Command == CommandPenalty || e.State.Command == CommandKickoff {
100-
e.State.NextCommand = CommandNormalStart
101-
e.State.NextCommandFor = ""
102-
return
103-
}
104-
primaryEvent := e.State.PrimaryGameEvent()
105-
if primaryEvent == nil {
106-
return
107-
}
108-
command, forTeam, err := e.CommandForEvent(primaryEvent)
109-
if err != nil {
110-
log.Print("Warn: ", err)
111-
return
112-
}
113-
e.State.NextCommand = command
114-
e.State.NextCommandFor = forTeam
115-
}
116-
117143
func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam Team, err error) {
118144
if event.IsSecondary() {
119145
return
@@ -217,49 +243,23 @@ func (e *Engine) updateMaxBots() {
217243
}
218244
}
219245

220-
func (e *Engine) loadStages() {
221-
e.StageTimes = map[Stage]time.Duration{}
222-
for _, stage := range Stages {
223-
e.StageTimes[stage] = 0
246+
func (e *Engine) updateNextCommand() {
247+
if e.State.Command == CommandPenalty || e.State.Command == CommandKickoff {
248+
e.State.NextCommand = CommandNormalStart
249+
e.State.NextCommandFor = ""
250+
return
224251
}
225-
e.StageTimes[StageFirstHalf] = e.config.Normal.HalfDuration
226-
e.StageTimes[StageHalfTime] = e.config.Normal.HalfTimeDuration
227-
e.StageTimes[StageSecondHalf] = e.config.Normal.HalfDuration
228-
e.StageTimes[StageOvertimeBreak] = e.config.Normal.BreakAfter
229-
e.StageTimes[StageOvertimeFirstHalf] = e.config.Overtime.HalfDuration
230-
e.StageTimes[StageOvertimeHalfTime] = e.config.Overtime.HalfTimeDuration
231-
e.StageTimes[StageOvertimeSecondHalf] = e.config.Overtime.HalfDuration
232-
e.StageTimes[StageShootoutBreak] = e.config.Overtime.BreakAfter
233-
}
234-
235-
func (e *Engine) countStageTime() bool {
236-
return e.State.Stage.IsPausedStage() || e.State.GameState() == GameStateRunning
237-
}
238-
239-
func (e *Engine) updateTimes(delta time.Duration) {
240-
if e.countStageTime() {
241-
e.State.StageTimeElapsed += delta
242-
e.State.StageTimeLeft -= delta
243-
244-
if e.State.StageTimeLeft+delta > 0 && e.State.StageTimeLeft <= 0 {
245-
e.LogTime("Stage time elapsed", "")
246-
}
247-
248-
for team, teamState := range e.State.TeamState {
249-
reduceYellowCardTimes(teamState, delta)
250-
e.removeElapsedYellowCards(team, teamState)
251-
e.updateMaxBots()
252-
}
252+
primaryEvent := e.State.PrimaryGameEvent()
253+
if primaryEvent == nil {
254+
return
253255
}
254-
255-
if e.State.GameState() == GameStateTimeout && e.State.CommandFor.Known() {
256-
e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft -= delta
257-
258-
timeLeft := e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft
259-
if timeLeft+delta > 0 && timeLeft <= 0 {
260-
e.LogTime("Timeout time elapsed", e.State.CommandFor)
261-
}
256+
command, forTeam, err := e.CommandForEvent(primaryEvent)
257+
if err != nil {
258+
log.Print("Warn: ", err)
259+
return
262260
}
261+
e.State.NextCommand = command
262+
e.State.NextCommandFor = forTeam
263263
}
264264

265265
func (e *Engine) processEvent(event Event) error {

0 commit comments

Comments
 (0)