ictgj-voting/model.go

140 lines
3.0 KiB
Go
Raw Normal View History

2017-04-03 21:32:13 +00:00
package main
2017-06-08 17:20:43 +00:00
import (
"errors"
"fmt"
2017-06-08 17:20:43 +00:00
"github.com/br0xen/boltease"
)
2017-04-03 21:32:13 +00:00
2017-10-11 23:03:27 +00:00
// 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
2017-10-10 12:39:42 +00:00
2017-10-11 23:03:27 +00:00
site *siteData // Configuration data for the site
jam *Gamejam // The currently active gamejam
clients []Client // Web clients that have connected to the server
2017-10-19 19:53:07 +00:00
clientsUpdated bool
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
// 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)
m.dbFileName = 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
2017-10-19 19:53:07 +00:00
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
}
2017-10-11 23:03:27 +00:00
// Load the jam data
2017-10-19 19:53:07 +00:00
if m.jam, err = m.LoadCurrentJam(); err != nil {
return nil, errors.New("Unable to load current jam: " + err.Error())
}
2017-10-10 12:39:42 +00:00
2017-10-11 23:03:27 +00:00
// Load web clients
m.clients = m.LoadAllClients()
2017-10-19 19:53:07 +00:00
return m, nil
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
func (m *model) openDB() error {
m.dbOpened += 1
2017-10-19 19:53:07 +00:00
if m.dbOpened == 1 {
2017-04-03 21:32:13 +00:00
var err error
2017-10-11 23:03:27 +00:00
m.bolt, err = boltease.Create(m.dbFileName, 0600, nil)
2017-04-03 21:32:13 +00:00
if err != nil {
return err
}
2017-06-12 23:23:46 +00:00
}
return nil
}
2017-10-11 23:03:27 +00:00
func (m *model) closeDB() error {
m.dbOpened -= 1
if m.dbOpened == 0 {
return m.bolt.CloseDB()
2017-04-03 21:32:13 +00:00
}
return nil
}
2017-10-11 23:03:27 +00:00
func (m *model) initDB() error {
2017-06-12 23:23:46 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
2017-06-12 23:23:46 +00:00
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
2017-06-12 23:23:46 +00:00
2017-04-21 18:17:18 +00:00
// Create the path to the bucket to store admin users
2017-10-11 23:03:27 +00:00
if err = m.bolt.MkBucketPath([]string{"users"}); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the web clients
if err = m.bolt.MkBucketPath([]string{"clients"}); err != nil {
2017-04-21 18:17:18 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the current jam & teams
if err = m.bolt.MkBucketPath([]string{"jam", "teams"}); err != nil {
return err
2017-10-10 12:39:42 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store the list of archived jams
if err = m.bolt.MkBucketPath([]string{"archive"}); err != nil {
2017-10-10 12:39:42 +00:00
return err
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// Create the path to the bucket to store site config data
return m.bolt.MkBucketPath([]string{"site"})
2017-04-03 21:32:13 +00:00
}
2017-10-11 23:03:27 +00:00
// saveChanges saves any parts of the model that have been flagged as changed to the database
func (m *model) saveChanges() error {
2017-04-03 21:32:13 +00:00
var err error
2017-10-11 23:03:27 +00:00
if err = m.openDB(); err != nil {
return err
}
2017-10-11 23:03:27 +00:00
defer m.closeDB()
//if m.site.NeedsSave() {
fmt.Println("Saving Site data to DB")
if err = m.site.SaveToDB(); err != nil {
return err
}
//}
//if m.jam.IsChanged {
fmt.Println("Saving Jam data to DB")
if err = m.jam.SaveToDB(); err != nil {
return err
}
m.jam.IsChanged = false
//}
//if m.clientsUpdated {
fmt.Println("Saving Client data to DB")
if err = m.SaveAllClients(); err != nil {
return err
2017-10-18 22:18:12 +00:00
}
m.clientsUpdated = false
//}
2017-10-11 23:03:27 +00:00
return nil
}