Skip to content

Commit b6fcccc

Browse files
committed
Initialize default engine config correctly
1 parent 1bf9c56 commit b6fcccc

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

internal/app/engine/config.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ import (
1010
"path/filepath"
1111
)
1212

13-
func DefaultConfig() (c Config) {
14-
c.AutoRefConfigs = map[string]*AutoRefConfig{}
15-
c.GameEventBehavior = map[string]Config_Behavior{}
13+
func DefaultConfig() (x Config) {
14+
x.AutoRefConfigs = map[string]*AutoRefConfig{}
15+
x.GameEventBehavior = map[string]Config_Behavior{}
1616
for _, event := range state.GameEventsForBehaviorConfig() {
17-
c.GameEventBehavior[event.String()] = Config_BEHAVIOR_ACCEPT_MAJORITY
17+
x.GameEventBehavior[event.String()] = Config_BEHAVIOR_ACCEPT_MAJORITY
1818
}
19+
x.Teams = []string{"Unknown", "Test Team"}
1920
return
2021
}
2122

2223
// ReadFrom loads a config from given file
23-
func (m *Config) ReadFrom(fileName string) (err error) {
24+
func (x *Config) ReadFrom(fileName string) (err error) {
2425

2526
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
2627
if err != nil {
@@ -31,21 +32,32 @@ func (m *Config) ReadFrom(fileName string) (err error) {
3132
return
3233
}
3334

34-
err = jsonpb.UnmarshalString(string(b), m)
35+
err = jsonpb.UnmarshalString(string(b), x)
3536
if err != nil {
3637
err = errors.Wrapf(err, "Could not unmarshal config file %v", fileName)
3738
}
3839

40+
defConfig := DefaultConfig()
41+
if x.AutoRefConfigs == nil {
42+
x.AutoRefConfigs = defConfig.AutoRefConfigs
43+
}
44+
if x.GameEventBehavior == nil {
45+
x.GameEventBehavior = defConfig.GameEventBehavior
46+
}
47+
if len(x.Teams) == 0 {
48+
x.Teams = defConfig.Teams
49+
}
50+
3951
return
4052
}
4153

4254
// 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)
55+
func (x *Config) LoadControllerConfig(configFileName string) {
56+
err := x.ReadFrom(configFileName)
4557
if err != nil {
4658
log.Printf("Could not load config: %v", err)
47-
*m = DefaultConfig()
48-
err = m.WriteTo(configFileName)
59+
*x = DefaultConfig()
60+
err = x.WriteTo(configFileName)
4961
if err != nil {
5062
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
5163
} else {
@@ -56,11 +68,11 @@ func (m *Config) LoadControllerConfig(configFileName string) {
5668
}
5769

5870
// WriteTo writes the config to the given file
59-
func (m *Config) WriteTo(fileName string) (err error) {
71+
func (x *Config) WriteTo(fileName string) (err error) {
6072
marshaler := jsonpb.Marshaler{Indent: " "}
61-
jsonStr, err := marshaler.MarshalToString(m)
73+
jsonStr, err := marshaler.MarshalToString(x)
6274
if err != nil {
63-
err = errors.Wrapf(err, "Could not marshal config %v", m)
75+
err = errors.Wrapf(err, "Could not marshal config %v", x)
6476
return
6577
}
6678
err = os.MkdirAll(filepath.Dir(fileName), 0755)
@@ -73,9 +85,9 @@ func (m *Config) WriteTo(fileName string) (err error) {
7385
return
7486
}
7587

76-
func (m *Config) StringJson() string {
88+
func (x *Config) StringJson() string {
7789
marshaler := jsonpb.Marshaler{}
78-
if str, err := marshaler.MarshalToString(m); err != nil {
90+
if str, err := marshaler.MarshalToString(x); err != nil {
7991
return err.Error()
8092
} else {
8193
return str

0 commit comments

Comments
 (0)