Skip to content

Commit 1e06689

Browse files
committed
Feature: Create separate engine config for behavior and autoRef specifics
1 parent 49343df commit 1e06689

27 files changed

+1335
-606
lines changed

config/engine.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"gameEventBehavior": {
3+
"AIMLESS_KICK": "GAME_EVENT_BEHAVIOR_ON",
4+
"ATTACKER_DOUBLE_TOUCHED_BALL": "GAME_EVENT_BEHAVIOR_ON",
5+
"ATTACKER_TOO_CLOSE_TO_DEFENSE_AREA": "GAME_EVENT_BEHAVIOR_ON",
6+
"ATTACKER_TOUCHED_BALL_IN_DEFENSE_AREA": "GAME_EVENT_BEHAVIOR_ON",
7+
"ATTACKER_TOUCHED_OPPONENT_IN_DEFENSE_AREA": "GAME_EVENT_BEHAVIOR_ON",
8+
"ATTACKER_TOUCHED_OPPONENT_IN_DEFENSE_AREA_SKIPPED": "GAME_EVENT_BEHAVIOR_ON",
9+
"BALL_LEFT_FIELD_GOAL_LINE": "GAME_EVENT_BEHAVIOR_ON",
10+
"BALL_LEFT_FIELD_TOUCH_LINE": "GAME_EVENT_BEHAVIOR_ON",
11+
"BOT_CRASH_DRAWN": "GAME_EVENT_BEHAVIOR_ON",
12+
"BOT_CRASH_UNIQUE": "GAME_EVENT_BEHAVIOR_ON",
13+
"BOT_CRASH_UNIQUE_SKIPPED": "GAME_EVENT_BEHAVIOR_ON",
14+
"BOT_DRIBBLED_BALL_TOO_FAR": "GAME_EVENT_BEHAVIOR_ON",
15+
"BOT_HELD_BALL_DELIBERATELY": "GAME_EVENT_BEHAVIOR_ON",
16+
"BOT_INTERFERED_PLACEMENT": "GAME_EVENT_BEHAVIOR_ON",
17+
"BOT_KICKED_BALL_TOO_FAST": "GAME_EVENT_BEHAVIOR_ON",
18+
"BOT_PUSHED_BOT": "GAME_EVENT_BEHAVIOR_ON",
19+
"BOT_PUSHED_BOT_SKIPPED": "GAME_EVENT_BEHAVIOR_ON",
20+
"BOT_SUBSTITUTION": "GAME_EVENT_BEHAVIOR_ON",
21+
"BOT_TIPPED_OVER": "GAME_EVENT_BEHAVIOR_ON",
22+
"BOT_TOO_FAST_IN_STOP": "GAME_EVENT_BEHAVIOR_ON",
23+
"BOUNDARY_CROSSING": "GAME_EVENT_BEHAVIOR_ON",
24+
"CHIPPED_GOAL": "GAME_EVENT_BEHAVIOR_ON",
25+
"DEFENDER_IN_DEFENSE_AREA": "GAME_EVENT_BEHAVIOR_ON",
26+
"DEFENDER_IN_DEFENSE_AREA_PARTIALLY": "GAME_EVENT_BEHAVIOR_ON",
27+
"DEFENDER_TOO_CLOSE_TO_KICK_POINT": "GAME_EVENT_BEHAVIOR_ON",
28+
"GOAL": "GAME_EVENT_BEHAVIOR_ON",
29+
"INDIRECT_GOAL": "GAME_EVENT_BEHAVIOR_ON",
30+
"INVALID_GOAL": "GAME_EVENT_BEHAVIOR_ON",
31+
"KEEPER_HELD_BALL": "GAME_EVENT_BEHAVIOR_ON",
32+
"KICK_TIMEOUT": "GAME_EVENT_BEHAVIOR_ON",
33+
"MULTIPLE_CARDS": "GAME_EVENT_BEHAVIOR_ON",
34+
"MULTIPLE_FOULS": "GAME_EVENT_BEHAVIOR_ON",
35+
"MULTIPLE_PLACEMENT_FAILURES": "GAME_EVENT_BEHAVIOR_ON",
36+
"NO_PROGRESS_IN_GAME": "GAME_EVENT_BEHAVIOR_ON",
37+
"PENALTY_KICK_FAILED": "GAME_EVENT_BEHAVIOR_ON",
38+
"PLACEMENT_FAILED": "GAME_EVENT_BEHAVIOR_ON",
39+
"PLACEMENT_SUCCEEDED": "GAME_EVENT_BEHAVIOR_ON",
40+
"POSSIBLE_GOAL": "GAME_EVENT_BEHAVIOR_ON",
41+
"PREPARED": "GAME_EVENT_BEHAVIOR_ON",
42+
"TOO_MANY_ROBOTS": "GAME_EVENT_BEHAVIOR_ON",
43+
"UNKNOWN_GAME_EVENT_TYPE": "GAME_EVENT_BEHAVIOR_ON",
44+
"UNSPORTING_BEHAVIOR_MAJOR": "GAME_EVENT_BEHAVIOR_ON",
45+
"UNSPORTING_BEHAVIOR_MINOR": "GAME_EVENT_BEHAVIOR_ON"
46+
},
47+
"autoRefConfigs": {
48+
}
49+
}

config/ssl-game-controller.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ server:
1414
trusted-keys-dir: config/trusted_keys/team
1515
ci:
1616
address: :10009
17+
engine:
18+
config-filename: config/engine.yaml
1719
game:
1820
state-store-file: state-store.json.stream
1921
yellow-card-duration: 2m

generateProto.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_state.pro
3434
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_change.proto
3535
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_api.proto
3636
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_engine.proto
37+
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_engine_config.proto
3738

3839
# continuous integration communication
3940
protoc -I"./proto" -I"$GOPATH/src" --go_out="$GOPATH/src" proto/ssl_gc_ci.proto

internal/app/api/server.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/golang/protobuf/proto"
1010
"github.com/golang/protobuf/ptypes"
1111
"github.com/gorilla/websocket"
12+
"github.com/odeke-em/go-uuid"
1213
"log"
1314
"net/http"
1415
"reflect"
@@ -27,6 +28,7 @@ type ServerConnection struct {
2728
lastPublishedState *state.State
2829
lastGcState *engine.GcState
2930
lastProtocolId int32
31+
lastConfig *engine.Config
3032
marshaler jsonpb.Marshaler
3133
}
3234

@@ -71,13 +73,15 @@ func (a *Server) WsHandler(w http.ResponseWriter, r *http.Request) {
7173

7274
func (s *ServerConnection) publish() {
7375
hook := make(chan engine.HookOut, 10)
74-
s.gcEngine.RegisterHook("apiServer", hook)
76+
hookId := "apiServer-" + uuid.New()
77+
s.gcEngine.RegisterHook(hookId, hook)
7578
defer func() {
76-
s.gcEngine.UnregisterHook("apiServer")
79+
s.gcEngine.UnregisterHook(hookId)
7780
close(hook)
7881
}()
7982

8083
s.publishGcState()
84+
s.publishConfig()
8185
s.publishState(s.gcEngine.CurrentState())
8286
s.publishProtocolFull()
8387

@@ -91,6 +95,7 @@ func (s *ServerConnection) publish() {
9195
s.publishProtocolDelta()
9296
} else if hookOut.State != nil {
9397
s.publishGcState()
98+
s.publishConfig()
9499
s.publishState(hookOut.State)
95100
if s.gcEngine.LatestChangeId() != s.lastProtocolId {
96101
s.publishProtocolFull()
@@ -124,6 +129,17 @@ func (s *ServerConnection) publishGcState() {
124129
}
125130
}
126131

132+
func (s *ServerConnection) publishConfig() {
133+
cfg := s.gcEngine.GetConfig()
134+
135+
if !reflect.DeepEqual(cfg, s.lastConfig) {
136+
out := Output{Config: cfg}
137+
s.publishOutput(&out)
138+
s.lastConfig = &engine.Config{}
139+
proto.Merge(s.lastConfig, cfg)
140+
}
141+
}
142+
127143
func (s *ServerConnection) publishProtocolFull() {
128144
changes := s.gcEngine.LatestChangesUntil(-1)
129145
entries := s.changesToProtocolEntries(changes)
@@ -227,6 +243,9 @@ func (a *Server) handleNewEventMessage(b []byte) {
227243
if in.ResetMatch != nil && *in.ResetMatch {
228244
a.gcEngine.ResetMatch()
229245
}
246+
if in.ConfigDelta != nil {
247+
a.gcEngine.UpdateConfig(in.ConfigDelta)
248+
}
230249
}
231250

232251
func stateChanged(s1, s2 *state.State) bool {
@@ -277,9 +296,6 @@ func stateChanged(s1, s2 *state.State) bool {
277296
if *s1.FirstKickoffTeam != *s2.FirstKickoffTeam {
278297
return true
279298
}
280-
if !reflect.DeepEqual(s1.GameEventBehavior, s2.GameEventBehavior) {
281-
return true
282-
}
283299
return false
284300
}
285301

internal/app/api/ssl_gc_api.pb.go

Lines changed: 54 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/app/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,17 @@ type ServerCi struct {
9494
Address string `yaml:"address"`
9595
}
9696

97+
type Engine struct {
98+
ConfigFilename string `yaml:"config-filename"`
99+
}
100+
97101
// Controller structure for the game controller
98102
type Controller struct {
99103
Network Network `yaml:"network"`
100104
Game Game `yaml:"game"`
101105
Server Server `yaml:"server"`
102106
TimeAcquisitionMode TimeAcquisitionMode `yaml:"time-acquisition-mode"`
107+
Engine Engine `yaml:"engine"`
103108
}
104109

105110
type TimeAcquisitionMode string
@@ -224,6 +229,8 @@ func DefaultControllerConfig() (c Controller) {
224229

225230
c.TimeAcquisitionMode = TimeAcquisitionModeSystem
226231

232+
c.Engine.ConfigFilename = "config/engine.yaml"
233+
227234
return
228235
}
229236

internal/app/config/testdata/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ server:
1414
trusted-keys-dir: config/trusted_keys/team
1515
ci:
1616
address: :10009
17+
engine:
18+
config-filename: config/engine.yaml
1719
game:
1820
state-store-file: state-store.json.stream
1921
yellow-card-duration: 2m

internal/app/engine/config.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package engine
2+
3+
import (
4+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
5+
"github.com/golang/protobuf/jsonpb"
6+
"github.com/pkg/errors"
7+
"io/ioutil"
8+
"log"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
func DefaultConfig() (c Config) {
14+
c.AutoRefConfigs = map[string]*AutoRefConfig{}
15+
c.GameEventBehavior = map[string]Config_GameEventBehavior{}
16+
for _, event := range state.AllGameEvents() {
17+
c.GameEventBehavior[event.String()] = Config_GAME_EVENT_BEHAVIOR_ON
18+
}
19+
return
20+
}
21+
22+
// ReadFrom loads a config from given file
23+
func (m *Config) ReadFrom(fileName string) (err error) {
24+
25+
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
26+
if err != nil {
27+
return
28+
}
29+
b, err := ioutil.ReadAll(f)
30+
if err != nil {
31+
return
32+
}
33+
34+
err = jsonpb.UnmarshalString(string(b), m)
35+
if err != nil {
36+
err = errors.Wrapf(err, "Could not unmarshal config file %v", fileName)
37+
}
38+
39+
return
40+
}
41+
42+
// LoadControllerConfig loads the controller config, creating a default one if it is not present yet
43+
func (m *Config) LoadControllerConfig(configFileName string) {
44+
err := m.ReadFrom(configFileName)
45+
if err != nil {
46+
log.Printf("Could not load config: %v", err)
47+
*m = DefaultConfig()
48+
err = m.WriteTo(configFileName)
49+
if err != nil {
50+
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
51+
} else {
52+
log.Println("New default config has been written to ", configFileName)
53+
}
54+
}
55+
return
56+
}
57+
58+
// WriteTo writes the config to the given file
59+
func (m *Config) WriteTo(fileName string) (err error) {
60+
marshaler := jsonpb.Marshaler{Indent: " "}
61+
jsonStr, err := marshaler.MarshalToString(m)
62+
if err != nil {
63+
err = errors.Wrapf(err, "Could not marshal config %v", m)
64+
return
65+
}
66+
err = os.MkdirAll(filepath.Dir(fileName), 0755)
67+
if err != nil {
68+
err = errors.Wrapf(err, "Could not create directory for config file: %v", fileName)
69+
return
70+
}
71+
err = ioutil.WriteFile(fileName, []byte(jsonStr), 0600)
72+
log.Printf("Written to %v", fileName)
73+
return
74+
}

0 commit comments

Comments
 (0)