]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/util/xs_glicko.go
Add command line and config file parsing.
[xonotic/xonstat.git] / xonstat / util / xs_glicko.go
1 package main
2
3 import (
4         "encoding/json"
5         "flag"
6         "fmt"
7         "log"
8         "os"
9 )
10
11 const DefaultStartGameID = 0
12 const DefaultEndGameID = -1
13 const DefaultRankingWindowDays = 7
14
15 type Config struct {
16         // database connection string
17         ConnStr string
18
19         // the starting game_id in the games table
20         StartGameID int
21
22         // the ending game_id in the games table
23         EndGameID int
24
25         // the number of days constituting the ranking window
26         RankingWindowDays int
27 }
28
29 func loadConfig(path string) (*Config, error) {
30         config := new(Config)
31
32         // defaults
33         config.ConnStr = "user=xonstat host=localhost dbname=xonstatdb sslmode=disable"
34         config.StartGameID = DefaultStartGameID
35         config.EndGameID = DefaultEndGameID
36         config.RankingWindowDays = DefaultRankingWindowDays
37
38         file, err := os.Open(path)
39         if err != nil {
40                 fmt.Println("Failed opening the file.")
41                 return config, err
42         }
43
44         decoder := json.NewDecoder(file)
45
46         // overwrite in-mem config with new values
47         err = decoder.Decode(config)
48         if err != nil {
49                 fmt.Println("Failed to decode the JSON.")
50                 return config, err
51         }
52
53         return config, nil
54 }
55
56 func main() {
57         path := flag.String("config", "xs_glicko.json", "configuration file path")
58         start := flag.Int("start", DefaultStartGameID, "starting game_id")
59         end := flag.Int("end", DefaultEndGameID, "ending game_id")
60         days := flag.Int("days", DefaultRankingWindowDays, "number of days in the ranking window")
61         flag.Parse()
62
63         config, err := loadConfig(*path)
64         if err != nil {
65                 log.Fatalf("Unable to load config file: %s.\n", err)
66         }
67
68         if *start != DefaultStartGameID {
69                 config.StartGameID = *start
70         }
71
72         if *end != DefaultEndGameID {
73                 config.EndGameID = *end
74         }
75
76         if *days != DefaultRankingWindowDays {
77                 config.RankingWindowDays = *days
78         }
79
80         fmt.Printf("%+v\n", config)
81 }