implemented guess endpoint

This commit is contained in:
Hazel Noack
2025-06-30 13:11:23 +02:00
parent 748c2de536
commit 1e0a4c6317
4 changed files with 69 additions and 13 deletions

View File

@@ -29,14 +29,15 @@ type Session struct {
func NewSession(phrase string) Session {
sessionName := petname.Generate(3, "-")
p := strings.Split(phrase, "")
s := Session{
id: lastSessionId,
Name: sessionName,
Users: []User{},
phrase: []string{phrase},
phrase: p,
AskedLetters: []string{},
DiscoveredPhrase: make([]string, len(phrase)),
DiscoveredPhrase: make([]string, len(p)),
Mistakes: 0,
userIndex: 0,
CurrentUser: nil,

View File

@@ -0,0 +1,47 @@
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)
}