Skip to content

Commit ade6a32

Browse files
committed
[bugfix] Update times within state faster than one second
All times in the state were only updated on full seconds. This is, because the UI should not be updated too frequently. But still, some times like the currentActionTime should be updated more frequently in the referee message that is send in a higher rate.
1 parent 8346a8d commit ade6a32

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

internal/app/controller/controller.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,13 @@ func (c *GameController) setupTimeProvider() {
110110
func (c *GameController) mainLoop() {
111111
defer c.historyPreserver.Close()
112112
for {
113-
c.timer.WaitTillNextFullSecond()
114-
c.Engine.Tick(c.timer.Delta())
113+
c.timer.WaitTillNextFull(time.Millisecond * 10)
115114

116-
c.publish()
115+
newFullSecond := c.Engine.UpdateTimes(c.timer.Delta())
116+
eventTriggered := c.Engine.TriggerTimedEvents(c.timer.Delta())
117+
if eventTriggered || newFullSecond {
118+
c.publish()
119+
}
117120
}
118121
}
119122

internal/app/controller/engine.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,62 @@ func (e *Engine) ResetGame() {
7878
}
7979
}
8080

81-
// Tick updates the times of the state and removes cards, if necessary
82-
func (e *Engine) Tick(delta time.Duration) {
83-
e.updateTimes(delta)
84-
85-
if e.State.MatchTimeStart.After(time.Unix(0, 0)) {
86-
e.State.MatchDuration = e.TimeProvider().Sub(e.State.MatchTimeStart)
87-
}
88-
if e.State.CurrentActionDeadline.After(time.Unix(0, 0)) {
89-
e.State.CurrentActionTimeRemaining = e.State.CurrentActionDeadline.Sub(e.TimeProvider())
90-
}
91-
}
92-
93-
func (e *Engine) updateTimes(delta time.Duration) {
81+
// TriggerTimedEvents checks for elapsed stage time, timeouts and cards
82+
func (e *Engine) TriggerTimedEvents(delta time.Duration) (eventTriggered bool) {
83+
eventTriggered = false
9484
if e.countStageTime() {
95-
e.State.StageTimeElapsed += delta
96-
e.State.StageTimeLeft -= delta
97-
9885
if e.State.StageTimeLeft+delta > 0 && e.State.StageTimeLeft <= 0 {
9986
e.LogTime("Stage time elapsed", "")
87+
eventTriggered = true
10088
}
10189

10290
for team, teamState := range e.State.TeamState {
103-
reduceYellowCardTimes(teamState, delta)
104-
e.removeElapsedYellowCards(team, teamState)
91+
eventTriggered = e.removeElapsedYellowCards(team, teamState) || eventTriggered
10592
e.updateMaxBots()
10693
}
10794
}
10895

10996
if e.State.GameState() == GameStateTimeout && e.State.CommandFor.Known() {
110-
e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft -= delta
111-
11297
timeLeft := e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft
11398
if timeLeft+delta > 0 && timeLeft <= 0 {
11499
e.LogTime("Timeout time elapsed", e.State.CommandFor)
100+
eventTriggered = true
115101
}
116102
}
103+
return
104+
}
105+
106+
// UpdateTimes updates the times of the state
107+
func (e *Engine) UpdateTimes(delta time.Duration) (newFullSecond bool) {
108+
if e.countStageTime() {
109+
newFullSecond = newFullSecond || isNewFullSecond(e.State.StageTimeElapsed, delta)
110+
111+
e.State.StageTimeElapsed += delta
112+
e.State.StageTimeLeft -= delta
113+
114+
for _, teamState := range e.State.TeamState {
115+
newFullSecond = reduceYellowCardTimes(teamState, delta) || newFullSecond
116+
}
117+
}
118+
119+
if e.State.GameState() == GameStateTimeout && e.State.CommandFor.Known() {
120+
newFullSecond = newFullSecond || isNewFullSecond(e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft, delta)
121+
e.State.TeamState[e.State.CommandFor].TimeoutTimeLeft -= delta
122+
}
123+
124+
curTime := e.TimeProvider()
125+
if e.State.MatchTimeStart.After(time.Unix(0, 0)) {
126+
newMatchDuration := curTime.Sub(e.State.MatchTimeStart)
127+
newFullSecond = newFullSecond || isNewFullSecond(e.State.MatchDuration, newMatchDuration-e.State.MatchDuration)
128+
e.State.MatchDuration = newMatchDuration
129+
}
130+
131+
if e.State.CurrentActionDeadline.After(time.Unix(0, 0)) {
132+
newCurrentActionTimeRemaining := e.State.CurrentActionDeadline.Sub(curTime)
133+
newFullSecond = newFullSecond || isNewFullSecond(e.State.CurrentActionTimeRemaining, newCurrentActionTimeRemaining-e.State.CurrentActionTimeRemaining)
134+
e.State.CurrentActionTimeRemaining = newCurrentActionTimeRemaining
135+
}
136+
return
117137
}
118138

119139
func (e *Engine) countStageTime() bool {
@@ -867,20 +887,30 @@ func strToDuration(s string) (duration time.Duration, err error) {
867887
return
868888
}
869889

870-
func reduceYellowCardTimes(teamState *TeamInfo, delta time.Duration) {
890+
func reduceYellowCardTimes(teamState *TeamInfo, delta time.Duration) (newFullSecond bool) {
891+
newFullSecond = false
871892
for i := range teamState.YellowCardTimes {
893+
newFullSecond = newFullSecond || isNewFullSecond(teamState.YellowCardTimes[i], delta)
872894
teamState.YellowCardTimes[i] -= delta
873895
}
896+
return
897+
}
898+
899+
func isNewFullSecond(t time.Duration, delta time.Duration) bool {
900+
return time.Duration(t+delta).Truncate(time.Second) > t.Truncate(time.Second)
874901
}
875902

876-
func (e *Engine) removeElapsedYellowCards(team Team, teamState *TeamInfo) {
903+
func (e *Engine) removeElapsedYellowCards(team Team, teamState *TeamInfo) (removed bool) {
904+
removed = false
877905
b := teamState.YellowCardTimes[:0]
878906
for _, x := range teamState.YellowCardTimes {
879907
if x > 0 {
880908
b = append(b, x)
881909
} else {
882910
e.LogTime("Yellow card time elapsed", team)
911+
removed = true
883912
}
884913
}
885914
teamState.YellowCardTimes = b
915+
return
886916
}

internal/app/controller/engine_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ func processTransitionFile(t *testing.T, fileName string) {
127127

128128
if s.Tick != nil {
129129
elapsedTime += *s.Tick
130-
e.Tick(*s.Tick)
130+
e.UpdateTimes(*s.Tick)
131+
e.TriggerTimedEvents(*s.Tick)
131132
}
132133

133134
if s.ExpectedStateDiff != nil {

0 commit comments

Comments
 (0)