Files
golias/handlers/elements.go
2025-05-09 14:39:33 +03:00

70 lines
1.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
}
session, ok := ctx.Value(models.CtxSessionKey).(*models.Session)
if !ok {
// trying to get color without a session -> error
http.Redirect(w, r, "/", 302)
return
}
state, err := loadState(session.Username)
if err != nil {
abortWithError(w, err.Error())
return
}
// TODO: whos move it is?
if state.Role != "guesser" {
err = errors.New("need to guesser to open the card")
abortWithError(w, err.Error())
return
}
log.Debug("got state", "state", state)
// 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,
}
tmpl.ExecuteTemplate(w, "cardword", cardword)
}