2017-04-03 21:32:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-07-10 00:18:03 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2017-04-03 21:32:13 +00:00
|
|
|
"net/http"
|
2017-06-30 19:35:36 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-04-03 21:32:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initPublicPage(w http.ResponseWriter, req *http.Request) *pageData {
|
2017-04-21 18:17:18 +00:00
|
|
|
p := InitPageData(w, req)
|
2017-04-03 21:32:13 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleMain(w http.ResponseWriter, req *http.Request) {
|
2017-06-22 15:34:57 +00:00
|
|
|
switch dbGetPublicSiteMode() {
|
|
|
|
case SiteModeWaiting:
|
2017-07-10 00:18:03 +00:00
|
|
|
page := initPublicPage(w, req)
|
|
|
|
page.SubTitle = ""
|
2017-06-22 15:34:57 +00:00
|
|
|
page.show("public-waiting.html", w)
|
|
|
|
case SiteModeVoting:
|
2017-06-30 19:35:36 +00:00
|
|
|
loadVotingPage(w, req)
|
2017-04-03 21:32:13 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 19:35:36 +00:00
|
|
|
|
|
|
|
func loadVotingPage(w http.ResponseWriter, req *http.Request) {
|
|
|
|
page := initPublicPage(w, req)
|
|
|
|
type votingPageData struct {
|
|
|
|
Teams []Team
|
|
|
|
Timestamp string
|
|
|
|
}
|
|
|
|
vpd := new(votingPageData)
|
2017-07-10 00:18:03 +00:00
|
|
|
tms := dbGetAllTeams()
|
|
|
|
|
|
|
|
// Randomize the team list
|
|
|
|
rand.Seed(time.Now().Unix())
|
|
|
|
for len(tms) > 0 {
|
|
|
|
i := rand.Intn(len(tms))
|
|
|
|
fmt.Println("Chose Team: " + tms[i].Name)
|
|
|
|
vpd.Teams = append(vpd.Teams, tms[i])
|
|
|
|
tms = append(tms[:i], tms[i+1:]...)
|
|
|
|
}
|
|
|
|
|
2017-06-30 19:35:36 +00:00
|
|
|
vpd.Timestamp = time.Now().Format(time.RFC3339)
|
|
|
|
page.TemplateData = vpd
|
|
|
|
page.show("public-voting.html", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePublicSaveVote(w http.ResponseWriter, req *http.Request) {
|
|
|
|
page := initPublicPage(w, req)
|
|
|
|
page.SubTitle = ""
|
|
|
|
|
|
|
|
// Check if we already have a vote for this client id/timestamp
|
|
|
|
ts := req.FormValue("timestamp")
|
|
|
|
timestamp, err := time.Parse(time.RFC3339, ts)
|
|
|
|
if err != nil {
|
|
|
|
page.session.setFlashMessage("Error parsing timestamp: "+ts, "error")
|
|
|
|
redirect("/", w, req)
|
|
|
|
}
|
|
|
|
if _, err := dbGetVote(page.ClientId, timestamp); err == nil {
|
|
|
|
// Duplicate vote... Cancel it.
|
|
|
|
page.session.setFlashMessage("Duplicate vote!", "error")
|
|
|
|
redirect("/", w, req)
|
|
|
|
}
|
|
|
|
// voteSlice is an ordered string slice of the voters preferences
|
|
|
|
voteCSV := req.FormValue("uservote")
|
|
|
|
voteSlice := strings.Split(voteCSV, ",")
|
|
|
|
if err := dbSaveVote(page.ClientId, timestamp, voteSlice); err != nil {
|
|
|
|
page.session.setFlashMessage("Error Saving Vote: "+err.Error(), "error")
|
|
|
|
}
|
2017-07-05 13:10:08 +00:00
|
|
|
if newVote, err := dbGetVote(page.ClientId, timestamp); err == nil {
|
2017-07-06 17:09:49 +00:00
|
|
|
site.Votes = append(site.Votes, *newVote)
|
2017-07-05 13:10:08 +00:00
|
|
|
}
|
2017-07-10 00:18:03 +00:00
|
|
|
page.session.setFlashMessage("Vote Saved!", "success large fading")
|
2017-06-30 19:35:36 +00:00
|
|
|
redirect("/", w, req)
|
|
|
|
}
|