implemented guess endpoint
This commit is contained in:
parent
748c2de536
commit
1e0a4c6317
@ -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,
|
||||
|
47
internal/rest_handler/guess_letter.go
Normal file
47
internal/rest_handler/guess_letter.go
Normal 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)
|
||||
}
|
5
main.go
5
main.go
@ -8,7 +8,6 @@ import (
|
||||
|
||||
"gitea.elara.ws/Hazel/hangman/internal/rest_handler"
|
||||
"gitea.elara.ws/Hazel/hangman/internal/view_handler"
|
||||
"gitea.elara.ws/Hazel/hangman/internal/words"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@ -27,9 +26,6 @@ func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(words.GetRandomWord())
|
||||
return
|
||||
|
||||
fmt.Println("wanna play hangman? Well ya cant since it isn't implemented yet..")
|
||||
|
||||
e := echo.New()
|
||||
@ -44,6 +40,7 @@ func main() {
|
||||
e.POST("/api/session", rest_handler.CreateSession)
|
||||
e.POST("/api/:session/user", rest_handler.CreateUser)
|
||||
e.POST("/api/:session/test-auth", rest_handler.TestAuth)
|
||||
e.POST("/api/:session/guess", rest_handler.GuessLetter)
|
||||
|
||||
e.GET("/", view_handler.CreateSession)
|
||||
e.GET("/:name", view_handler.CreateUser)
|
||||
|
@ -36,6 +36,12 @@ class User:
|
||||
})
|
||||
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:
|
||||
payload = json.dumps(body).encode("utf-8")
|
||||
|
||||
@ -89,13 +95,18 @@ var Words []string = []string{"""
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
build_word_dict()
|
||||
|
||||
s = Session()
|
||||
print(s)
|
||||
s.add_user(name="Hazel")
|
||||
u = s.add_user(name="OtherHazel")
|
||||
print(u)
|
||||
a = s.add_user(name="Hazel")
|
||||
b = s.add_user(name="OtherHazel")
|
||||
|
||||
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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user