ictgj-voting/model_games.go

293 lines
6.4 KiB
Go
Raw Normal View History

2017-04-21 18:17:18 +00:00
package main
2017-10-19 19:53:07 +00:00
import (
"errors"
"fmt"
2017-10-19 19:53:07 +00:00
"github.com/pborman/uuid"
)
2017-10-18 22:18:12 +00:00
2017-10-11 23:03:27 +00:00
/**
* Game
* A team's game, including links, description, and screenshots
*/
2017-04-21 18:17:18 +00:00
type Game struct {
2017-06-15 17:35:53 +00:00
Name string
TeamId string
Link string
2017-06-15 17:35:53 +00:00
Description string
Screenshots []Screenshot
2017-10-12 13:51:53 +00:00
mPath []string // The path in the DB to this game
}
// Create a new game object
2017-10-19 19:53:07 +00:00
func NewGame(tmId string) (*Game, error) {
if tmId == "" {
return nil, errors.New("Team ID is required")
}
2017-10-12 13:51:53 +00:00
return &Game{
TeamId: tmId,
mPath: []string{"jam", "teams", tmId, "game"},
2017-10-19 19:53:07 +00:00
}, nil
2017-10-12 13:51:53 +00:00
}
2017-10-19 19:53:07 +00:00
func (gm *Game) GetScreenshot(ssId string) (*Screenshot, error) {
2017-10-12 13:51:53 +00:00
for _, ss := range gm.Screenshots {
if ss.UUID == ssId {
2017-10-19 19:53:07 +00:00
return &ss, nil
}
}
return nil, errors.New("Invalid Id")
}
func (gm *Game) RemoveScreenshot(ssId string) error {
idx := -1
for i, ss := range gm.Screenshots {
if ss.UUID == ssId {
idx = i
2017-10-12 13:51:53 +00:00
}
}
2017-10-19 19:53:07 +00:00
if idx < 0 {
return errors.New("Invalid Id")
}
fmt.Print("Removing Screenshot (", ssId, ") (IDX:", idx, ")\n")
2017-10-19 19:53:07 +00:00
gm.Screenshots = append(gm.Screenshots[:idx], gm.Screenshots[idx+1:]...)
2017-10-12 13:51:53 +00:00
return nil
2017-06-15 17:35:53 +00:00
}
type Screenshot struct {
2017-07-14 12:28:09 +00:00
UUID string
2017-06-15 17:35:53 +00:00
Description string
Image string
2017-07-14 12:28:09 +00:00
Thumbnail string
Filetype string
2017-10-12 13:51:53 +00:00
mPath []string // The path in the DB to this screenshot
}
// Create a Screenshot Object
2017-10-19 19:53:07 +00:00
func NewScreenshot(tmId, ssId string) (*Screenshot, error) {
if tmId == "" {
return nil, errors.New("Team ID is required")
}
if ssId == "" {
// Generate a new UUID
ssId = uuid.New()
}
2017-10-12 13:51:53 +00:00
return &Screenshot{
UUID: ssId,
mPath: []string{"jam", "teams", tmId, "game", "screenshots", ssId},
2017-10-19 19:53:07 +00:00
}, nil
2017-04-21 18:17:18 +00:00
}
2017-06-08 20:43:11 +00:00
2017-10-18 22:18:12 +00:00
/**
* DB Functions
* These are generally just called when the app starts up, or when the periodic 'save' runs
*/
2017-10-11 23:03:27 +00:00
// Load a team's game from the DB and return it
2017-10-19 19:53:07 +00:00
func (gj *Gamejam) LoadTeamGame(tmId string) (*Game, error) {
2017-10-11 23:03:27 +00:00
var err error
if err = gj.m.openDB(); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
defer gj.m.closeDB()
2017-10-19 19:53:07 +00:00
gm, err := NewGame(tmId)
if err != nil {
return nil, err
}
2017-10-12 13:51:53 +00:00
if gm.Name, err = gj.m.bolt.GetValue(gm.mPath, "name"); err != nil {
2017-10-11 23:03:27 +00:00
gm.Name = ""
}
2017-10-12 13:51:53 +00:00
if gm.Description, err = gj.m.bolt.GetValue(gm.mPath, "description"); err != nil {
2017-10-11 23:03:27 +00:00
gm.Description = ""
}
2017-10-12 13:51:53 +00:00
if gm.Link, err = gj.m.bolt.GetValue(gm.mPath, "link"); err != nil {
2017-10-11 23:03:27 +00:00
gm.Link = ""
}
2017-10-19 19:53:07 +00:00
2017-10-11 23:03:27 +00:00
// Now get the game screenshots
gm.Screenshots = gj.LoadTeamGameScreenshots(tmId)
2017-10-19 19:53:07 +00:00
return gm, nil
2017-10-11 23:03:27 +00:00
}
2017-10-12 13:51:53 +00:00
// Load a games screenshots from the DB
2017-10-11 23:03:27 +00:00
func (gj *Gamejam) LoadTeamGameScreenshots(tmId string) []Screenshot {
var err error
if err = gj.m.openDB(); err != nil {
return nil
}
defer gj.m.closeDB()
var ret []Screenshot
2017-10-19 19:53:07 +00:00
gm, err := NewGame(tmId)
if err != nil {
return ret
}
2017-10-12 13:51:53 +00:00
ssBktPath := append(gm.mPath, "screenshots")
2017-10-11 23:03:27 +00:00
var ssIds []string
ssIds, _ = gj.m.bolt.GetBucketList(ssBktPath)
for _, v := range ssIds {
2017-10-19 19:53:07 +00:00
ssLd, _ := gj.LoadTeamGameScreenshot(tmId, v)
2017-10-11 23:03:27 +00:00
if ssLd != nil {
2017-10-19 19:53:07 +00:00
ret = append(ret, *ssLd)
2017-10-11 23:03:27 +00:00
}
}
return ret
}
2017-10-12 13:51:53 +00:00
// Load a screenshot from the DB
2017-10-19 19:53:07 +00:00
func (gj *Gamejam) LoadTeamGameScreenshot(tmId, ssId string) (*Screenshot, error) {
2017-10-11 23:03:27 +00:00
var err error
if err = gj.m.openDB(); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
defer gj.m.closeDB()
2017-10-19 19:53:07 +00:00
ret, err := NewScreenshot(tmId, ssId)
if err != nil {
return nil, err
}
2017-10-12 13:51:53 +00:00
if ret.Description, err = gj.m.bolt.GetValue(ret.mPath, "description"); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
2017-10-12 13:51:53 +00:00
if ret.Image, err = gj.m.bolt.GetValue(ret.mPath, "image"); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
2017-10-12 13:51:53 +00:00
if ret.Thumbnail, err = gj.m.bolt.GetValue(ret.mPath, "thumbnail"); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
if ret.Thumbnail == "" {
ret.Thumbnail = ret.Image
}
2017-10-12 13:51:53 +00:00
if ret.Filetype, err = gj.m.bolt.GetValue(ret.mPath, "filetype"); err != nil {
2017-10-19 19:53:07 +00:00
return nil, err
2017-10-11 23:03:27 +00:00
}
2017-10-19 19:53:07 +00:00
return ret, err
2017-10-11 23:03:27 +00:00
}
2017-10-18 22:18:12 +00:00
// Save a game to the DB
2017-10-12 13:51:53 +00:00
func (gj *Gamejam) SaveGame(gm *Game) error {
2017-06-15 17:35:53 +00:00
var err error
2017-10-12 13:51:53 +00:00
if err = gj.m.openDB(); err != nil {
return err
2017-06-15 17:35:53 +00:00
}
2017-10-12 13:51:53 +00:00
defer gj.m.closeDB()
2017-06-15 17:35:53 +00:00
2017-10-12 13:51:53 +00:00
if err := gj.m.bolt.MkBucketPath(gm.mPath); err != nil {
return err
2017-06-15 17:35:53 +00:00
}
2017-10-19 19:53:07 +00:00
var tm *Team
if tm, err = gj.GetTeamById(gm.TeamId); err != nil {
return err
}
2017-10-12 13:51:53 +00:00
if gm.Name == "" {
gm.Name = tm.Name + "'s Game"
2017-10-11 23:03:27 +00:00
}
2017-10-12 13:51:53 +00:00
if err := gj.m.bolt.SetValue(gm.mPath, "name", gm.Name); err != nil {
return err
}
if err := gj.m.bolt.SetValue(gm.mPath, "link", gm.Link); err != nil {
return err
}
if err := gj.m.bolt.SetValue(gm.mPath, "description", gm.Description); err != nil {
return err
}
if err := gj.m.bolt.MkBucketPath(append(gm.mPath, "screenshots")); err != nil {
return err
}
return gj.SaveScreenshots(gm)
2017-10-11 23:03:27 +00:00
}
2017-10-12 13:51:53 +00:00
// Save all of the game's screenshots to the DB
// Remove screenshots from the DB that aren't in the game object
func (gj *Gamejam) SaveScreenshots(gm *Game) error {
2017-06-15 17:35:53 +00:00
var err error
2017-10-12 13:51:53 +00:00
if err = gj.m.openDB(); err != nil {
return err
2017-06-15 17:35:53 +00:00
}
2017-10-12 13:51:53 +00:00
defer gj.m.closeDB()
2017-06-15 17:35:53 +00:00
2017-10-12 13:51:53 +00:00
for _, ss := range gm.Screenshots {
2017-10-19 19:53:07 +00:00
if err = gj.SaveScreenshot(&ss); err != nil {
2017-10-12 13:51:53 +00:00
return err
}
2017-07-14 12:28:09 +00:00
}
2017-10-12 13:51:53 +00:00
// Now remove unused screenshots
ssPath := append(gm.mPath, "screenshots")
2017-10-19 19:53:07 +00:00
var ssIds []string
2017-10-12 13:51:53 +00:00
if ssIds, err = gj.m.bolt.GetBucketList(ssPath); err != nil {
return err
2017-06-15 17:35:53 +00:00
}
2017-10-12 13:51:53 +00:00
for i := range ssIds {
2017-10-19 19:53:07 +00:00
ss, _ := gm.GetScreenshot(ssIds[i])
if ss != nil {
// A valid screenshot, next
continue
}
if ss, err = NewScreenshot(gm.TeamId, ssIds[i]); err != nil {
// Error building screenshot to delete...
continue
}
if err = gj.DeleteScreenshot(ss); err != nil {
return err
2017-10-12 13:51:53 +00:00
}
}
2017-10-19 19:53:07 +00:00
return nil
2017-10-12 13:51:53 +00:00
}
2017-06-15 17:35:53 +00:00
2017-10-12 13:51:53 +00:00
// Save a screenshot
2017-10-19 19:53:07 +00:00
func (gj *Gamejam) SaveScreenshot(ss *Screenshot) error {
2017-10-12 13:51:53 +00:00
var err error
if err = gj.m.openDB(); err != nil {
return err
2017-06-15 17:35:53 +00:00
}
2017-10-12 13:51:53 +00:00
defer gj.m.closeDB()
if err = gj.m.bolt.MkBucketPath(ss.mPath); err != nil {
2017-06-15 17:35:53 +00:00
return err
}
2017-10-12 13:51:53 +00:00
if err = gj.m.bolt.SetValue(ss.mPath, "description", ss.Description); err != nil {
2017-06-15 17:35:53 +00:00
return err
}
2017-10-12 13:51:53 +00:00
if err = gj.m.bolt.SetValue(ss.mPath, "image", ss.Image); err != nil {
2017-07-14 12:28:09 +00:00
return err
}
2017-10-12 13:51:53 +00:00
if err = gj.m.bolt.SetValue(ss.mPath, "filetype", ss.Filetype); err != nil {
2017-07-14 12:28:09 +00:00
return err
}
2017-10-12 13:51:53 +00:00
return nil
}
// Delete a screenshot
func (gj *Gamejam) DeleteScreenshot(ss *Screenshot) error {
var err error
if err = gj.m.openDB(); err != nil {
return nil
}
defer gj.m.closeDB()
2017-10-12 13:51:53 +00:00
ssPath := ss.mPath[:len(ss.mPath)-1]
return gj.m.bolt.DeleteBucket(ssPath, ss.UUID)
2017-06-15 17:35:53 +00:00
}
2017-10-18 22:18:12 +00:00
/**
* In Memory functions
* This is generally how the app accesses client data
*/
// Set the given team's game to gm
func (gj *Gamejam) UpdateGame(tmId string, gm *Game) error {
2017-10-19 19:53:07 +00:00
tm, err := gj.GetTeamById(tmId)
if err != nil {
return errors.New("Error getting team: " + err.Error())
2017-10-18 22:18:12 +00:00
}
tm.Game = gm
2017-10-19 19:53:07 +00:00
gj.IsChanged = true
2017-10-18 22:18:12 +00:00
return nil
}