/
context.go
184 lines (166 loc) · 3.77 KB
/
context.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package foosbot
import (
"flag"
"github.com/cheggaaa/pb"
"log"
"os"
)
var (
Verbose = true
compress = flag.Bool("compress", false, "compresses the database")
decompress = flag.Bool("decompress", false, "decompresses the database")
)
type Indexing struct {
outcomesMap map[string]*Outcome
teamsMap map[string]*Team
teamsPlayerMap map[string]map[string]*Player
playersMap map[string]*Player
playersNameMap map[string]*Player
playersTeamMap map[string][]*Team
}
func (idx *Indexing) Reset() {
idx.outcomesMap = map[string]*Outcome{}
idx.teamsMap = map[string]*Team{}
idx.teamsPlayerMap = map[string]map[string]*Player{}
idx.playersMap = map[string]*Player{}
idx.playersNameMap = map[string]*Player{}
idx.playersTeamMap = map[string][]*Team{}
}
type Context struct {
DatabaseName string
Matches []*Match
Outcomes []*Outcome
Teams []*Team
Players []*Player
indexes *Indexing
}
func (ctx *Context) Reset() {
ctx.Matches = []*Match{}
ctx.Outcomes = []*Outcome{}
ctx.Teams = []*Team{}
ctx.Players = []*Player{}
ctx.indexes.Reset()
}
func NewContext() *Context {
return newContext()
}
func newContext() *Context {
indexes := new(Indexing)
indexes.Reset()
ctx := &Context{
DatabaseName: DefaultDatabaseName,
indexes: indexes,
}
return ctx
}
func (ctx *Context) AddMatchWithOutcome(outcome *Outcome) {
match := NewMatch(outcome)
ctx.AddMatch(match, outcome)
}
func (ctx *Context) AddMatch(match *Match, outcome *Outcome) {
ctx.Matches = append(ctx.Matches, match)
m, ok := ctx.indexes.outcomesMap[outcome.ID]
if ok {
outcome = m
m.Occurrences++
return
}
outcome.Occurrences++
ctx.Outcomes = append(ctx.Outcomes, outcome)
ctx.indexes.outcomesMap[outcome.ID] = outcome
return
}
func (ctx *Context) AddTeam(team *Team) {
_, ok := ctx.indexes.teamsMap[team.ID]
if ok {
return
}
ctx.Teams = append(ctx.Teams, team)
ctx.indexes.teamsMap[team.ID] = team
ctx.indexes.teamsPlayerMap[team.ID] = map[string]*Player{}
for _, player := range team.Players {
ctx.AddPlayer(player)
ctx.indexes.teamsPlayerMap[team.ID][player.ID] = player
ctx.indexes.playersTeamMap[player.ID] = append(ctx.indexes.playersTeamMap[player.ID], team)
}
return
}
func (ctx *Context) AddPlayer(player *Player) {
_, ok := ctx.indexes.playersMap[player.ID]
if ok {
return
}
ctx.Players = append(ctx.Players, player)
ctx.indexes.playersMap[player.ID] = player
ctx.indexes.playersNameMap[player.Name] = player
return
}
func (ctx *Context) Store() error {
if Verbose {
log.Println("storing database")
}
err := StoreDB(ctx, *compress)
if err != nil {
log.Printf("error storing database: %s", err)
return err
}
return nil
}
func (ctx *Context) Load() error {
var bar *pb.ProgressBar
if Verbose {
log.Println("loading database")
}
db, err := LoadDB(ctx.DatabaseName, *decompress)
if os.IsNotExist(err) {
log.Printf("database not found")
return nil
} else if err != nil {
log.Printf("error loading database: %s", err)
return err
}
if Verbose {
log.Println("loading teams")
bar = pb.StartNew(len(db.Teams))
}
for _, team := range db.Teams {
ctx.AddTeam(team)
if Verbose {
bar.Increment()
}
}
if Verbose {
bar.Finish()
}
if Verbose {
log.Println("loading match history")
bar = pb.StartNew(len(db.Outcomes))
}
loadedOutcomes := map[string]*Outcome{}
for _, outcome := range db.Outcomes {
loadedOutcomes[outcome.ID] = outcome
if Verbose {
bar.Increment()
}
}
if Verbose {
bar.Finish()
}
if Verbose {
bar = pb.StartNew(len(db.Matches))
}
for _, match := range db.Matches {
outcome, ok := loadedOutcomes[match.OutcomeID]
if !ok {
log.Panicf("corrupted history %q", match.ID)
}
ctx.AddMatch(match, outcome)
if Verbose {
bar.Increment()
}
}
if Verbose {
bar.Finish()
}
return nil
}