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 { func NewSession(phrase string) Session {
sessionName := petname.Generate(3, "-") sessionName := petname.Generate(3, "-")
p := strings.Split(phrase, "")
s := Session{ s := Session{
id: lastSessionId, id: lastSessionId,
Name: sessionName, Name: sessionName,
Users: []User{}, Users: []User{},
phrase: []string{phrase}, phrase: p,
AskedLetters: []string{}, AskedLetters: []string{},
DiscoveredPhrase: make([]string, len(phrase)), DiscoveredPhrase: make([]string, len(p)),
Mistakes: 0, Mistakes: 0,
userIndex: 0, userIndex: 0,
CurrentUser: nil, 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)
}

View File

@ -8,7 +8,6 @@ import (
"gitea.elara.ws/Hazel/hangman/internal/rest_handler" "gitea.elara.ws/Hazel/hangman/internal/rest_handler"
"gitea.elara.ws/Hazel/hangman/internal/view_handler" "gitea.elara.ws/Hazel/hangman/internal/view_handler"
"gitea.elara.ws/Hazel/hangman/internal/words"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
@ -27,9 +26,6 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
} }
func main() { func main() {
fmt.Println(words.GetRandomWord())
return
fmt.Println("wanna play hangman? Well ya cant since it isn't implemented yet..") fmt.Println("wanna play hangman? Well ya cant since it isn't implemented yet..")
e := echo.New() e := echo.New()
@ -44,6 +40,7 @@ func main() {
e.POST("/api/session", rest_handler.CreateSession) e.POST("/api/session", rest_handler.CreateSession)
e.POST("/api/:session/user", rest_handler.CreateUser) e.POST("/api/:session/user", rest_handler.CreateUser)
e.POST("/api/:session/test-auth", rest_handler.TestAuth) e.POST("/api/:session/test-auth", rest_handler.TestAuth)
e.POST("/api/:session/guess", rest_handler.GuessLetter)
e.GET("/", view_handler.CreateSession) e.GET("/", view_handler.CreateSession)
e.GET("/:name", view_handler.CreateUser) e.GET("/:name", view_handler.CreateUser)

View File

@ -36,6 +36,12 @@ class User:
}) })
print(r.content) print(r.content)
def guess(self, letter: str):
r = self.signed_request("/guess", {
"Guess": letter
})
print(r.content)
def signed_request(self, endpoint: str, body: dict) -> requests.Response: def signed_request(self, endpoint: str, body: dict) -> requests.Response:
payload = json.dumps(body).encode("utf-8") payload = json.dumps(body).encode("utf-8")
@ -89,13 +95,18 @@ var Words []string = []string{"""
if __name__ == "__main__": if __name__ == "__main__":
build_word_dict()
s = Session() s = Session()
print(s) print(s)
s.add_user(name="Hazel") a = s.add_user(name="Hazel")
u = s.add_user(name="OtherHazel") b = s.add_user(name="OtherHazel")
print(u)
u.test_auth() print(b)
b.test_auth()
b.guess("e")
a.guess("e")
b.guess("e")
b.guess("a")
a.guess("i")
b.guess("o")
a.guess("u")