ictgj-voting/model_votes.go

152 lines
3.4 KiB
Go
Raw Normal View History

package main
2017-10-11 23:03:27 +00:00
import (
2017-10-19 19:53:07 +00:00
"errors"
"fmt"
2017-10-11 23:03:27 +00:00
"strconv"
"time"
)
2017-06-30 19:35:36 +00:00
// A Choice is a ranking of a game in a vote
type GameChoice struct {
Team string // UUID of team
Rank int
}
// A Vote is a collection of game rankings
type Vote struct {
Timestamp time.Time
2017-06-30 19:35:36 +00:00
ClientId string // UUID of client
Choices []GameChoice
2017-10-19 19:53:07 +00:00
mPath []string // The path in the DB to this team
}
func NewVote(clId string, tm time.Time) (*Vote, error) {
if clId == "" {
return nil, errors.New("Client ID is required")
}
if tm.IsZero() {
tm = time.Now()
}
vt := new(Vote)
vt.Timestamp = tm
2017-10-19 19:53:07 +00:00
vt.mPath = []string{"jam", "votes", clId, tm.Format(time.RFC3339)}
2017-10-19 19:53:07 +00:00
return vt, nil
}
func (vt *Vote) SetChoices(ch []string) error {
// Clear any previous choices from this vote
vt.Choices = []GameChoice{}
for i, v := range ch {
vt.Choices = append(vt.Choices, GameChoice{Rank: i, Team: v})
}
return nil
}
func (gj *Gamejam) GetVoteWithTimeString(clId, ts string) (*Vote, error) {
timestamp, err := time.Parse(time.RFC3339, ts)
if err != nil {
return nil, err
}
return gj.GetVote(clId, timestamp)
}
func (gj *Gamejam) GetVote(clId string, ts time.Time) (*Vote, error) {
for i := range gj.Votes {
if gj.Votes[i].ClientId == clId && gj.Votes[i].Timestamp == ts {
return &gj.Votes[i], nil
2017-10-19 19:53:07 +00:00
}
}
return nil, errors.New("Couldn't find requested vote")
}
func (gj *Gamejam) AddVote(vt *Vote) error {
// Make sure that this isn't a duplicate
if _, err := gj.GetVote(vt.ClientId, vt.Timestamp); err == nil {
return errors.New("Duplicate Vote")
}
gj.Votes = append(gj.Votes, *vt)
return nil
}
2017-10-19 19:53:07 +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
// LoadAllVotes loads all votes for the jam out of the database
func (gj *Gamejam) LoadAllVotes() []Vote {
2017-10-19 19:53:07 +00:00
var err error
2017-10-11 23:03:27 +00:00
var ret []Vote
2017-10-19 19:53:07 +00:00
if err = gj.m.openDB(); err != nil {
return ret
2017-10-11 23:03:27 +00:00
}
defer gj.m.closeDB()
votesPath := []string{"jam", "votes"}
2017-10-19 19:53:07 +00:00
var cliUUIDs []string
2017-10-21 22:47:01 +00:00
if cliUUIDs, err = gj.m.bolt.GetBucketList(votesPath); err != nil {
2017-10-11 23:03:27 +00:00
return ret
}
for _, cId := range cliUUIDs {
vtsPth := append(votesPath, cId)
2017-10-19 19:53:07 +00:00
var times []string
2017-10-21 22:47:01 +00:00
if times, err = gj.m.bolt.GetBucketList(vtsPth); err != nil {
2017-10-11 23:03:27 +00:00
// Error reading this bucket, move on to the next
continue
}
for _, t := range times {
fmt.Println("Loading Vote", cId, t)
2017-10-19 19:53:07 +00:00
if vt, err := gj.LoadVote(cId, t); err == nil {
ret = append(ret, *vt)
2017-10-11 23:03:27 +00:00
}
}
}
return ret
}
// Load a vote from the DB and return it
2017-10-19 19:53:07 +00:00
func (gj *Gamejam) LoadVote(clientId, t string) (*Vote, error) {
2017-10-11 23:03:27 +00:00
var tm time.Time
2017-10-19 19:53:07 +00:00
var err error
2017-10-11 23:03:27 +00:00
if tm, err = time.Parse(time.RFC3339, t); err != nil {
2017-10-19 19:53:07 +00:00
return nil, errors.New("Error loading vote: " + err.Error())
}
vt, err := NewVote(clientId, tm)
if err != nil {
return nil, errors.New("Error creating vote: " + err.Error())
2017-10-11 23:03:27 +00:00
}
var choices []string
if choices, err = gj.m.bolt.GetKeyList(vt.mPath); err != nil {
2017-10-19 19:53:07 +00:00
return nil, errors.New("Error creating vote: " + err.Error())
2017-10-11 23:03:27 +00:00
}
for _, v := range choices {
2017-10-19 19:53:07 +00:00
ch := new(GameChoice)
2017-10-11 23:03:27 +00:00
var rank int
if rank, err = strconv.Atoi(v); err == nil {
ch.Rank = rank
if ch.Team, err = gj.m.bolt.GetValue(vt.mPath, v); err == nil {
vt.Choices = append(vt.Choices, *ch)
}
2017-10-11 23:03:27 +00:00
}
}
2017-10-19 19:53:07 +00:00
return vt, nil
}
func (gj *Gamejam) SaveVote(vt *Vote) error {
var err error
if err = gj.m.openDB(); err != nil {
return err
}
defer gj.m.closeDB()
for _, v := range vt.Choices {
m.bolt.SetValue(vt.mPath, strconv.Itoa(v.Rank), v.Team)
}
return nil
2017-10-11 23:03:27 +00:00
}