@@ -68,15 +68,16 @@ func (s GameControllerState) DeepCopy() (c GameControllerState) {
6868}
6969
7070type 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
8081}
8182
8283func NewEngine (config config.Game , seed int64 ) (e Engine ) {
@@ -90,6 +91,7 @@ func NewEngine(config config.Game, seed int64) (e Engine) {
9091 e .TimeProvider = func () time.Time { return time .Now () }
9192 e .LastTimeUpdate = e .TimeProvider ()
9293 e .Rand = rand .New (rand .NewSource (seed ))
94+ e .recentGameEvents = []* GameEvent {}
9395 return
9496}
9597
@@ -274,6 +276,7 @@ func (e *Engine) setCurrentActionTimeout(timeout time.Duration) {
274276func (e * Engine ) AddGameEvent (gameEvent * GameEvent ) {
275277 e .LogGameEvent (gameEvent , e .State .DeepCopy ())
276278 e .State .GameEvents = append (e .State .GameEvents , gameEvent )
279+ e .recentGameEvents = append (e .recentGameEvents , gameEvent )
277280}
278281
279282func (e * Engine ) QueueGameEvent (gameEvent * GameEvent ) {
@@ -827,9 +830,17 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
827830
828831 event .SetOccurred (e .TimeProvider ())
829832
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+
830841 e .applyGameEventFilters (event )
831842
832- if e .State . IsRecentGameEvent (event ) {
843+ if e .IsRecentGameEvent (event ) {
833844 // only add event to list, not to protocol
834845 e .State .GameEvents = append (e .State .GameEvents , event )
835846 return nil
@@ -1160,3 +1171,18 @@ func (e *Engine) removeElapsedYellowCards(team Team, teamState *TeamInfo) (remov
11601171 teamState .YellowCardTimes = b
11611172 return
11621173}
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