Skip to content

Commit 251871d

Browse files
committed
[bugfix] Process additional events after original one has been processed
1 parent aad69a1 commit 251871d

File tree

2 files changed

+79
-24
lines changed

2 files changed

+79
-24
lines changed

internal/app/controller/engine.go

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func (e *Engine) AddGameEvent(gameEvent GameEvent) {
212212
}
213213

214214
func (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

651668
func (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

891935
func revokeCard(card *EventCard, teamState *TeamInfo) error {

internal/app/controller/uiProtocol.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controller
22

33
import (
44
"fmt"
5+
"log"
56
"time"
67
)
78

@@ -41,6 +42,7 @@ type UiProtocolEntry struct {
4142

4243
// LogGameEvent adds a game event to the protocol
4344
func (e *Engine) LogGameEvent(event GameEvent) {
45+
log.Printf("Log game event: %v", event)
4446
entry := UiProtocolEntry{
4547
Timestamp: e.TimeProvider().UnixNano(),
4648
StageTime: e.State.StageTimeElapsed,
@@ -54,6 +56,7 @@ func (e *Engine) LogGameEvent(event GameEvent) {
5456

5557
// LogIgnoredGameEvent adds an ignored game event to the protocol
5658
func (e *Engine) LogIgnoredGameEvent(event GameEvent) {
59+
log.Printf("Log ignored game event: %v", event)
5760
entry := UiProtocolEntry{
5861
Timestamp: e.TimeProvider().UnixNano(),
5962
StageTime: e.State.StageTimeElapsed,
@@ -71,6 +74,7 @@ func (e *Engine) LogCommand() {
7174
if e.State.PlacementPos != nil {
7275
description = fmt.Sprintf("place pos: %v", e.State.PlacementPos)
7376
}
77+
log.Printf("Log command '%v': %v", string(e.State.Command), description)
7478
entry := UiProtocolEntry{
7579
Timestamp: e.TimeProvider().UnixNano(),
7680
StageTime: e.State.StageTimeElapsed,
@@ -84,6 +88,7 @@ func (e *Engine) LogCommand() {
8488

8589
// LogCard adds a card to the protocol
8690
func (e *Engine) LogCard(card *EventCard) {
91+
log.Printf("Log card: %v", card)
8792
entry := UiProtocolEntry{
8893
Timestamp: e.TimeProvider().UnixNano(),
8994
StageTime: e.State.StageTimeElapsed,
@@ -96,6 +101,7 @@ func (e *Engine) LogCard(card *EventCard) {
96101

97102
// LogTime adds a time event (like stage time ends or card time ends) to the protocol
98103
func (e *Engine) LogTime(description string, forTeam Team) {
104+
log.Printf("Log time for %v: %v", forTeam, description)
99105
entry := UiProtocolEntry{
100106
Timestamp: e.TimeProvider().UnixNano(),
101107
StageTime: e.State.StageTimeElapsed,
@@ -108,6 +114,7 @@ func (e *Engine) LogTime(description string, forTeam Team) {
108114

109115
// LogStage adds a stage change to the protocol
110116
func (e *Engine) LogStage(stage Stage) {
117+
log.Printf("Log stage: %v", stage)
111118
entry := UiProtocolEntry{
112119
Timestamp: e.TimeProvider().UnixNano(),
113120
StageTime: e.State.StageTimeElapsed,
@@ -119,6 +126,7 @@ func (e *Engine) LogStage(stage Stage) {
119126

120127
// LogModify adds a modification to the protocol
121128
func (e *Engine) LogModify(m EventModifyValue) {
129+
log.Printf("Log modify: %v", m)
122130
entry := UiProtocolEntry{
123131
Timestamp: e.TimeProvider().UnixNano(),
124132
StageTime: e.State.StageTimeElapsed,
@@ -133,6 +141,7 @@ func (e *Engine) LogModify(m EventModifyValue) {
133141
// LogTeamGoalkeeperChange adds a goalkeeper change from a team to the protocol
134142
func (e *Engine) LogTeamGoalkeeperChange(forTeam Team, oldGoalkeeperId int, newGoalkeeperId int) {
135143
description := fmt.Sprintf("%v -> %v", oldGoalkeeperId, newGoalkeeperId)
144+
log.Printf("Log goal keeper change for %v: %v", forTeam, description)
136145
entry := UiProtocolEntry{
137146
Timestamp: e.TimeProvider().UnixNano(),
138147
StageTime: e.State.StageTimeElapsed,
@@ -146,6 +155,7 @@ func (e *Engine) LogTeamGoalkeeperChange(forTeam Team, oldGoalkeeperId int, newG
146155

147156
// LogTeamBotSubstitutionChange adds a bot substitution intend change from a team to the protocol
148157
func (e *Engine) LogTeamBotSubstitutionChange(forTeam Team, substituteBot bool) {
158+
log.Printf("Log bot substitution for %v: %v", forTeam, substituteBot)
149159
description := fmt.Sprintf("%v", substituteBot)
150160
entry := UiProtocolEntry{
151161
Timestamp: e.TimeProvider().UnixNano(),
@@ -160,6 +170,7 @@ func (e *Engine) LogTeamBotSubstitutionChange(forTeam Team, substituteBot bool)
160170

161171
// LogHint adds a hint for the game-controller operator to the protocol
162172
func (e *Engine) LogHint(hint string, description string, team Team) {
173+
log.Printf("Log hint '%v' for %v: %v", hint, team, description)
163174
entry := UiProtocolEntry{
164175
Timestamp: e.TimeProvider().UnixNano(),
165176
StageTime: e.State.StageTimeElapsed,

0 commit comments

Comments
 (0)