@@ -3,6 +3,7 @@ package controller
3
3
import (
4
4
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
5
5
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
6
+ "github.com/RoboCup-SSL/ssl-game-controller/pkg/timer"
6
7
"github.com/pkg/errors"
7
8
"log"
8
9
"math/rand"
@@ -29,15 +30,16 @@ func NewEngineState() (e EngineState) {
29
30
}
30
31
31
32
type 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
41
43
}
42
44
43
45
func NewEngine (config config.Game , seed int64 ) (e Engine ) {
@@ -46,6 +48,7 @@ func NewEngine(config config.Game, seed int64) (e Engine) {
46
48
e .loadStages ()
47
49
e .ResetGame ()
48
50
e .TimeProvider = func () time.Time { return time .Now () }
51
+ e .LastTimeUpdate = e .TimeProvider ()
49
52
e .Rand = rand .New (rand .NewSource (seed ))
50
53
return
51
54
}
@@ -78,8 +81,20 @@ func (e *Engine) ResetGame() {
78
81
}
79
82
}
80
83
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 ) {
83
98
eventTriggered = false
84
99
if e .countStageTime () {
85
100
if e .State .StageTimeLeft + delta > 0 && e .State .StageTimeLeft <= 0 {
@@ -103,8 +118,9 @@ func (e *Engine) TriggerTimedEvents(delta time.Duration) (eventTriggered bool) {
103
118
return
104
119
}
105
120
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
+
108
124
if e .countStageTime () {
109
125
newFullSecond = newFullSecond || isNewFullSecond (e .State .StageTimeElapsed , delta )
110
126
@@ -121,18 +137,18 @@ func (e *Engine) UpdateTimes(delta time.Duration) (newFullSecond bool) {
121
137
e .State .TeamState [e .State .CommandFor ].TimeoutTimeLeft -= delta
122
138
}
123
139
124
- curTime := e .TimeProvider ()
125
140
if e .State .MatchTimeStart .After (time .Unix (0 , 0 )) {
126
- newMatchDuration := curTime .Sub (e .State .MatchTimeStart )
141
+ newMatchDuration := currentTime .Sub (e .State .MatchTimeStart )
127
142
newFullSecond = newFullSecond || isNewFullSecond (e .State .MatchDuration , newMatchDuration - e .State .MatchDuration )
128
143
e .State .MatchDuration = newMatchDuration
129
144
}
130
145
131
146
if e .State .CurrentActionDeadline .After (time .Unix (0 , 0 )) {
132
- newCurrentActionTimeRemaining := e .State .CurrentActionDeadline .Sub (curTime )
147
+ newCurrentActionTimeRemaining := e .State .CurrentActionDeadline .Sub (currentTime )
133
148
newFullSecond = newFullSecond || isNewFullSecond (e .State .CurrentActionTimeRemaining , newCurrentActionTimeRemaining - e .State .CurrentActionTimeRemaining )
134
149
e .State .CurrentActionTimeRemaining = newCurrentActionTimeRemaining
135
150
}
151
+
136
152
return
137
153
}
138
154
0 commit comments