-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_archive.go
350 lines (324 loc) · 8.57 KB
/
model_archive.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"errors"
"fmt"
"strconv"
"time"
"github.com/br0xen/boltease"
"github.com/pborman/uuid"
)
// Archived GameJams are in their own DB files in the data directory
// named `gamejam_<uuid>.db`
type Archive struct {
Jams []ArchivedGamejam
m *model // The model that holds the main archive data
mPath []string // The path in the (main) db to the archives
}
func NewArchive(m *model) *Archive {
arc := new(Archive)
arc.m = m
arc.mPath = []string{"archive"}
return arc
}
func (m *model) LoadArchive() (*Archive, error) {
if err := m.openDB(); err != nil {
return nil, err
}
defer m.closeDB()
arc := NewArchive(m)
vals, err := m.bolt.GetValueList(arc.mPath)
if err != nil {
// There apparently aren't any archived Jams
return arc, nil
}
for _, v := range vals {
arcgj, err := NewArchivedGamejam(v)
if err == nil {
arc.Jams = append(arc.Jams, *arcgj)
}
}
return arc, nil
}
func (m *model) SaveArchive() error {
var err error
if err = m.openDB(); err != nil {
return err
}
defer m.closeDB()
fmt.Println("Saving Archive")
for k, v := range m.archive.Jams {
fmt.Printf("> %d. %s\n", k, v.UUID)
m.bolt.SetValue([]string{"archive"}, strconv.Itoa(k), v.UUID)
}
return nil
}
func (m *model) ArchiveCurrentJam() error {
gj := new(ArchivedGamejam)
if m.jam.UUID == "" {
m.jam.UUID = uuid.New()
}
gj.UUID = m.jam.UUID
gj.Name = m.jam.Name
// We save the teams to the archive in their ranked order
for k := range m.jam.Teams {
gj.Teams = append(gj.Teams, m.jam.Teams[k])
}
for k := range m.jam.Votes {
gj.Votes = append(gj.Votes, m.jam.Votes[k])
}
rankings := getCondorcetResult()
for _, v := range rankings {
for _, tv := range v.Teams {
gj.Rankings = append(gj.Rankings, tv.UUID)
}
}
err := gj.Save()
if err != nil {
return err
}
m.archive.Jams = append(m.archive.Jams, *gj)
// Now we need to clear the current jam
m.jam = NewGamejam(m)
// Delete the Teams/Votes buckets from the jam
if err := m.openDB(); err != nil {
return err
}
defer m.closeDB()
if err := m.bolt.DeleteBucket([]string{"jam"}, "teams"); err != nil {
return err
}
if err := m.bolt.DeleteBucket([]string{"jam"}, "votes"); err != nil {
return err
}
return m.saveChanges()
}
type ArchivedGamejam struct {
UUID string
Name string
Date time.Time
Rankings []string
Teams []Team
Votes []Vote
}
func NewArchivedGamejam(uuid string) (*ArchivedGamejam, error) {
gj := new(ArchivedGamejam)
gj.UUID = uuid
bolt, err := boltease.Create(DataDir+"/gamejam_"+uuid+".db", 0600, nil)
defer bolt.CloseDB()
if err != nil {
return nil, err
}
gj.Name, err = bolt.GetValue([]string{"jam"}, "name")
if err != nil {
return nil, err
}
// Now load in all of the teams
var tmUUIDs []string
if tmUUIDs, err = bolt.GetBucketList([]string{"jam", "teams"}); err != nil {
return nil, err
}
for _, v := range tmUUIDs {
tm, err := gj.LoadTeam(bolt, v)
if err != nil {
return nil, err
}
gj.Teams = append(gj.Teams, *tm)
}
// Now load the votes
gj.Votes = gj.LoadAllVotes(bolt)
// And finally, the Rankings
var ranks []string
if ranks, err = bolt.GetKeyList([]string{"jam", "rankings"}); err != nil {
return nil, err
}
for _, v := range ranks {
var tmUUID string
tmUUID, err = bolt.GetValue([]string{"jam", "rankings"}, v)
if err != nil {
return nil, err
}
gj.Rankings = append(gj.Rankings, tmUUID)
}
// We could pull votes too... But I'm not right now.
return gj, nil
}
func (a *ArchivedGamejam) LoadTeam(openbolt *boltease.DB, uuid string) (*Team, error) {
tm := NewTeam(uuid)
var err error
if tm.Name, err = openbolt.GetValue(tm.mPath, "name"); err != nil {
return nil, errors.New("Error loading team: " + err.Error())
}
// Load the Team Members
var memberUuids []string
mbrsPath := append(tm.mPath, "members")
if memberUuids, err = openbolt.GetBucketList(mbrsPath); err == nil {
for _, v := range memberUuids {
mbr, err := NewTeamMember(uuid, v)
if err != nil {
return nil, err
}
if mbr.Name, err = openbolt.GetValue(mbr.mPath, "name"); err != nil {
return nil, errors.New("Error loading team member: " + err.Error())
}
tm.Members = append(tm.Members, *mbr)
}
}
// Load the Team's Game
tm.Game, err = NewGame(uuid)
if err != nil {
return nil, err
}
if tm.Game.Name, err = openbolt.GetValue(tm.Game.mPath, "name"); err != nil {
tm.Game.Name = ""
}
if tm.Game.Link, err = openbolt.GetValue(tm.Game.mPath, "link"); err != nil {
tm.Game.Link = ""
}
return tm, nil
}
// LoadAllVotes loads all votes for the jam out of the database
func (a *ArchivedGamejam) LoadAllVotes(openbolt *boltease.DB) []Vote {
var err error
var ret []Vote
votesPath := []string{"jam", "votes"}
var cliUUIDs []string
if cliUUIDs, err = openbolt.GetBucketList(votesPath); err != nil {
return ret
}
for _, cId := range cliUUIDs {
vtsPth := append(votesPath, cId)
var times []string
if times, err = openbolt.GetBucketList(vtsPth); err != nil {
// Error reading this bucket, move on to the next
continue
}
for _, t := range times {
fmt.Println("Loading Vote", cId, t)
if vt, err := a.LoadVote(openbolt, cId, t); err == nil {
ret = append(ret, *vt)
}
}
}
return ret
}
// Load a vote from the DB and return it
func (a *ArchivedGamejam) LoadVote(openbolt *boltease.DB, clientId, t string) (*Vote, error) {
var tm time.Time
var err error
if tm, err = time.Parse(time.RFC3339, t); err != nil {
return nil, errors.New("Error loading vote: " + err.Error())
}
vt, err := NewVote(clientId, tm)
if err != nil {
return nil, errors.New("Error creating vote: " + err.Error())
}
var choices []string
if choices, err = openbolt.GetKeyList(vt.mPath); err != nil {
return nil, errors.New("Error creating vote: " + err.Error())
}
for _, v := range choices {
ch := new(GameChoice)
var rank int
if rank, err = strconv.Atoi(v); err == nil {
ch.Rank = rank
if ch.Team, err = openbolt.GetValue(vt.mPath, v); err == nil {
vt.Choices = append(vt.Choices, *ch)
}
}
}
if vt.VoterStatus, err = openbolt.GetValue(vt.mPath, "voterstatus"); err != nil {
vt.VoterStatus = ""
}
if vt.Discovery, err = openbolt.GetValue(vt.mPath, "discovery"); err != nil {
vt.Discovery = ""
}
return vt, nil
}
func (a *ArchivedGamejam) Save() error {
bolt, err := boltease.Create(DataDir+"/gamejam_"+a.UUID+".db", 0600, nil)
defer bolt.CloseDB()
if err != nil {
return err
}
// Gamejam info
if err := bolt.SetValue([]string{"jam"}, "uuid", a.UUID); err != nil {
return err
}
if err := bolt.SetValue([]string{"jam"}, "name", a.Name); err != nil {
return err
}
// Teams info
for _, tm := range a.Teams {
if err := bolt.SetValue(tm.mPath, "name", tm.Name); err != nil {
return err
}
for _, mbr := range tm.Members {
if err = bolt.SetValue(mbr.mPath, "name", mbr.Name); err != nil {
return err
}
if err = bolt.SetValue(mbr.mPath, "slackid", mbr.SlackId); err != nil {
return err
}
if err = bolt.SetValue(mbr.mPath, "twitter", mbr.Twitter); err != nil {
return err
}
if err = bolt.SetValue(mbr.mPath, "email", mbr.Email); err != nil {
return err
}
}
// The team's game
gm := tm.Game
if err := bolt.MkBucketPath(gm.mPath); err != nil {
return err
}
if err := bolt.SetValue(gm.mPath, "name", gm.Name); err != nil {
return err
}
if err := bolt.SetValue(gm.mPath, "link", gm.Link); err != nil {
return err
}
if err := bolt.SetValue(gm.mPath, "description", gm.Description); err != nil {
return err
}
if err := bolt.SetValue(gm.mPath, "framework", gm.Framework); err != nil {
return err
}
// Save screenshots
if err := bolt.MkBucketPath(append(gm.mPath, "screenshots")); err != nil {
return err
}
for _, ss := range gm.Screenshots {
if err = bolt.MkBucketPath(ss.mPath); err != nil {
return err
}
if err = bolt.SetValue(ss.mPath, "description", ss.Description); err != nil {
return err
}
if err = bolt.SetValue(ss.mPath, "image", ss.Image); err != nil {
return err
}
if err = bolt.SetValue(ss.mPath, "filetype", ss.Filetype); err != nil {
return err
}
}
}
// All teams are archived
// Move on to votes
for _, vt := range a.Votes {
for _, v := range vt.Choices {
bolt.SetValue(vt.mPath, strconv.Itoa(v.Rank), v.Team)
}
bolt.SetValue(vt.mPath, "voterstatus", vt.VoterStatus)
bolt.SetValue(vt.mPath, "discovery", vt.Discovery)
}
// And the rankings
if err = bolt.MkBucketPath([]string{"jam", "rankings"}); err != nil {
return err
}
for kr, vr := range a.Rankings {
if err = bolt.SetValue([]string{"jam", "rankings"}, strconv.Itoa(kr), vr); err != nil {
return err
}
}
return nil
}