Skip to content

Commit 7cf4d35

Browse files
committed
[refactoring] Move config to its own package
1 parent 3826bc5 commit 7cf4d35

File tree

7 files changed

+69
-63
lines changed

7 files changed

+69
-63
lines changed
Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package controller
1+
package config
22

33
import (
44
"github.com/pkg/errors"
@@ -7,16 +7,17 @@ import (
77
"time"
88
)
99

10-
// ConfigSpecial holds configs that are different between normal and overtime halves
11-
type ConfigSpecial struct {
10+
// Special holds configs that are different between normal and overtime halves
11+
type Special struct {
1212
HalfDuration time.Duration `yaml:"half-duration"`
1313
HalfTimeDuration time.Duration `yaml:"half-time-duration"`
1414
TimeoutDuration time.Duration `yaml:"timeout-duration"`
1515
Timeouts int `yaml:"timeouts"`
1616
BreakAfter time.Duration `yaml:"break-after"`
1717
}
1818

19-
type ConfigGeometry struct {
19+
// Geometry holds sizes of the field and distance for certain rules
20+
type Geometry struct {
2021
FieldLength float64 `yaml:"field-length"`
2122
FieldWidth float64 `yaml:"field-width"`
2223
DefenseAreaDepth float64 `yaml:"defense-area-depth"`
@@ -27,56 +28,56 @@ type ConfigGeometry struct {
2728
PlacementOffsetDefenseArea float64 `yaml:"placement-offset-defense-area"`
2829
}
2930

30-
// ConfigGame holds configs that are valid for the whole game
31-
type ConfigGame struct {
32-
YellowCardDuration time.Duration `yaml:"yellow-card-duration"`
33-
DefaultDivision Division `yaml:"default-division"`
34-
Normal ConfigSpecial `yaml:"normal"`
35-
Overtime ConfigSpecial `yaml:"overtime"`
36-
TeamChoiceTimeout time.Duration `yaml:"team-choice-timeout"`
37-
DefaultGeometry map[Division]*ConfigGeometry `yaml:"default-geometry"`
38-
MultipleCardStep int `yaml:"multiple-card-step"`
39-
MultipleFoulStep int `yaml:"multiple-foul-step"`
40-
MultiplePlacementFailures int `yaml:"multiple-placement-failures"`
41-
MaxBots map[Division]int `yaml:"max-bots"`
42-
AutoRefProposalTimeout time.Duration `yaml:"auto-ref-proposal-timeout"`
31+
// Game holds configs that are valid for the whole game
32+
type Game struct {
33+
YellowCardDuration time.Duration `yaml:"yellow-card-duration"`
34+
DefaultDivision Division `yaml:"default-division"`
35+
Normal Special `yaml:"normal"`
36+
Overtime Special `yaml:"overtime"`
37+
TeamChoiceTimeout time.Duration `yaml:"team-choice-timeout"`
38+
DefaultGeometry map[Division]*Geometry `yaml:"default-geometry"`
39+
MultipleCardStep int `yaml:"multiple-card-step"`
40+
MultipleFoulStep int `yaml:"multiple-foul-step"`
41+
MultiplePlacementFailures int `yaml:"multiple-placement-failures"`
42+
MaxBots map[Division]int `yaml:"max-bots"`
43+
AutoRefProposalTimeout time.Duration `yaml:"auto-ref-proposal-timeout"`
4344
}
4445

45-
// ConfigNetwork holds configs for network communication
46-
type ConfigNetwork struct {
46+
// Network holds configs for network communication
47+
type Network struct {
4748
PublishAddress string `yaml:"publish-address"`
4849
VisionAddress string `yaml:"vision-address"`
4950
}
5051

51-
// ConfigServer holds configs for the available server services
52-
type ConfigServer struct {
53-
AutoRef ConfigServerAutoRef `yaml:"auto-ref"`
54-
Team ConfigServerTeam `yaml:"team"`
52+
// Server holds configs for the available server services
53+
type Server struct {
54+
AutoRef ServerAutoRef `yaml:"auto-ref"`
55+
Team ServerTeam `yaml:"team"`
5556
}
5657

57-
// ConfigServerAutoRef holds configs for the autoRef server
58-
type ConfigServerAutoRef struct {
58+
// ServerAutoRef holds configs for the autoRef server
59+
type ServerAutoRef struct {
5960
Address string `yaml:"address"`
6061
TrustedKeysDir string `yaml:"trusted-keys-dir"`
6162
}
6263

63-
// ConfigServerTeam holds configs for the team server
64-
type ConfigServerTeam struct {
64+
// ServerTeam holds configs for the team server
65+
type ServerTeam struct {
6566
Address string `yaml:"address"`
6667
TrustedKeysDir string `yaml:"trusted-keys-dir"`
6768
}
6869

69-
// Config structure for the game controller
70-
type Config struct {
71-
Network ConfigNetwork `yaml:"network"`
72-
Game ConfigGame `yaml:"game"`
73-
Server ConfigServer `yaml:"server"`
70+
// Controller structure for the game controller
71+
type Controller struct {
72+
Network Network `yaml:"network"`
73+
Game Game `yaml:"game"`
74+
Server Server `yaml:"server"`
7475
}
7576

76-
// LoadConfig loads a config from given file
77-
func LoadConfig(fileName string) (config Config, err error) {
77+
// LoadControllerConfig loads a config from given file
78+
func LoadControllerConfig(fileName string) (config Controller, err error) {
7879

79-
config = DefaultConfig()
80+
config = DefaultControllerConfig()
8081

8182
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
8283
if err != nil {
@@ -96,8 +97,8 @@ func LoadConfig(fileName string) (config Config, err error) {
9697
return
9798
}
9899

99-
// DefaultConfig creates a config with default values
100-
func DefaultConfig() (c Config) {
100+
// DefaultControllerConfig creates a config with default values
101+
func DefaultControllerConfig() (c Controller) {
101102
c.Network.PublishAddress = "224.5.23.1:10003"
102103
c.Network.VisionAddress = "224.5.23.2:10006"
103104
c.Game.YellowCardDuration = 2 * time.Minute
@@ -126,8 +127,8 @@ func DefaultConfig() (c Config) {
126127
c.Server.Team.Address = ":10008"
127128
c.Server.Team.TrustedKeysDir = "config/trusted_keys/team"
128129

129-
c.Game.DefaultGeometry = map[Division]*ConfigGeometry{}
130-
c.Game.DefaultGeometry[DivA] = new(ConfigGeometry)
130+
c.Game.DefaultGeometry = map[Division]*Geometry{}
131+
c.Game.DefaultGeometry[DivA] = new(Geometry)
131132
c.Game.DefaultGeometry[DivA].FieldLength = 12
132133
c.Game.DefaultGeometry[DivA].FieldWidth = 9
133134
c.Game.DefaultGeometry[DivA].DefenseAreaDepth = 1.2
@@ -137,7 +138,7 @@ func DefaultConfig() (c Config) {
137138
c.Game.DefaultGeometry[DivA].PlacementOffsetTouchLine = 0.2
138139
c.Game.DefaultGeometry[DivA].PlacementOffsetDefenseArea = 1.0
139140

140-
c.Game.DefaultGeometry[DivB] = new(ConfigGeometry)
141+
c.Game.DefaultGeometry[DivB] = new(Geometry)
141142
c.Game.DefaultGeometry[DivB].FieldLength = 9
142143
c.Game.DefaultGeometry[DivB].FieldWidth = 6
143144
c.Game.DefaultGeometry[DivB].DefenseAreaDepth = 1
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
package controller
1+
package config
22

33
import (
44
"reflect"
55
"testing"
66
)
77

88
func Test_LoadConfig(t *testing.T) {
9-
defConfig := DefaultConfig()
10-
loadedConfig, err := LoadConfig("testdata/config.yaml")
9+
defConfig := DefaultControllerConfig()
10+
loadedConfig, err := LoadControllerConfig("testdata/config.yaml")
1111

1212
if err != nil {
1313
t.Fatal(err)

internal/app/config/division.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package config
2+
3+
type Division string
4+
5+
const (
6+
DivA Division = "DivA"
7+
DivB Division = "DivB"
8+
)

internal/app/controller/controller.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controller
22

33
import (
4+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
45
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/rcon"
56
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/vision"
67
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
@@ -17,7 +18,7 @@ const configFileName = "config/ssl-game-controller.yaml"
1718

1819
// GameController controls a game
1920
type GameController struct {
20-
Config Config
21+
Config config.Controller
2122
Publisher Publisher
2223
ApiServer ApiServer
2324
AutoRefServer *rcon.AutoRefServer
@@ -309,20 +310,20 @@ func (c *GameController) timeoutTeamChoice() {
309310
}
310311
}
311312

312-
func loadPublisher(config Config) Publisher {
313+
func loadPublisher(config config.Controller) Publisher {
313314
publisher, err := NewPublisher(config.Network.PublishAddress)
314315
if err != nil {
315316
log.Printf("Could not start publisher on %v. %v", config.Network.PublishAddress, err)
316317
}
317318
return publisher
318319
}
319320

320-
func loadConfig() Config {
321-
config, err := LoadConfig(configFileName)
321+
func loadConfig() config.Controller {
322+
cfg, err := config.LoadControllerConfig(configFileName)
322323
if err != nil {
323324
log.Printf("Could not load config: %v", err)
324325
}
325-
return config
326+
return cfg
326327
}
327328

328329
// publish publishes the state to the UI and the teams

internal/app/controller/engine.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controller
22

33
import (
44
"fmt"
5+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
56
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
67
"github.com/pkg/errors"
78
"log"
@@ -16,13 +17,13 @@ type Engine struct {
1617
State *State
1718
RefereeEvents []RefereeEvent
1819
StageTimes map[Stage]time.Duration
19-
config ConfigGame
20+
config config.Game
2021
TimeProvider func() time.Time
2122
History History
22-
Geometry ConfigGeometry
23+
Geometry config.Geometry
2324
}
2425

25-
func NewEngine(config ConfigGame) (e Engine) {
26+
func NewEngine(config config.Game) (e Engine) {
2627
e.config = config
2728
e.loadStages()
2829
e.ResetGame()
@@ -144,7 +145,7 @@ func (e *Engine) CommandForEvent(event *GameEvent) (command RefCommand, forTeam
144145

145146
forTeam = event.ByTeam().Opposite()
146147

147-
if e.State.Division == DivA && event.Type == GameEventPlacementFailedByTeamInFavor {
148+
if e.State.Division == config.DivA && event.Type == GameEventPlacementFailedByTeamInFavor {
148149
for _, e := range e.State.GameEvents {
149150
switch e.Type {
150151
case
@@ -403,7 +404,7 @@ func (e *Engine) processCommand(c *EventCommand) error {
403404
func (e *Engine) processModify(m *EventModifyValue) error {
404405
// process team-independent modifies
405406
if m.Division != nil {
406-
if *m.Division == DivA || *m.Division == DivB {
407+
if *m.Division == config.DivA || *m.Division == config.DivB {
407408
e.State.Division = *m.Division
408409
} else {
409410
return errors.Errorf("Invalid division: %v", *m.Division)

internal/app/controller/events.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controller
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
67
"log"
78
"reflect"
89
"time"
@@ -113,7 +114,7 @@ type EventModifyValue struct {
113114
FoulCounter *int `json:"foulCounter,omitempty"`
114115
BallPlacementFailures *int `json:"ballPlacementFailures,omitempty"`
115116
CanPlaceBall *bool `json:"canPlaceBall,omitempty"`
116-
Division *Division `json:"division,omitempty"`
117+
Division *config.Division `json:"division,omitempty"`
117118
AutoContinue *bool `json:"autoContinue,omitempty"`
118119
GameEventBehavior *EventModifyGameEventBehavior `json:"gameEventBehavior,omitempty"`
119120
BotSubstitutionIntend *bool `json:"botSubstitutionIntend,omitempty"`

internal/app/controller/state.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controller
22

33
import (
44
"encoding/json"
5+
"github.com/RoboCup-SSL/ssl-game-controller/internal/app/config"
56
"github.com/RoboCup-SSL/ssl-game-controller/pkg/refproto"
67
"github.com/pkg/errors"
78
"time"
@@ -63,13 +64,6 @@ func NewTeam(team refproto.Team) Team {
6364
return TeamUnknown
6465
}
6566

66-
type Division string
67-
68-
const (
69-
DivA Division = "DivA"
70-
DivB Division = "DivB"
71-
)
72-
7367
// Stage represents the different stages of a game
7468
type Stage string
7569

@@ -284,7 +278,7 @@ type State struct {
284278
MatchTimeStart time.Time `json:"matchTimeStart"`
285279
MatchDuration time.Duration `json:"matchDuration"`
286280
TeamState map[Team]*TeamInfo `json:"teamState"`
287-
Division Division `json:"division"`
281+
Division config.Division `json:"division"`
288282
PlacementPos *Location `json:"placementPos"`
289283
AutoContinue bool `json:"autoContinue"`
290284
NextCommand RefCommand `json:"nextCommand"`
@@ -313,7 +307,7 @@ func NewState() (s *State) {
313307
*s.TeamState[TeamBlue] = newTeamInfo()
314308
s.TeamState[TeamBlue].OnPositiveHalf = !s.TeamState[TeamYellow].OnPositiveHalf
315309

316-
s.Division = DivA
310+
s.Division = config.DivA
317311
s.AutoContinue = true
318312

319313
s.GameEventBehavior = map[GameEventType]GameEventBehavior{}

0 commit comments

Comments
 (0)