@@ -3,6 +3,7 @@ package controller
33import (
44 "github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
55 "github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
6+ "github.com/RoboCup-SSL/ssl-game-controller/pkg/timer"
67 "github.com/pkg/errors"
78 "log"
89 "math/rand"
@@ -29,15 +30,16 @@ func NewEngineState() (e EngineState) {
2930}
3031
3132type Engine struct {
32- State * State
33- UiProtocol []UiProtocolEntry
34- EngineState EngineState
35- StageTimes map [Stage ]time.Duration
36- config config.Game
37- TimeProvider func () time.Time
38- History History
39- Geometry config.Geometry
40- Rand * rand.Rand
33+ State * State
34+ UiProtocol []UiProtocolEntry
35+ EngineState EngineState
36+ StageTimes map [Stage ]time.Duration
37+ config config.Game
38+ TimeProvider timer.TimeProvider
39+ LastTimeUpdate time.Time
40+ History History
41+ Geometry config.Geometry
42+ Rand * rand.Rand
4143}
4244
4345func NewEngine (config config.Game , seed int64 ) (e Engine ) {
@@ -46,6 +48,7 @@ func NewEngine(config config.Game, seed int64) (e Engine) {
4648 e .loadStages ()
4749 e .ResetGame ()
4850 e .TimeProvider = func () time.Time { return time .Now () }
51+ e .LastTimeUpdate = e .TimeProvider ()
4952 e .Rand = rand .New (rand .NewSource (seed ))
5053 return
5154}
@@ -78,8 +81,20 @@ func (e *Engine) ResetGame() {
7881 }
7982}
8083
81- // TriggerTimedEvents checks for elapsed stage time, timeouts and cards
82- func (e * Engine ) TriggerTimedEvents (delta time.Duration ) (eventTriggered bool ) {
84+ // Update updates times and triggers events if needed
85+ func (e * Engine ) Update () (newFullSecond bool , eventTriggered bool ) {
86+ currentTime := e .TimeProvider ()
87+ delta := currentTime .Sub (e .LastTimeUpdate )
88+ e .LastTimeUpdate = currentTime
89+
90+ newFullSecond = e .updateTimes (currentTime , delta )
91+ eventTriggered = e .triggerTimedEvents (delta )
92+
93+ return
94+ }
95+
96+ // triggerTimedEvents checks for elapsed stage time, timeouts and cards
97+ func (e * Engine ) triggerTimedEvents (delta time.Duration ) (eventTriggered bool ) {
8398 eventTriggered = false
8499 if e .countStageTime () {
85100 if e .State .StageTimeLeft + delta > 0 && e .State .StageTimeLeft <= 0 {
@@ -103,8 +118,9 @@ func (e *Engine) TriggerTimedEvents(delta time.Duration) (eventTriggered bool) {
103118 return
104119}
105120
106- // UpdateTimes updates the times of the state
107- func (e * Engine ) UpdateTimes (delta time.Duration ) (newFullSecond bool ) {
121+ // updateTimes updates the times of the state
122+ func (e * Engine ) updateTimes (currentTime time.Time , delta time.Duration ) (newFullSecond bool ) {
123+
108124 if e .countStageTime () {
109125 newFullSecond = newFullSecond || isNewFullSecond (e .State .StageTimeElapsed , delta )
110126
@@ -121,18 +137,18 @@ func (e *Engine) UpdateTimes(delta time.Duration) (newFullSecond bool) {
121137 e .State .TeamState [e .State .CommandFor ].TimeoutTimeLeft -= delta
122138 }
123139
124- curTime := e .TimeProvider ()
125140 if e .State .MatchTimeStart .After (time .Unix (0 , 0 )) {
126- newMatchDuration := curTime .Sub (e .State .MatchTimeStart )
141+ newMatchDuration := currentTime .Sub (e .State .MatchTimeStart )
127142 newFullSecond = newFullSecond || isNewFullSecond (e .State .MatchDuration , newMatchDuration - e .State .MatchDuration )
128143 e .State .MatchDuration = newMatchDuration
129144 }
130145
131146 if e .State .CurrentActionDeadline .After (time .Unix (0 , 0 )) {
132- newCurrentActionTimeRemaining := e .State .CurrentActionDeadline .Sub (curTime )
147+ newCurrentActionTimeRemaining := e .State .CurrentActionDeadline .Sub (currentTime )
133148 newFullSecond = newFullSecond || isNewFullSecond (e .State .CurrentActionTimeRemaining , newCurrentActionTimeRemaining - e .State .CurrentActionTimeRemaining )
134149 e .State .CurrentActionTimeRemaining = newCurrentActionTimeRemaining
135150 }
151+
136152 return
137153}
138154
0 commit comments