From cdd0ce6a44e2aa65c3913512ece03711848b8412 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 12 Oct 2017 08:51:53 -0500 Subject: [PATCH] Switching dev machines --- model_clients.go | 53 ++++++++------ model_gamejam.go | 11 +-- model_games.go | 182 +++++++++++++++++++++++++++++++---------------- model_teams.go | 164 ++++++++++++++---------------------------- 4 files changed, 214 insertions(+), 196 deletions(-) diff --git a/model_clients.go b/model_clients.go index 2d60e9e..c3d8af3 100644 --- a/model_clients.go +++ b/model_clients.go @@ -15,6 +15,15 @@ type Client struct { Auth bool Name string IP string + + mPath []string // The path in the DB to this client +} + +func NewClient(id string) *Client { + return &Client{ + UUID: id, + mPath: []string{"clients", id}, + } } // Load all clients @@ -26,7 +35,8 @@ func (m *model) LoadAllClients() []Client { defer m.closeDB() var clientUids []string - if clientUids, err = m.bolt.GetBucketList([]string{"clients"}); err != nil { + cliPath := []string{"clients"} + if clientUids, err = m.bolt.GetBucketList(cliPath); err != nil { return err } for _, v := range clientUids { @@ -44,11 +54,10 @@ func (m *model) LoadClient(clId string) *Client { } defer m.closeDB() - cl := new(Client) - cl.UUID = id - cl.Auth, _ = m.bolt.GetBool([]string{"clients", id}, "auth") - cl.Name, _ = m.bolt.GetValue([]string{"clients", id}, "name") - cl.IP, _ = m.bolt.GetValue([]string{"clients", id}, "ip") + cl := NewClient(clId) + cl.Auth, _ = m.bolt.GetBool(cl.mPath, "auth") + cl.Name, _ = m.bolt.GetValue(cl.mPath, "name") + cl.IP, _ = m.bolt.GetValue(cl.mPath, "ip") return cl } @@ -61,25 +70,25 @@ func (m *model) getClientById(ip string) *Client { return nil } +func (m *model) SaveClient(cl *Client) error { + var err error + if err = m.openDB(); err != nil { + return nil + } + defer m.closeDB() + + if err = db.bolt.SetBool(cl.mPath, "auth", c.Auth); err != nil { + return err + } + if err = db.bolt.SetValue(cl.mPath, "name", c.Name); err != nil { + return err + } + return db.bolt.SetValue(cl.mPath, "ip", c.IP) +} + /** * OLD FUNCTIONS */ -func (c *Client) save() error { - var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - if err = db.bolt.SetBool([]string{"clients", c.UUID}, "auth", c.Auth); err != nil { - return err - } - if err = db.bolt.SetValue([]string{"clients", c.UUID}, "name", c.Name); err != nil { - return err - } - return db.bolt.SetValue([]string{"clients", c.UUID}, "ip", c.IP) -} - func (c *Client) saveVote(timestamp time.Time, votes []string) error { var err error if err = db.open(); err != nil { diff --git a/model_gamejam.go b/model_gamejam.go index 82ef444..56defb0 100644 --- a/model_gamejam.go +++ b/model_gamejam.go @@ -13,13 +13,15 @@ type Gamejam struct { Teams []Team Votes []Vote - m *model + m *model // The model that holds this gamejam's data + mPath []string // The path in the db to this gamejam updates []string } func NewGamejam(m *model) *Gamejam { gj := new(Gamejam) gj.m = m + gj.mPath = []string{"jam"} return gj } @@ -30,9 +32,8 @@ func (m *model) LoadCurrentJam() *Gamejam { defer m.closeDB() var err error - jamPath := []string{"jam"} gj := NewGamejam(m) - gj.Name, _ = m.bolt.GetValue(jamPath, "name") + gj.Name, _ = m.bolt.GetValue(gj.mPath, "name") // Load all teams gj.Teams = gj.LoadAllTeams() @@ -57,10 +58,10 @@ func (gj *Gamejam) needsSave() bool { } func (gj *Gamejam) saveToDB() error { - if err := s.m.openDB(); err != nil { + if err := gj.m.openDB(); err != nil { return err } - defer s.m.closeDB() + defer gj.m.closeDB() for i := range updates { // TODO: Save diff --git a/model_games.go b/model_games.go index 6d325ae..e03ca80 100644 --- a/model_games.go +++ b/model_games.go @@ -1,7 +1,5 @@ package main -import "errors" - /** * Game * A team's game, including links, description, and screenshots @@ -12,6 +10,25 @@ type Game struct { Link string Description string Screenshots []Screenshot + + mPath []string // The path in the DB to this game +} + +// Create a new game object +func NewGame(tmId string) *Game { + return &Game{ + TeamId: tmId, + mPath: []string{"jam", "teams", tmId, "game"}, + } +} + +func (gm *Game) GetScreenshot(ssId string) *Screenshot { + for _, ss := range gm.Screenshots { + if ss.UUID == ssId { + return ss + } + } + return nil } type Screenshot struct { @@ -20,6 +37,16 @@ type Screenshot struct { Image string Thumbnail string Filetype string + + mPath []string // The path in the DB to this screenshot +} + +// Create a Screenshot Object +func NewScreenshot(tmId, ssId string) *Screenshot { + return &Screenshot{ + UUID: ssId, + mPath: []string{"jam", "teams", tmId, "game", "screenshots", ssId}, + } } // Load a team's game from the DB and return it @@ -30,16 +57,14 @@ func (gj *Gamejam) LoadTeamGame(tmId string) *Game { } defer gj.m.closeDB() - gamePath := []string{"jam", "teams", tmId, "game"} - gm := new(Game) - gm.TeamId = tm.UUID - if gm.Name, err = gj.m.bolt.GetValue(gamePath, "name"); err != nil { + gm := NewGame(tmId) + if gm.Name, err = gj.m.bolt.GetValue(gm.mPath, "name"); err != nil { gm.Name = "" } - if gm.Description, err = gj.m.bolt.GetValue(gamePath, "description"); err != nil { + if gm.Description, err = gj.m.bolt.GetValue(gm.mPath, "description"); err != nil { gm.Description = "" } - if gm.Link, err = gj.m.bolt.GetValue(gamePath, "link"); err != nil { + if gm.Link, err = gj.m.bolt.GetValue(gm.mPath, "link"); err != nil { gm.Link = "" } // Now get the game screenshots @@ -48,6 +73,7 @@ func (gj *Gamejam) LoadTeamGame(tmId string) *Game { return &gm } +// Load a games screenshots from the DB func (gj *Gamejam) LoadTeamGameScreenshots(tmId string) []Screenshot { var err error if err = gj.m.openDB(); err != nil { @@ -56,7 +82,8 @@ func (gj *Gamejam) LoadTeamGameScreenshots(tmId string) []Screenshot { defer gj.m.closeDB() var ret []Screenshot - ssBktPath := []string{"jam", "teams", tmId, "game", "screenshots"} + gm := NewGame(tmId) + ssBktPath := append(gm.mPath, "screenshots") var ssIds []string ssIds, _ = gj.m.bolt.GetBucketList(ssBktPath) for _, v := range ssIds { @@ -68,6 +95,7 @@ func (gj *Gamejam) LoadTeamGameScreenshots(tmId string) []Screenshot { return ret } +// Load a screenshot from the DB func (gj *Gamejam) LoadTeamGameScreenshot(tmId, ssId string) *Screenshot { var err error if err = gj.m.openDB(); err != nil { @@ -75,87 +103,121 @@ func (gj *Gamejam) LoadTeamGameScreenshot(tmId, ssId string) *Screenshot { } defer gj.m.closeDB() - var ret []Screenshot - ssPath := []string{"jam", "teams", tmId, "game", "screenshots", ssId} - ret := new(Screenshot) - ret.UUID = ssId - if ret.Description, err = gj.m.bolt.GetValue(ssPath, "description"); err != nil { + ret := NewScreenshot(tmId, ssId) + if ret.Description, err = gj.m.bolt.GetValue(ret.mPath, "description"); err != nil { return nil } - if ret.Image, err = gj.m.bolt.GetValue(ssPath, "image"); err != nil { + if ret.Image, err = gj.m.bolt.GetValue(ret.mPath, "image"); err != nil { return nil } - if ret.Thumbnail, err = gj.m.bolt.GetValue(ssPath, "thumbnail"); err != nil { + if ret.Thumbnail, err = gj.m.bolt.GetValue(ret.mPath, "thumbnail"); err != nil { return nil } if ret.Thumbnail == "" { ret.Thumbnail = ret.Image } - if ret.Filetype, err = gj.m.bolt.GetValue(ssPath, "filetype"); err != nil { + if ret.Filetype, err = gj.m.bolt.GetValue(ret.mPath, "filetype"); err != nil { return nil } return ret } -/** - * OLD FUNCTIONS - */ - -// Create a new game object, must have a valid team id -func (db *currJamDb) newGame(tmId string) *Game { +// Save a game to the given model's DB +func (gj *Gamejam) SaveGame(gm *Game) error { + //func (gm *Game) save(m *model) error { var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - tm := db.getTeam(tmId) - if tm == nil { - return nil - } - return &Game{TeamId: tmId} -} - -func (db *currJamDb) getAllGames() []Game { - var ret []Game - tms := db.getAllTeams() - for i := range tms { - ret = append(ret, *tms[i].getGame()) - } - return ret -} - -func (gm *Game) save() error { - var err error - if err = db.open(); err != nil { + if err = gj.m.openDB(); err != nil { return err } - defer db.close() + defer gj.m.closeDB() - tm := db.getTeam(gm.TeamId) - if tm == nil { - return errors.New("Invalid Team: " + gm.TeamId) - } - gamePath := []string{"teams", gm.TeamId, "game"} - if err := db.bolt.MkBucketPath(gamePath); err != nil { + /* + tm := gj.getTeam(gm.TeamId) + if tm == nil { + return errors.New("Invalid Team: " + gm.TeamId) + } + */ + if err := gj.m.bolt.MkBucketPath(gm.mPath); err != nil { return err } if gm.Name == "" { gm.Name = tm.Name + "'s Game" } - if err := db.bolt.SetValue(gamePath, "name", gm.Name); err != nil { + if err := gj.m.bolt.SetValue(gm.mPath, "name", gm.Name); err != nil { return err } - if err := db.bolt.SetValue(gamePath, "link", gm.Link); err != nil { + if err := gj.m.bolt.SetValue(gm.mPath, "link", gm.Link); err != nil { return err } - if err := db.bolt.SetValue(gamePath, "description", gm.Description); err != nil { + if err := gj.m.bolt.SetValue(gm.mPath, "description", gm.Description); err != nil { return err } - if err := db.bolt.MkBucketPath(append(gamePath, "screenshots")); err != nil { + if err := gj.m.bolt.MkBucketPath(append(gm.mPath, "screenshots")); err != nil { return err } - - return err + return gj.SaveScreenshots(gm) +} + +// 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 { + var err error + if err = gj.m.openDB(); err != nil { + return err + } + defer gj.m.closeDB() + + for _, ss := range gm.Screenshots { + if err = gj.SaveScreenshot(gm.TeamId, ss); err != nil { + return err + } + } + // Now remove unused screenshots + ssPath := append(gm.mPath, "screenshots") + if ssIds, err = gj.m.bolt.GetBucketList(ssPath); err != nil { + return err + } + for i := range ssIds { + if gm.GetScreenshot(ssIds[i]) == nil { + if err = gj.DeleteScreenshot(NewScreenshot(tm.TeamId, ssIds[i])); err != nil { + return err + } + } + } +} + +// Save a screenshot +func (gj *Gamejam) SaveScreenshot(tmId string, ss *Screenshot) error { + var err error + if err = gj.m.openDB(); err != nil { + return err + } + defer gj.m.closeDB() + + if err = gj.m.bolt.MkBucketPath(ss.mPath); err != nil { + return err + } + if err = gj.m.bolt.SetValue(ss.mPath, "description", ss.Description); err != nil { + return err + } + if err = gj.m.bolt.SetValue(ss.mPath, "image", ss.Image); err != nil { + return err + } + if err = gj.m.bolt.SetValue(ss.mPath, "filetype", ss.Filetype); err != nil { + return err + } + 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() + + ssPath := ss.mPath[:len(ss.mPath)-1] + return gj.m.bolt.DeleteBucket(ssPath, ss.UUID) } diff --git a/model_teams.go b/model_teams.go index aa8844e..df98872 100644 --- a/model_teams.go +++ b/model_teams.go @@ -14,13 +14,15 @@ type Team struct { Name string Members []TeamMember Game *Game + + mPath []string // The path in the DB to this team } // Create a team -func NewTeam(nm string) *Team { +func NewTeam(id string) *Team { return &Team{ - UUID: uuid.New(), - Name: nm, + UUID: id, + mPath: []string{"jam", "teams", id}, } } @@ -32,10 +34,12 @@ type TeamMember struct { Email string } -// Create a new team member, only a name is required -func NewTeamMember(nm string) *TeamMember { - m := TeamMember{Name: nm} - return &m +// Create a new team member +func NewTeamMember(tmId, uId string) *TeamMember { + return &TeamMember{ + UUID: uId, + mPath: []string{"jam", "teams", tmId, "members", uId}, + } } // LoadAllTeams loads all teams for the jam out of the database @@ -47,7 +51,6 @@ func (gj *Gamejam) LoadAllTeams() []Team { } defer gj.m.closeDB() - teamsPath := []string{"jam", "teams"} if tmUUIDs, err = m.bolt.GetBucketList(mbrsPath); err != nil { return ret } @@ -69,10 +72,8 @@ func (gj *Gamejam) LoadTeam(uuid string) *Team { defer gj.m.closeDB() // Team Data - tmPath := []string{"jam", "teams", uuid} - tm := new(Team) - tm.UUID = uuid - if tm.Name, err = gj.m.bolt.GetValue(tmPath, "name"); err != nil { + tm := NewTeam(uuid) + if tm.Name, err = gj.m.bolt.GetValue(tm.mPath, "name"); err != nil { return nil } @@ -82,6 +83,7 @@ func (gj *Gamejam) LoadTeam(uuid string) *Team { // Team Game tm.Game = gj.LoadTeamGame(uuid) + return tm } // Load the members of a team from the DB and return them @@ -95,7 +97,8 @@ func (gj *Gamejam) LoadTeamMembers(tmId string) []TeamMember { // Team Members var memberUuids []string - mbrsPath := []string{"jam", "teams", tmId, "members"} + tm := NewTeam(tmId) + mbrsPath := append(tm.mPath, "members") if memberUuids, err = gj.m.bolt.GetBucketList(mbrsPath); err == nil { for _, v := range memberUuids { mbr := gj.LoadTeamMember(tmId, v) @@ -115,25 +118,57 @@ func (gj *Gamejam) LoadTeamMember(tmId, mbrId string) *TeamMember { } defer gj.m.closeDB() - mbr := new(TeamMember) - mbr.UUID = v - teamMbrPath := append(mbrsPath, mbr.UUID) + mbr := NewTeamMember(tmId, mbrId) // Name is the only required field - if mbr.Name, err = gj.m.bolt.GetValue(teamMbrPath, "name"); err != nil { + if mbr.Name, err = gj.m.bolt.GetValue(mbr.mPath, "name"); err != nil { return nil } - if mbr.SlackId, err = gj.m.bolt.GetValue(teamMbrPath, "slackid"); err != nil { + if mbr.SlackId, err = gj.m.bolt.GetValue(mbr.mPath, "slackid"); err != nil { mbr.SlackId = "" } - if mbr.Twitter, err = gj.m.bolt.GetValue(teamMbrPath, "twitter"); err != nil { + if mbr.Twitter, err = gj.m.bolt.GetValue(mbr.mPath, "twitter"); err != nil { mbr.Twitter = "" } - if mbr.Email, err = gj.m.bolt.GetValue(teamMbrPath, "email"); err != nil { + if mbr.Email, err = gj.m.bolt.GetValue(mbr.mPath, "email"); err != nil { mbr.Email = "" } return mbr } +func (gj *Gamejam) SaveTeam(tm *Team) error { + var err error + if err = gj.m.openDB(); err != nil { + return err + } + defer gj.m.closeDB() + + // Save team data + if err = gj.m.bolt.SetValue(tm.mPath, "name"); err != nil { + return err + } + + // Save team members + for _, mbr := range tm.Members { + if err = gj.m.bolt.SetValue(mbr.mPath, "name", mbr.Name); err != nil { + return err + } + if err = gj.m.bolt.SetValue(mbr.mPath, "slackid", mbr.SlackId); err != nil { + return err + } + if err = gj.m.bolt.SetValue(mbr.mPath, "twitter", mbr.Twitter); err != nil { + return err + } + if err = gj.m.bolt.SetValue(mbr.mPath, "email", mbr.Email); err != nil { + return err + } + } + + // Save team game + if err = gj.m.bolt.SetValue(tm. + +} + + /** * OLD FUNCTIONS */ @@ -284,95 +319,6 @@ func (tm *Team) delete() error { return db.bolt.DeleteBucket(teamPath, tm.UUID) } -func (tm *Team) getGame() *Game { - var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - gamePath := []string{"teams", tm.UUID, "game"} - gm := new(Game) - if gm.Name, err = db.bolt.GetValue(gamePath, "name"); err != nil { - gm.Name = "" - } - gm.TeamId = tm.UUID - if gm.Description, err = db.bolt.GetValue(gamePath, "description"); err != nil { - gm.Description = "" - } - if gm.Link, err = db.bolt.GetValue(gamePath, "link"); err != nil { - gm.Link = "" - } - gm.Screenshots = tm.getScreenshots() - return gm -} - -// Screenshots are saved as base64 encoded pngs -func (tm *Team) saveScreenshot(ss *Screenshot) error { - var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - ssPath := []string{"teams", tm.UUID, "game", "screenshots"} - // Generate a UUID for this screenshot - uuid := uuid.New() - ssPath = append(ssPath, uuid) - if err := db.bolt.MkBucketPath(ssPath); err != nil { - return err - } - if err := db.bolt.SetValue(ssPath, "description", ss.Description); err != nil { - return err - } - if err := db.bolt.SetValue(ssPath, "image", ss.Image); err != nil { - return err - } - if err := db.bolt.SetValue(ssPath, "thumbnail", ss.Thumbnail); err != nil { - return err - } - if err := db.bolt.SetValue(ssPath, "filetype", ss.Filetype); err != nil { - return err - } - return nil -} - -func (tm *Team) deleteScreenshot(ssId string) error { - var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - ssPath := []string{"teams", tm.UUID, "game", "screenshots"} - return db.bolt.DeleteBucket(ssPath, ssId) -} - -func (tm *Team) getTeamMember(mbrId string) *TeamMember { - var err error - if err = db.open(); err != nil { - return nil - } - defer db.close() - - mbr := new(TeamMember) - mbr.UUID = mbrId - teamMbrPath := []string{"teams", tm.UUID, "members", mbr.UUID} - if mbr.Name, err = db.bolt.GetValue(teamMbrPath, "name"); err != nil { - return nil - } - if mbr.SlackId, err = db.bolt.GetValue(teamMbrPath, "slackid"); err != nil { - return nil - } - if mbr.Twitter, err = db.bolt.GetValue(teamMbrPath, "twitter"); err != nil { - return nil - } - if mbr.Email, err = db.bolt.GetValue(teamMbrPath, "email"); err != nil { - return nil - } - return mbr -} - func (tm *Team) getTeamMembers() []TeamMember { var ret []TeamMember var err error