Skip to content

Commit 2235499

Browse files
committed
[test,bugfix] Test kick timeout and no progress in game
1 parent 4cd8e6b commit 2235499

11 files changed

+265
-130
lines changed

internal/app/controller/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewGameController() (c *GameController) {
5050
c.TeamServer.LoadTrustedKeys(c.Config.Server.Team.TrustedKeysDir)
5151
c.TeamServer.ProcessTeamRequest = c.ProcessTeamRequests
5252

53-
c.Engine = NewEngine(c.Config.Game)
53+
c.Engine = NewEngine(c.Config.Game, time.Now().Unix())
5454
c.timer = timer.NewTimer()
5555

5656
c.setupTimeProvider()

internal/app/controller/engine.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
66
"github.com/pkg/errors"
77
"log"
8+
"math/rand"
89
"strconv"
910
"strings"
1011
"time"
@@ -18,13 +19,15 @@ type Engine struct {
1819
TimeProvider func() time.Time
1920
History History
2021
Geometry config.Geometry
22+
Rand *rand.Rand
2123
}
2224

23-
func NewEngine(config config.Game) (e Engine) {
25+
func NewEngine(config config.Game, seed int64) (e Engine) {
2426
e.config = config
2527
e.loadStages()
2628
e.ResetGame()
2729
e.TimeProvider = func() time.Time { return time.Now() }
30+
e.Rand = rand.New(rand.NewSource(seed))
2831
return
2932
}
3033

@@ -223,7 +226,8 @@ func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam
223226
err = errors.Errorf("Unhandled game event: %v", e.State.GameEvents)
224227
}
225228

226-
if e.State.Division == config.DivA && // For division A
229+
if forTeam.Known() &&
230+
e.State.Division == config.DivA && // For division A
227231
!e.State.TeamState[forTeam].CanPlaceBall && // If team in favor can not place the ball
228232
e.State.TeamState[forTeam.Opposite()].CanPlaceBall && // If opponent team can place the ball
229233
primaryGameEvent.Type.resultsFromBallLeavingField() { // event is caused by the ball leaving the field
@@ -241,6 +245,13 @@ func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam
241245
return
242246
}
243247

248+
func (e *Engine) randomTeam() Team {
249+
if e.Rand.Intn(2) == 0 {
250+
return TeamYellow
251+
}
252+
return TeamBlue
253+
}
254+
244255
func (g GameEventType) resultsFromBallLeavingField() bool {
245256
switch g {
246257
case
@@ -661,7 +672,13 @@ func (e *Engine) processGameEvent(event *GameEvent) error {
661672

662673
func (e *Engine) placeBall(event *GameEvent) {
663674
teamInFavor := event.ByTeam().Opposite()
664-
if e.State.PlacementPos == nil || teamInFavor.Unknown() || e.State.noTeamCanPlaceBall() {
675+
676+
if teamInFavor.Unknown() {
677+
// select a team by 50% chance
678+
teamInFavor = e.randomTeam()
679+
}
680+
681+
if e.State.PlacementPos == nil || e.State.noTeamCanPlaceBall() {
665682
// placement not possible, human ref must help out
666683
e.SendCommand(CommandHalt, "")
667684
return

internal/app/controller/engine_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func processTransitionFile(t *testing.T, fileName string) {
9999
cfg.DefaultDivision = *stateTransitions.InitialState.Division
100100
}
101101

102-
e := NewEngine(cfg)
102+
e := NewEngine(cfg, 0)
103103
initialTime := initialTime()
104104
elapsedTime := time.Duration(0)
105105
e.TimeProvider = func() time.Time {

internal/app/controller/placementPos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (e *Engine) BallPlacementPos() *Location {
9292
case GameEventKeeperHeldBall:
9393
return e.validateProtoLocation(event.Details.KeeperHeldBall.Location)
9494
case GameEventKickTimeout:
95-
return e.validateLocation(e.State.PlacementPos)
95+
return e.validateProtoLocation(event.Details.KickTimeout.Location)
9696
case GameEventNoProgressInGame:
9797
return e.validateProtoLocation(event.Details.NoProgressInGame.Location)
9898
case GameEventPlacementFailed:

internal/app/controller/placementPos_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func TestEngine_BallPlacementPos(t *testing.T) {
1111
cfg := config.DefaultControllerConfig().Game
12-
engine := NewEngine(cfg)
12+
engine := NewEngine(cfg, 0)
1313
engine.State.TeamState[TeamYellow].OnPositiveHalf = true
1414
engine.State.TeamState[TeamBlue].OnPositiveHalf = false
1515
engine.Geometry.PlacementOffsetTouchLine = 0.2

internal/app/controller/testdata/engine_test_command_kickoff.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ transitions:
2020
command:
2121
type: normalStart
2222
expectedStateDiff:
23-
stage: First Half
24-
stageTimeLeft: 300000000000
2523
command: normalStart
2624
commandFor: ""
2725
nextCommand: ""
26+
stage: First Half
27+
stageTimeLeft: 5m
2828
matchTimeStart: "2010-01-01T00:00:00Z"
2929
currentActionDeadline: "2010-01-01T00:00:10Z"
30-
currentActionTimeRemaining: 10000000000
30+
currentActionTimeRemaining: 10s
3131
- event:
3232
command:
3333
type: stop
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
initialState:
2+
stage: First Half
3+
command: direct
4+
commandFor: Yellow
5+
division: DivA
6+
transitions:
7+
- event:
8+
gameEvent:
9+
type: kickTimeout
10+
details:
11+
kicktimeout:
12+
byteam: 1 # Yellow
13+
time: 10.0
14+
location:
15+
x: 1.0
16+
y: 2.0
17+
expectedStateDiff:
18+
command: ballPlacement
19+
commandFor: Blue
20+
nextCommand: indirect
21+
nextCommandFor: Blue
22+
placementPos:
23+
x: 1.0
24+
y: 2.0
25+
currentActionDeadline: "2010-01-01T00:00:30Z"
26+
currentActionTimeRemaining: 30s
27+
gameEvents:
28+
- type: kickTimeout
29+
details:
30+
kicktimeout:
31+
byteam: 1 # Yellow
32+
time: 10.0
33+
location:
34+
x: 1.0
35+
y: 2.0
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
initialState:
2+
stage: Pre-First Half
3+
command: halt
4+
division: DivA
5+
transitions:
6+
- event:
7+
command:
8+
type: stop
9+
expectedStateDiff:
10+
command: stop
11+
- tick: 1s # make sure that the stage time is not proceeded
12+
- event:
13+
command:
14+
type: kickoff
15+
forTeam: Blue
16+
expectedStateDiff:
17+
command: kickoff
18+
commandFor: Blue
19+
nextCommand: normalStart
20+
- event:
21+
gameEvent:
22+
type: prepared
23+
details:
24+
prepared:
25+
timetaken: 1.0
26+
expectedStateDiff:
27+
command: normalStart
28+
commandFor: ""
29+
nextCommand: ""
30+
stage: First Half
31+
stageTimeLeft: 5m
32+
matchTimeStart: "2010-01-01T00:00:01Z"
33+
currentActionDeadline: "2010-01-01T00:00:11Z"
34+
currentActionTimeRemaining: 10s
35+
- tick: 1s # make sure that the stage time is not proceeded
36+
expectedStateDiff:
37+
currentActionTimeRemaining: 9s
38+
stageTimeElapsed: 1s
39+
stageTimeLeft: 4m59s
40+
matchDuration: 1s
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
initialState:
2+
stage: First Half
3+
command: forceStart
4+
commandFor: Yellow
5+
division: DivA
6+
transitions:
7+
- event:
8+
gameEvent:
9+
type: noProgressInGame
10+
details:
11+
noprogressingame:
12+
time: 10.0
13+
location:
14+
x: 1.0
15+
y: 2.0
16+
expectedStateDiff:
17+
command: ballPlacement
18+
commandFor: Yellow
19+
nextCommand: forceStart
20+
placementPos:
21+
x: 1.0
22+
y: 2.0
23+
currentActionDeadline: "2010-01-01T00:00:30Z"
24+
currentActionTimeRemaining: 30s
25+
gameEvents:
26+
- type: noProgressInGame
27+
details:
28+
noprogressingame:
29+
time: 10.0
30+
location:
31+
x: 1.0
32+
y: 2.0

0 commit comments

Comments
 (0)