Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

config.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package server
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "strings"
  10. )
  11. // Config embodies the configuration for a game. These are populated in various
  12. // ways (json POSTed to server, config file, constants) and are typically owned
  13. // by a Controller.
  14. type Config struct {
  15. Tick int `json:"tick"` // ms
  16. Timescale float64 `json:"timescale"`
  17. Delta float64 `json:"delta"`
  18. Width int `json:"width"`
  19. Height int `json:"height"`
  20. ObstacleCount int `json:"obstacle_count"`
  21. Obstacles []Obstacle `json:'obstacles'`
  22. MaxPoints int `json:"max_points"`
  23. Mode string `json:"mode"`
  24. }
  25. const (
  26. TICK = 60 // ms, this is how often physics is updated
  27. TIMESCALE = 1.0 // this tweaks the temporal duration of a TICK
  28. WIDTH = 800
  29. HEIGHT = 550
  30. OBSTACLE_COUNT = 5
  31. MAX_POINTS = 500 // allowing for 50 pts in every category
  32. DEFAULT_MODE = "deathmatch"
  33. )
  34. // LoadConfig takes the location of a json file that contains values desired to
  35. // be used in the creation of games. Priority is given to values specified at
  36. // game cration time using control channel, followed by values in config file,
  37. // followed by constants defined above.
  38. func LoadConfig(filename string) (Config, error) {
  39. c := Config{
  40. Tick: TICK,
  41. Timescale: TIMESCALE,
  42. Width: WIDTH,
  43. Height: HEIGHT,
  44. ObstacleCount: OBSTACLE_COUNT,
  45. MaxPoints: MAX_POINTS,
  46. Mode: DEFAULT_MODE,
  47. }
  48. home := os.Getenv("HOME")
  49. if len(filename) > 1 && filename[:2] == "~/" {
  50. filename = strings.Replace(filename, "~", home, 1)
  51. }
  52. if _, err := os.Stat(filename); os.IsNotExist(err) {
  53. log.Printf("%+v not found, using defaults", filename)
  54. } else {
  55. log.Printf("found config file: %s", filename)
  56. f, err := ioutil.ReadFile(filename)
  57. if err != nil {
  58. return c, err
  59. }
  60. err = json.Unmarshal(f, &c)
  61. if err != nil {
  62. return c, errors.New(fmt.Sprintf("config parse error: %s", err))
  63. }
  64. }
  65. c.Delta = float64(c.Tick) / 1000.0 * c.Timescale
  66. log.Printf("final config: %#v", c)
  67. return c, nil
  68. }