Skip to content

Commit 7d714d3

Browse files
committed
[feature] Implement multiple cards event, refactoring multiple fouls
1 parent 2b13ca7 commit 7d714d3

File tree

8 files changed

+198
-180
lines changed

8 files changed

+198
-180
lines changed

internal/app/controller/engine.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (e *Engine) Continue() error {
8989
case
9090
GameEventGoal,
9191
GameEventDefenderInDefenseArea,
92-
GameEventMultipleYellowCards:
92+
GameEventMultipleCards:
9393
default:
9494
// the placement failed by team in favor
9595
// the game is continued by the other team
@@ -132,7 +132,7 @@ func (e *Engine) Continue() error {
132132
e.SendCommand(CommandForceStart, TeamUnknown)
133133
case
134134
GameEventDefenderInDefenseArea,
135-
GameEventMultipleYellowCards:
135+
GameEventMultipleCards:
136136
e.SendCommand(CommandPenalty, teamInFavor)
137137
case
138138
GameEventBotInterferedPlacement,
@@ -360,7 +360,11 @@ func (e *Engine) processModify(m *EventModifyValue) error {
360360
teamState.OnPositiveHalf = *m.OnPositiveHalf
361361
e.State.TeamState[m.ForTeam.Opposite()].OnPositiveHalf = !*m.OnPositiveHalf
362362
} else if m.FoulCounter != nil {
363+
incremented := *m.FoulCounter > teamState.FoulCounter
363364
teamState.FoulCounter = *m.FoulCounter
365+
if incremented {
366+
e.FoulCounterIncremented(m.ForTeam)
367+
}
364368
} else if m.BallPlacementFailures != nil {
365369
teamState.BallPlacementFailures = *m.BallPlacementFailures
366370
} else if m.YellowCardTime != nil {
@@ -453,7 +457,7 @@ func (e *Engine) processCard(card *EventCard) (err error) {
453457
}
454458
teamState := e.State.TeamState[card.ForTeam]
455459
if card.Operation == CardOperationAdd {
456-
addCard(card, teamState, e.config.YellowCardDuration)
460+
e.addCard(card, card.ForTeam, e.config.YellowCardDuration)
457461
} else if card.Operation == CardOperationRevoke {
458462
err = revokeCard(card, teamState)
459463
} else if card.Operation == CardOperationModify {
@@ -480,15 +484,16 @@ func modifyCard(card *EventCard, teamState *TeamInfo) error {
480484
return nil
481485
}
482486

483-
func addCard(card *EventCard, teamState *TeamInfo, duration time.Duration) {
487+
func (e *Engine) addCard(card *EventCard, team Team, duration time.Duration) {
484488
if card.Type == CardTypeYellow {
485489
log.Printf("Add yellow card for team %v", card.ForTeam)
486-
teamState.YellowCards++
487-
teamState.YellowCardTimes = append(teamState.YellowCardTimes, duration)
490+
e.State.TeamState[team].YellowCards++
491+
e.State.TeamState[team].YellowCardTimes = append(e.State.TeamState[team].YellowCardTimes, duration)
488492
} else if card.Type == CardTypeRed {
489493
log.Printf("Add red card for team %v", card.ForTeam)
490-
teamState.RedCards++
494+
e.State.TeamState[team].RedCards++
491495
}
496+
e.CardNumberIncremented(team)
492497
}
493498

494499
func (e *Engine) processTrigger(t *EventTrigger) (err error) {
@@ -519,13 +524,18 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
519524
return errors.Errorf("Incomplete game event: %v", event)
520525
}
521526

527+
e.AddGameEvent(*event)
528+
522529
if event.IncrementsFoulCounter() {
523530
team := event.ByTeam()
524531
if team.Unknown() {
525532
e.State.TeamState[TeamYellow].FoulCounter++
533+
e.FoulCounterIncremented(TeamYellow)
526534
e.State.TeamState[TeamBlue].FoulCounter++
535+
e.FoulCounterIncremented(TeamBlue)
527536
} else {
528537
e.State.TeamState[team].FoulCounter++
538+
e.FoulCounterIncremented(team)
529539
}
530540
}
531541

@@ -534,18 +544,17 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
534544
if team.Unknown() {
535545
return errors.New("Missing team in game event")
536546
}
537-
addCard(&EventCard{Type: CardTypeYellow, ForTeam: team, Operation: CardOperationAdd}, e.State.TeamState[team], e.config.YellowCardDuration)
547+
e.addCard(&EventCard{Type: CardTypeYellow, ForTeam: team, Operation: CardOperationAdd}, team, e.config.YellowCardDuration)
538548
}
539549

540550
if event.AddsRedCard() {
541551
team := event.ByTeam()
542552
if team.Unknown() {
543553
return errors.New("Missing team in game event")
544554
}
545-
addCard(&EventCard{Type: CardTypeRed, ForTeam: team, Operation: CardOperationAdd}, e.State.TeamState[team], 0)
555+
e.addCard(&EventCard{Type: CardTypeRed, ForTeam: team, Operation: CardOperationAdd}, team, 0)
546556
}
547557

548-
e.AddGameEvent(*event)
549558
e.State.PlacementPos = e.BallPlacementPos()
550559

551560
if e.State.GameState() != GameStateHalted && !event.IsContinued() && !event.IsSecondary() {
@@ -564,19 +573,30 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
564573
}
565574

566575
log.Printf("Processed game event %v", event)
576+
return nil
577+
}
567578

568-
for _, team := range []Team{TeamBlue, TeamYellow} {
569-
counter := e.State.TeamState[team].FoulCounter
570-
counterPunished := e.State.TeamState[team].FoulCounterPunished
571-
if counter > 0 && counter%3 == 0 && counter != counterPunished {
572-
e.State.TeamState[team].FoulCounterPunished = counter
573-
teamProto := team.toProto()
574-
return e.processGameEvent(&GameEvent{Type: GameEventMultipleFouls,
575-
Details: GameEventDetails{MultipleFouls: &refproto.GameEvent_MultipleFouls{ByTeam: &teamProto}}})
579+
func (e *Engine) FoulCounterIncremented(team Team) {
580+
if e.State.TeamState[team].FoulCounter%3 == 0 {
581+
teamProto := team.toProto()
582+
event := GameEvent{Type: GameEventMultipleFouls,
583+
Details: GameEventDetails{MultipleFouls: &refproto.GameEvent_MultipleFouls{ByTeam: &teamProto}}}
584+
if err := e.processGameEvent(&event); err != nil {
585+
log.Print("Could not process new secondary game event: ", err)
576586
}
577587
}
588+
}
578589

579-
return nil
590+
func (e *Engine) CardNumberIncremented(team Team) {
591+
cards := e.State.TeamState[team].YellowCards + e.State.TeamState[team].RedCards
592+
if cards%3 == 0 {
593+
teamProto := team.toProto()
594+
event := GameEvent{Type: GameEventMultipleCards,
595+
Details: GameEventDetails{MultipleCards: &refproto.GameEvent_MultipleCards{ByTeam: &teamProto}}}
596+
if err := e.processGameEvent(&event); err != nil {
597+
log.Print("Could not process new secondary game event: ", err)
598+
}
599+
}
580600
}
581601

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

internal/app/controller/gameEvent.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const (
3838
GameEventKeeperHeldBall GameEventType = "keeperHeldBall"
3939
GameEventUnsportiveBehaviorMinor GameEventType = "unsportiveBehaviorMinor"
4040
GameEventUnsportiveBehaviorMajor GameEventType = "unsportiveBehaviorMajor"
41-
GameEventMultipleYellowCards GameEventType = "multipleYellowCards"
41+
GameEventMultipleCards GameEventType = "multipleCards"
4242
GameEventMultipleFouls GameEventType = "multipleFouls"
4343
GameEventKickTimeout GameEventType = "kickTimeout"
4444
GameEventNoProgressInGame GameEventType = "noProgressInGame"
@@ -189,8 +189,8 @@ func (e GameEvent) ToProto() *refproto.GameEvent {
189189
protoEvent.Event = &refproto.GameEvent_UnsportiveBehaviorMinor_{UnsportiveBehaviorMinor: e.Details.UnsportiveBehaviorMinor}
190190
case GameEventUnsportiveBehaviorMajor:
191191
protoEvent.Event = &refproto.GameEvent_UnsportiveBehaviorMajor_{UnsportiveBehaviorMajor: e.Details.UnsportiveBehaviorMajor}
192-
case GameEventMultipleYellowCards:
193-
protoEvent.Event = &refproto.GameEvent_MultipleYellowCards_{MultipleYellowCards: e.Details.MultipleYellowCards}
192+
case GameEventMultipleCards:
193+
protoEvent.Event = &refproto.GameEvent_MultipleCards_{MultipleCards: e.Details.MultipleCards}
194194
case GameEventMultipleFouls:
195195
protoEvent.Event = &refproto.GameEvent_MultipleFouls_{MultipleFouls: e.Details.MultipleFouls}
196196
case GameEventKickTimeout:
@@ -237,7 +237,7 @@ type GameEventDetails struct {
237237
KeeperHeldBall *refproto.GameEvent_KeeperHeldBall `json:"keeperHeldBall,omitempty"`
238238
UnsportiveBehaviorMinor *refproto.GameEvent_UnsportiveBehaviorMinor `json:"unsportiveBehaviorMinor,omitempty"`
239239
UnsportiveBehaviorMajor *refproto.GameEvent_UnsportiveBehaviorMajor `json:"unsportiveBehaviorMajor,omitempty"`
240-
MultipleYellowCards *refproto.GameEvent_MultipleYellowCards `json:"multipleYellowCards,omitempty"`
240+
MultipleCards *refproto.GameEvent_MultipleCards `json:"multiple,omitempty"`
241241
MultipleFouls *refproto.GameEvent_MultipleFouls `json:"multipleFouls,omitempty"`
242242
KickTimeout *refproto.GameEvent_KickTimeout `json:"kickTimeout,omitempty"`
243243
NoProgressInGame *refproto.GameEvent_NoProgressInGame `json:"noProgressInGame,omitempty"`
@@ -327,8 +327,8 @@ func (d GameEventDetails) EventType() GameEventType {
327327
if d.UnsportiveBehaviorMajor != nil {
328328
return GameEventUnsportiveBehaviorMajor
329329
}
330-
if d.MultipleYellowCards != nil {
331-
return GameEventMultipleYellowCards
330+
if d.MultipleCards != nil {
331+
return GameEventMultipleCards
332332
}
333333
if d.MultipleFouls != nil {
334334
return GameEventMultipleFouls
@@ -514,7 +514,7 @@ func (d GameEventDetails) Description() string {
514514
}
515515
return ""
516516
}
517-
if d.MultipleYellowCards != nil {
517+
if d.MultipleCards != nil {
518518
return ""
519519
}
520520
if d.MultipleFouls != nil {
@@ -563,7 +563,7 @@ func NewGameEventDetails(event refproto.GameEvent) (d GameEventDetails) {
563563
d.KeeperHeldBall = event.GetKeeperHeldBall()
564564
d.UnsportiveBehaviorMinor = event.GetUnsportiveBehaviorMinor()
565565
d.UnsportiveBehaviorMajor = event.GetUnsportiveBehaviorMajor()
566-
d.MultipleYellowCards = event.GetMultipleYellowCards()
566+
d.MultipleCards = event.GetMultipleCards()
567567
d.MultipleFouls = event.GetMultipleFouls()
568568
d.KickTimeout = event.GetKickTimeout()
569569
d.NoProgressInGame = event.GetNoProgressInGame()

internal/app/controller/state.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ type TeamInfo struct {
234234
TimeoutTimeLeft time.Duration `json:"timeoutTimeLeft"`
235235
OnPositiveHalf bool `json:"onPositiveHalf"`
236236
FoulCounter int `json:"foulCounter"`
237-
FoulCounterPunished int `json:"foulCounterPunished"`
238237
BallPlacementFailures int `json:"ballPlacementFailures"`
239238
CanPlaceBall bool `json:"canPlaceBall"`
240239
}

0 commit comments

Comments
 (0)