108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"golias/models"
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
func HandleShowCreateForm(w http.ResponseWriter, r *http.Request) {
|
|
show := true
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl.ExecuteTemplate(w, "createform", show)
|
|
}
|
|
|
|
func HandleHideCreateForm(w http.ResponseWriter, r *http.Request) {
|
|
show := false
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl.ExecuteTemplate(w, "createform", show)
|
|
}
|
|
|
|
func HandleShowColor(w http.ResponseWriter, r *http.Request) {
|
|
word := r.URL.Query().Get("word")
|
|
ctx := r.Context()
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
fi, err := getFullInfoByCtx(ctx)
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
if fi.State.Role != "guesser" {
|
|
err = errors.New("need to guesser to open the card")
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
// whos move it is?
|
|
if fi.State.Team != models.UserTeam(fi.Room.TeamTurn) {
|
|
err = errors.New("not your team's move")
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
log.Debug("got state", "state", fi)
|
|
// TODO: update room score
|
|
color, exists := roundWords[word]
|
|
log.Debug("got show-color request", "word", word, "color", color)
|
|
if !exists {
|
|
abortWithError(w, "word is not found")
|
|
return
|
|
}
|
|
cardword := models.WordCard{
|
|
Word: word,
|
|
Color: models.StrToWordColor(color),
|
|
Revealed: true,
|
|
}
|
|
fi.Room.RevealSpecificWord(word)
|
|
fi.Room.UpdateCounter()
|
|
action := models.Action{
|
|
Actor: fi.State.Username,
|
|
ActorColor: string(fi.State.Team),
|
|
WordColor: color,
|
|
Action: "guessed",
|
|
Word: word,
|
|
}
|
|
fi.Room.ActionHistory = append(fi.Room.ActionHistory, action)
|
|
// if opened card is of color of opp team, change turn
|
|
switch color {
|
|
case "black":
|
|
// game over
|
|
fi.Room.IsRunning = false
|
|
fi.Room.IsOver = true
|
|
case "white", fi.Room.GetOppositeTeamColor():
|
|
// end turn
|
|
fi.Room.TeamTurn = fi.Room.GetOppositeTeamColor()
|
|
}
|
|
if err := saveFullInfo(fi); err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
notify(models.NotifyRoomUpdatePrefix+fi.Room.ID, "")
|
|
tmpl.ExecuteTemplate(w, "cardword", cardword)
|
|
}
|
|
|
|
func HandleActionHistory(w http.ResponseWriter, r *http.Request) {
|
|
fi, err := getFullInfoByCtx(r.Context())
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl, err := template.ParseGlob("components/*.html")
|
|
if err != nil {
|
|
abortWithError(w, err.Error())
|
|
return
|
|
}
|
|
tmpl.ExecuteTemplate(w, "actionhistory", fi.Room.ActionHistory)
|
|
}
|