@@ -212,6 +212,7 @@ func (e *Engine) AddGameEvent(gameEvent GameEvent) {
212212}
213213
214214func (e * Engine ) Continue () {
215+ log .Println ("Continue" )
215216 substitutionIntend := e .State .BotSubstitutionIntend ()
216217 if substitutionIntend != TeamUnknown {
217218 e .State .TeamState [TeamBlue ].BotSubstitutionIntend = false
@@ -224,6 +225,7 @@ func (e *Engine) Continue() {
224225 e .LogHint ("botSubstitution" , "game halted for bot substitution" , substitutionIntend )
225226 e .SendCommand (CommandHalt , "" )
226227 } else if e .State .NextCommand != CommandUnknown {
228+ log .Printf ("Let game continue with next command" )
227229 e .SendCommand (e .State .NextCommand , e .State .NextCommandFor )
228230 } else {
229231 if e .State .Command != CommandStop {
@@ -508,13 +510,23 @@ func (e *Engine) processTeamModify(m *EventModifyValue) error {
508510 incremented := * m .FoulCounter > teamState .FoulCounter
509511 teamState .FoulCounter = * m .FoulCounter
510512 if incremented {
511- e .FoulCounterIncremented (m .ForTeam )
513+ if aEvent := e .FoulCounterIncremented (m .ForTeam ); aEvent != nil {
514+ err := e .processGameEvent (aEvent )
515+ if err != nil {
516+ return err
517+ }
518+ }
512519 }
513520 } else if m .BallPlacementFailures != nil {
514521 incremented := * m .BallPlacementFailures > teamState .BallPlacementFailures
515522 teamState .BallPlacementFailures = * m .BallPlacementFailures
516523 if incremented {
517- e .PlacementFailuresIncremented (m .ForTeam )
524+ if aEvent := e .PlacementFailuresIncremented (m .ForTeam ); aEvent != nil {
525+ err := e .processGameEvent (aEvent )
526+ if err != nil {
527+ return err
528+ }
529+ }
518530 }
519531 } else if m .YellowCardTime != nil {
520532 cardId := m .YellowCardTime .CardID
@@ -609,7 +621,12 @@ func (e *Engine) processCard(card *EventCard) (err error) {
609621 }
610622 teamState := e .State .TeamState [card .ForTeam ]
611623 if card .Operation == CardOperationAdd {
612- e .addCard (card , card .ForTeam , e .config .YellowCardDuration )
624+ event := e .addCard (card , card .ForTeam , e .config .YellowCardDuration )
625+ if event != nil {
626+ if err := e .processGameEvent (event ); err != nil {
627+ log .Printf ("Could not process new additional game event: %v" , err )
628+ }
629+ }
613630 } else if card .Operation == CardOperationRevoke {
614631 err = revokeCard (card , teamState )
615632 } else if card .Operation == CardOperationModify {
@@ -636,7 +653,7 @@ func modifyCard(card *EventCard, teamState *TeamInfo) error {
636653 return nil
637654}
638655
639- func (e * Engine ) addCard (card * EventCard , team Team , duration time.Duration ) {
656+ func (e * Engine ) addCard (card * EventCard , team Team , duration time.Duration ) * GameEvent {
640657 if card .Type == CardTypeYellow {
641658 log .Printf ("Add yellow card for team %v" , card .ForTeam )
642659 e .State .TeamState [team ].YellowCards ++
@@ -645,7 +662,7 @@ func (e *Engine) addCard(card *EventCard, team Team, duration time.Duration) {
645662 log .Printf ("Add red card for team %v" , card .ForTeam )
646663 e .State .TeamState [team ].RedCards ++
647664 }
648- e .CardNumberIncremented (team )
665+ return e .CardNumberIncremented (team )
649666}
650667
651668func (e * Engine ) processTrigger (t * EventTrigger ) (err error ) {
@@ -693,16 +710,24 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
693710 }
694711 event .SetOccurred (e .TimeProvider ())
695712
713+ var additionalEvents []* GameEvent
714+
696715 if ! e .State .MatchesRecentGameEvent (event ) && event .IncrementsFoulCounter () {
697716 team := event .ByTeam ()
698717 if team .Unknown () {
699718 e .State .TeamState [TeamYellow ].FoulCounter ++
700- e .FoulCounterIncremented (TeamYellow )
719+ if aEvent := e .FoulCounterIncremented (TeamYellow ); aEvent != nil {
720+ additionalEvents = append (additionalEvents , aEvent )
721+ }
701722 e .State .TeamState [TeamBlue ].FoulCounter ++
702- e .FoulCounterIncremented (TeamBlue )
723+ if aEvent := e .FoulCounterIncremented (TeamBlue ); aEvent != nil {
724+ additionalEvents = append (additionalEvents , aEvent )
725+ }
703726 } else {
704727 e .State .TeamState [team ].FoulCounter ++
705- e .FoulCounterIncremented (team )
728+ if aEvent := e .FoulCounterIncremented (team ); aEvent != nil {
729+ additionalEvents = append (additionalEvents , aEvent )
730+ }
706731 }
707732 }
708733
@@ -713,15 +738,21 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
713738 if team .Unknown () {
714739 return errors .New ("Missing team in game event" )
715740 }
716- e .addCard (& EventCard {Type : CardTypeYellow , ForTeam : team , Operation : CardOperationAdd }, team , e .config .YellowCardDuration )
741+ aEvent := e .addCard (& EventCard {Type : CardTypeYellow , ForTeam : team , Operation : CardOperationAdd }, team , e .config .YellowCardDuration )
742+ if aEvent != nil {
743+ additionalEvents = append (additionalEvents , aEvent )
744+ }
717745 }
718746
719747 if event .AddsRedCard () {
720748 team := event .ByTeam ()
721749 if team .Unknown () {
722750 return errors .New ("Missing team in game event" )
723751 }
724- e .addCard (& EventCard {Type : CardTypeRed , ForTeam : team , Operation : CardOperationAdd }, team , 0 )
752+ aEvent := e .addCard (& EventCard {Type : CardTypeRed , ForTeam : team , Operation : CardOperationAdd }, team , 0 )
753+ if aEvent != nil {
754+ additionalEvents = append (additionalEvents , aEvent )
755+ }
725756 }
726757
727758 if event .Type == GameEventPlacementFailed {
@@ -730,7 +761,10 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
730761 return errors .New ("Missing team in game event" )
731762 }
732763 e .State .TeamState [team ].BallPlacementFailures ++
733- e .PlacementFailuresIncremented (team )
764+ aEvent := e .PlacementFailuresIncremented (team )
765+ if aEvent != nil {
766+ additionalEvents = append (additionalEvents , aEvent )
767+ }
734768 }
735769
736770 if event .Type == GameEventPlacementSucceeded {
@@ -757,17 +791,26 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
757791 e .State .PlacementPos = e .BallPlacementPos ()
758792
759793 if e .State .GameState () == GameStateHalted {
760- log .Printf ("Warn: Received a game event while halted: %v" , event )
794+ log .Printf ("Received a game event while halted: %v" , event )
761795 } else if event .Type == GameEventDefenderTooCloseToKickPoint {
762796 // stop the game and let bots move away from the ball first. The autoRef will continue the game afterwards
763797 e .SendCommand (CommandStop , "" )
764798 } else if ! event .IsSkipped () && ! event .IsSecondary () && ! e .State .Command .IsPrepare () {
765799 e .placeBall (event )
766800 } else if e .State .AutoContinue && event .IsContinueGame () {
767801 e .Continue ()
802+ } else {
803+ log .Printf ("No change in game with event %v" , event )
768804 }
769805
770806 log .Printf ("Processed game event %v" , event )
807+
808+ for _ , aEvent := range additionalEvents {
809+ if err := e .processGameEvent (aEvent ); err != nil {
810+ log .Print ("Could not process new additional game event: " , err )
811+ }
812+ }
813+
771814 return nil
772815}
773816
@@ -777,6 +820,7 @@ func (e *Engine) placeBall(event *GameEvent) {
777820 if teamInFavor .Unknown () {
778821 // select a team by 50% chance
779822 teamInFavor = e .randomTeam ()
823+ log .Printf ("No team in favor. Choosing randomly: %v" , teamInFavor )
780824 }
781825
782826 if e .State .PlacementPos == nil || e .State .noTeamCanPlaceBall () {
@@ -796,16 +840,19 @@ func (e *Engine) placeBall(event *GameEvent) {
796840 event .Type .resultsFromBallLeavingField () {
797841 // Rule: All free kicks that were a result of the ball leaving the field, are awarded to the opposing team.
798842 e .SendCommand (CommandBallPlacement , teamInFavor .Opposite ())
843+ log .Printf ("Let opponent place the ball in DivA" )
799844 } else if e .State .Division == config .DivB && // For division B
800845 ! e .State .TeamState [teamInFavor ].CanPlaceBall && // If team in favor can not place the ball
801846 e .State .TeamState [teamInFavor .Opposite ()].CanPlaceBall && // If opponent team can place the ball
802847 event .Type != GameEventPlacementFailed { // opponent team has not failed recently
803848 // Rule: [...] the team is allowed to bring the ball into play, after the ball was placed by the opposing team.
804849 e .SendCommand (CommandBallPlacement , teamInFavor .Opposite ())
850+ log .Printf ("Let opponent place the ball in DivB" )
805851 } else if e .State .TeamState [teamInFavor ].CanPlaceBall &&
806852 ! e .allTeamsFailedPlacement () {
807853 // If team can place ball, let it place
808854 e .SendCommand (CommandBallPlacement , teamInFavor )
855+ log .Printf ("Let team in favor place ball" )
809856 } else {
810857 // If team can not place ball, human ref has to help out
811858 e .SendCommand (CommandHalt , "" )
@@ -853,39 +900,36 @@ func (e *Engine) applyGameEventFilters(gameEvent *GameEvent) {
853900 e .filterAimlessKickForDivA (gameEvent )
854901}
855902
856- func (e * Engine ) FoulCounterIncremented (team Team ) {
903+ func (e * Engine ) FoulCounterIncremented (team Team ) * GameEvent {
857904 if e .State .TeamState [team ].FoulCounter % e .config .MultipleFoulStep == 0 {
858905 teamProto := team .toProto ()
859906 event := GameEvent {Type : GameEventMultipleFouls ,
860907 Details : GameEventDetails {MultipleFouls : & refproto.GameEvent_MultipleFouls {ByTeam : & teamProto }}}
861- if err := e .processGameEvent (& event ); err != nil {
862- log .Print ("Could not process new secondary game event: " , err )
863- }
908+ return & event
864909 }
910+ return nil
865911}
866912
867- func (e * Engine ) CardNumberIncremented (team Team ) {
913+ func (e * Engine ) CardNumberIncremented (team Team ) * GameEvent {
868914 cards := e .State .TeamState [team ].YellowCards + e .State .TeamState [team ].RedCards
869915 if cards % e .config .MultipleCardStep == 0 {
870916 teamProto := team .toProto ()
871917 event := GameEvent {Type : GameEventMultipleCards ,
872918 Details : GameEventDetails {MultipleCards : & refproto.GameEvent_MultipleCards {ByTeam : & teamProto }}}
873- if err := e .processGameEvent (& event ); err != nil {
874- log .Print ("Could not process new secondary game event: " , err )
875- }
919+ return & event
876920 }
921+ return nil
877922}
878923
879- func (e * Engine ) PlacementFailuresIncremented (team Team ) {
924+ func (e * Engine ) PlacementFailuresIncremented (team Team ) * GameEvent {
880925 if e .State .TeamState [team ].BallPlacementFailures == e .config .MultiplePlacementFailures {
881926 teamProto := team .toProto ()
882927 e .State .TeamState [team ].CanPlaceBall = false
883928 event := GameEvent {Type : GameEventMultiplePlacementFailures ,
884929 Details : GameEventDetails {MultiplePlacementFailures : & refproto.GameEvent_MultiplePlacementFailures {ByTeam : & teamProto }}}
885- if err := e .processGameEvent (& event ); err != nil {
886- log .Print ("Could not process new secondary game event: " , err )
887- }
930+ return & event
888931 }
932+ return nil
889933}
890934
891935func revokeCard (card * EventCard , teamState * TeamInfo ) error {
0 commit comments