@@ -212,6 +212,7 @@ func (e *Engine) AddGameEvent(gameEvent GameEvent) {
212
212
}
213
213
214
214
func (e * Engine ) Continue () {
215
+ log .Println ("Continue" )
215
216
substitutionIntend := e .State .BotSubstitutionIntend ()
216
217
if substitutionIntend != TeamUnknown {
217
218
e .State .TeamState [TeamBlue ].BotSubstitutionIntend = false
@@ -224,6 +225,7 @@ func (e *Engine) Continue() {
224
225
e .LogHint ("botSubstitution" , "game halted for bot substitution" , substitutionIntend )
225
226
e .SendCommand (CommandHalt , "" )
226
227
} else if e .State .NextCommand != CommandUnknown {
228
+ log .Printf ("Let game continue with next command" )
227
229
e .SendCommand (e .State .NextCommand , e .State .NextCommandFor )
228
230
} else {
229
231
if e .State .Command != CommandStop {
@@ -508,13 +510,23 @@ func (e *Engine) processTeamModify(m *EventModifyValue) error {
508
510
incremented := * m .FoulCounter > teamState .FoulCounter
509
511
teamState .FoulCounter = * m .FoulCounter
510
512
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
+ }
512
519
}
513
520
} else if m .BallPlacementFailures != nil {
514
521
incremented := * m .BallPlacementFailures > teamState .BallPlacementFailures
515
522
teamState .BallPlacementFailures = * m .BallPlacementFailures
516
523
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
+ }
518
530
}
519
531
} else if m .YellowCardTime != nil {
520
532
cardId := m .YellowCardTime .CardID
@@ -609,7 +621,12 @@ func (e *Engine) processCard(card *EventCard) (err error) {
609
621
}
610
622
teamState := e .State .TeamState [card .ForTeam ]
611
623
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
+ }
613
630
} else if card .Operation == CardOperationRevoke {
614
631
err = revokeCard (card , teamState )
615
632
} else if card .Operation == CardOperationModify {
@@ -636,7 +653,7 @@ func modifyCard(card *EventCard, teamState *TeamInfo) error {
636
653
return nil
637
654
}
638
655
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 {
640
657
if card .Type == CardTypeYellow {
641
658
log .Printf ("Add yellow card for team %v" , card .ForTeam )
642
659
e .State .TeamState [team ].YellowCards ++
@@ -645,7 +662,7 @@ func (e *Engine) addCard(card *EventCard, team Team, duration time.Duration) {
645
662
log .Printf ("Add red card for team %v" , card .ForTeam )
646
663
e .State .TeamState [team ].RedCards ++
647
664
}
648
- e .CardNumberIncremented (team )
665
+ return e .CardNumberIncremented (team )
649
666
}
650
667
651
668
func (e * Engine ) processTrigger (t * EventTrigger ) (err error ) {
@@ -693,16 +710,24 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
693
710
}
694
711
event .SetOccurred (e .TimeProvider ())
695
712
713
+ var additionalEvents []* GameEvent
714
+
696
715
if ! e .State .MatchesRecentGameEvent (event ) && event .IncrementsFoulCounter () {
697
716
team := event .ByTeam ()
698
717
if team .Unknown () {
699
718
e .State .TeamState [TeamYellow ].FoulCounter ++
700
- e .FoulCounterIncremented (TeamYellow )
719
+ if aEvent := e .FoulCounterIncremented (TeamYellow ); aEvent != nil {
720
+ additionalEvents = append (additionalEvents , aEvent )
721
+ }
701
722
e .State .TeamState [TeamBlue ].FoulCounter ++
702
- e .FoulCounterIncremented (TeamBlue )
723
+ if aEvent := e .FoulCounterIncremented (TeamBlue ); aEvent != nil {
724
+ additionalEvents = append (additionalEvents , aEvent )
725
+ }
703
726
} else {
704
727
e .State .TeamState [team ].FoulCounter ++
705
- e .FoulCounterIncremented (team )
728
+ if aEvent := e .FoulCounterIncremented (team ); aEvent != nil {
729
+ additionalEvents = append (additionalEvents , aEvent )
730
+ }
706
731
}
707
732
}
708
733
@@ -713,15 +738,21 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
713
738
if team .Unknown () {
714
739
return errors .New ("Missing team in game event" )
715
740
}
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
+ }
717
745
}
718
746
719
747
if event .AddsRedCard () {
720
748
team := event .ByTeam ()
721
749
if team .Unknown () {
722
750
return errors .New ("Missing team in game event" )
723
751
}
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
+ }
725
756
}
726
757
727
758
if event .Type == GameEventPlacementFailed {
@@ -730,7 +761,10 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
730
761
return errors .New ("Missing team in game event" )
731
762
}
732
763
e .State .TeamState [team ].BallPlacementFailures ++
733
- e .PlacementFailuresIncremented (team )
764
+ aEvent := e .PlacementFailuresIncremented (team )
765
+ if aEvent != nil {
766
+ additionalEvents = append (additionalEvents , aEvent )
767
+ }
734
768
}
735
769
736
770
if event .Type == GameEventPlacementSucceeded {
@@ -757,17 +791,26 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
757
791
e .State .PlacementPos = e .BallPlacementPos ()
758
792
759
793
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 )
761
795
} else if event .Type == GameEventDefenderTooCloseToKickPoint {
762
796
// stop the game and let bots move away from the ball first. The autoRef will continue the game afterwards
763
797
e .SendCommand (CommandStop , "" )
764
798
} else if ! event .IsSkipped () && ! event .IsSecondary () && ! e .State .Command .IsPrepare () {
765
799
e .placeBall (event )
766
800
} else if e .State .AutoContinue && event .IsContinueGame () {
767
801
e .Continue ()
802
+ } else {
803
+ log .Printf ("No change in game with event %v" , event )
768
804
}
769
805
770
806
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
+
771
814
return nil
772
815
}
773
816
@@ -777,6 +820,7 @@ func (e *Engine) placeBall(event *GameEvent) {
777
820
if teamInFavor .Unknown () {
778
821
// select a team by 50% chance
779
822
teamInFavor = e .randomTeam ()
823
+ log .Printf ("No team in favor. Choosing randomly: %v" , teamInFavor )
780
824
}
781
825
782
826
if e .State .PlacementPos == nil || e .State .noTeamCanPlaceBall () {
@@ -796,16 +840,19 @@ func (e *Engine) placeBall(event *GameEvent) {
796
840
event .Type .resultsFromBallLeavingField () {
797
841
// Rule: All free kicks that were a result of the ball leaving the field, are awarded to the opposing team.
798
842
e .SendCommand (CommandBallPlacement , teamInFavor .Opposite ())
843
+ log .Printf ("Let opponent place the ball in DivA" )
799
844
} else if e .State .Division == config .DivB && // For division B
800
845
! e .State .TeamState [teamInFavor ].CanPlaceBall && // If team in favor can not place the ball
801
846
e .State .TeamState [teamInFavor .Opposite ()].CanPlaceBall && // If opponent team can place the ball
802
847
event .Type != GameEventPlacementFailed { // opponent team has not failed recently
803
848
// Rule: [...] the team is allowed to bring the ball into play, after the ball was placed by the opposing team.
804
849
e .SendCommand (CommandBallPlacement , teamInFavor .Opposite ())
850
+ log .Printf ("Let opponent place the ball in DivB" )
805
851
} else if e .State .TeamState [teamInFavor ].CanPlaceBall &&
806
852
! e .allTeamsFailedPlacement () {
807
853
// If team can place ball, let it place
808
854
e .SendCommand (CommandBallPlacement , teamInFavor )
855
+ log .Printf ("Let team in favor place ball" )
809
856
} else {
810
857
// If team can not place ball, human ref has to help out
811
858
e .SendCommand (CommandHalt , "" )
@@ -853,39 +900,36 @@ func (e *Engine) applyGameEventFilters(gameEvent *GameEvent) {
853
900
e .filterAimlessKickForDivA (gameEvent )
854
901
}
855
902
856
- func (e * Engine ) FoulCounterIncremented (team Team ) {
903
+ func (e * Engine ) FoulCounterIncremented (team Team ) * GameEvent {
857
904
if e .State .TeamState [team ].FoulCounter % e .config .MultipleFoulStep == 0 {
858
905
teamProto := team .toProto ()
859
906
event := GameEvent {Type : GameEventMultipleFouls ,
860
907
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
864
909
}
910
+ return nil
865
911
}
866
912
867
- func (e * Engine ) CardNumberIncremented (team Team ) {
913
+ func (e * Engine ) CardNumberIncremented (team Team ) * GameEvent {
868
914
cards := e .State .TeamState [team ].YellowCards + e .State .TeamState [team ].RedCards
869
915
if cards % e .config .MultipleCardStep == 0 {
870
916
teamProto := team .toProto ()
871
917
event := GameEvent {Type : GameEventMultipleCards ,
872
918
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
876
920
}
921
+ return nil
877
922
}
878
923
879
- func (e * Engine ) PlacementFailuresIncremented (team Team ) {
924
+ func (e * Engine ) PlacementFailuresIncremented (team Team ) * GameEvent {
880
925
if e .State .TeamState [team ].BallPlacementFailures == e .config .MultiplePlacementFailures {
881
926
teamProto := team .toProto ()
882
927
e .State .TeamState [team ].CanPlaceBall = false
883
928
event := GameEvent {Type : GameEventMultiplePlacementFailures ,
884
929
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
888
931
}
932
+ return nil
889
933
}
890
934
891
935
func revokeCard (card * EventCard , teamState * TeamInfo ) error {
0 commit comments