|
1 | 1 | package controller
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "github.com/pkg/errors" |
4 | 5 | "gopkg.in/yaml.v2"
|
5 |
| - "log" |
6 | 6 | "os"
|
7 | 7 | "time"
|
8 | 8 | )
|
9 | 9 |
|
10 |
| -type RefBoxConfigSpecial struct { |
| 10 | +// special configs that are different between normal and overtime halves |
| 11 | +type ConfigSpecial struct { |
11 | 12 | HalfDuration time.Duration `yaml:"half-duration"`
|
12 | 13 | HalfTimeDuration time.Duration `yaml:"half-time-duration"`
|
13 | 14 | TimeoutDuration time.Duration `yaml:"timeout-duration"`
|
14 | 15 | Timeouts int `yaml:"timeouts"`
|
15 | 16 | BreakAfter time.Duration `yaml:"break-after"`
|
16 | 17 | }
|
17 | 18 |
|
18 |
| -type RefBoxConfigGlobal struct { |
| 19 | +// global configs |
| 20 | +type ConfigGlobal struct { |
19 | 21 | YellowCardDuration time.Duration `yaml:"yellow-card-duration"`
|
20 | 22 | }
|
21 | 23 |
|
22 |
| -type RefBoxConfigPublish struct { |
| 24 | +// publish configs |
| 25 | +type ConfigPublish struct { |
23 | 26 | Address string `yaml:"address"`
|
24 | 27 | }
|
25 | 28 |
|
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"` |
31 | 35 | }
|
32 | 36 |
|
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() |
34 | 41 |
|
35 | 42 | f, err := os.OpenFile(fileName, os.O_RDONLY, 0600)
|
36 | 43 | 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) |
38 | 53 | }
|
39 | 54 |
|
| 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) { |
40 | 79 | b := make([]byte, 10000)
|
41 | 80 | n, err := f.Read(b)
|
42 | 81 | if err != nil {
|
43 |
| - log.Fatal("Can not read config files ", err) |
| 82 | + return []byte{}, errors.Errorf("Can not read config files: %v", err) |
44 | 83 | }
|
45 | 84 | 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") |
47 | 86 | }
|
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 |
56 | 88 | }
|
0 commit comments