From 322743e33d55be1210de8897a1eeab081ff765c1 Mon Sep 17 00:00:00 2001 From: Grail Finder Date: Fri, 2 May 2025 15:21:28 +0300 Subject: [PATCH] Feat: show colors by pressing words --- components/cardword.html | 13 +++++++++++++ components/index.html | 6 +++--- handlers/elements.go | 22 ++++++++++++++++++++++ handlers/handlers.go | 23 ----------------------- main.go | 4 ++-- models/main.go | 13 ++++++++----- 6 files changed, 48 insertions(+), 33 deletions(-) create mode 100644 components/cardword.html diff --git a/components/cardword.html b/components/cardword.html new file mode 100644 index 0000000..2f23e3e --- /dev/null +++ b/components/cardword.html @@ -0,0 +1,13 @@ +{{define "cardword"}} +
{{.Word}} +
+{{end}} diff --git a/components/index.html b/components/index.html index e22a826..5b2eb57 100644 --- a/components/index.html +++ b/components/index.html @@ -49,7 +49,7 @@

Word Color Cards

{{range $word, $color := .}} -
+ cursor: pointer;" + hx-get="/word/show-color?word={{$word}}" hx-trigger="click" hx-swap="outerHTML transition:true swap:.2s"> {{$word}}
{{end}} diff --git a/handlers/elements.go b/handlers/elements.go index e539df8..4b91113 100644 --- a/handlers/elements.go +++ b/handlers/elements.go @@ -1,6 +1,7 @@ package handlers import ( + "golias/models" "html/template" "net/http" ) @@ -24,3 +25,24 @@ func HandleHideCreateForm(w http.ResponseWriter, r *http.Request) { } tmpl.ExecuteTemplate(w, "createform", show) } + +func HandleShowColor(w http.ResponseWriter, r *http.Request) { + word := r.URL.Query().Get("word") + color, exists := roundWords[word] + log.Debug("got show-color request", "word", word, "color", color) + if !exists { + abortWithError(w, "word is not found") + return + } + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + abortWithError(w, err.Error()) + return + } + cardword := models.WordCard{ + Word: word, + Color: models.StrToWordColor(color), + Revealed: true, + } + tmpl.ExecuteTemplate(w, "cardword", cardword) +} diff --git a/handlers/handlers.go b/handlers/handlers.go index 609984f..b6053d7 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -36,29 +36,6 @@ func HandlePing(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) } -func HandleShowColor(w http.ResponseWriter, r *http.Request) { - word := r.URL.Query().Get("word") - color, exists := roundWords[word] - if !exists { - http.Error(w, "word not found", http.StatusNotFound) - return - } - - w.Header().Set("Content-Type", "text/html") - w.Write([]byte( - fmt.Sprintf(`
%s
`, color, word))) -} - func HandleHome(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseGlob("components/*.html") if err != nil { diff --git a/main.go b/main.go index 6fb3156..d26bcfc 100644 --- a/main.go +++ b/main.go @@ -16,16 +16,16 @@ func ListenToRequests(port string) error { ReadTimeout: time.Second * 5, WriteTimeout: time.Second * 5, } - fs := http.FileServer(http.Dir("assets/")) mux.Handle("GET /assets/", http.StripPrefix("/assets/", fs)) - + // mux.HandleFunc("GET /ping", handlers.HandlePing) mux.HandleFunc("GET /", handlers.HandleHome) mux.HandleFunc("POST /login", handlers.HandleFrontLogin) //elements mux.HandleFunc("GET /room/createform", handlers.HandleShowCreateForm) mux.HandleFunc("GET /room/hideform", handlers.HandleHideCreateForm) + mux.HandleFunc("GET /word/show-color", handlers.HandleShowColor) slog.Info("Listening", "addr", port) return server.ListenAndServe() } diff --git a/models/main.go b/models/main.go index 45ac3b8..d169b48 100644 --- a/models/main.go +++ b/models/main.go @@ -5,10 +5,11 @@ import "time" type WordColor string const ( - WordColorWhite = "white" - WordColorBlue = "blue" - WordColorRed = "red" - WordColorBlack = "black" + WordColorWhite = "white" + WordColorBlue = "blue" + WordColorRed = "red" + WordColorBlack = "black" + WordColorUknown = "beige" ) func StrToWordColor(s string) WordColor { @@ -22,7 +23,7 @@ func StrToWordColor(s string) WordColor { case "black": return WordColorBlack default: - return "beige" + return WordColorUknown } } @@ -34,6 +35,7 @@ type Room struct { RoomLink string CreatorName string `json:"creator_name"` PlayerList []string `json:"player_list"` + TeamTurn string RedMime string BlueMime string RedGuessers []string @@ -56,6 +58,7 @@ type RoomPublic struct { CreatorName string `json:"creator_name"` GameSettings *GameSettings `json:"settings"` RedMime string + TeamTurn string BlueMime string RedGuessers []string BlueGuessers []string