@@ -78,42 +78,62 @@ func (e *Engine) ResetGame() {
78
78
}
79
79
}
80
80
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
94
84
if e .countStageTime () {
95
- e .State .StageTimeElapsed += delta
96
- e .State .StageTimeLeft -= delta
97
-
98
85
if e .State .StageTimeLeft + delta > 0 && e .State .StageTimeLeft <= 0 {
99
86
e .LogTime ("Stage time elapsed" , "" )
87
+ eventTriggered = true
100
88
}
101
89
102
90
for team , teamState := range e .State .TeamState {
103
- reduceYellowCardTimes (teamState , delta )
104
- e .removeElapsedYellowCards (team , teamState )
91
+ eventTriggered = e .removeElapsedYellowCards (team , teamState ) || eventTriggered
105
92
e .updateMaxBots ()
106
93
}
107
94
}
108
95
109
96
if e .State .GameState () == GameStateTimeout && e .State .CommandFor .Known () {
110
- e .State .TeamState [e .State .CommandFor ].TimeoutTimeLeft -= delta
111
-
112
97
timeLeft := e .State .TeamState [e .State .CommandFor ].TimeoutTimeLeft
113
98
if timeLeft + delta > 0 && timeLeft <= 0 {
114
99
e .LogTime ("Timeout time elapsed" , e .State .CommandFor )
100
+ eventTriggered = true
115
101
}
116
102
}
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
117
137
}
118
138
119
139
func (e * Engine ) countStageTime () bool {
@@ -867,20 +887,30 @@ func strToDuration(s string) (duration time.Duration, err error) {
867
887
return
868
888
}
869
889
870
- func reduceYellowCardTimes (teamState * TeamInfo , delta time.Duration ) {
890
+ func reduceYellowCardTimes (teamState * TeamInfo , delta time.Duration ) (newFullSecond bool ) {
891
+ newFullSecond = false
871
892
for i := range teamState .YellowCardTimes {
893
+ newFullSecond = newFullSecond || isNewFullSecond (teamState .YellowCardTimes [i ], delta )
872
894
teamState .YellowCardTimes [i ] -= delta
873
895
}
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 )
874
901
}
875
902
876
- func (e * Engine ) removeElapsedYellowCards (team Team , teamState * TeamInfo ) {
903
+ func (e * Engine ) removeElapsedYellowCards (team Team , teamState * TeamInfo ) (removed bool ) {
904
+ removed = false
877
905
b := teamState .YellowCardTimes [:0 ]
878
906
for _ , x := range teamState .YellowCardTimes {
879
907
if x > 0 {
880
908
b = append (b , x )
881
909
} else {
882
910
e .LogTime ("Yellow card time elapsed" , team )
911
+ removed = true
883
912
}
884
913
}
885
914
teamState .YellowCardTimes = b
915
+ return
886
916
}
0 commit comments