package main import ( _ "embed" "encoding/json" "image/color" "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"` Started bool `json:"started"` } type ClientConfig struct { UserID int `json:"user_id"` Username string `json:"username"` Passwd string `json:"passwd"` Online bool `json:"online"` TimeLeft time.Duration `json:"time_left"` CompanyID uint8 `json:"company_id` Ready bool `json:"ready"` Color string `json:"color"` } type Config struct { 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"` } // 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) c.StatsMonthly = make(map[int]map[string]*StatMonthly) c.StatsDaily = make(map[int]map[string]*StatDaily) 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 { return c.Save(path) } err = json.Unmarshal(b, &c) if err != nil { return err } return c.Save(path) } // 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 { if id == 255 { return false } 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 } 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 } }