-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_gamejam.go
92 lines (78 loc) · 1.82 KB
/
model_gamejam.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
package main
import (
"errors"
"fmt"
"strings"
"time"
)
/**
* Gamejam
* Gamejam is the struct for any gamejam (current or archived)
*/
type Gamejam struct {
UUID string
Name string
Date time.Time
Teams []Team
Votes []Vote
m *model // The model that holds this gamejam's data
mPath []string // The path in the db to this gamejam
IsChanged bool // Flag to tell if we need to update the db
}
func NewGamejam(m *model) *Gamejam {
gj := new(Gamejam)
gj.Name = time.Now().Format("2006-01-02T15:04:05 Game Jam")
gj.m = m
gj.mPath = []string{"jam"}
return gj
}
/**
* DB Functions
* These are generally just called when the app starts up, or when the periodic 'save' runs
*/
func (m *model) LoadCurrentJam() (*Gamejam, error) {
if err := m.openDB(); err != nil {
return nil, err
}
defer m.closeDB()
gj := NewGamejam(m)
gj.Name, _ = m.bolt.GetValue(gj.mPath, "name")
// Load all teams
gj.Teams = gj.LoadAllTeams()
// Load all votes
gj.Votes = gj.LoadAllVotes()
return gj, nil
}
// Save everything to the DB whether it's flagged as changed or not
func (gj *Gamejam) SaveToDB() error {
if err := gj.m.openDB(); err != nil {
return err
}
defer gj.m.closeDB()
var errs []error
if err := gj.m.bolt.SetValue(gj.mPath, "name", gj.Name); err != nil {
errs = append(errs, err)
}
// Save all Teams
for _, tm := range gj.Teams {
fmt.Println("Saving Team " + tm.Name + " data to DB")
if err := gj.SaveTeam(&tm); err != nil {
errs = append(errs, err)
}
}
// Save all Votes
for _, vt := range gj.Votes {
if err := gj.SaveVote(&vt); err != nil {
errs = append(errs, err)
}
}
if len(errs) > 0 {
var errTxt string
for i := range errs {
errTxt = errTxt + errs[i].Error() + "\n"
}
errTxt = strings.TrimSpace(errTxt)
return errors.New("Error(s) saving to DB: " + errTxt)
}
return nil
}