From f57d917297becd204b87fff514056c5e8587609a Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 2 Oct 2017 08:44:55 -0500 Subject: [PATCH] Started working on archive gamejam dbs --- .gitignore | 4 +-- model_gamejam.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 model_gamejam.go diff --git a/.gitignore b/.gitignore index 1b169a6..2eacb04 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,5 @@ gjvote.linuxarm gjvote.win386.exe gjvote.win64.exe -# Ignore the DB -gjvote.db +# Ignore the DBs +*.db diff --git a/model_gamejam.go b/model_gamejam.go new file mode 100644 index 0000000..45b900f --- /dev/null +++ b/model_gamejam.go @@ -0,0 +1,86 @@ +package main + +import ( + "time" + + "github.com/br0xen/boltease" +) + +type Gamejam struct { + UUID string + Name string + Date time.Time + Teams []Team + Votes []Vote + + db *boltease.DB + dbOpened int +} + +// Archived Gamejam data is stored in it's own file to keep things nice and organized +func (gj *Gamejam) openDB() error { + gj.dbOpened += 1 + if gj.dbOpened == 1 { + var err error + gj.db, err = boltease.Create(gj.UUID+".db", 0600, nil) + if err != nil { + return err + } + } + return nil +} + +func (gj *Gamejam) closeDB() error { + gj.dbOpened -= 1 + if gj.dbOpened == 0 { + return gj.db.CloseDB() + } + return nil +} + +// archiveGameJam creates a separate gamejam file and populates it with the +// given name, teams, and votes +func archiveGamejam(nm string, teams []Team, votes []Vote) error { + // TODO + return nil +} + +// dbGetGamejam returns a gamejam with the given uuid +// or nil if it couldn't be found +func dbGetGamejam(id string) *Gamejam { + var err error + if err = openDatabase(); err != nil { + return nil + } + defer closeDatabase() + + ret := Gamejam{UUID: id} + // TODO: Load gamejam teams, other details + return ret +} + +// dbGetGamejamByName looks for a gamejam with the given name +// and returns it, or it returns nil if it couldn't find it +func dbGetGamejamByName(nm string) *Gamejam { + var err error + if err = openDatabase(); err != nil { + return err + } + defer closeDatabase() + + var gjid string + if gjs, err = db.GetBucketList([]string{"gamejams"}); err == nil { + for _, v := range gjUids { + tstNm, _ := db.GetValue([]string{"gamejams", v}, "name") + if tstNm == nm { + // We've got it + gjid = v + break + } + } + } + if gjid == "" { + return nil + } + return dbGetGamejam(gjid) +}