ictgj-voting/model_teams.go

393 lines
8.0 KiB
Go
Raw Normal View History

2017-04-21 18:17:18 +00:00
package main
import (
2017-06-08 20:43:11 +00:00
"errors"
2017-04-21 18:17:18 +00:00
"github.com/pborman/uuid"
)
2017-10-11 23:03:27 +00:00
/**
* Team
*/
2017-04-21 18:17:18 +00:00
type Team struct {
UUID string
Name string
Members []TeamMember
Game *Game
2017-10-12 13:51:53 +00:00
mPath []string // The path in the DB to this team
2017-04-21 18:17:18 +00:00
}
2017-10-11 23:03:27 +00:00
// Create a team
2017-10-12 13:51:53 +00:00
func NewTeam(id string) *Team {
2017-10-11 23:03:27 +00:00
return &Team{
2017-10-12 13:51:53 +00:00
UUID: id,
mPath: []string{"jam", "teams", id},
2017-10-11 23:03:27 +00:00
}
}
type TeamMember struct {
UUID string
Name string
SlackId string
Twitter string
Email string
}
2017-10-12 13:51:53 +00:00
// Create a new team member
func NewTeamMember(tmId, uId string) *TeamMember {
return &TeamMember{
UUID: uId,
mPath: []string{"jam", "teams", tmId, "members", uId},
}
2017-10-11 23:03:27 +00:00
}
// LoadAllTeams loads all teams for the jam out of the database
func (gj *Gamejam) LoadAllTeams() []Team {
var err error
var ret []Team
if err = gj.m.openDB(); err != nil {
return err
}
defer gj.m.closeDB()
if tmUUIDs, err = m.bolt.GetBucketList(mbrsPath); err != nil {
return ret
}
for _, v := range tmUUIDs {
tm := gj.LoadTeam(v)
if tm != nil {
ret = append(ret, tm)
}
}
return ret
}
// Load a team out of the database
func (gj *Gamejam) LoadTeam(uuid string) *Team {
var err error
if err = gj.m.openDB(); err != nil {
return err
}
defer gj.m.closeDB()
// Team Data
2017-10-12 13:51:53 +00:00
tm := NewTeam(uuid)
if tm.Name, err = gj.m.bolt.GetValue(tm.mPath, "name"); err != nil {
2017-10-11 23:03:27 +00:00
return nil
}
// Team Members
tm.Members = gj.LoadTeamMembers(uuid)
// Team Game
tm.Game = gj.LoadTeamGame(uuid)
2017-10-12 13:51:53 +00:00
return tm
2017-10-11 23:03:27 +00:00
}
// Load the members of a team from the DB and return them
func (gj *Gamejam) LoadTeamMembers(tmId string) []TeamMember {
var err error
var ret []TeamMember
if err = gj.m.openDB(); err != nil {
return ret
}
defer gj.m.closeDB()
// Team Members
var memberUuids []string
2017-10-12 13:51:53 +00:00
tm := NewTeam(tmId)
mbrsPath := append(tm.mPath, "members")
2017-10-11 23:03:27 +00:00
if memberUuids, err = gj.m.bolt.GetBucketList(mbrsPath); err == nil {
for _, v := range memberUuids {
mbr := gj.LoadTeamMember(tmId, v)
if mbr != nil {
ret = append(ret, mbr)
}
}
}
return ret
}
// Load a team member from the DB and return it
func (gj *Gamejam) LoadTeamMember(tmId, mbrId string) *TeamMember {
var err error
if err = gj.m.openDB(); err != nil {
return nil
}
defer gj.m.closeDB()
2017-10-12 13:51:53 +00:00
mbr := NewTeamMember(tmId, mbrId)
2017-10-11 23:03:27 +00:00
// Name is the only required field
2017-10-12 13:51:53 +00:00
if mbr.Name, err = gj.m.bolt.GetValue(mbr.mPath, "name"); err != nil {
2017-10-11 23:03:27 +00:00
return nil
}
2017-10-12 13:51:53 +00:00
if mbr.SlackId, err = gj.m.bolt.GetValue(mbr.mPath, "slackid"); err != nil {
2017-10-11 23:03:27 +00:00
mbr.SlackId = ""
}
2017-10-12 13:51:53 +00:00
if mbr.Twitter, err = gj.m.bolt.GetValue(mbr.mPath, "twitter"); err != nil {
2017-10-11 23:03:27 +00:00
mbr.Twitter = ""
}
2017-10-12 13:51:53 +00:00
if mbr.Email, err = gj.m.bolt.GetValue(mbr.mPath, "email"); err != nil {
2017-10-11 23:03:27 +00:00
mbr.Email = ""
}
return mbr
}
2017-10-12 13:51:53 +00:00
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.
}
2017-10-11 23:03:27 +00:00
/**
* OLD FUNCTIONS
*/
// NewTeam creates a team with name nm and stores it in the DB
func NewTeam(nm string) error {
2017-04-21 18:17:18 +00:00
var err error
if err = db.open(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
defer db.close()
2017-04-21 18:17:18 +00:00
// Generate a UUID
uuid := uuid.New()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams", uuid}
2017-04-21 18:17:18 +00:00
if err := db.bolt.MkBucketPath(teamPath); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
if err := db.bolt.SetValue(teamPath, "name", nm); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
if err := db.bolt.MkBucketPath(append(teamPath, "members")); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
gamePath := append(teamPath, "game")
if err := db.bolt.MkBucketPath(gamePath); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
if err := db.bolt.SetValue(append(gamePath), "name", ""); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
return db.bolt.MkBucketPath(append(gamePath, "screenshots"))
2017-04-21 18:17:18 +00:00
}
// getTeam returns a team with the given id, or nil
2017-10-11 23:03:27 +00:00
func (db *currJamDb) getTeam(id string) *Team {
2017-04-21 18:17:18 +00:00
var err error
if err = db.open(); err != nil {
return nil
2017-04-21 18:17:18 +00:00
}
defer db.close()
2017-04-21 18:17:18 +00:00
teamPath := []string{"teams", id}
tm := new(Team)
tm.UUID = id
if tm.Name, err = db.bolt.GetValue(teamPath, "name"); err != nil {
return nil
}
tm.Members = tm.getTeamMembers()
tm.Game = tm.getGame()
return tm
}
// This function returns the team for a specific member
2017-10-11 23:03:27 +00:00
func (db *currJamDb) getTeamForMember(mbrid string) (*Team, error) {
var err error
if err = db.open(); err != nil {
return nil, err
}
defer db.close()
teams := db.getAllTeams()
for i := range teams {
var tmMbrs []TeamMember
tmMbrs = teams[i].getTeamMembers()
if err == nil {
for j := range tmMbrs {
if tmMbrs[j].UUID == mbrid {
return &teams[i], nil
}
2017-04-21 18:17:18 +00:00
}
}
}
return nil, errors.New("Unable to find team member")
2017-04-21 18:17:18 +00:00
}
// getAllTeams returns all teams in the database
2017-10-11 23:03:27 +00:00
func (db *currJamDb) getAllTeams() []Team {
2017-04-21 18:17:18 +00:00
var ret []Team
var err error
if err = db.open(); err != nil {
2017-04-21 18:17:18 +00:00
return ret
}
defer db.close()
2017-04-21 18:17:18 +00:00
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
var teamUids []string
if teamUids, err = db.bolt.GetBucketList(teamPath); err != nil {
2017-04-21 18:17:18 +00:00
return ret
}
2017-06-08 17:20:43 +00:00
for _, v := range teamUids {
if tm := db.getTeam(v); tm != nil {
2017-06-08 17:20:43 +00:00
ret = append(ret, *tm)
2017-04-21 18:17:18 +00:00
}
}
return ret
}
// getTeamByName returns a team with the given name or nil
2017-10-11 23:03:27 +00:00
func (db *currJamDb) getTeamByName(nm string) *Team {
2017-04-21 18:17:18 +00:00
var err error
if err = db.open(); err != nil {
2017-04-21 18:17:18 +00:00
return nil
}
defer db.close()
2017-04-21 18:17:18 +00:00
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
2017-04-21 18:17:18 +00:00
var teamUids []string
if teamUids, err = db.bolt.GetBucketList(teamPath); err != nil {
2017-04-21 18:17:18 +00:00
for _, v := range teamUids {
var name string
if name, err = db.bolt.GetValue(append(teamPath, v), "name"); name == nm {
return db.getTeam(v)
2017-04-21 18:17:18 +00:00
}
}
}
return nil
}
// save saves the team to the db
func (tm *Team) save() error {
2017-04-21 18:17:18 +00:00
var err error
if err = db.open(); err != nil {
2017-06-12 23:23:46 +00:00
return err
2017-04-21 18:17:18 +00:00
}
defer db.close()
2017-04-21 18:17:18 +00:00
teamPath := []string{"teams", tm.UUID}
if err = db.bolt.SetValue(teamPath, "name", tm.Name); err != nil {
return err
}
// TODO: Save Team Members
// TODO: Save Team Game
return nil
2017-06-08 17:20:43 +00:00
}
// delete removes the team from the database
func (tm *Team) delete() error {
2017-06-08 17:20:43 +00:00
var err error
if err = db.open(); err != nil {
2017-04-21 18:17:18 +00:00
return err
}
defer db.close()
2017-06-08 17:20:43 +00:00
teamPath := []string{"teams"}
return db.bolt.DeleteBucket(teamPath, tm.UUID)
2017-04-21 18:17:18 +00:00
}
2017-06-08 20:43:11 +00:00
func (tm *Team) getTeamMembers() []TeamMember {
var ret []TeamMember
2017-06-12 23:23:46 +00:00
var err error
if err = db.open(); err != nil {
return ret
2017-06-12 23:23:46 +00:00
}
defer db.close()
2017-06-12 23:23:46 +00:00
teamPath := []string{"teams", tm.UUID, "members"}
var memberUuids []string
if memberUuids, err = db.bolt.GetBucketList(teamPath); err == nil {
for _, v := range memberUuids {
var mbr *TeamMember
if mbr = tm.getTeamMember(v); mbr != nil {
ret = append(ret, *mbr)
2017-06-08 20:43:11 +00:00
}
2017-06-12 23:23:46 +00:00
}
}
return ret
2017-06-12 23:23:46 +00:00
}
func (tm *Team) updateTeamMember(mbr *TeamMember) error {
2017-06-12 23:23:46 +00:00
var err error
if err = db.open(); err != nil {
return err
2017-06-12 23:23:46 +00:00
}
defer db.close()
2017-06-12 23:23:46 +00:00
if mbr.UUID == "" {
mbrs := tm.getTeamMembers()
if len(mbrs) > 0 {
for i := range mbrs {
if mbrs[i].Name == mbr.Name {
mbr.UUID = mbrs[i].UUID
break
2017-06-12 23:23:46 +00:00
}
2017-06-08 20:43:11 +00:00
}
}
}
if mbr.UUID == "" {
// It's really a new one
mbr.UUID = uuid.New()
2017-06-12 23:23:46 +00:00
}
mbrPath := []string{"teams", tm.UUID, "members", mbr.UUID}
if db.bolt.SetValue(mbrPath, "name", mbr.Name) != nil {
return err
}
if db.bolt.SetValue(mbrPath, "slackid", mbr.SlackId) != nil {
return err
}
if db.bolt.SetValue(mbrPath, "twitter", mbr.Twitter) != nil {
return err
}
if db.bolt.SetValue(mbrPath, "email", mbr.Email) != nil {
return err
}
return nil
2017-06-12 23:23:46 +00:00
}
// deleteTeamMember removes a member from the database
func (tm *Team) deleteTeamMember(mbr *TeamMember) error {
2017-06-12 23:23:46 +00:00
var err error
if err = db.open(); err != nil {
2017-06-12 23:23:46 +00:00
return err
}
defer db.close()
2017-06-12 23:23:46 +00:00
teamPath := []string{"teams", tm.UUID, "members"}
return db.bolt.DeleteBucket(teamPath, mbr.UUID)
2017-06-08 20:43:11 +00:00
}