2017-06-08 17:20:43 +00:00
|
|
|
package main
|
|
|
|
|
2017-06-08 20:43:11 +00:00
|
|
|
import (
|
2017-06-21 20:07:17 +00:00
|
|
|
"io/ioutil"
|
2017-06-08 20:43:11 +00:00
|
|
|
"net/http"
|
2017-06-21 20:07:17 +00:00
|
|
|
|
|
|
|
"encoding/base64"
|
2017-06-08 20:43:11 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
2017-06-08 17:20:43 +00:00
|
|
|
|
|
|
|
func handleAdminGames(w http.ResponseWriter, req *http.Request, page *pageData) {
|
2017-06-08 20:43:11 +00:00
|
|
|
vars := mux.Vars(req)
|
|
|
|
page.SubTitle = "Games"
|
2017-06-15 17:35:53 +00:00
|
|
|
teamId := vars["id"]
|
|
|
|
if teamId == "" {
|
|
|
|
// Games List
|
2017-06-21 22:09:26 +00:00
|
|
|
type gamePage struct {
|
|
|
|
Game *Game
|
|
|
|
Team *Team
|
|
|
|
}
|
2017-06-15 17:35:53 +00:00
|
|
|
type gamesPageData struct {
|
2017-06-21 22:09:26 +00:00
|
|
|
Games []gamePage
|
|
|
|
}
|
|
|
|
gpd := new(gamesPageData)
|
|
|
|
allGames := dbGetAllGames()
|
|
|
|
for _, gm := range allGames {
|
|
|
|
gamePage := new(gamePage)
|
|
|
|
gamePage.Game = &gm
|
|
|
|
gamePage.Team = dbGetTeam(gm.TeamId)
|
|
|
|
gpd.Games = append(gpd.Games, *gamePage)
|
2017-06-15 17:35:53 +00:00
|
|
|
}
|
2017-06-21 22:09:26 +00:00
|
|
|
page.TemplateData = gpd
|
2017-06-15 17:35:53 +00:00
|
|
|
page.SubTitle = "Games"
|
|
|
|
page.show("admin-games.html", w)
|
|
|
|
} else {
|
2017-06-08 20:43:11 +00:00
|
|
|
switch vars["function"] {
|
|
|
|
case "save":
|
|
|
|
name := req.FormValue("gamename")
|
2017-06-15 17:35:53 +00:00
|
|
|
desc := req.FormValue("gamedesc")
|
2017-06-08 20:43:11 +00:00
|
|
|
if dbIsValidTeam(teamId) {
|
2017-06-15 17:35:53 +00:00
|
|
|
if err := dbUpdateTeamGame(teamId, name, desc); err != nil {
|
|
|
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
|
|
|
} else {
|
|
|
|
page.session.setFlashMessage("Team game updated", "success")
|
2017-06-08 20:43:11 +00:00
|
|
|
}
|
2017-06-15 17:35:53 +00:00
|
|
|
redirect("/admin/teams/"+teamId, w, req)
|
|
|
|
}
|
|
|
|
case "screenshotupload":
|
|
|
|
if err := saveScreenshots(teamId, req); err != nil {
|
|
|
|
page.session.setFlashMessage("Error updating game: "+err.Error(), "error")
|
|
|
|
}
|
|
|
|
redirect("/admin/teams/"+teamId, w, req)
|
2017-06-21 22:09:26 +00:00
|
|
|
case "screenshotdelete":
|
|
|
|
ssid := vars["subid"]
|
|
|
|
if err := dbDeleteTeamGameScreenshot(teamId, ssid); err != nil {
|
|
|
|
page.session.setFlashMessage("Error deleting screenshot: "+err.Error(), "error")
|
|
|
|
}
|
|
|
|
redirect("/admin/teams/"+teamId, w, req)
|
2017-06-15 17:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveScreenshots(teamId string, req *http.Request) error {
|
2017-06-21 20:07:17 +00:00
|
|
|
file, _, err := req.FormFile("newssfile")
|
2017-06-15 17:35:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-21 20:07:17 +00:00
|
|
|
data, err := ioutil.ReadAll(file)
|
|
|
|
str := base64.StdEncoding.EncodeToString(data)
|
|
|
|
return dbSaveTeamGameScreenshot(teamId, &Screenshot{Image: str})
|
2017-06-08 17:20:43 +00:00
|
|
|
}
|