Skip to content

Commit 394384a

Browse files
committed
[refactoring] Cleanup config code, make config file optional
1 parent 506f52a commit 394384a

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

internal/app/controller/config.go

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,88 @@
11
package controller
22

33
import (
4+
"github.com/pkg/errors"
45
"gopkg.in/yaml.v2"
5-
"log"
66
"os"
77
"time"
88
)
99

10-
type RefBoxConfigSpecial struct {
10+
// special configs that are different between normal and overtime halves
11+
type ConfigSpecial struct {
1112
HalfDuration time.Duration `yaml:"half-duration"`
1213
HalfTimeDuration time.Duration `yaml:"half-time-duration"`
1314
TimeoutDuration time.Duration `yaml:"timeout-duration"`
1415
Timeouts int `yaml:"timeouts"`
1516
BreakAfter time.Duration `yaml:"break-after"`
1617
}
1718

18-
type RefBoxConfigGlobal struct {
19+
// global configs
20+
type ConfigGlobal struct {
1921
YellowCardDuration time.Duration `yaml:"yellow-card-duration"`
2022
}
2123

22-
type RefBoxConfigPublish struct {
24+
// publish configs
25+
type ConfigPublish struct {
2326
Address string `yaml:"address"`
2427
}
2528

26-
type RefBoxConfig struct {
27-
Publish RefBoxConfigPublish `yaml:"publish"`
28-
Global RefBoxConfigGlobal `yaml:"global"`
29-
Normal RefBoxConfigSpecial `yaml:"normal"`
30-
Overtime RefBoxConfigSpecial `yaml:"overtime"`
29+
// Config structure for the game controller
30+
type Config struct {
31+
Publish ConfigPublish `yaml:"publish"`
32+
Global ConfigGlobal `yaml:"global"`
33+
Normal ConfigSpecial `yaml:"normal"`
34+
Overtime ConfigSpecial `yaml:"overtime"`
3135
}
3236

33-
func LoadRefBoxConfig(fileName string) RefBoxConfig {
37+
// Load a config from given file
38+
func LoadConfig(fileName string) (config Config, err error) {
39+
40+
config = DefaultConfig()
3441

3542
f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
3643
if err != nil {
37-
log.Fatal("Can not open config files ", err)
44+
err = errors.Errorf("Can not open config file %v. %v", fileName, err)
45+
return
46+
}
47+
48+
b, err := readAll(f)
49+
50+
err = yaml.Unmarshal(b, &config)
51+
if err != nil {
52+
err = errors.Errorf("Could not unmarshal config file %v. %v", fileName, err)
3853
}
3954

55+
return
56+
}
57+
58+
// Create a config with default values
59+
func DefaultConfig() (c Config) {
60+
c.Publish.Address = "224.5.23.1:10003"
61+
c.Global.YellowCardDuration = 2 * time.Minute
62+
63+
c.Normal.HalfDuration = 5 * time.Minute
64+
c.Normal.HalfTimeDuration = 5 * time.Minute
65+
c.Normal.Timeouts = 4
66+
c.Normal.TimeoutDuration = 5 * time.Minute
67+
c.Normal.BreakAfter = 5 * time.Minute
68+
69+
c.Overtime.HalfDuration = 2*time.Minute + 30*time.Second
70+
c.Overtime.HalfTimeDuration = 2 * time.Minute
71+
c.Overtime.Timeouts = 2
72+
c.Overtime.TimeoutDuration = 5 * time.Minute
73+
c.Overtime.BreakAfter = 2 * time.Minute
74+
75+
return
76+
}
77+
78+
func readAll(f *os.File) ([]byte, error) {
4079
b := make([]byte, 10000)
4180
n, err := f.Read(b)
4281
if err != nil {
43-
log.Fatal("Can not read config files ", err)
82+
return []byte{}, errors.Errorf("Can not read config files: %v", err)
4483
}
4584
if n == len(b) {
46-
log.Fatal("Buffer size for reading config file is too small")
85+
return []byte{}, errors.New("Buffer size for reading config file is too small")
4786
}
48-
49-
config := RefBoxConfig{}
50-
err = yaml.Unmarshal(b[:n], &config)
51-
if err != nil {
52-
log.Fatal("Could not unmarshal config file ", err)
53-
}
54-
55-
return config
87+
return b[:n], nil
5688
}

internal/app/controller/refBox.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,34 @@ type RefBox struct {
2121
MatchTimeStart time.Time
2222
notifyUpdateState chan struct{}
2323
StateHistory []RefBoxState
24-
Config RefBoxConfig
24+
Config Config
2525
stateHistoryFile *os.File
2626
lastStateFile *os.File
2727
StageTimes map[RefBoxStage]time.Duration
2828
Publisher RefBoxPublisher
2929
}
3030

3131
func NewRefBox() (refBox *RefBox) {
32+
3233
refBox = new(RefBox)
34+
refBox.Config = loadConfig()
3335
refBox.timer = timer.NewTimer()
3436
refBox.notifyUpdateState = make(chan struct{})
3537
refBox.MatchTimeStart = time.Unix(0, 0)
36-
refBox.Config = LoadRefBoxConfig(configFileName)
3738
refBox.State = NewRefBoxState(refBox.Config)
3839
refBox.Publisher = NewRefBoxPublisher(refBox.Config.Publish.Address)
3940

4041
return
4142
}
4243

44+
func loadConfig() Config {
45+
config, err := LoadConfig(configFileName)
46+
if err != nil {
47+
log.Printf("Warning: Could not load config: %v", err)
48+
}
49+
return config
50+
}
51+
4352
func RunRefBox() {
4453
refBox.Run()
4554
}

internal/app/controller/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type RefBoxState struct {
7676
TeamState map[Team]*RefBoxTeamState `json:"teamState"`
7777
}
7878

79-
func NewRefBoxState(config RefBoxConfig) (refBoxState *RefBoxState) {
79+
func NewRefBoxState(config Config) (refBoxState *RefBoxState) {
8080
refBoxState = new(RefBoxState)
8181
refBoxState.Stage = StagePreGame
8282
refBoxState.GameState = GameStateHalted

0 commit comments

Comments
 (0)