Cleaning up the models, making things a little nicer
Resolves issue #23
This commit is contained in:
parent
d398959dd9
commit
a225e3b6ad
@ -11,19 +11,20 @@ func handleAdminClients(w http.ResponseWriter, req *http.Request, page *pageData
|
|||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
page.SubTitle = "Clients"
|
page.SubTitle = "Clients"
|
||||||
clientId := vars["id"]
|
clientId := vars["id"]
|
||||||
|
client := db.getClient(clientId)
|
||||||
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
|
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
|
||||||
if clientId == "" {
|
if clientId == "" {
|
||||||
type clientsPageData struct {
|
type clientsPageData struct {
|
||||||
Clients []Client
|
Clients []Client
|
||||||
}
|
}
|
||||||
page.TemplateData = clientsPageData{Clients: dbGetAllClients()}
|
page.TemplateData = clientsPageData{Clients: db.getAllClients()}
|
||||||
page.SubTitle = "Clients"
|
page.SubTitle = "Clients"
|
||||||
page.show("admin-clients.html", w)
|
page.show("admin-clients.html", w)
|
||||||
} else {
|
} else {
|
||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "add":
|
case "add":
|
||||||
page.SubTitle = "Authenticate Client"
|
page.SubTitle = "Authenticate Client"
|
||||||
cli := dbGetClient(clientId)
|
cli := db.getClient(clientId)
|
||||||
if cli.IP == "" {
|
if cli.IP == "" {
|
||||||
cli.IP = clientIp
|
cli.IP = clientIp
|
||||||
}
|
}
|
||||||
@ -38,14 +39,17 @@ func handleAdminClients(w http.ResponseWriter, req *http.Request, page *pageData
|
|||||||
email := req.FormValue("email")
|
email := req.FormValue("email")
|
||||||
password := req.FormValue("password")
|
password := req.FormValue("password")
|
||||||
clientName := req.FormValue("clientname")
|
clientName := req.FormValue("clientname")
|
||||||
|
// Authentication isn't required to set a client name
|
||||||
if clientName != "" {
|
if clientName != "" {
|
||||||
dbSetClientName(clientId, clientName)
|
client.Name = clientName
|
||||||
}
|
}
|
||||||
dbUpdateClientIP(clientId, clientIp)
|
client.IP = clientIp
|
||||||
|
client.save()
|
||||||
if page.LoggedIn || doLogin(email, password) == nil {
|
if page.LoggedIn || doLogin(email, password) == nil {
|
||||||
// Received a valid login
|
// Received a valid login
|
||||||
// Authenticate the client
|
// Authenticate the client
|
||||||
if dbAuthClient(clientId, clientIp) == nil {
|
client.Auth = true
|
||||||
|
if client.save() == nil {
|
||||||
page.session.setFlashMessage("Client Authenticated", "success")
|
page.session.setFlashMessage("Client Authenticated", "success")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Client Authentication Failed", "error")
|
page.session.setFlashMessage("Client Authentication Failed", "error")
|
||||||
@ -56,17 +60,17 @@ func handleAdminClients(w http.ResponseWriter, req *http.Request, page *pageData
|
|||||||
}
|
}
|
||||||
redirect("/", w, req)
|
redirect("/", w, req)
|
||||||
case "deauth":
|
case "deauth":
|
||||||
dbDeAuthClient(clientId)
|
client.Auth = false
|
||||||
|
if client.save() == nil {
|
||||||
|
page.session.setFlashMessage("Client De-Authenticated", "success")
|
||||||
|
} else {
|
||||||
|
page.session.setFlashMessage("Client De-Authentication Failed", "success")
|
||||||
|
}
|
||||||
redirect("/admin/clients", w, req)
|
redirect("/admin/clients", w, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func clientIsAuthenticated(cid string, req *http.Request) bool {
|
|
||||||
return dbClientIsAuth(cid)
|
|
||||||
//return clientIsServer(req) || dbClientIsAuth(cid)
|
|
||||||
}
|
|
||||||
|
|
||||||
func clientIsServer(req *http.Request) bool {
|
func clientIsServer(req *http.Request) bool {
|
||||||
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
|
clientIp, _, _ := net.SplitHostPort(req.RemoteAddr)
|
||||||
ifaces, err := net.Interfaces()
|
ifaces, err := net.Interfaces()
|
||||||
|
@ -72,7 +72,7 @@ func handleAdminSetAuthMode(w http.ResponseWriter, req *http.Request, page *page
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
page.session.setFlashMessage("Invalid Authentication Mode: "+vars["id"], "error")
|
page.session.setFlashMessage("Invalid Authentication Mode: "+vars["id"], "error")
|
||||||
}
|
}
|
||||||
if dbSetAuthMode(newMode) != nil {
|
if db.setAuthMode(newMode) != nil {
|
||||||
page.session.setFlashMessage("Invalid Authentication Mode: "+vars["id"], "error")
|
page.session.setFlashMessage("Invalid Authentication Mode: "+vars["id"], "error")
|
||||||
}
|
}
|
||||||
redirect("/admin", w, req)
|
redirect("/admin", w, req)
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"image"
|
"image"
|
||||||
"image/gif"
|
"image/gif"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
@ -25,46 +24,50 @@ func handleAdminGames(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
Teams []Team
|
Teams []Team
|
||||||
}
|
}
|
||||||
gpd := new(gamesPageData)
|
gpd := new(gamesPageData)
|
||||||
gpd.Teams = dbGetAllTeams()
|
gpd.Teams = db.getAllTeams()
|
||||||
page.TemplateData = gpd
|
page.TemplateData = gpd
|
||||||
page.SubTitle = "Games"
|
page.SubTitle = "Games"
|
||||||
page.show("admin-games.html", w)
|
page.show("admin-games.html", w)
|
||||||
} else {
|
} else {
|
||||||
|
tm := db.getTeam(teamId)
|
||||||
|
if tm != nil {
|
||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "save":
|
case "save":
|
||||||
name := req.FormValue("gamename")
|
gm := newGame(tm.UUID)
|
||||||
desc := req.FormValue("gamedesc")
|
gm.Name = req.FormValue("gamename")
|
||||||
link := req.FormValue("gamelink")
|
gm.Link = req.FormValue("gamelink")
|
||||||
if dbIsValidTeam(teamId) {
|
gm.Description = req.FormValue("gamedesc")
|
||||||
if err := dbUpdateTeamGame(teamId, name, link, desc); err != nil {
|
if err := gm.save(); err != nil {
|
||||||
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Team game updated", "success")
|
page.session.setFlashMessage("Team game updated", "success")
|
||||||
}
|
}
|
||||||
redirect("/admin/teams/"+teamId+"#game", w, req)
|
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
|
||||||
}
|
|
||||||
case "screenshotupload":
|
case "screenshotupload":
|
||||||
if err := saveScreenshots(teamId, req); err != nil {
|
if err := saveScreenshots(tm, req); err != nil {
|
||||||
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
||||||
}
|
}
|
||||||
redirect("/admin/teams/"+teamId+"#game", w, req)
|
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
|
||||||
case "screenshotdelete":
|
case "screenshotdelete":
|
||||||
ssid := vars["subid"]
|
ssid := vars["subid"]
|
||||||
if err := dbDeleteTeamGameScreenshot(teamId, ssid); err != nil {
|
if err := tm.deleteScreenshot(ssid); err != nil {
|
||||||
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
|
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
|
||||||
}
|
}
|
||||||
redirect("/admin/teams/"+teamId+"#game", w, req)
|
redirect("/admin/teams/"+tm.UUID+"#game", w, req)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
page.session.setFlashMessage("Not a valid team id", "error")
|
||||||
|
redirect("/admin/teams", w, req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveScreenshots(teamId string, req *http.Request) error {
|
func saveScreenshots(tm *Team, req *http.Request) error {
|
||||||
var err error
|
var err error
|
||||||
file, hdr, err := req.FormFile("newssfile")
|
file, hdr, err := req.FormFile("newssfile")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println("File Received: " + hdr.Filename)
|
|
||||||
extIdx := strings.LastIndex(hdr.Filename, ".")
|
extIdx := strings.LastIndex(hdr.Filename, ".")
|
||||||
fltp := "png"
|
fltp := "png"
|
||||||
if len(hdr.Filename) > extIdx {
|
if len(hdr.Filename) > extIdx {
|
||||||
@ -90,7 +93,7 @@ func saveScreenshots(teamId string, req *http.Request) error {
|
|||||||
}
|
}
|
||||||
thmString = base64.StdEncoding.EncodeToString(thmBuf.Bytes())
|
thmString = base64.StdEncoding.EncodeToString(thmBuf.Bytes())
|
||||||
|
|
||||||
return dbSaveTeamGameScreenshot(teamId, &Screenshot{
|
return tm.saveScreenshot(&Screenshot{
|
||||||
Image: base64.StdEncoding.EncodeToString(buf.Bytes()),
|
Image: base64.StdEncoding.EncodeToString(buf.Bytes()),
|
||||||
Thumbnail: thmString,
|
Thumbnail: thmString,
|
||||||
Filetype: fltp,
|
Filetype: fltp,
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func refreshTeamsInMemory() {
|
func refreshTeamsInMemory() {
|
||||||
site.Teams = dbGetAllTeams()
|
site.Teams = db.getAllTeams()
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData) {
|
func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData) {
|
||||||
@ -23,11 +23,11 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "save":
|
case "save":
|
||||||
name := req.FormValue("teamname")
|
name := req.FormValue("teamname")
|
||||||
if dbGetTeamByName(name) != nil {
|
if db.getTeamByName(name) != nil {
|
||||||
// A team with that name already exists
|
// A team with that name already exists
|
||||||
page.session.setFlashMessage("A team with the name "+name+" already exists!", "error")
|
page.session.setFlashMessage("A team with the name "+name+" already exists!", "error")
|
||||||
} else {
|
} else {
|
||||||
if err := dbCreateNewTeam(name); err != nil {
|
if err := db.newTeam(name); err != nil {
|
||||||
page.session.setFlashMessage(err.Error(), "error")
|
page.session.setFlashMessage(err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Team "+name+" created!", "success")
|
page.session.setFlashMessage("Team "+name+" created!", "success")
|
||||||
@ -41,13 +41,13 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
}
|
}
|
||||||
} else if teamId != "" {
|
} else if teamId != "" {
|
||||||
// Functions for existing team
|
// Functions for existing team
|
||||||
if dbIsValidTeam(teamId) {
|
tm := db.getTeam(teamId)
|
||||||
|
if tm != nil {
|
||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "save":
|
case "save":
|
||||||
tm := new(Team)
|
|
||||||
tm.UUID = teamId
|
tm.UUID = teamId
|
||||||
tm.Name = req.FormValue("teamname")
|
tm.Name = req.FormValue("teamname")
|
||||||
if err := dbUpdateTeam(teamId, tm); err != nil {
|
if err := tm.save(); err != nil {
|
||||||
page.session.setFlashMessage("Error updating team: "+err.Error(), "error")
|
page.session.setFlashMessage("Error updating team: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Team Updated!", "success")
|
page.session.setFlashMessage("Team Updated!", "success")
|
||||||
@ -56,20 +56,20 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
redirect("/admin/teams", w, req)
|
redirect("/admin/teams", w, req)
|
||||||
case "delete":
|
case "delete":
|
||||||
var err error
|
var err error
|
||||||
t := dbGetTeam(teamId)
|
if err = tm.delete(); err != nil {
|
||||||
if err = dbDeleteTeam(teamId); err != nil {
|
|
||||||
page.session.setFlashMessage("Error deleting team: "+err.Error(), "error")
|
page.session.setFlashMessage("Error deleting team: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Team "+t.Name+" Deleted", "success")
|
page.session.setFlashMessage("Team "+tm.Name+" Deleted", "success")
|
||||||
}
|
}
|
||||||
refreshTeamsInMemory()
|
refreshTeamsInMemory()
|
||||||
redirect("/admin/teams", w, req)
|
redirect("/admin/teams", w, req)
|
||||||
case "savemember":
|
case "savemember":
|
||||||
mbrName := req.FormValue("newmembername")
|
mbrName := req.FormValue("newmembername")
|
||||||
mbrSlack := req.FormValue("newmemberslackid")
|
mbr := newTeamMember(mbrName)
|
||||||
mbrTwitter := req.FormValue("newmembertwitter")
|
mbr.SlackId = req.FormValue("newmemberslackid")
|
||||||
mbrEmail := req.FormValue("newmemberemail")
|
mbr.Twitter = req.FormValue("newmembertwitter")
|
||||||
if err := dbAddTeamMember(teamId, mbrName, mbrEmail, mbrSlack, mbrTwitter); err != nil {
|
mbr.Email = req.FormValue("newmemberemail")
|
||||||
|
if err := tm.updateTeamMember(mbr); err != nil {
|
||||||
page.session.setFlashMessage("Error adding team member: "+err.Error(), "error")
|
page.session.setFlashMessage("Error adding team member: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage(mbrName+" added to team!", "success")
|
page.session.setFlashMessage(mbrName+" added to team!", "success")
|
||||||
@ -77,18 +77,21 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
refreshTeamsInMemory()
|
refreshTeamsInMemory()
|
||||||
redirect("/admin/teams/"+teamId+"#members", w, req)
|
redirect("/admin/teams/"+teamId+"#members", w, req)
|
||||||
case "deletemember":
|
case "deletemember":
|
||||||
mbrId := req.FormValue("memberid")
|
m := tm.getTeamMember(req.FormValue("memberid"))
|
||||||
m, _ := dbGetTeamMember(teamId, mbrId)
|
if m != nil {
|
||||||
if err := dbDeleteTeamMember(teamId, mbrId); err != nil {
|
if err := tm.deleteTeamMember(m); err != nil {
|
||||||
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
|
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage(m.Name+" deleted from team", "success")
|
page.session.setFlashMessage(m.Name+" deleted from team", "success")
|
||||||
}
|
}
|
||||||
refreshTeamsInMemory()
|
refreshTeamsInMemory()
|
||||||
|
} else {
|
||||||
|
page.session.setFlashMessage("Couldn't find team member to delete", "error")
|
||||||
|
}
|
||||||
redirect("/admin/teams/"+teamId+"#members", w, req)
|
redirect("/admin/teams/"+teamId+"#members", w, req)
|
||||||
default:
|
default:
|
||||||
page.SubTitle = "Edit Team"
|
page.SubTitle = "Edit Team"
|
||||||
t := dbGetTeam(teamId)
|
t := db.getTeam(teamId)
|
||||||
page.TemplateData = t
|
page.TemplateData = t
|
||||||
page.show("admin-editteam.html", w)
|
page.show("admin-editteam.html", w)
|
||||||
}
|
}
|
||||||
@ -101,7 +104,7 @@ func handleAdminTeams(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
type teamsPageData struct {
|
type teamsPageData struct {
|
||||||
Teams []Team
|
Teams []Team
|
||||||
}
|
}
|
||||||
page.TemplateData = teamsPageData{Teams: dbGetAllTeams()}
|
page.TemplateData = teamsPageData{Teams: db.getAllTeams()}
|
||||||
page.SubTitle = "Teams"
|
page.SubTitle = "Teams"
|
||||||
page.show("admin-teams.html", w)
|
page.show("admin-teams.html", w)
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ func handleAdminDoLogin(w http.ResponseWriter, req *http.Request) {
|
|||||||
// If it can't, it returns an error
|
// If it can't, it returns an error
|
||||||
func doLogin(email, password string) error {
|
func doLogin(email, password string) error {
|
||||||
if strings.TrimSpace(email) != "" && strings.TrimSpace(password) != "" {
|
if strings.TrimSpace(email) != "" && strings.TrimSpace(password) != "" {
|
||||||
return dbCheckCredentials(email, password)
|
return db.checkCredentials(email, password)
|
||||||
}
|
}
|
||||||
return errors.New("Invalid Credentials")
|
return errors.New("Invalid Credentials")
|
||||||
}
|
}
|
||||||
@ -53,12 +53,12 @@ func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "save":
|
case "save":
|
||||||
email = req.FormValue("email")
|
email = req.FormValue("email")
|
||||||
if dbIsValidUserEmail(email) {
|
if db.isValidUserEmail(email) {
|
||||||
// User already exists
|
// User already exists
|
||||||
page.session.setFlashMessage("A user with email address "+email+" already exists!", "error")
|
page.session.setFlashMessage("A user with email address "+email+" already exists!", "error")
|
||||||
} else {
|
} else {
|
||||||
password := req.FormValue("password")
|
password := req.FormValue("password")
|
||||||
if err := dbUpdateUserPassword(email, string(password)); err != nil {
|
if err := db.updateUserPassword(email, string(password)); err != nil {
|
||||||
page.session.setFlashMessage(err.Error(), "error")
|
page.session.setFlashMessage(err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("User "+email+" created!", "success")
|
page.session.setFlashMessage("User "+email+" created!", "success")
|
||||||
@ -73,10 +73,10 @@ func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "save":
|
case "save":
|
||||||
var err error
|
var err error
|
||||||
if dbIsValidUserEmail(email) {
|
if db.isValidUserEmail(email) {
|
||||||
password := req.FormValue("password")
|
password := req.FormValue("password")
|
||||||
if password != "" {
|
if password != "" {
|
||||||
if err = dbUpdateUserPassword(email, password); err != nil {
|
if err = db.updateUserPassword(email, password); err != nil {
|
||||||
page.session.setFlashMessage(err.Error(), "error")
|
page.session.setFlashMessage(err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("User "+email+" created!", "success")
|
page.session.setFlashMessage("User "+email+" created!", "success")
|
||||||
@ -86,8 +86,8 @@ func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
}
|
}
|
||||||
case "delete":
|
case "delete":
|
||||||
var err error
|
var err error
|
||||||
if dbIsValidUserEmail(email) {
|
if db.isValidUserEmail(email) {
|
||||||
if err = dbDeleteUser(email); err != nil {
|
if err = db.deleteUser(email); err != nil {
|
||||||
page.session.setFlashMessage(err.Error(), "error")
|
page.session.setFlashMessage(err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("User "+email+" deleted!", "success")
|
page.session.setFlashMessage("User "+email+" deleted!", "success")
|
||||||
@ -96,7 +96,7 @@ func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
redirect("/admin/users", w, req)
|
redirect("/admin/users", w, req)
|
||||||
default:
|
default:
|
||||||
page.SubTitle = "Edit Admin User"
|
page.SubTitle = "Edit Admin User"
|
||||||
if !dbIsValidUserEmail(email) {
|
if !db.isValidUserEmail(email) {
|
||||||
page.session.setFlashMessage("Couldn't find the requested user, please try again.", "error")
|
page.session.setFlashMessage("Couldn't find the requested user, please try again.", "error")
|
||||||
redirect("/admin/users", w, req)
|
redirect("/admin/users", w, req)
|
||||||
}
|
}
|
||||||
@ -107,7 +107,7 @@ func handleAdminUsers(w http.ResponseWriter, req *http.Request, page *pageData)
|
|||||||
type usersPageData struct {
|
type usersPageData struct {
|
||||||
Users []string
|
Users []string
|
||||||
}
|
}
|
||||||
page.TemplateData = usersPageData{Users: dbGetAllUsers()}
|
page.TemplateData = usersPageData{Users: db.getAllUsers()}
|
||||||
|
|
||||||
page.SubTitle = "Admin Users"
|
page.SubTitle = "Admin Users"
|
||||||
page.show("admin-users.html", w)
|
page.show("admin-users.html", w)
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -56,14 +55,12 @@ func getCondorcetResult() []Ranking {
|
|||||||
teamWins[allPairs[i].winner.UUID]++
|
teamWins[allPairs[i].winner.UUID]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(teamWins)
|
|
||||||
|
|
||||||
// Rank them by wins
|
// Rank them by wins
|
||||||
rankedWins := make(map[int][]string)
|
rankedWins := make(map[int][]string)
|
||||||
for k, v := range teamWins {
|
for k, v := range teamWins {
|
||||||
rankedWins[v] = append(rankedWins[v], k)
|
rankedWins[v] = append(rankedWins[v], k)
|
||||||
}
|
}
|
||||||
fmt.Println(rankedWins)
|
|
||||||
currRank := 1
|
currRank := 1
|
||||||
for len(rankedWins) > 0 {
|
for len(rankedWins) > 0 {
|
||||||
topWins := 0
|
topWins := 0
|
||||||
|
59
main.go
59
main.go
@ -23,29 +23,6 @@ import (
|
|||||||
const AppName = "gjvote"
|
const AppName = "gjvote"
|
||||||
const DbName = AppName + ".db"
|
const DbName = AppName + ".db"
|
||||||
|
|
||||||
// SiteData is stuff that stays the same
|
|
||||||
type siteData struct {
|
|
||||||
Title string
|
|
||||||
Port int
|
|
||||||
SessionName string
|
|
||||||
ServerDir string
|
|
||||||
DevMode bool
|
|
||||||
|
|
||||||
CurrentJam string
|
|
||||||
|
|
||||||
Teams []Team
|
|
||||||
Votes []Vote
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *siteData) getTeamByUUID(uuid string) *Team {
|
|
||||||
for i := range s.Teams {
|
|
||||||
if s.Teams[i].UUID == uuid {
|
|
||||||
return &s.Teams[i]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// pageData is stuff that changes per request
|
// pageData is stuff that changes per request
|
||||||
type pageData struct {
|
type pageData struct {
|
||||||
Site *siteData
|
Site *siteData
|
||||||
@ -85,8 +62,9 @@ var site *siteData
|
|||||||
var r *mux.Router
|
var r *mux.Router
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
db = new(gjDatabase)
|
||||||
loadConfig()
|
loadConfig()
|
||||||
dbSaveSiteConfig(site)
|
site.save()
|
||||||
initialize()
|
initialize()
|
||||||
|
|
||||||
r = mux.NewRouter()
|
r = mux.NewRouter()
|
||||||
@ -132,7 +110,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func loadConfig() {
|
func loadConfig() {
|
||||||
site = dbGetSiteConfig()
|
site = db.getSiteConfig()
|
||||||
|
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
for _, v := range os.Args {
|
for _, v := range os.Args {
|
||||||
@ -177,9 +155,9 @@ func loadConfig() {
|
|||||||
|
|
||||||
func initialize() {
|
func initialize() {
|
||||||
// Check if the database has been created
|
// Check if the database has been created
|
||||||
assertError(initDatabase())
|
assertError(db.initialize())
|
||||||
|
|
||||||
if !dbHasUser() {
|
if !db.hasUser() {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Println("Create new Admin user")
|
fmt.Println("Create new Admin user")
|
||||||
fmt.Print("Email: ")
|
fmt.Print("Email: ")
|
||||||
@ -197,20 +175,20 @@ func initialize() {
|
|||||||
fmt.Println("Entered Passwords don't match!")
|
fmt.Println("Entered Passwords don't match!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertError(dbUpdateUserPassword(email, string(pw1)))
|
assertError(db.updateUserPassword(email, string(pw1)))
|
||||||
}
|
}
|
||||||
if !dbHasCurrentJam() {
|
if !db.hasCurrentJam() {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Println("Create New Game Jam")
|
fmt.Println("Create New Game Jam")
|
||||||
fmt.Print("GameJam Name: ")
|
fmt.Print("GameJam Name: ")
|
||||||
gjName, _ := reader.ReadString('\n')
|
gjName, _ := reader.ReadString('\n')
|
||||||
gjName = strings.TrimSpace(gjName)
|
gjName = strings.TrimSpace(gjName)
|
||||||
if dbSetCurrentJam(gjName) != nil {
|
if db.setCurrentJam(gjName) != nil {
|
||||||
fmt.Println("Error saving Current Jam")
|
fmt.Println("Error saving Current Jam")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jmNm, err := dbGetCurrentJam()
|
jmNm, err := db.getCurrentJam()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println("Current Jam Name: " + jmNm)
|
fmt.Println("Current Jam Name: " + jmNm)
|
||||||
} else {
|
} else {
|
||||||
@ -218,8 +196,8 @@ func initialize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load all votes into memory
|
// Load all votes into memory
|
||||||
site.Votes = dbGetAllVotes()
|
site.Votes = db.getAllVotes()
|
||||||
site.Teams = dbGetAllTeams()
|
site.Teams = db.getAllTeams()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loggingHandler(h http.Handler) http.Handler {
|
func loggingHandler(h http.Handler) http.Handler {
|
||||||
@ -246,7 +224,7 @@ func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
|
|||||||
// First check if we're logged in
|
// First check if we're logged in
|
||||||
userEmail, _ := p.session.getStringValue("email")
|
userEmail, _ := p.session.getStringValue("email")
|
||||||
// With a valid account
|
// With a valid account
|
||||||
p.LoggedIn = dbIsValidUserEmail(userEmail)
|
p.LoggedIn = db.isValidUserEmail(userEmail)
|
||||||
|
|
||||||
p.Site = site
|
p.Site = site
|
||||||
p.SubTitle = "GameJam Voting"
|
p.SubTitle = "GameJam Voting"
|
||||||
@ -282,19 +260,20 @@ func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
|
|||||||
}
|
}
|
||||||
p.HideAdminMenu = true
|
p.HideAdminMenu = true
|
||||||
|
|
||||||
if p.CurrentJam, err = dbGetCurrentJam(); err != nil {
|
if p.CurrentJam, err = db.getCurrentJam(); err != nil {
|
||||||
p.FlashMessage = "Error Loading Current GameJam: " + err.Error()
|
p.FlashMessage = "Error Loading Current GameJam: " + err.Error()
|
||||||
p.FlashClass = "error"
|
p.FlashClass = "error"
|
||||||
}
|
}
|
||||||
|
|
||||||
p.ClientId = p.session.getClientId()
|
p.ClientId = p.session.getClientId()
|
||||||
p.ClientIsAuth = clientIsAuthenticated(p.ClientId, req)
|
cl := db.getClient(p.ClientId)
|
||||||
|
p.ClientIsAuth = cl.Auth
|
||||||
p.ClientIsServer = clientIsServer(req)
|
p.ClientIsServer = clientIsServer(req)
|
||||||
|
|
||||||
// Public Mode
|
// Public Mode
|
||||||
p.PublicMode = dbGetPublicSiteMode()
|
p.PublicMode = db.getPublicSiteMode()
|
||||||
// Authentication Mode
|
// Authentication Mode
|
||||||
p.AuthMode = dbGetAuthMode()
|
p.AuthMode = db.getAuthMode()
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -330,7 +309,7 @@ func redirect(url string, w http.ResponseWriter, req *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func resetToDefaults() {
|
func resetToDefaults() {
|
||||||
def := GetDefaultSiteConfig()
|
def := NewSiteData()
|
||||||
fmt.Println("Reset settings to defaults?")
|
fmt.Println("Reset settings to defaults?")
|
||||||
fmt.Print(site.Title, " -> ", def.Title, "\n")
|
fmt.Print(site.Title, " -> ", def.Title, "\n")
|
||||||
fmt.Print(site.Port, " -> ", def.Port, "\n")
|
fmt.Print(site.Port, " -> ", def.Port, "\n")
|
||||||
@ -341,7 +320,7 @@ func resetToDefaults() {
|
|||||||
conf, _ := reader.ReadString('\n')
|
conf, _ := reader.ReadString('\n')
|
||||||
conf = strings.ToUpper(strings.TrimSpace(conf))
|
conf = strings.ToUpper(strings.TrimSpace(conf))
|
||||||
if strings.HasPrefix(conf, "Y") {
|
if strings.HasPrefix(conf, "Y") {
|
||||||
if dbSaveSiteConfig(def) != nil {
|
if def.save() != nil {
|
||||||
errorExit("Error resetting to defaults")
|
errorExit("Error resetting to defaults")
|
||||||
}
|
}
|
||||||
fmt.Println("Reset to defaults")
|
fmt.Println("Reset to defaults")
|
||||||
|
119
model.go
119
model.go
@ -7,14 +7,12 @@ import (
|
|||||||
"github.com/br0xen/boltease"
|
"github.com/br0xen/boltease"
|
||||||
)
|
)
|
||||||
|
|
||||||
var db *boltease.DB
|
type gjDatabase struct {
|
||||||
var dbOpened int
|
bolt *boltease.DB
|
||||||
|
dbOpened int
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
var db *gjDatabase
|
||||||
SiteModeWaiting = iota
|
|
||||||
SiteModeVoting
|
|
||||||
SiteModeError
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AuthModeAuthentication = iota
|
AuthModeAuthentication = iota
|
||||||
@ -22,20 +20,11 @@ const (
|
|||||||
AuthModeError
|
AuthModeError
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetDefaultSiteConfig() *siteData {
|
func (db *gjDatabase) open() error {
|
||||||
ret := new(siteData)
|
db.dbOpened += 1
|
||||||
ret.Title = "ICT GameJam"
|
if db.dbOpened == 1 {
|
||||||
ret.Port = 8080
|
|
||||||
ret.SessionName = "ict-gamejam"
|
|
||||||
ret.ServerDir = "./"
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func openDatabase() error {
|
|
||||||
dbOpened += 1
|
|
||||||
if dbOpened == 1 {
|
|
||||||
var err error
|
var err error
|
||||||
db, err = boltease.Create(DbName, 0600, nil)
|
db.bolt, err = boltease.Create(DbName, 0600, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -43,60 +32,60 @@ func openDatabase() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func closeDatabase() error {
|
func (db *gjDatabase) close() error {
|
||||||
dbOpened -= 1
|
db.dbOpened -= 1
|
||||||
if dbOpened == 0 {
|
if db.dbOpened == 0 {
|
||||||
return db.CloseDB()
|
return db.bolt.CloseDB()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDatabase() error {
|
func (db *gjDatabase) initialize() error {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
// Create the path to the bucket to store admin users
|
// Create the path to the bucket to store admin users
|
||||||
if err := db.MkBucketPath([]string{"users"}); err != nil {
|
if err := db.bolt.MkBucketPath([]string{"users"}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Create the path to the bucket to store jam informations
|
// Create the path to the bucket to store jam informations
|
||||||
if err := db.MkBucketPath([]string{"jam"}); err != nil {
|
if err := db.bolt.MkBucketPath([]string{"jam"}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Create the path to the bucket to store site config data
|
// Create the path to the bucket to store site config data
|
||||||
return db.MkBucketPath([]string{"site"})
|
return db.bolt.MkBucketPath([]string{"site"})
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbSetCurrentJam(name string) error {
|
func (db *gjDatabase) setCurrentJam(name string) error {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
return db.SetValue([]string{"site"}, "current-jam", name)
|
return db.bolt.SetValue([]string{"site"}, "current-jam", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbHasCurrentJam() bool {
|
func (db *gjDatabase) hasCurrentJam() bool {
|
||||||
var err error
|
var err error
|
||||||
if _, err = dbGetCurrentJam(); err != nil {
|
if _, err = db.getCurrentJam(); err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetCurrentJam() (string, error) {
|
func (db *gjDatabase) getCurrentJam() (string, error) {
|
||||||
var ret string
|
var ret string
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
ret, err = db.GetValue([]string{"site"}, "current-jam")
|
ret, err = db.bolt.GetValue([]string{"site"}, "current-jam")
|
||||||
|
|
||||||
if err == nil && strings.TrimSpace(ret) == "" {
|
if err == nil && strings.TrimSpace(ret) == "" {
|
||||||
return ret, errors.New("No Jam Name Specified")
|
return ret, errors.New("No Jam Name Specified")
|
||||||
@ -104,69 +93,49 @@ func dbGetCurrentJam() (string, error) {
|
|||||||
return ret, err
|
return ret, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetSiteConfig() *siteData {
|
func (db *gjDatabase) getSiteConfig() *siteData {
|
||||||
var ret *siteData
|
var ret *siteData
|
||||||
def := GetDefaultSiteConfig()
|
def := NewSiteData()
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return def
|
return def
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
ret = new(siteData)
|
ret = new(siteData)
|
||||||
siteConf := []string{"site"}
|
siteConf := []string{"site"}
|
||||||
if ret.Title, err = db.GetValue(siteConf, "title"); err != nil {
|
if ret.Title, err = db.bolt.GetValue(siteConf, "title"); err != nil {
|
||||||
ret.Title = def.Title
|
ret.Title = def.Title
|
||||||
}
|
}
|
||||||
if ret.Port, err = db.GetInt(siteConf, "port"); err != nil {
|
if ret.Port, err = db.bolt.GetInt(siteConf, "port"); err != nil {
|
||||||
ret.Port = def.Port
|
ret.Port = def.Port
|
||||||
}
|
}
|
||||||
if ret.SessionName, err = db.GetValue(siteConf, "session-name"); err != nil {
|
if ret.SessionName, err = db.bolt.GetValue(siteConf, "session-name"); err != nil {
|
||||||
ret.SessionName = def.SessionName
|
ret.SessionName = def.SessionName
|
||||||
}
|
}
|
||||||
if ret.ServerDir, err = db.GetValue(siteConf, "server-dir"); err != nil {
|
if ret.ServerDir, err = db.bolt.GetValue(siteConf, "server-dir"); err != nil {
|
||||||
ret.ServerDir = def.ServerDir
|
ret.ServerDir = def.ServerDir
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbSaveSiteConfig(dat *siteData) error {
|
func (db *gjDatabase) getAuthMode() int {
|
||||||
var err error
|
if ret, err := db.bolt.GetInt([]string{"site"}, "auth-mode"); err != nil {
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
siteConf := []string{"site"}
|
|
||||||
if err = db.SetValue(siteConf, "title", dat.Title); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = db.SetInt(siteConf, "port", dat.Port); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = db.SetValue(siteConf, "session-name", dat.SessionName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return db.SetValue(siteConf, "server-dir", dat.ServerDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetAuthMode() int {
|
|
||||||
if ret, err := db.GetInt([]string{"site"}, "auth-mode"); err != nil {
|
|
||||||
return AuthModeAuthentication
|
return AuthModeAuthentication
|
||||||
} else {
|
} else {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbSetAuthMode(mode int) error {
|
func (db *gjDatabase) setAuthMode(mode int) error {
|
||||||
if mode < 0 || mode >= AuthModeError {
|
if mode < 0 || mode >= AuthModeError {
|
||||||
return errors.New("Invalid site mode")
|
return errors.New("Invalid site mode")
|
||||||
}
|
}
|
||||||
return db.SetInt([]string{"site"}, "auth-mode", mode)
|
return db.bolt.SetInt([]string{"site"}, "auth-mode", mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetPublicSiteMode() int {
|
func (db *gjDatabase) getPublicSiteMode() int {
|
||||||
if ret, err := db.GetInt([]string{"site"}, "public-mode"); err != nil {
|
if ret, err := db.bolt.GetInt([]string{"site"}, "public-mode"); err != nil {
|
||||||
return SiteModeWaiting
|
return SiteModeWaiting
|
||||||
} else {
|
} else {
|
||||||
return ret
|
return ret
|
||||||
@ -177,5 +146,5 @@ func dbSetPublicSiteMode(mode int) error {
|
|||||||
if mode < 0 || mode >= SiteModeError {
|
if mode < 0 || mode >= SiteModeError {
|
||||||
return errors.New("Invalid site mode")
|
return errors.New("Invalid site mode")
|
||||||
}
|
}
|
||||||
return db.SetInt([]string{"site"}, "public-mode", mode)
|
return db.bolt.SetInt([]string{"site"}, "public-mode", mode)
|
||||||
}
|
}
|
||||||
|
160
model_clients.go
160
model_clients.go
@ -1,5 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
UUID string
|
UUID string
|
||||||
Auth bool
|
Auth bool
|
||||||
@ -7,49 +14,49 @@ type Client struct {
|
|||||||
IP string
|
IP string
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetAllClients() []Client {
|
func (db *gjDatabase) getAllClients() []Client {
|
||||||
var ret []Client
|
var ret []Client
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
var clientUids []string
|
var clientUids []string
|
||||||
if clientUids, err = db.GetBucketList([]string{"clients"}); err != nil {
|
if clientUids, err = db.bolt.GetBucketList([]string{"clients"}); err != nil {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
for _, v := range clientUids {
|
for _, v := range clientUids {
|
||||||
if cl := dbGetClient(v); cl != nil {
|
if cl := db.getClient(v); cl != nil {
|
||||||
ret = append(ret, *cl)
|
ret = append(ret, *cl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetClient(id string) *Client {
|
func (db *gjDatabase) getClient(id string) *Client {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
cl := new(Client)
|
cl := new(Client)
|
||||||
cl.UUID = id
|
cl.UUID = id
|
||||||
cl.Auth = dbClientIsAuth(id)
|
cl.Auth, _ = db.bolt.GetBool([]string{"clients", id}, "auth")
|
||||||
cl.Name, _ = db.GetValue([]string{"clients", id}, "name")
|
cl.Name, _ = db.bolt.GetValue([]string{"clients", id}, "name")
|
||||||
cl.IP, _ = db.GetValue([]string{"clients", id}, "ip")
|
cl.IP, _ = db.bolt.GetValue([]string{"clients", id}, "ip")
|
||||||
return cl
|
return cl
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetClientByIp(ip string) *Client {
|
func (db *gjDatabase) getClientByIp(ip string) *Client {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
allClients := dbGetAllClients()
|
allClients := db.getAllClients()
|
||||||
for i := range allClients {
|
for i := range allClients {
|
||||||
if allClients[i].IP == ip {
|
if allClients[i].IP == ip {
|
||||||
return &allClients[i]
|
return &allClients[i]
|
||||||
@ -58,85 +65,90 @@ func dbGetClientByIp(ip string) *Client {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbSetClientName(cid, name string) error {
|
func (c *Client) save() error {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
err = db.SetValue([]string{"clients", cid}, "name", name)
|
if err = db.bolt.SetBool([]string{"clients", c.UUID}, "auth", c.Auth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetClientName(cid string) string {
|
|
||||||
if err := db.OpenDB(); err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
if err = db.bolt.SetValue([]string{"clients", c.UUID}, "name", c.Name); err != nil {
|
||||||
|
return err
|
||||||
name, _ := db.GetValue([]string{"clients", cid}, "name")
|
}
|
||||||
return name
|
return db.bolt.SetValue([]string{"clients", c.UUID}, "ip", c.IP)
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbAddDeauthClient(cid, ip string) error {
|
func (c *Client) getVotes() []Vote {
|
||||||
|
var ret []Vote
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return ret
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
err = db.SetBool([]string{"clients", cid}, "auth", false)
|
var times []string
|
||||||
if err != nil {
|
votesBkt := []string{"votes", c.UUID}
|
||||||
return err
|
if times, err = db.bolt.GetBucketList(votesBkt); err != nil {
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
return db.SetValue([]string{"clients", cid}, "ip", ip)
|
for _, t := range times {
|
||||||
|
var tm time.Time
|
||||||
|
if tm, err = time.Parse(time.RFC3339, t); err == nil {
|
||||||
|
var vt *Vote
|
||||||
|
if vt, err = c.getVote(tm); err == nil {
|
||||||
|
ret = append(ret, *vt)
|
||||||
|
} else {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbAuthClient(cid, ip string) error {
|
func (c *Client) getVote(timestamp time.Time) (*Vote, error) {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
err = db.SetBool([]string{"clients", cid}, "auth", true)
|
vt := new(Vote)
|
||||||
if err != nil {
|
vt.Timestamp = timestamp
|
||||||
return err
|
vt.ClientId = c.UUID
|
||||||
|
votesBkt := []string{"votes", c.UUID, timestamp.Format(time.RFC3339)}
|
||||||
|
var choices []string
|
||||||
|
if choices, err = db.bolt.GetKeyList(votesBkt); err != nil {
|
||||||
|
// Couldn't find the vote...
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return db.SetValue([]string{"clients", cid}, "ip", ip)
|
for _, v := range choices {
|
||||||
|
ch := new(GameChoice)
|
||||||
|
var rank int
|
||||||
|
|
||||||
|
if rank, err = strconv.Atoi(v); err == nil {
|
||||||
|
ch.Rank = rank
|
||||||
|
ch.Team, err = db.bolt.GetValue(votesBkt, v)
|
||||||
|
vt.Choices = append(vt.Choices, *ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vt, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbDeAuthClient(cid string) error {
|
func (c *Client) saveVote(timestamp time.Time, votes []string) error {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
// Make sure we don't clobber a duplicate vote
|
||||||
|
votesBkt := []string{"votes", c.UUID, timestamp.Format(time.RFC3339)}
|
||||||
|
for i := range votes {
|
||||||
|
if strings.TrimSpace(votes[i]) != "" {
|
||||||
|
db.bolt.SetValue(votesBkt, strconv.Itoa(i), votes[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
|
|
||||||
return db.SetBool([]string{"clients", cid}, "auth", false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbClientIsAuth(cid string) bool {
|
|
||||||
var err error
|
|
||||||
if err = db.OpenDB(); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
|
|
||||||
var isAuth bool
|
|
||||||
if isAuth, err = db.GetBool([]string{"clients", cid}, "auth"); err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return isAuth
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbUpdateClientIP(cid, ip string) error {
|
|
||||||
var err error
|
|
||||||
if err = db.OpenDB(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
|
|
||||||
return db.SetValue([]string{"clients", cid}, "ip", ip)
|
|
||||||
}
|
}
|
||||||
|
159
model_games.go
159
model_games.go
@ -1,10 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "errors"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/pborman/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Game struct {
|
type Game struct {
|
||||||
Name string
|
Name string
|
||||||
@ -22,150 +18,61 @@ type Screenshot struct {
|
|||||||
Filetype string
|
Filetype string
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbUpdateTeamGame(teamId, name, link, desc string) error {
|
// Create a new game object, must have a valid team id
|
||||||
|
func newGame(tmId string) *Game {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
// Make sure the team is valid
|
tm := db.getTeam(tmId)
|
||||||
tm := dbGetTeam(teamId)
|
|
||||||
if tm == nil {
|
if tm == nil {
|
||||||
return errors.New("Invalid team")
|
return nil
|
||||||
}
|
}
|
||||||
gamePath := []string{"teams", teamId, "game"}
|
return &Game{TeamId: tmId}
|
||||||
|
}
|
||||||
|
|
||||||
if err := db.MkBucketPath(gamePath); err != nil {
|
func (gm *Game) save() error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if name == "" {
|
defer db.close()
|
||||||
name = tm.Name + "'s Game"
|
|
||||||
|
tm := db.getTeam(gm.TeamId)
|
||||||
|
if tm == nil {
|
||||||
|
return errors.New("Invalid Team: " + gm.TeamId)
|
||||||
}
|
}
|
||||||
if err := db.SetValue(gamePath, "name", name); err != nil {
|
gamePath := []string{"teams", gm.TeamId, "game"}
|
||||||
|
if err := db.bolt.MkBucketPath(gamePath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.SetValue(gamePath, "link", link); err != nil {
|
|
||||||
|
if gm.Name == "" {
|
||||||
|
gm.Name = tm.Name + "'s Game"
|
||||||
|
}
|
||||||
|
if err := db.bolt.SetValue(gamePath, "name", gm.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.SetValue(gamePath, "description", desc); err != nil {
|
if err := db.bolt.SetValue(gamePath, "link", gm.Link); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.MkBucketPath(append(gamePath, "screenshots")); err != nil {
|
if err := db.bolt.SetValue(gamePath, "description", gm.Description); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := db.bolt.MkBucketPath(append(gamePath, "screenshots")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetAllGames() []Game {
|
func (db *gjDatabase) getAllGames() []Game {
|
||||||
var ret []Game
|
var ret []Game
|
||||||
tms := dbGetAllTeams()
|
tms := db.getAllTeams()
|
||||||
for i := range tms {
|
for i := range tms {
|
||||||
ret = append(ret, *dbGetTeamGame(tms[i].UUID))
|
ret = append(ret, *tms[i].getGame())
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetTeamGame(teamId string) *Game {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
gamePath := []string{"teams", teamId, "game"}
|
|
||||||
gm := new(Game)
|
|
||||||
if gm.Name, err = db.GetValue(gamePath, "name"); err != nil {
|
|
||||||
gm.Name = ""
|
|
||||||
}
|
|
||||||
gm.TeamId = teamId
|
|
||||||
if gm.Description, err = db.GetValue(gamePath, "description"); err != nil {
|
|
||||||
gm.Description = ""
|
|
||||||
}
|
|
||||||
if gm.Link, err = db.GetValue(gamePath, "link"); err != nil {
|
|
||||||
gm.Link = ""
|
|
||||||
}
|
|
||||||
gm.Screenshots = dbGetTeamGameScreenshots(teamId)
|
|
||||||
return gm
|
|
||||||
}
|
|
||||||
|
|
||||||
// Screenshots are saved as base64 encoded pngs
|
|
||||||
func dbGetTeamGameScreenshots(teamId string) []Screenshot {
|
|
||||||
var ret []Screenshot
|
|
||||||
var err error
|
|
||||||
ssPath := []string{"teams", teamId, "game", "screenshots"}
|
|
||||||
var ssIds []string
|
|
||||||
if ssIds, err = db.GetBucketList(ssPath); err != nil {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
for _, v := range ssIds {
|
|
||||||
if ss := dbGetTeamGameScreenshot(teamId, v); ss != nil {
|
|
||||||
ret = append(ret, *ss)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetTeamGameScreenshot(teamId, ssId string) *Screenshot {
|
|
||||||
var err error
|
|
||||||
ssPath := []string{"teams", teamId, "game", "screenshots", ssId}
|
|
||||||
ret := new(Screenshot)
|
|
||||||
ret.UUID = ssId
|
|
||||||
if ret.Description, err = db.GetValue(ssPath, "description"); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if ret.Image, err = db.GetValue(ssPath, "image"); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if ret.Thumbnail, err = db.GetValue(ssPath, "thumbnail"); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
if ret.Thumbnail == "" {
|
|
||||||
ret.Thumbnail = ret.Image
|
|
||||||
}
|
|
||||||
if ret.Filetype, err = db.GetValue(ssPath, "filetype"); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbSaveTeamGameScreenshot(teamId string, ss *Screenshot) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
ssPath := []string{"teams", teamId, "game", "screenshots"}
|
|
||||||
// Generate a UUID for this screenshot
|
|
||||||
uuid := uuid.New()
|
|
||||||
ssPath = append(ssPath, uuid)
|
|
||||||
if err := db.MkBucketPath(ssPath); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := db.SetValue(ssPath, "description", ss.Description); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := db.SetValue(ssPath, "image", ss.Image); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := db.SetValue(ssPath, "thumbnail", ss.Thumbnail); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := db.SetValue(ssPath, "filetype", ss.Filetype); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbDeleteTeamGameScreenshot(teamId, ssId string) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
ssPath := []string{"teams", teamId, "game", "screenshots"}
|
|
||||||
return db.DeleteBucket(ssPath, ssId)
|
|
||||||
}
|
|
||||||
|
62
model_site.go
Normal file
62
model_site.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
const (
|
||||||
|
SiteModeWaiting = iota
|
||||||
|
SiteModeVoting
|
||||||
|
SiteModeError
|
||||||
|
)
|
||||||
|
|
||||||
|
// SiteData is stuff that stays the same
|
||||||
|
type siteData struct {
|
||||||
|
Title string
|
||||||
|
Port int
|
||||||
|
SessionName string
|
||||||
|
ServerDir string
|
||||||
|
DevMode bool
|
||||||
|
Mode int
|
||||||
|
|
||||||
|
CurrentJam string
|
||||||
|
|
||||||
|
Teams []Team
|
||||||
|
Votes []Vote
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSiteData returns a siteData object with the default values
|
||||||
|
func NewSiteData() *siteData {
|
||||||
|
ret := new(siteData)
|
||||||
|
ret.Title = "ICT GameJam"
|
||||||
|
ret.Port = 8080
|
||||||
|
ret.SessionName = "ict-gamejam"
|
||||||
|
ret.ServerDir = "./"
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *siteData) getTeamByUUID(uuid string) *Team {
|
||||||
|
for i := range s.Teams {
|
||||||
|
if s.Teams[i].UUID == uuid {
|
||||||
|
return &s.Teams[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// save 's' to the database
|
||||||
|
func (s *siteData) save() error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
siteConf := []string{"site"}
|
||||||
|
if err = db.bolt.SetValue(siteConf, "title", s.Title); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = db.bolt.SetInt(siteConf, "port", s.Port); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err = db.bolt.SetValue(siteConf, "session-name", s.SessionName); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return db.bolt.SetValue(siteConf, "server-dir", s.ServerDir)
|
||||||
|
}
|
525
model_teams.go
525
model_teams.go
@ -13,250 +13,68 @@ type Team struct {
|
|||||||
Game *Game
|
Game *Game
|
||||||
}
|
}
|
||||||
|
|
||||||
type TeamMember struct {
|
// newTeam creates a team with name nm and stores it in the DB
|
||||||
UUID string
|
func (db *gjDatabase) newTeam(nm string) error {
|
||||||
Name string
|
|
||||||
SlackId string
|
|
||||||
Twitter string
|
|
||||||
Email string
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbCreateNewTeam(nm string) error {
|
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
// Generate a UUID
|
// Generate a UUID
|
||||||
uuid := uuid.New()
|
uuid := uuid.New()
|
||||||
teamPath := []string{"teams", uuid}
|
teamPath := []string{"teams", uuid}
|
||||||
|
|
||||||
if err := db.MkBucketPath(teamPath); err != nil {
|
if err := db.bolt.MkBucketPath(teamPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.SetValue(teamPath, "name", nm); err != nil {
|
if err := db.bolt.SetValue(teamPath, "name", nm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.MkBucketPath(append(teamPath, "members")); err != nil {
|
if err := db.bolt.MkBucketPath(append(teamPath, "members")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
gamePath := append(teamPath, "game")
|
gamePath := append(teamPath, "game")
|
||||||
if err := db.MkBucketPath(gamePath); err != nil {
|
if err := db.bolt.MkBucketPath(gamePath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := db.SetValue(append(gamePath), "name", ""); err != nil {
|
if err := db.bolt.SetValue(append(gamePath), "name", ""); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return db.MkBucketPath(append(gamePath, "screenshots"))
|
return db.bolt.MkBucketPath(append(gamePath, "screenshots"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbIsValidTeam(id string) bool {
|
// getTeam returns a team with the given id, or nil
|
||||||
|
func (db *gjDatabase) getTeam(id string) *Team {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams"}
|
|
||||||
if teamUids, err := db.GetBucketList(teamPath); err == nil {
|
|
||||||
for _, v := range teamUids {
|
|
||||||
if v == id {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetAllTeams() []Team {
|
|
||||||
var ret []Team
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams"}
|
|
||||||
var teamUids []string
|
|
||||||
if teamUids, err = db.GetBucketList(teamPath); err != nil {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
for _, v := range teamUids {
|
|
||||||
if tm := dbGetTeam(v); tm != nil {
|
|
||||||
ret = append(ret, *tm)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetTeam(id string) *Team {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
teamPath := []string{"teams", id}
|
teamPath := []string{"teams", id}
|
||||||
tm := new(Team)
|
tm := new(Team)
|
||||||
tm.UUID = id
|
tm.UUID = id
|
||||||
if tm.Name, err = db.GetValue(teamPath, "name"); err != nil {
|
if tm.Name, err = db.bolt.GetValue(teamPath, "name"); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
tm.Members, _ = dbGetTeamMembers(id)
|
tm.Members = tm.getTeamMembers()
|
||||||
tm.Game = dbGetTeamGame(id)
|
tm.Game = tm.getGame()
|
||||||
return tm
|
return tm
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetTeamByName(nm string) *Team {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams"}
|
|
||||||
var teamUids []string
|
|
||||||
if teamUids, err = db.GetBucketList(teamPath); err != nil {
|
|
||||||
for _, v := range teamUids {
|
|
||||||
var name string
|
|
||||||
if name, err = db.GetValue(append(teamPath, v), "name"); name == nm {
|
|
||||||
return dbGetTeam(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbUpdateTeam(id string, tm *Team) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams", id}
|
|
||||||
return db.SetValue(teamPath, "name", tm.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbDeleteTeam(id string) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams"}
|
|
||||||
return db.DeleteBucket(teamPath, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbEditTeamGame(teamid, name string) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
gamePath := []string{"teams", teamid, "game"}
|
|
||||||
return db.SetValue(gamePath, "name", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbAddTeamMember(teamid, mbrName, mbrEmail, mbrSlack, mbrTwitter string) error {
|
|
||||||
// First check if this member already exists on this team
|
|
||||||
mbrs, _ := dbGetTeamMembers(teamid)
|
|
||||||
if len(mbrs) > 0 {
|
|
||||||
for i := range mbrs {
|
|
||||||
if mbrs[i].Name == mbrName {
|
|
||||||
return dbUpdateTeamMember(teamid, mbrs[i].UUID, mbrName, mbrEmail, mbrSlack, mbrTwitter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// It's really an add
|
|
||||||
mbrId := uuid.New()
|
|
||||||
return dbUpdateTeamMember(teamid, mbrId, mbrName, mbrEmail, mbrSlack, mbrTwitter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbUpdateTeamMember(teamid, mbrId, mbrName, mbrEmail, mbrSlack, mbrTwitter string) error {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
mbrPath := []string{"teams", teamid, "members", mbrId}
|
|
||||||
if db.SetValue(mbrPath, "name", mbrName) != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if db.SetValue(mbrPath, "slackid", mbrSlack) != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if db.SetValue(mbrPath, "twitter", mbrTwitter) != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if db.SetValue(mbrPath, "email", mbrEmail) != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetTeamMembers(teamid string) ([]TeamMember, error) {
|
|
||||||
var ret []TeamMember
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return ret, err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
teamPath := []string{"teams", teamid, "members"}
|
|
||||||
var memberUuids []string
|
|
||||||
if memberUuids, err = db.GetBucketList(teamPath); err == nil {
|
|
||||||
for _, v := range memberUuids {
|
|
||||||
var mbr *TeamMember
|
|
||||||
if mbr, err = dbGetTeamMember(teamid, v); err == nil {
|
|
||||||
ret = append(ret, *mbr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetTeamMember(teamid, mbrid string) (*TeamMember, error) {
|
|
||||||
var err error
|
|
||||||
if err = openDatabase(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer closeDatabase()
|
|
||||||
|
|
||||||
mbr := new(TeamMember)
|
|
||||||
teamMbrPath := []string{"teams", teamid, "members", mbrid}
|
|
||||||
mbr.UUID = mbrid
|
|
||||||
if mbr.Name, err = db.GetValue(teamMbrPath, "name"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if mbr.SlackId, err = db.GetValue(teamMbrPath, "slackid"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if mbr.Twitter, err = db.GetValue(teamMbrPath, "twitter"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if mbr.Email, err = db.GetValue(teamMbrPath, "email"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return mbr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function returns the team for a specific member
|
// This function returns the team for a specific member
|
||||||
func dbGetMembersTeam(mbrid string) (*Team, error) {
|
func (db *gjDatabase) getTeamForMember(mbrid string) (*Team, error) {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
teams := dbGetAllTeams()
|
teams := db.getAllTeams()
|
||||||
for i := range teams {
|
for i := range teams {
|
||||||
var tmMbrs []TeamMember
|
var tmMbrs []TeamMember
|
||||||
tmMbrs, err = dbGetTeamMembers(teams[i].UUID)
|
tmMbrs = teams[i].getTeamMembers()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
for j := range tmMbrs {
|
for j := range tmMbrs {
|
||||||
if tmMbrs[j].UUID == mbrid {
|
if tmMbrs[j].UUID == mbrid {
|
||||||
@ -268,40 +86,289 @@ func dbGetMembersTeam(mbrid string) (*Team, error) {
|
|||||||
return nil, errors.New("Unable to find team member")
|
return nil, errors.New("Unable to find team member")
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function searches all teams for a member with the given name
|
// getAllTeams returns all teams in the database
|
||||||
func dbGetTeamMembersByName(mbrName string) ([]TeamMember, error) {
|
func (db *gjDatabase) getAllTeams() []Team {
|
||||||
var ret []TeamMember
|
var ret []Team
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return ret, err
|
return ret
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
teams := dbGetAllTeams()
|
teamPath := []string{"teams"}
|
||||||
for i := range teams {
|
var teamUids []string
|
||||||
var tmMbrs []TeamMember
|
if teamUids, err = db.bolt.GetBucketList(teamPath); err != nil {
|
||||||
tmMbrs, err = dbGetTeamMembers(teams[i].UUID)
|
return ret
|
||||||
if err == nil {
|
}
|
||||||
for j := range tmMbrs {
|
for _, v := range teamUids {
|
||||||
if tmMbrs[j].Name == mbrName {
|
if tm := db.getTeam(v); tm != nil {
|
||||||
ret = append(ret, tmMbrs[j])
|
ret = append(ret, *tm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return ret
|
||||||
}
|
|
||||||
if len(ret) == 0 {
|
|
||||||
return nil, errors.New("Couldn't find any members with the requested name")
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbDeleteTeamMember(teamId, mbrId string) error {
|
// getTeamByName returns a team with the given name or nil
|
||||||
|
func (db *gjDatabase) getTeamByName(nm string) *Team {
|
||||||
var err error
|
var err error
|
||||||
if err = openDatabase(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
teamPath := []string{"teams"}
|
||||||
|
var teamUids []string
|
||||||
|
if teamUids, err = db.bolt.GetBucketList(teamPath); err != nil {
|
||||||
|
for _, v := range teamUids {
|
||||||
|
var name string
|
||||||
|
if name, err = db.bolt.GetValue(append(teamPath, v), "name"); name == nm {
|
||||||
|
return db.getTeam(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// save saves the team to the db
|
||||||
|
func (tm *Team) save() error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer closeDatabase()
|
defer db.close()
|
||||||
|
|
||||||
teamPath := []string{"teams", teamId, "members"}
|
teamPath := []string{"teams", tm.UUID}
|
||||||
return db.DeleteBucket(teamPath, mbrId)
|
if err = db.bolt.SetValue(teamPath, "name", tm.Name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Save Team Members
|
||||||
|
// TODO: Save Team Game
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete removes the team from the database
|
||||||
|
func (tm *Team) delete() error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
teamPath := []string{"teams"}
|
||||||
|
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) getScreenshots() []Screenshot {
|
||||||
|
var ret []Screenshot
|
||||||
|
var err error
|
||||||
|
ssPath := []string{"teams", tm.UUID, "game", "screenshots"}
|
||||||
|
var ssIds []string
|
||||||
|
if ssIds, err = db.bolt.GetBucketList(ssPath); err != nil {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
for _, v := range ssIds {
|
||||||
|
if ss := tm.getScreenshot(v); ss != nil {
|
||||||
|
ret = append(ret, *ss)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *Team) getScreenshot(ssId string) *Screenshot {
|
||||||
|
var err error
|
||||||
|
ssPath := []string{"teams", tm.UUID, "game", "screenshots", ssId}
|
||||||
|
ret := new(Screenshot)
|
||||||
|
ret.UUID = ssId
|
||||||
|
if ret.Description, err = db.bolt.GetValue(ssPath, "description"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ret.Image, err = db.bolt.GetValue(ssPath, "image"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ret.Thumbnail, err = db.bolt.GetValue(ssPath, "thumbnail"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if ret.Thumbnail == "" {
|
||||||
|
ret.Thumbnail = ret.Image
|
||||||
|
}
|
||||||
|
if ret.Filetype, err = db.bolt.GetValue(ssPath, "filetype"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TeamMember struct {
|
||||||
|
UUID string
|
||||||
|
Name string
|
||||||
|
SlackId string
|
||||||
|
Twitter string
|
||||||
|
Email string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a new team member, only a name is required
|
||||||
|
func newTeamMember(nm string) *TeamMember {
|
||||||
|
m := TeamMember{Name: nm}
|
||||||
|
return &m
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
if err = db.open(); err != nil {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tm *Team) updateTeamMember(mbr *TeamMember) error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if mbr.UUID == "" {
|
||||||
|
// It's really a new one
|
||||||
|
mbr.UUID = uuid.New()
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteTeamMember removes a member from the database
|
||||||
|
func (tm *Team) deleteTeamMember(mbr *TeamMember) error {
|
||||||
|
var err error
|
||||||
|
if err = db.open(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.close()
|
||||||
|
|
||||||
|
teamPath := []string{"teams", tm.UUID, "members"}
|
||||||
|
return db.bolt.DeleteBucket(teamPath, mbr.UUID)
|
||||||
}
|
}
|
||||||
|
@ -4,73 +4,73 @@ import "golang.org/x/crypto/bcrypt"
|
|||||||
|
|
||||||
// dbHasUser
|
// dbHasUser
|
||||||
// Returns true if there are any users in the database
|
// Returns true if there are any users in the database
|
||||||
func dbHasUser() bool {
|
func (db *gjDatabase) hasUser() bool {
|
||||||
return len(dbGetAllUsers()) > 0
|
return len(db.getAllUsers()) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetAllUsers() []string {
|
func (db *gjDatabase) getAllUsers() []string {
|
||||||
if err := db.OpenDB(); err != nil {
|
if err := db.open(); err != nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
usrs, err := db.GetBucketList([]string{"users"})
|
usrs, err := db.bolt.GetBucketList([]string{"users"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}
|
return []string{}
|
||||||
}
|
}
|
||||||
return usrs
|
return usrs
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbIsValidUserEmail(email string) bool {
|
func (db *gjDatabase) isValidUserEmail(email string) bool {
|
||||||
if err := db.OpenDB(); err != nil {
|
if err := db.open(); err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
usrPath := []string{"users", email}
|
usrPath := []string{"users", email}
|
||||||
_, err := db.GetValue(usrPath, "password")
|
_, err := db.bolt.GetValue(usrPath, "password")
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbCheckCredentials(email, pw string) error {
|
func (db *gjDatabase) checkCredentials(email, pw string) error {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
var uPw string
|
var uPw string
|
||||||
usrPath := []string{"users", email}
|
usrPath := []string{"users", email}
|
||||||
if uPw, err = db.GetValue(usrPath, "password"); err != nil {
|
if uPw, err = db.bolt.GetValue(usrPath, "password"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return bcrypt.CompareHashAndPassword([]byte(uPw), []byte(pw))
|
return bcrypt.CompareHashAndPassword([]byte(uPw), []byte(pw))
|
||||||
}
|
}
|
||||||
|
|
||||||
// dbUpdateUserPassword
|
// updateUserPassword
|
||||||
// Takes an email address and a password
|
// Takes an email address and a password
|
||||||
// Creates the user if it doesn't exist, encrypts the password
|
// Creates the user if it doesn't exist, encrypts the password
|
||||||
// and updates it in the db
|
// and updates it in the db
|
||||||
func dbUpdateUserPassword(email, password string) error {
|
func (db *gjDatabase) updateUserPassword(email, password string) error {
|
||||||
cryptPw, cryptError := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
cryptPw, cryptError := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||||
if cryptError != nil {
|
if cryptError != nil {
|
||||||
return cryptError
|
return cryptError
|
||||||
}
|
}
|
||||||
if err := db.OpenDB(); err != nil {
|
if err := db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
usrPath := []string{"users", email}
|
usrPath := []string{"users", email}
|
||||||
return db.SetValue(usrPath, "password", string(cryptPw))
|
return db.bolt.SetValue(usrPath, "password", string(cryptPw))
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbDeleteUser(email string) error {
|
func (db *gjDatabase) deleteUser(email string) error {
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
return db.DeleteBucket([]string{"users"}, email)
|
return db.bolt.DeleteBucket([]string{"users"}, email)
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A Choice is a ranking of a game in a vote
|
// A Choice is a ranking of a game in a vote
|
||||||
type GameChoice struct {
|
type GameChoice struct {
|
||||||
@ -20,94 +15,17 @@ type Vote struct {
|
|||||||
Choices []GameChoice
|
Choices []GameChoice
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetAllVotes() []Vote {
|
func (db *gjDatabase) getAllVotes() []Vote {
|
||||||
var ret []Vote
|
var ret []Vote
|
||||||
var err error
|
var err error
|
||||||
if err = db.OpenDB(); err != nil {
|
if err = db.open(); err != nil {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
defer db.CloseDB()
|
defer db.close()
|
||||||
|
|
||||||
votesBkt := []string{"votes"}
|
clients := db.getAllClients()
|
||||||
var clients []string
|
for _, cl := range clients {
|
||||||
if clients, err = db.GetBucketList(votesBkt); err != nil {
|
ret = append(ret, cl.getVotes()...)
|
||||||
// Couldn't get the list of clients
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
for _, clid := range clients {
|
|
||||||
ret = append(ret, dbGetClientVotes(clid)...)
|
|
||||||
}
|
}
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func dbGetClientVotes(clientId string) []Vote {
|
|
||||||
var ret []Vote
|
|
||||||
var err error
|
|
||||||
if err = db.OpenDB(); err != nil {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
|
|
||||||
var times []string
|
|
||||||
votesBkt := []string{"votes", clientId}
|
|
||||||
if times, err = db.GetBucketList(votesBkt); err != nil {
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
for _, t := range times {
|
|
||||||
var tm time.Time
|
|
||||||
if tm, err = time.Parse(time.RFC3339, t); err == nil {
|
|
||||||
var vt *Vote
|
|
||||||
if vt, err = dbGetVote(clientId, tm); err == nil {
|
|
||||||
ret = append(ret, *vt)
|
|
||||||
} else {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbGetVote(clientId string, timestamp time.Time) (*Vote, error) {
|
|
||||||
var err error
|
|
||||||
if err = db.OpenDB(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
|
|
||||||
vt := new(Vote)
|
|
||||||
vt.Timestamp = timestamp
|
|
||||||
vt.ClientId = clientId
|
|
||||||
votesBkt := []string{"votes", clientId, timestamp.Format(time.RFC3339)}
|
|
||||||
var choices []string
|
|
||||||
if choices, err = db.GetKeyList(votesBkt); err != nil {
|
|
||||||
// Couldn't find the vote...
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
for _, v := range choices {
|
|
||||||
ch := new(GameChoice)
|
|
||||||
var rank int
|
|
||||||
|
|
||||||
if rank, err = strconv.Atoi(v); err == nil {
|
|
||||||
ch.Rank = rank
|
|
||||||
ch.Team, err = db.GetValue(votesBkt, v)
|
|
||||||
vt.Choices = append(vt.Choices, *ch)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return vt, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func dbSaveVote(clientId string, timestamp time.Time, votes []string) error {
|
|
||||||
var err error
|
|
||||||
if err = db.OpenDB(); err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
defer db.CloseDB()
|
|
||||||
// Make sure we don't clobber a duplicate vote
|
|
||||||
votesBkt := []string{"votes", clientId, timestamp.Format(time.RFC3339)}
|
|
||||||
for i := range votes {
|
|
||||||
if strings.TrimSpace(votes[i]) != "" {
|
|
||||||
db.SetValue(votesBkt, strconv.Itoa(i), votes[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
@ -43,7 +43,7 @@ func (p *pageSession) getClientId() string {
|
|||||||
fmt.Println(" Client IP:" + clientIp)
|
fmt.Println(" Client IP:" + clientIp)
|
||||||
if clientIp != "127.0.0.1" {
|
if clientIp != "127.0.0.1" {
|
||||||
fmt.Println(" Pulling data by IP")
|
fmt.Println(" Pulling data by IP")
|
||||||
cli = dbGetClientByIp(clientIp)
|
cli = db.getClientByIp(clientIp)
|
||||||
}
|
}
|
||||||
if cli != nil {
|
if cli != nil {
|
||||||
clientId = cli.UUID
|
clientId = cli.UUID
|
||||||
|
@ -17,7 +17,7 @@ func initPublicPage(w http.ResponseWriter, req *http.Request) *pageData {
|
|||||||
|
|
||||||
func handleMain(w http.ResponseWriter, req *http.Request) {
|
func handleMain(w http.ResponseWriter, req *http.Request) {
|
||||||
page := initPublicPage(w, req)
|
page := initPublicPage(w, req)
|
||||||
if dbGetPublicSiteMode() == SiteModeWaiting {
|
if db.getPublicSiteMode() == SiteModeWaiting {
|
||||||
page.SubTitle = ""
|
page.SubTitle = ""
|
||||||
page.show("public-waiting.html", w)
|
page.show("public-waiting.html", w)
|
||||||
} else {
|
} else {
|
||||||
@ -28,7 +28,7 @@ func handleMain(w http.ResponseWriter, req *http.Request) {
|
|||||||
func loadVotingPage(w http.ResponseWriter, req *http.Request) {
|
func loadVotingPage(w http.ResponseWriter, req *http.Request) {
|
||||||
page := initPublicPage(w, req)
|
page := initPublicPage(w, req)
|
||||||
// Client authentication required
|
// Client authentication required
|
||||||
if (dbGetAuthMode() == AuthModeAuthentication) && !page.ClientIsAuth {
|
if (db.getAuthMode() == AuthModeAuthentication) && !page.ClientIsAuth {
|
||||||
page.show("unauthorized.html", w)
|
page.show("unauthorized.html", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ func loadVotingPage(w http.ResponseWriter, req *http.Request) {
|
|||||||
Timestamp string
|
Timestamp string
|
||||||
}
|
}
|
||||||
vpd := new(votingPageData)
|
vpd := new(votingPageData)
|
||||||
tms := dbGetAllTeams()
|
tms := db.getAllTeams()
|
||||||
|
|
||||||
// Randomize the team list
|
// Randomize the team list
|
||||||
rand.Seed(time.Now().Unix())
|
rand.Seed(time.Now().Unix())
|
||||||
@ -55,7 +55,7 @@ func loadVotingPage(w http.ResponseWriter, req *http.Request) {
|
|||||||
func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
||||||
page := initPublicPage(w, req)
|
page := initPublicPage(w, req)
|
||||||
// Client authentication required
|
// Client authentication required
|
||||||
if (dbGetAuthMode() == AuthModeAuthentication) && !page.ClientIsAuth {
|
if (db.getAuthMode() == AuthModeAuthentication) && !page.ClientIsAuth {
|
||||||
page.show("unauthorized.html", w)
|
page.show("unauthorized.html", w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -69,7 +69,8 @@ func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
|||||||
page.session.setFlashMessage("Error parsing timestamp: "+ts, "error")
|
page.session.setFlashMessage("Error parsing timestamp: "+ts, "error")
|
||||||
redirect("/", w, req)
|
redirect("/", w, req)
|
||||||
}
|
}
|
||||||
if _, err := dbGetVote(page.ClientId, timestamp); err == nil {
|
client := db.getClient(page.ClientId)
|
||||||
|
if _, err := client.getVote(timestamp); err == nil {
|
||||||
// Duplicate vote... Cancel it.
|
// Duplicate vote... Cancel it.
|
||||||
page.session.setFlashMessage("Duplicate vote!", "error")
|
page.session.setFlashMessage("Duplicate vote!", "error")
|
||||||
redirect("/", w, req)
|
redirect("/", w, req)
|
||||||
@ -77,10 +78,10 @@ func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
|||||||
// voteSlice is an ordered string slice of the voters preferences
|
// voteSlice is an ordered string slice of the voters preferences
|
||||||
voteCSV := req.FormValue("uservote")
|
voteCSV := req.FormValue("uservote")
|
||||||
voteSlice := strings.Split(voteCSV, ",")
|
voteSlice := strings.Split(voteCSV, ",")
|
||||||
if err := dbSaveVote(page.ClientId, timestamp, voteSlice); err != nil {
|
if err := client.saveVote(timestamp, voteSlice); err != nil {
|
||||||
page.session.setFlashMessage("Error Saving Vote: "+err.Error(), "error")
|
page.session.setFlashMessage("Error Saving Vote: "+err.Error(), "error")
|
||||||
}
|
}
|
||||||
if newVote, err := dbGetVote(page.ClientId, timestamp); err == nil {
|
if newVote, err := client.getVote(timestamp); err == nil {
|
||||||
site.Votes = append(site.Votes, *newVote)
|
site.Votes = append(site.Votes, *newVote)
|
||||||
}
|
}
|
||||||
page.session.setFlashMessage("Vote Saved!", "success large fading")
|
page.session.setFlashMessage("Vote Saved!", "success large fading")
|
||||||
@ -90,7 +91,12 @@ func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
|||||||
func handleThumbnailRequest(w http.ResponseWriter, req *http.Request) {
|
func handleThumbnailRequest(w http.ResponseWriter, req *http.Request) {
|
||||||
// Thumbnail requests are open even without client authentication
|
// Thumbnail requests are open even without client authentication
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
ss := dbGetTeamGameScreenshot(vars["teamid"], vars["imageid"])
|
tm := db.getTeam(vars["teamid"])
|
||||||
|
if tm == nil {
|
||||||
|
http.Error(w, "Couldn't find image", 404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ss := tm.getScreenshot(vars["imageid"])
|
||||||
if ss == nil {
|
if ss == nil {
|
||||||
http.Error(w, "Couldn't find image", 404)
|
http.Error(w, "Couldn't find image", 404)
|
||||||
return
|
return
|
||||||
@ -107,7 +113,12 @@ func handleThumbnailRequest(w http.ResponseWriter, req *http.Request) {
|
|||||||
func handleImageRequest(w http.ResponseWriter, req *http.Request) {
|
func handleImageRequest(w http.ResponseWriter, req *http.Request) {
|
||||||
// Image requests are open even without client authentication
|
// Image requests are open even without client authentication
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
ss := dbGetTeamGameScreenshot(vars["teamid"], vars["imageid"])
|
tm := db.getTeam(vars["teamid"])
|
||||||
|
if tm == nil {
|
||||||
|
http.Error(w, "Couldn't find image", 404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ss := tm.getScreenshot(vars["imageid"])
|
||||||
if ss == nil {
|
if ss == nil {
|
||||||
http.Error(w, "Couldn't find image", 404)
|
http.Error(w, "Couldn't find image", 404)
|
||||||
return
|
return
|
||||||
@ -123,70 +134,72 @@ func handleImageRequest(w http.ResponseWriter, req *http.Request) {
|
|||||||
|
|
||||||
func handleTeamMgmtRequest(w http.ResponseWriter, req *http.Request) {
|
func handleTeamMgmtRequest(w http.ResponseWriter, req *http.Request) {
|
||||||
// Team Management pages are open even without client authentication
|
// Team Management pages are open even without client authentication
|
||||||
if dbGetPublicSiteMode() == SiteModeVoting {
|
if db.getPublicSiteMode() == SiteModeVoting {
|
||||||
redirect("/", w, req)
|
redirect("/", w, req)
|
||||||
}
|
}
|
||||||
page := initPublicPage(w, req)
|
page := initPublicPage(w, req)
|
||||||
vars := mux.Vars(req)
|
vars := mux.Vars(req)
|
||||||
page.SubTitle = "Team Details"
|
page.SubTitle = "Team Details"
|
||||||
teamId := vars["id"]
|
teamId := vars["id"]
|
||||||
if teamId != "" {
|
tm := db.getTeam(teamId)
|
||||||
|
if tm != nil {
|
||||||
// Team self-management functions
|
// Team self-management functions
|
||||||
if !dbIsValidTeam(teamId) {
|
|
||||||
http.Error(w, "Page Not Found", 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch vars["function"] {
|
switch vars["function"] {
|
||||||
case "":
|
case "":
|
||||||
page.SubTitle = "Team Management"
|
page.SubTitle = "Team Management"
|
||||||
t := dbGetTeam(teamId)
|
page.TemplateData = tm
|
||||||
page.TemplateData = t
|
|
||||||
page.show("public-teammgmt.html", w)
|
page.show("public-teammgmt.html", w)
|
||||||
case "savemember":
|
case "savemember":
|
||||||
mbrName := req.FormValue("newmembername")
|
m := newTeamMember(req.FormValue("newmembername"))
|
||||||
mbrSlack := req.FormValue("newmemberslackid")
|
m.SlackId = req.FormValue("newmemberslackid")
|
||||||
mbrTwitter := req.FormValue("newmembertwitter")
|
m.Twitter = req.FormValue("newmembertwitter")
|
||||||
mbrEmail := req.FormValue("newmemberemail")
|
m.Email = req.FormValue("newmemberemail")
|
||||||
if err := dbAddTeamMember(teamId, mbrName, mbrEmail, mbrSlack, mbrTwitter); err != nil {
|
if err := tm.updateTeamMember(m); err != nil {
|
||||||
page.session.setFlashMessage("Error adding team member: "+err.Error(), "error")
|
page.session.setFlashMessage("Error adding team member: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage(mbrName+" added to team!", "success")
|
page.session.setFlashMessage(m.Name+" added to team!", "success")
|
||||||
}
|
}
|
||||||
refreshTeamsInMemory()
|
refreshTeamsInMemory()
|
||||||
redirect("/team/"+teamId+"#members", w, req)
|
redirect("/team/"+tm.UUID+"#members", w, req)
|
||||||
case "deletemember":
|
case "deletemember":
|
||||||
mbrId := req.FormValue("memberid")
|
mbrId := req.FormValue("memberid")
|
||||||
m, _ := dbGetTeamMember(teamId, mbrId)
|
m := tm.getTeamMember(mbrId)
|
||||||
if err := dbDeleteTeamMember(teamId, mbrId); err != nil {
|
if m != nil {
|
||||||
|
if err := tm.deleteTeamMember(m); err != nil {
|
||||||
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
|
page.session.setFlashMessage("Error deleting team member: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage(m.Name+" deleted from team", "success")
|
page.session.setFlashMessage(m.Name+" deleted from team", "success")
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
page.session.setFlashMessage("Couldn't find member to delete", "error")
|
||||||
|
}
|
||||||
refreshTeamsInMemory()
|
refreshTeamsInMemory()
|
||||||
redirect("/team/"+teamId, w, req)
|
redirect("/team/"+tm.UUID, w, req)
|
||||||
case "savegame":
|
case "savegame":
|
||||||
name := req.FormValue("gamename")
|
gm := newGame(tm.UUID)
|
||||||
link := req.FormValue("gamelink")
|
gm.Name = req.FormValue("gamename")
|
||||||
desc := req.FormValue("gamedesc")
|
gm.Link = req.FormValue("gamelink")
|
||||||
if dbIsValidTeam(teamId) {
|
gm.Description = req.FormValue("gamedesc")
|
||||||
if err := dbUpdateTeamGame(teamId, name, link, desc); err != nil {
|
if err := gm.save(); err != nil {
|
||||||
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
||||||
} else {
|
} else {
|
||||||
page.session.setFlashMessage("Team game updated", "success")
|
page.session.setFlashMessage("Team game updated", "success")
|
||||||
}
|
}
|
||||||
redirect("/team/"+teamId, w, req)
|
redirect("/team/"+tm.UUID, w, req)
|
||||||
}
|
|
||||||
case "screenshotupload":
|
case "screenshotupload":
|
||||||
if err := saveScreenshots(teamId, req); err != nil {
|
if err := saveScreenshots(tm, req); err != nil {
|
||||||
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
||||||
}
|
}
|
||||||
redirect("/team/"+teamId, w, req)
|
redirect("/team/"+tm.UUID, w, req)
|
||||||
case "screenshotdelete":
|
case "screenshotdelete":
|
||||||
ssid := vars["subid"]
|
ssid := vars["subid"]
|
||||||
if err := dbDeleteTeamGameScreenshot(teamId, ssid); err != nil {
|
if err := tm.deleteScreenshot(ssid); err != nil {
|
||||||
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
|
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
|
||||||
}
|
}
|
||||||
redirect("/team/"+teamId, w, req)
|
redirect("/team/"+tm.UUID, w, req)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Page Not Found", 404)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user