-
Notifications
You must be signed in to change notification settings - Fork 44
/
config.go
184 lines (170 loc) · 4 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package engine
import (
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/state"
"github.com/pkg/errors"
"google.golang.org/protobuf/encoding/protojson"
"io"
"log"
"os"
"path/filepath"
)
var defaultTeams = []string{
"AIS",
"AMC",
"Ararabots",
"A-Team",
"Carrossel Caipira",
"Cerberus",
"CMμs",
"Delft Mercurians",
"Eagles",
"ER-Force",
"GreenTea",
"Immortals",
"ITAndroids",
"KgpKubs",
"KIKS",
"luhbots",
"Maracatronics",
"MCT Susano Logics",
"MIT Roboteam",
"MRL",
"NAELIC",
"NAMeC",
"Neon",
"NEUIslanders",
"OMID",
"OP-AmP",
"OrcaBOT",
"Parsian",
"Pequi Mecânico",
"Red Dragons",
"RFC Cambridge",
"Ri-one",
"RobôCin",
"RoboDragons",
"RoboFEI",
"RoboIME",
"RoboJackets",
"RoboTeam Twente",
"SPbUnited",
"SRC",
"SSH",
"STOx’s",
"Sysmic Robotics",
"TauraBOTS",
"Test Team",
"TIGERs Mannheim",
"Titans",
"Tritons RCSC",
"TurtleRabbit",
"UBC Thunderbots",
"ULtron",
"UMass Minutebots",
"UnBall",
"Unknown",
"URoboRus",
"UTBots",
"Warthog Robotics",
"ZJUNlict",
}
func DefaultConfig() (x Config) {
x.AutoRefConfigs = map[string]*AutoRefConfig{}
x.GameEventBehavior = map[string]Config_Behavior{}
for _, event := range state.GameEventsForBehaviorConfig() {
x.GameEventBehavior[event.String()] = Config_BEHAVIOR_ACCEPT_MAJORITY
}
// Set common defaults that are usually set at a competition
x.GameEventBehavior[state.GameEvent_POSSIBLE_GOAL.String()] = Config_BEHAVIOR_ACCEPT
x.GameEventBehavior[state.GameEvent_BALL_LEFT_FIELD_GOAL_LINE.String()] = Config_BEHAVIOR_ACCEPT
x.GameEventBehavior[state.GameEvent_BALL_LEFT_FIELD_TOUCH_LINE.String()] = Config_BEHAVIOR_ACCEPT
x.Teams = defaultTeams
x.AutoContinue = new(bool)
*x.AutoContinue = true
return
}
// ReadFrom loads a config from given file
func (x *Config) ReadFrom(fileName string) (err error) {
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
if err != nil {
return
}
b, err := io.ReadAll(f)
if err != nil {
return
}
err = protojson.Unmarshal(b, x)
if err != nil {
err = errors.Wrapf(err, "Could not unmarshal config file %v", fileName)
}
defConfig := DefaultConfig()
if x.AutoRefConfigs == nil {
x.AutoRefConfigs = defConfig.AutoRefConfigs
}
for key, value := range defConfig.AutoRefConfigs {
if _, ok := x.AutoRefConfigs[key]; !ok {
x.AutoRefConfigs[key] = value
}
}
if x.GameEventBehavior == nil {
x.GameEventBehavior = map[string]Config_Behavior{}
}
for key, value := range defConfig.GameEventBehavior {
if _, ok := x.GameEventBehavior[key]; !ok {
x.GameEventBehavior[key] = value
}
}
uniqueTeams := map[string]bool{}
for _, t := range defaultTeams {
uniqueTeams[t] = true
}
for _, t := range x.Teams {
uniqueTeams[t] = true
}
x.Teams = make([]string, 0, len(uniqueTeams))
for t := range uniqueTeams {
x.Teams = append(x.Teams, t)
}
if x.AutoContinue == nil {
x.AutoContinue = defConfig.AutoContinue
}
return
}
// LoadControllerConfig loads the controller config, creating a default one if it is not present yet
func (x *Config) LoadControllerConfig(configFileName string) {
err := x.ReadFrom(configFileName)
if err != nil {
log.Printf("Could not load config: %v", err)
*x = DefaultConfig()
err = x.WriteTo(configFileName)
if err != nil {
log.Printf("Failed to write a default config file to %v: %v", configFileName, err)
} else {
log.Println("New default config has been written to ", configFileName)
}
}
return
}
// WriteTo writes the config to the given file
func (x *Config) WriteTo(fileName string) (err error) {
marshaler := protojson.MarshalOptions{Indent: " "}
b, err := marshaler.Marshal(x)
if err != nil {
err = errors.Wrapf(err, "Could not marshal config %v", x)
return
}
err = os.MkdirAll(filepath.Dir(fileName), 0755)
if err != nil {
err = errors.Wrapf(err, "Could not create directory for config file: %v", fileName)
return
}
err = os.WriteFile(fileName, b, 0600)
return
}
func (x *Config) StringJson() string {
if b, err := protojson.Marshal(x); err != nil {
return err.Error()
} else {
return string(b)
}
}