]> git.xonotic.org Git - xonotic/xonstat.git/blob - xonstat/util/xs_glicko.go
Connect to the DB and pass it to a GameProcessor.
[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         "github.com/jmoiron/sqlx"
11         _ "github.com/lib/pq"
12 )
13
14 const DefaultStartGameID = 0
15 const DefaultEndGameID = -1
16 const DefaultRankingWindowDays = 7
17
18 type Config struct {
19         // database connection string
20         ConnStr string
21
22         // the starting game_id in the games table
23         StartGameID int
24
25         // the ending game_id in the games table
26         EndGameID int
27
28         // the number of days constituting the ranking window
29         RankingWindowDays int
30 }
31
32 func loadConfig(path string) (*Config, error) {
33         config := new(Config)
34
35         // defaults
36         config.ConnStr = "user=xonstat host=localhost dbname=xonstatdb sslmode=disable"
37         config.StartGameID = DefaultStartGameID
38         config.EndGameID = DefaultEndGameID
39         config.RankingWindowDays = DefaultRankingWindowDays
40
41         file, err := os.Open(path)
42         if err != nil {
43                 fmt.Println("Failed opening the file.")
44                 return config, err
45         }
46
47         decoder := json.NewDecoder(file)
48
49         // overwrite in-mem config with new values
50         err = decoder.Decode(config)
51         if err != nil {
52                 fmt.Println("Failed to decode the JSON.")
53                 return config, err
54         }
55
56         return config, nil
57 }
58
59 type GameProcessor struct {
60         config *Config
61         db     *sqlx.DB
62 }
63
64 func NewGameProcessor(config Config) *GameProcessor {
65         processor := new(GameProcessor)
66
67         db, err := sqlx.Connect("postgres", config.ConnStr)
68         if err != nil {
69                 log.Fatal(err)
70         }
71         processor.db = db
72
73         return processor
74 }
75
76 func (gp *GameProcessor) GameIDsInRange() []int {
77         gameIDs := make([]int, 0)
78         // fetch game_ids using gp.db
79         return gameIDs
80 }
81
82 func main() {
83         path := flag.String("config", "xs_glicko.json", "configuration file path")
84         start := flag.Int("start", DefaultStartGameID, "starting game_id")
85         end := flag.Int("end", DefaultEndGameID, "ending game_id")
86         days := flag.Int("days", DefaultRankingWindowDays, "number of days in the ranking window")
87         flag.Parse()
88
89         config, err := loadConfig(*path)
90         if err != nil {
91                 log.Fatalf("Unable to load config file: %s.\n", err)
92         }
93
94         if *start != DefaultStartGameID {
95                 config.StartGameID = *start
96         }
97
98         if *end != DefaultEndGameID {
99                 config.EndGameID = *end
100         }
101
102         if *days != DefaultRankingWindowDays {
103                 config.RankingWindowDays = *days
104         }
105
106         processor := NewGameProcessor(*config)
107         fmt.Printf("%+v\n", processor)
108 }