gottdad/config.go

175 lines
3.9 KiB
Go
Raw Normal View History

2021-11-06 16:33:16 +01:00
package main
import (
_ "embed"
"encoding/json"
2021-12-11 04:50:24 +01:00
"image/color"
2021-11-06 16:33:16 +01:00
"io/ioutil"
"time"
"github.com/tidwall/pretty"
)
//go:embed config.sample.json
var cfgSample []byte
type ServerConfig struct {
Addr string `json:"addr"`
Passwd string `json:"passwd"`
}
type TelegramConfig struct {
AdminID int64 `json:"admin_id"`
ChatID int64 `json:"chat_id"`
URL string `json:"url"`
Token string `json:"token"`
}
type GameConfig struct {
TimeZone string `json:"timezone"`
DailyAllotment time.Duration `json:"daily_allotment"`
StartingAllotment time.Duration `json:"starting_allotment"`
Threshold time.Duration `json:"threshold"`
StartDate time.Time `json:"start_date"`
2021-12-04 05:49:20 +01:00
Started bool `json:"started"`
2021-11-06 16:33:16 +01:00
}
type ClientConfig struct {
UserID int `json:"user_id"`
Username string `json:"username"`
2021-11-10 05:56:53 +01:00
Passwd string `json:"passwd"`
2021-11-06 16:33:16 +01:00
Online bool `json:"online"`
TimeLeft time.Duration `json:"time_left"`
CompanyID uint8 `json:"company_id`
2021-12-04 05:49:20 +01:00
Ready bool `json:"ready"`
2021-12-11 03:38:14 +01:00
Color string `json:"color"`
2021-11-06 16:33:16 +01:00
}
type Config struct {
2021-12-10 14:22:10 +01:00
Server *ServerConfig `json:"server"`
Telegram *TelegramConfig `json:"telegram"`
Game *GameConfig `json:"game"`
Clients map[int]*ClientConfig `json:"clients"`
StatsMonthly map[int]map[string]*StatMonthly `json:"stats_monthly"`
StatsDaily map[int]map[string]*StatDaily `json:"stats_daily"`
2021-11-06 16:33:16 +01:00
}
// Init values for a config based on package defaults
func (c *Config) Init() error {
err := json.Unmarshal(cfgSample, &c)
c.Clients = make(map[int]*ClientConfig)
2021-12-10 14:22:10 +01:00
c.StatsMonthly = make(map[int]map[string]*StatMonthly)
c.StatsDaily = make(map[int]map[string]*StatDaily)
2021-12-05 10:08:12 +01:00
2021-11-06 16:33:16 +01:00
if err != nil {
return err
}
return nil
}
// Load config values from a given file
func (c *Config) Load(path string) error {
err := c.Init()
if err != nil {
return err
}
b, err := ioutil.ReadFile(path)
if err != nil {
2021-11-06 17:13:45 +01:00
return c.Save(path)
2021-11-06 16:33:16 +01:00
}
err = json.Unmarshal(b, &c)
if err != nil {
return err
}
2021-11-06 17:13:45 +01:00
return c.Save(path)
2021-11-06 16:33:16 +01:00
}
// Save config values to a given file
func (c *Config) Save(path string) error {
b, err := c.Export()
if err != nil {
return nil
}
err = ioutil.WriteFile(path, b, 0644)
return err
}
// Export config values as a bytestream
func (c *Config) Export() ([]byte, error) {
b, err := json.Marshal(c)
if err != nil {
return b, err
}
return pretty.Pretty(b), nil
}
func (c *Config) CompanyIsRegistered(id uint8) bool {
2021-11-28 14:41:00 +01:00
if id == 255 {
return false
}
2021-11-06 16:33:16 +01:00
for _, cc := range c.Clients {
if cc.CompanyID == id {
return true
}
}
return false
}
func (c *Config) GetCompanyClient(id uint8) *ClientConfig {
for _, cc := range c.Clients {
if cc.CompanyID == id {
return cc
}
}
return nil
}
2021-12-11 04:50:24 +01:00
func (c *Config) GetClientColor(id int) color.Color {
cc, ok := c.Clients[id]
if !ok {
return color.RGBA{0, 0, 0, 255} // Black
}
switch cc.Color {
case "Dark Blue":
return color.RGBA{28, 68, 140, 255}
case "Pale Green":
return color.RGBA{76, 116, 88, 255}
case "Pink":
return color.RGBA{188, 84, 108, 255}
case "Yellow":
return color.RGBA{212, 156, 32, 255}
case "Red":
return color.RGBA{196, 0, 0, 255}
case "Light Blue":
return color.RGBA{52, 112, 132, 255}
case "Green":
return color.RGBA{84, 132, 20, 255}
case "Dark Green":
return color.RGBA{80, 104, 60, 255}
case "Blue":
return color.RGBA{24, 120, 220, 255}
case "Cream":
return color.RGBA{184, 112, 80, 255}
case "Mauve":
return color.RGBA{80, 80, 116, 255}
case "Purple":
return color.RGBA{104, 76, 196, 255}
case "Orange":
return color.RGBA{252, 156, 0, 255}
case "Brown":
return color.RGBA{124, 104, 72, 255}
case "Gray":
return color.RGBA{116, 116, 116, 255}
case "White":
return color.RGBA{184, 184, 184, 255}
default:
return color.RGBA{0, 0, 0, 255} // Black
}
}