@@ -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
119139func (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}
0 commit comments