aboutsummaryrefslogtreecommitdiff
path: root/internal/config/config.go
blob: 48f1a692ac35f5467954918c559f889e07f06941 (plain)
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
package config

import (
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	Game   GameConfig   `yaml:"game"`
	Colors ColorsConfig `yaml:"colors"`
	Telnet TelnetConfig `yaml:"telnet"`
	HTTP   HTTPConfig   `yaml:"http"`
	HTTPS  HTTPSConfig  `yaml:"https"`
}

type ColorsConfig map[string]string

func DefaultColors() ColorsConfig {
	return ColorsConfig{
		"room_name":     "fg=cyan bold",
		"room_number":   "dim",
		"room_desc":     "fg=white",
		"direction":     "fg=cyan",
		"exit_direction": "fg=cyan",
		"exit_name":     "fg=green",
		"mob_name":      "fg=bright_red",
		"friendly_npc":  "fg=green",
		"hostile_npc":   "fg=bright_red",
		"damage":        "fg=red",
		"enemy_hp":      "fg=red",
		"character_hp":  "fg=green",
		"xp":            "fg=yellow",
		"level_up":      "fg=bright_yellow bold",
		"item":          "fg=green",
		"player_name":   "fg=bright_white",
		"death":         "fg=red bold",
		"victory":       "fg=green",
		"miss":          "dim",
		"error":         "fg=red",
	}
}

type GameConfig struct {
	TickLength int `yaml:"tick_length"`
}

type TelnetConfig struct {
	Enabled bool `yaml:"enabled"`
	Port    int  `yaml:"port"`
}

type HTTPConfig struct {
	Enabled bool `yaml:"enabled"`
	Port    int  `yaml:"port"`
}

type HTTPSConfig struct {
	Enabled  bool   `yaml:"enabled"`
	Port     int    `yaml:"port"`
	CertFile string `yaml:"cert_file"`
	KeyFile  string `yaml:"key_file"`
}

func Default() *Config {
	return &Config{
		Game: GameConfig{
			TickLength: 600,
		},
		Colors: DefaultColors(),
		Telnet: TelnetConfig{
			Enabled: true,
			Port:    4000,
		},
		HTTP: HTTPConfig{
			Enabled: false,
			Port:    8080,
		},
		HTTPS: HTTPSConfig{
			Enabled: false,
			Port:    8443,
		},
	}
}

func Load(path string) (*Config, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}
	cfg := Default()
	if err := yaml.Unmarshal(data, cfg); err != nil {
		return nil, err
	}
	return cfg, nil
}

func SaveDefault(path string) error {
	cfg := Default()
	data, err := yaml.Marshal(cfg)
	if err != nil {
		return err
	}
	return os.WriteFile(path, data, 0644)
}