This repository has been archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
162 lines (136 loc) · 3.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"encoding/csv"
"errors"
"flag"
"fmt"
"kaepora/internal/back"
"kaepora/internal/bot"
"kaepora/internal/config"
"kaepora/internal/generator/oot"
"kaepora/internal/generator/oot/settings"
"kaepora/internal/global"
"kaepora/internal/web"
"log"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"github.com/google/uuid"
_ "github.com/mattn/go-sqlite3"
)
func main() {
log.SetFlags(0) // we syslog in prod so we don't care about time here
flag.Parse()
switch flag.Arg(0) { // commands not requiring a back
case "version":
fmt.Fprintf(os.Stdout, "Kaepora %s\n", global.Version)
return
case "help":
fmt.Fprint(os.Stdout, help())
return
case "settings":
if err := generateSettingsStats(flag.Arg(1)); err != nil {
log.Fatal(err)
}
return
}
conf, err := config.NewFromUserConfigDir()
if err != nil {
log.Fatal(err)
}
log.Printf("info: Starting Kaepora %s", global.Version)
b, err := back.New("sqlite3", "./kaepora.db", conf)
if err != nil {
log.Fatal(err)
}
switch flag.Arg(0) {
case "fixtures":
if err := b.LoadFixtures(); err != nil {
log.Fatal(err)
}
case "serve":
if err := serve(b, conf); err != nil {
log.Fatal(err)
}
case "rerank":
if err := b.Rerank(flag.Arg(1)); err != nil {
log.Fatal(err)
}
default:
fmt.Fprint(os.Stderr, help())
os.Exit(1)
}
}
func help() string {
return fmt.Sprintf(`
Kaepora is a tool to manage the "Ocarina of Time: Randomizer"
competitive ladder.
Usage: %[1]s COMMAND [ARGS…]
COMMANDS
fixtures create default data for quick testing during development
help display this help
serve start the Discord bot
version display the current version
rerank SHORTCODE recompute all rankings in a league
settings FILENAME output settings randomizer stats
`,
os.Args[0],
)
}
func serve(b *back.Back, conf *config.Config) error {
done := make(chan struct{})
signaled := make(chan os.Signal, 1)
signal.Notify(signaled, syscall.SIGINT, syscall.SIGTERM)
bot, err := bot.New(b, conf)
if err != nil {
return err
}
server, err := web.NewServer(b, conf)
if err != nil {
return err
}
var wg sync.WaitGroup
go b.Run(&wg, done)
go bot.Serve(&wg, done)
go server.Serve(&wg, done)
sig := <-signaled
log.Printf("warning: received signal %d", sig)
close(done)
log.Print("info: waiting for complete shutdown")
wg.Wait()
log.Print("info: shutdown complete")
return nil
}
func generateSettingsStats(path string) error {
if path == "" {
return errors.New("you must specify a shuffled JSON configuration file name")
}
baseDir, err := oot.GetBaseDir()
if err != nil {
return err
}
s, err := settings.Load(filepath.Join(baseDir, path))
if err != nil {
return err
}
max := 102400
count := map[string]map[string]int{} // name => value => count
for i := 0; i < max; i++ {
settings := s.Shuffle(uuid.New().String(), oot.SettingsCostBudget)
for name, value := range settings {
if _, ok := count[name]; !ok {
count[name] = map[string]int{}
}
count[name][fmt.Sprintf("%v", value)]++
}
}
stats := web.NamedPct2DFrom2DMap(count, max)
w := csv.NewWriter(os.Stdout)
for _, v := range stats {
_ = w.Write([]string{v.Name, v.Value, fmt.Sprintf("%.2f", v.Pct)})
}
w.Flush()
return nil
}