@@ -68,15 +68,16 @@ func (s GameControllerState) DeepCopy() (c GameControllerState) {
68
68
}
69
69
70
70
type Engine struct {
71
- State * State
72
- GcState * GameControllerState
73
- StageTimes map [Stage ]time.Duration
74
- config config.Game
75
- TimeProvider timer.TimeProvider
76
- LastTimeUpdate time.Time
77
- PersistentState * PersistentState
78
- Geometry config.Geometry
79
- Rand * rand.Rand
71
+ State * State
72
+ GcState * GameControllerState
73
+ StageTimes map [Stage ]time.Duration
74
+ config config.Game
75
+ TimeProvider timer.TimeProvider
76
+ LastTimeUpdate time.Time
77
+ PersistentState * PersistentState
78
+ Geometry config.Geometry
79
+ Rand * rand.Rand
80
+ recentGameEvents []* GameEvent
80
81
}
81
82
82
83
func NewEngine (config config.Game , seed int64 ) (e Engine ) {
@@ -90,6 +91,7 @@ func NewEngine(config config.Game, seed int64) (e Engine) {
90
91
e .TimeProvider = func () time.Time { return time .Now () }
91
92
e .LastTimeUpdate = e .TimeProvider ()
92
93
e .Rand = rand .New (rand .NewSource (seed ))
94
+ e .recentGameEvents = []* GameEvent {}
93
95
return
94
96
}
95
97
@@ -274,6 +276,7 @@ func (e *Engine) setCurrentActionTimeout(timeout time.Duration) {
274
276
func (e * Engine ) AddGameEvent (gameEvent * GameEvent ) {
275
277
e .LogGameEvent (gameEvent , e .State .DeepCopy ())
276
278
e .State .GameEvents = append (e .State .GameEvents , gameEvent )
279
+ e .recentGameEvents = append (e .recentGameEvents , gameEvent )
277
280
}
278
281
279
282
func (e * Engine ) QueueGameEvent (gameEvent * GameEvent ) {
@@ -827,9 +830,17 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
827
830
828
831
event .SetOccurred (e .TimeProvider ())
829
832
833
+ // cleanup old recent events
834
+ for i , event := range e .recentGameEvents {
835
+ if event .occurred .Before (e .TimeProvider ().Add (- time .Second * 2 )) {
836
+ e .recentGameEvents = e .recentGameEvents [i + 1 :]
837
+ break
838
+ }
839
+ }
840
+
830
841
e .applyGameEventFilters (event )
831
842
832
- if e .State . IsRecentGameEvent (event ) {
843
+ if e .IsRecentGameEvent (event ) {
833
844
// only add event to list, not to protocol
834
845
e .State .GameEvents = append (e .State .GameEvents , event )
835
846
return nil
@@ -1160,3 +1171,18 @@ func (e *Engine) removeElapsedYellowCards(team Team, teamState *TeamInfo) (remov
1160
1171
teamState .YellowCardTimes = b
1161
1172
return
1162
1173
}
1174
+
1175
+ func (e * Engine ) FindMatchingRecentGameEvent (event * GameEvent ) * GameEvent {
1176
+ for _ , gameEvent := range e .recentGameEvents {
1177
+ if gameEvent .Type == event .Type &&
1178
+ event .Occurred ().Sub (gameEvent .Occurred ()) < 3 * time .Second {
1179
+ return gameEvent
1180
+ }
1181
+ }
1182
+ return nil
1183
+ }
1184
+
1185
+ func (e * Engine ) IsRecentGameEvent (event * GameEvent ) bool {
1186
+ matchingEvent := e .FindMatchingRecentGameEvent (event )
1187
+ return matchingEvent != nil
1188
+ }
0 commit comments