Skip to content

Commit ddd77eb

Browse files
committed
[feature] React to autoRef flags
1 parent 54bcd83 commit ddd77eb

File tree

3 files changed

+114
-3
lines changed

3 files changed

+114
-3
lines changed

internal/app/engine/autorefflags.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package engine
2+
3+
import (
4+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
5+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/statemachine"
6+
"time"
7+
)
8+
9+
// processAutoRefFlags processes the flags send by the autoRefs and triggers changes based on them
10+
func (e *Engine) processAutoRefFlags() {
11+
if len(e.gcState.AutoRefState) == 0 {
12+
return
13+
}
14+
15+
if e.stateStore.LatestEntry() != nil {
16+
lastChangeTs := goTime(e.stateStore.LatestEntry().Timestamp)
17+
now := e.timeProvider()
18+
if lastChangeTs.Before(now.Add(time.Millisecond * 200)) {
19+
// Do nothing within the first 200ms after any change
20+
return
21+
}
22+
}
23+
24+
readyToContinue := true
25+
ballPlaced := true
26+
var lastProgressSum, lastProgressNum uint64
27+
for _, autoRefState := range e.gcState.AutoRefState {
28+
if autoRefState.ReadyToContinue != nil {
29+
readyToContinue = readyToContinue && *autoRefState.ReadyToContinue
30+
}
31+
if autoRefState.BallPlaced != nil {
32+
ballPlaced = ballPlaced && *autoRefState.BallPlaced
33+
}
34+
if autoRefState.LastProgress != nil {
35+
lastProgressNum++
36+
lastProgressSum += *autoRefState.LastProgress
37+
}
38+
}
39+
40+
if *e.currentState.Command.Type == state.Command_BALL_PLACEMENT && readyToContinue {
41+
e.Enqueue(createGameEventChange(state.GameEvent_PLACEMENT_SUCCEEDED, state.GameEvent{
42+
Event: &state.GameEvent_PlacementSucceeded_{
43+
PlacementSucceeded: &state.GameEvent_PlacementSucceeded{
44+
ByTeam: e.currentState.Command.ForTeam,
45+
TimeTaken: nil, // TODO
46+
Precision: nil,
47+
Distance: nil,
48+
},
49+
},
50+
}))
51+
} else if (e.currentState.Command.IsPrepare()) &&
52+
readyToContinue &&
53+
e.currentState.NextCommand != nil &&
54+
e.currentState.GetAutoContinue() {
55+
e.Enqueue(&statemachine.Change{
56+
Origin: &changeOriginEngine,
57+
Change: &statemachine.Change_Continue{
58+
Continue: &statemachine.Continue{},
59+
},
60+
})
61+
} else if *e.currentState.Command.Type == state.Command_STOP &&
62+
readyToContinue &&
63+
e.currentState.NextCommand != nil &&
64+
e.currentState.GetAutoContinue() {
65+
if ballPlaced {
66+
e.Enqueue(&statemachine.Change{
67+
Origin: &changeOriginEngine,
68+
Change: &statemachine.Change_Continue{
69+
Continue: &statemachine.Continue{},
70+
},
71+
})
72+
} else {
73+
e.Enqueue(&statemachine.Change{
74+
Origin: &changeOriginEngine,
75+
Change: &statemachine.Change_StartBallPlacement{
76+
StartBallPlacement: &statemachine.StartBallPlacement{},
77+
},
78+
})
79+
}
80+
} else if e.currentState.Command.IsRunning() && lastProgressNum > 0 {
81+
lastProgress := lastProgressSum / lastProgressNum
82+
lastProgressS := lastProgress / 1000000
83+
lastProgressNs := (lastProgress - lastProgressS*1000000) * 1000
84+
lastProgressDt := e.timeProvider().Sub(time.Unix(int64(lastProgressS), int64(lastProgressNs)))
85+
if lastProgressDt > e.gameConfig.NoProgressTimeout[e.currentState.Division.Div()] {
86+
duration := float32(lastProgressDt.Seconds())
87+
e.Enqueue(createGameEventChange(state.GameEvent_NO_PROGRESS_IN_GAME, state.GameEvent{
88+
Event: &state.GameEvent_NoProgressInGame_{
89+
NoProgressInGame: &state.GameEvent_NoProgressInGame{
90+
Location: nil, // TODO required for ball placement
91+
Time: &duration,
92+
},
93+
},
94+
}))
95+
}
96+
}
97+
}
98+
99+
func createGameEventChange(eventType state.GameEvent_Type, event state.GameEvent) *statemachine.Change {
100+
event.Type = &eventType
101+
event.Origin = []string{changeOriginEngine}
102+
return &statemachine.Change{
103+
Origin: &changeOriginEngine,
104+
Change: &statemachine.Change_AddGameEvent{
105+
AddGameEvent: &statemachine.AddGameEvent{
106+
GameEvent: &event,
107+
},
108+
},
109+
}
110+
}

internal/app/engine/engine.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ func (e *Engine) processChanges() {
160160
}
161161
e.processChange(change)
162162
case <-time.After(10 * time.Millisecond):
163-
e.Tick()
163+
e.tick()
164+
e.processAutoRefFlags()
164165
}
165166
}
166167
}

internal/app/engine/timed.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111
)
1212

13-
// Tick updates the timers of the state and triggers changes if required
14-
func (e *Engine) Tick() {
13+
// tick updates the timers of the state and triggers changes if required
14+
func (e *Engine) tick() {
1515
currentTime := e.timeProvider()
1616
delta := currentTime.Sub(e.lastTimeUpdate)
1717
e.lastTimeUpdate = currentTime

0 commit comments

Comments
 (0)