You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
1.5 KiB

package botserv
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/user"
"strings"
)
type Config struct {
Tick int `json:"tick"` // ms
Timescale float32 `json:"timescale"`
Delta float32 `json:"delta"`
Width int `json:"width"`
Height int `json:"height"`
Obstacles int `json:"obstacles"`
MaxPoints int `json:"max_points"`
Mode string `json:"mode"`
}
const (
TICK = 60
TIMESCALE = 1.0
WIDTH = 800
HEIGHT = 550
OBSTACLES = 5
MAX_POINTS = 500 // allowing for 50 pts in every category
DEFAULT_MODE = "deathmatch"
)
func LoadConfig(filename string) (Config, error) {
c := Config{
Tick: TICK,
Timescale: TIMESCALE,
Width: WIDTH,
Height: HEIGHT,
Obstacles: OBSTACLES,
MaxPoints: MAX_POINTS,
Mode: DEFAULT_MODE,
}
u, err := user.Current()
if err != nil {
return c, err
}
if len(filename) > 1 && filename[:2] == "~/" {
filename = strings.Replace(filename, "~", u.HomeDir, 1)
}
if _, err := os.Stat(filename); os.IsNotExist(err) {
log.Printf("%+v not found, using defaults", filename)
} else {
log.Printf("found config file: %s", filename)
f, err := ioutil.ReadFile(filename)
if err != nil {
return c, err
}
err = json.Unmarshal(f, &c)
if err != nil {
return c, errors.New(fmt.Sprintf("config parse error: %s", err))
}
}
c.Delta = (float32(c.Tick) / 1000.0) * float32(c.Timescale)
log.Printf("final config: %+v", c)
return c, nil
}