2017-04-21 18:17:18 +00:00
|
|
|
package main
|
|
|
|
|
2017-10-19 19:53:07 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
2017-10-21 22:47:01 +00:00
|
|
|
"fmt"
|
2017-10-19 19:53:07 +00:00
|
|
|
|
|
|
|
"github.com/pborman/uuid"
|
|
|
|
)
|
2017-04-21 18:17:18 +00:00
|
|
|
|
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-19 19:53:07 +00:00
|
|
|
if id == "" {
|
|
|
|
id = uuid.New()
|
|
|
|
}
|
2017-10-21 22:47:01 +00:00
|
|
|
// Create an emtpy game for the team
|
|
|
|
gm, _ := NewGame(id)
|
2017-10-11 23:03:27 +00:00
|
|
|
return &Team{
|
2017-10-12 13:51:53 +00:00
|
|
|
UUID: id,
|
2017-10-21 22:47:01 +00:00
|
|
|
Game: gm,
|
2017-10-12 13:51:53 +00:00
|
|
|
mPath: []string{"jam", "teams", id},
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 19:53:07 +00:00
|
|
|
func (gj *Gamejam) GetTeamById(id string) (*Team, error) {
|
|
|
|
for i := range gj.Teams {
|
|
|
|
if gj.Teams[i].UUID == id {
|
|
|
|
return &gj.Teams[i], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errors.New("Invalid Team Id given")
|
|
|
|
}
|
|
|
|
|
2017-10-11 23:03:27 +00:00
|
|
|
type TeamMember struct {
|
|
|
|
UUID string
|
|
|
|
Name string
|
|
|
|
SlackId string
|
|
|
|
Twitter string
|
|
|
|
Email string
|
2017-10-18 22:18:12 +00:00
|
|
|
|
|
|
|
mPath []string // The path in the DB to this team member
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 13:51:53 +00:00
|
|
|
// Create a new team member
|
2017-10-19 19:53:07 +00:00
|
|
|
func NewTeamMember(tmId, uId string) (*TeamMember, error) {
|
|
|
|
if tmId == "" {
|
|
|
|
return nil, errors.New("Team ID is required")
|
|
|
|
}
|
|
|
|
if uId == "" {
|
|
|
|
uId = uuid.New()
|
|
|
|
}
|
2017-10-12 13:51:53 +00:00
|
|
|
return &TeamMember{
|
|
|
|
UUID: uId,
|
|
|
|
mPath: []string{"jam", "teams", tmId, "members", uId},
|
2017-10-19 19:53:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTeamMember adds a new team member
|
|
|
|
func (tm *Team) AddTeamMember(mbr *TeamMember) error {
|
|
|
|
lkup, _ := tm.GetTeamMemberById(mbr.UUID)
|
|
|
|
if lkup != nil {
|
|
|
|
return errors.New("A Team Member with that Id already exists")
|
|
|
|
}
|
|
|
|
tm.Members = append(tm.Members, *mbr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTeamMemberById returns a member with the given uuid
|
|
|
|
// or an error if it couldn't find it
|
|
|
|
func (tm *Team) GetTeamMemberById(uuid string) (*TeamMember, error) {
|
|
|
|
for i := range tm.Members {
|
|
|
|
if tm.Members[i].UUID == uuid {
|
|
|
|
return &tm.Members[i], nil
|
|
|
|
}
|
2017-10-12 13:51:53 +00:00
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
return nil, errors.New("Invalid Team Member Id given")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *Team) RemoveTeamMemberById(id string) error {
|
|
|
|
idx := -1
|
|
|
|
for i := range tm.Members {
|
|
|
|
if tm.Members[i].UUID == id {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx < 0 {
|
|
|
|
return errors.New("Invalid Team Member ID given")
|
|
|
|
}
|
|
|
|
tm.Members = append(tm.Members[:idx], tm.Members[idx+1:]...)
|
|
|
|
return nil
|
2017-10-11 23:03:27 +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
|
|
|
// 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 {
|
2017-10-19 19:53:07 +00:00
|
|
|
return ret
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
defer gj.m.closeDB()
|
|
|
|
|
2017-10-19 19:53:07 +00:00
|
|
|
var tmUUIDs []string
|
|
|
|
tmsPath := append(gj.mPath, "teams")
|
2017-10-21 22:47:01 +00:00
|
|
|
if tmUUIDs, err = gj.m.bolt.GetBucketList(tmsPath); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
2017-10-11 23:03:27 +00:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
for _, v := range tmUUIDs {
|
2017-10-19 19:53:07 +00:00
|
|
|
tm, _ := gj.LoadTeam(v)
|
2017-10-11 23:03:27 +00:00
|
|
|
if tm != nil {
|
2017-10-19 19:53:07 +00:00
|
|
|
ret = append(ret, *tm)
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load a team out of the database
|
2017-10-19 19:53:07 +00:00
|
|
|
func (gj *Gamejam) LoadTeam(uuid string) (*Team, 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()
|
|
|
|
|
|
|
|
// 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-19 19:53:07 +00:00
|
|
|
return nil, errors.New("Error loading team: " + err.Error())
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Team Members
|
|
|
|
tm.Members = gj.LoadTeamMembers(uuid)
|
|
|
|
|
|
|
|
// Team Game
|
2017-10-19 19:53:07 +00:00
|
|
|
if tm.Game, err = gj.LoadTeamGame(uuid); err != nil {
|
|
|
|
return nil, errors.New("Error loading team game: " + err.Error())
|
|
|
|
}
|
2017-10-11 23:03:27 +00:00
|
|
|
|
2017-10-19 19:53:07 +00:00
|
|
|
return tm, nil
|
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 {
|
2017-10-19 19:53:07 +00:00
|
|
|
mbr, _ := gj.LoadTeamMember(tmId, v)
|
2017-10-11 23:03:27 +00:00
|
|
|
if mbr != nil {
|
2017-10-19 19:53:07 +00:00
|
|
|
ret = append(ret, *mbr)
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load a team member from the DB and return it
|
2017-10-19 19:53:07 +00:00
|
|
|
func (gj *Gamejam) LoadTeamMember(tmId, mbrId string) (*TeamMember, 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
|
|
|
mbr, err := NewTeamMember(tmId, mbrId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("Error loading team member: " + err.Error())
|
|
|
|
}
|
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-19 19:53:07 +00:00
|
|
|
return nil, errors.New("Error loading team member: " + err.Error())
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
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 = ""
|
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
return mbr, nil
|
2017-10-11 23:03:27 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-10-19 19:53:07 +00:00
|
|
|
if err = gj.m.bolt.SetValue(tm.mPath, "name", tm.Name); err != nil {
|
2017-10-12 13:51:53 +00:00
|
|
|
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
|
2017-10-19 19:53:07 +00:00
|
|
|
return gj.SaveGame(tm.Game)
|
2017-10-12 13:51:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
// Delete the team tm
|
2017-10-19 19:53:07 +00:00
|
|
|
// TODO: Deletes should be done all at once when syncing memory to the DB
|
|
|
|
/*
|
2017-10-18 22:18:12 +00:00
|
|
|
func (gj *Gamejam) DeleteTeam(tm *Team) error {
|
2017-04-21 18:17:18 +00:00
|
|
|
var err error
|
2017-10-18 22:18:12 +00:00
|
|
|
if err = gj.m.openDB(); err != nil {
|
2017-04-21 18:17:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-18 22:18:12 +00:00
|
|
|
defer gj.m.closeDB()
|
2017-04-21 18:17:18 +00:00
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
if len(tm.mPath) < 2 {
|
|
|
|
return errors.New("Invalid team path: " + string(tm.mPath))
|
2017-04-21 18:17:18 +00:00
|
|
|
}
|
2017-10-18 22:18:12 +00:00
|
|
|
return gj.m.bolt.DeleteBucket(tm.mPath[:len(tm.mPath)-1], tm.UUID)
|
2017-04-21 18:17:18 +00:00
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
*/
|
2017-04-21 18:17:18 +00:00
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
// Delete the TeamMember mbr from Team tm
|
2017-10-19 19:53:07 +00:00
|
|
|
// TODO: Deletes should be done all at once when syncing memory to the DB
|
|
|
|
/*
|
2017-10-18 22:18:12 +00:00
|
|
|
func (gj *Gamejam) DeleteTeamMember(tm *Team, mbr *TeamMember) error {
|
2017-04-21 18:17:18 +00:00
|
|
|
var err error
|
2017-10-18 22:18:12 +00:00
|
|
|
if err = gj.m.openDB(); err != nil {
|
2017-10-02 23:06:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-18 22:18:12 +00:00
|
|
|
defer gj.m.closeDB()
|
2017-10-02 23:06:35 +00:00
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
if len(mbr.mPath) < 2 {
|
|
|
|
return errors.New("Invalid team path: " + string(tm.mPath))
|
2017-04-21 18:17:18 +00:00
|
|
|
}
|
2017-10-18 22:18:12 +00:00
|
|
|
return gj.m.bolt.DeleteBucket(mbr.mPath[:len(mbr.mPath)-1], mbr.UUID)
|
2017-04-21 18:17:18 +00:00
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
*/
|
2017-06-08 20:43:11 +00:00
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
/**
|
|
|
|
* In Memory functions
|
|
|
|
* This is generally how the app accesses data
|
|
|
|
*/
|
2017-06-12 23:23:46 +00:00
|
|
|
|
2017-10-19 19:53:07 +00:00
|
|
|
// Add a team
|
|
|
|
func (gj *Gamejam) AddTeam(tm *Team) error {
|
2017-10-21 22:47:01 +00:00
|
|
|
if _, err := gj.GetTeamById(tm.UUID); err == nil {
|
2017-10-19 19:53:07 +00:00
|
|
|
return errors.New("A team with that ID already exists")
|
|
|
|
}
|
2017-10-21 22:47:01 +00:00
|
|
|
if _, err := gj.GetTeamByName(tm.Name); err == nil {
|
2017-10-19 19:53:07 +00:00
|
|
|
return errors.New("A team with that Name already exists")
|
2017-06-12 23:23:46 +00:00
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
gj.Teams = append(gj.Teams, *tm)
|
2017-10-18 22:18:12 +00:00
|
|
|
return nil
|
2017-06-12 23:23:46 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 22:18:12 +00:00
|
|
|
// Find a team by name
|
2017-10-19 19:53:07 +00:00
|
|
|
func (gj *Gamejam) GetTeamByName(nm string) (*Team, error) {
|
2017-10-18 22:18:12 +00:00
|
|
|
for i := range gj.Teams {
|
|
|
|
if gj.Teams[i].Name == nm {
|
2017-10-19 19:53:07 +00:00
|
|
|
return &gj.Teams[i], nil
|
2017-06-08 20:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-19 19:53:07 +00:00
|
|
|
return nil, errors.New("Invalid team name given")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a team by id
|
|
|
|
func (gj *Gamejam) RemoveTeamById(id string) error {
|
|
|
|
idx := -1
|
|
|
|
for i := range gj.Teams {
|
|
|
|
if gj.Teams[i].UUID == id {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
return errors.New("Invalid Team ID given")
|
|
|
|
}
|
|
|
|
gj.Teams = append(gj.Teams[:idx], gj.Teams[idx+1:]...)
|
2017-10-02 23:06:35 +00:00
|
|
|
return nil
|
2017-06-12 23:23:46 +00:00
|
|
|
}
|