hangman/internal/rest_handler/guess_letter.go
2025-06-30 13:11:23 +02:00

48 lines
1.1 KiB
Go

package rest_handler
import (
"bytes"
"fmt"
"io"
"net/http"
"gitea.elara.ws/Hazel/hangman/internal/game"
"github.com/labstack/echo/v4"
)
func GuessLetter(c echo.Context) error {
session := game.GetSession(c.Param("session"))
sig := c.Request().Header.Get("signature")
body, _ := io.ReadAll(c.Request().Body)
u := session.VerifySignature(sig, body)
if u == nil {
return c.String(http.StatusBadRequest, "This user was not fount in the current session.")
}
if !session.CurrentUser.PublicKey.Equal(u.PublicKey) {
return c.String(http.StatusBadRequest, "It's not the turn of user "+u.Name+". It's the turn of "+session.CurrentUser.Name+".")
}
c.Request().Body = io.NopCloser(bytes.NewBuffer(body))
type BodyContent struct {
Guess string
}
var bodyContent BodyContent
err := c.Bind(&bodyContent)
if err != nil {
fmt.Println(err)
return c.String(http.StatusBadRequest, err.Error())
}
_, err = session.GuessLetter(bodyContent.Guess)
if err != nil {
return c.String(http.StatusBadRequest, err.Error())
}
return c.JSON(http.StatusOK, session)
}