@@ -89,7 +89,7 @@ func (e *Engine) Continue() error {
89
89
case
90
90
GameEventGoal ,
91
91
GameEventDefenderInDefenseArea ,
92
- GameEventMultipleYellowCards :
92
+ GameEventMultipleCards :
93
93
default :
94
94
// the placement failed by team in favor
95
95
// the game is continued by the other team
@@ -132,7 +132,7 @@ func (e *Engine) Continue() error {
132
132
e .SendCommand (CommandForceStart , TeamUnknown )
133
133
case
134
134
GameEventDefenderInDefenseArea ,
135
- GameEventMultipleYellowCards :
135
+ GameEventMultipleCards :
136
136
e .SendCommand (CommandPenalty , teamInFavor )
137
137
case
138
138
GameEventBotInterferedPlacement ,
@@ -360,7 +360,11 @@ func (e *Engine) processModify(m *EventModifyValue) error {
360
360
teamState .OnPositiveHalf = * m .OnPositiveHalf
361
361
e .State .TeamState [m .ForTeam .Opposite ()].OnPositiveHalf = ! * m .OnPositiveHalf
362
362
} else if m .FoulCounter != nil {
363
+ incremented := * m .FoulCounter > teamState .FoulCounter
363
364
teamState .FoulCounter = * m .FoulCounter
365
+ if incremented {
366
+ e .FoulCounterIncremented (m .ForTeam )
367
+ }
364
368
} else if m .BallPlacementFailures != nil {
365
369
teamState .BallPlacementFailures = * m .BallPlacementFailures
366
370
} else if m .YellowCardTime != nil {
@@ -453,7 +457,7 @@ func (e *Engine) processCard(card *EventCard) (err error) {
453
457
}
454
458
teamState := e .State .TeamState [card .ForTeam ]
455
459
if card .Operation == CardOperationAdd {
456
- addCard (card , teamState , e .config .YellowCardDuration )
460
+ e . addCard (card , card . ForTeam , e .config .YellowCardDuration )
457
461
} else if card .Operation == CardOperationRevoke {
458
462
err = revokeCard (card , teamState )
459
463
} else if card .Operation == CardOperationModify {
@@ -480,15 +484,16 @@ func modifyCard(card *EventCard, teamState *TeamInfo) error {
480
484
return nil
481
485
}
482
486
483
- func addCard (card * EventCard , teamState * TeamInfo , duration time.Duration ) {
487
+ func ( e * Engine ) addCard (card * EventCard , team Team , duration time.Duration ) {
484
488
if card .Type == CardTypeYellow {
485
489
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 )
488
492
} else if card .Type == CardTypeRed {
489
493
log .Printf ("Add red card for team %v" , card .ForTeam )
490
- teamState .RedCards ++
494
+ e . State . TeamState [ team ] .RedCards ++
491
495
}
496
+ e .CardNumberIncremented (team )
492
497
}
493
498
494
499
func (e * Engine ) processTrigger (t * EventTrigger ) (err error ) {
@@ -519,13 +524,18 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
519
524
return errors .Errorf ("Incomplete game event: %v" , event )
520
525
}
521
526
527
+ e .AddGameEvent (* event )
528
+
522
529
if event .IncrementsFoulCounter () {
523
530
team := event .ByTeam ()
524
531
if team .Unknown () {
525
532
e .State .TeamState [TeamYellow ].FoulCounter ++
533
+ e .FoulCounterIncremented (TeamYellow )
526
534
e .State .TeamState [TeamBlue ].FoulCounter ++
535
+ e .FoulCounterIncremented (TeamBlue )
527
536
} else {
528
537
e .State .TeamState [team ].FoulCounter ++
538
+ e .FoulCounterIncremented (team )
529
539
}
530
540
}
531
541
@@ -534,18 +544,17 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
534
544
if team .Unknown () {
535
545
return errors .New ("Missing team in game event" )
536
546
}
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 )
538
548
}
539
549
540
550
if event .AddsRedCard () {
541
551
team := event .ByTeam ()
542
552
if team .Unknown () {
543
553
return errors .New ("Missing team in game event" )
544
554
}
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 )
546
556
}
547
557
548
- e .AddGameEvent (* event )
549
558
e .State .PlacementPos = e .BallPlacementPos ()
550
559
551
560
if e .State .GameState () != GameStateHalted && ! event .IsContinued () && ! event .IsSecondary () {
@@ -564,19 +573,30 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
564
573
}
565
574
566
575
log .Printf ("Processed game event %v" , event )
576
+ return nil
577
+ }
567
578
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 )
576
586
}
577
587
}
588
+ }
578
589
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
+ }
580
600
}
581
601
582
602
func revokeCard (card * EventCard , teamState * TeamInfo ) error {
0 commit comments