-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.go
147 lines (127 loc) · 3.37 KB
/
model.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
package main
import (
"errors"
"fmt"
"os"
"github.com/br0xen/boltease"
)
// model stores the current jam in memory, and has the ability to access archived dbs
type model struct {
bolt *boltease.DB
dbOpened int
dbFileName string
site *siteData // Configuration data for the site
jam *Gamejam // The currently active gamejam
clients []Client // Web clients that have connected to the server
archive *Archive // The archive of past game jams
clientsUpdated bool
}
// Update Flags: Which parts of the model need to be updated
const (
UpdateSiteData = iota
UpdateJamData
)
func NewModel() (*model, error) {
var err error
m := new(model)
// make sure the data directory exists
if err = os.MkdirAll(DataDir, os.ModePerm); err != nil {
return nil, errors.New("Unable to create Data Directory: " + err.Error())
}
m.dbFileName = DataDir + "/" + DbName
if err = m.openDB(); err != nil {
return nil, errors.New("Unable to open DB: " + err.Error())
}
defer m.closeDB()
// Initialize the DB
if err = m.initDB(); err != nil {
return nil, errors.New("Unable to initialize DB: " + err.Error())
}
// Load the site data
m.site = NewSiteData(m)
if err = m.site.LoadFromDB(); err != nil {
// Error loading from the DB, set to defaults
def := NewSiteData(m)
m.site = def
}
// Load the jam data
if m.jam, err = m.LoadCurrentJam(); err != nil {
return nil, errors.New("Unable to load current jam: " + err.Error())
}
// Load web clients
m.clients = m.LoadAllClients()
// Load the archives
if m.archive, err = m.LoadArchive(); err != nil {
return nil, errors.New("Unable to load game jam archive: " + err.Error())
}
return m, nil
}
func (m *model) openDB() error {
m.dbOpened += 1
if m.dbOpened == 1 {
var err error
m.bolt, err = boltease.Create(m.dbFileName, 0600, nil)
if err != nil {
return err
}
}
return nil
}
func (m *model) closeDB() error {
m.dbOpened -= 1
if m.dbOpened == 0 {
return m.bolt.CloseDB()
}
return nil
}
func (m *model) initDB() error {
var err error
if err = m.openDB(); err != nil {
return err
}
defer m.closeDB()
// Create the path to the bucket to store admin users
if err = m.bolt.MkBucketPath([]string{"users"}); err != nil {
return err
}
// Create the path to the bucket to store the web clients
if err = m.bolt.MkBucketPath([]string{"clients"}); err != nil {
return err
}
// Create the path to the bucket to store the current jam & teams
if err = m.bolt.MkBucketPath([]string{"jam", "teams"}); err != nil {
return err
}
// Create the path to the bucket to store the list of archived jams
if err = m.bolt.MkBucketPath([]string{"archive"}); err != nil {
return err
}
// Create the path to the bucket to store site config data
return m.bolt.MkBucketPath([]string{"site"})
}
// saveChanges saves any parts of the model that have been flagged as changed to the database
func (m *model) saveChanges() error {
var err error
if err = m.openDB(); err != nil {
return err
}
defer m.closeDB()
fmt.Println("Saving Site data to DB")
if err = m.site.SaveToDB(); err != nil {
return err
}
fmt.Println("Saving Jam data to DB")
if err = m.jam.SaveToDB(); err != nil {
return err
}
m.jam.IsChanged = false
fmt.Println("Saving Client data to DB")
if err = m.SaveAllClients(); err != nil {
return err
}
m.clientsUpdated = false
if err = m.SaveArchive(); err != nil {
return err
}
return nil
}