added frontend that can play very bare bones
This commit is contained in:
parent
9c8b8177ad
commit
ad286d745f
@ -59,12 +59,13 @@ func GetSession(name string) (*Session, error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Session) AddUser(user User) {
|
||||
func (s *Session) AddUser(user User) *User {
|
||||
s.Users = append(s.Users, user)
|
||||
|
||||
// fmt.Printf("#### Adding %v:\t%p\n", s.Users[len(s.Users)-1].Name, &(s.Users[len(s.Users)-1]))
|
||||
// append changes the pointers to the users because it needs to resize that slice
|
||||
s.CurrentUser = &(s.Users[s.userIndex])
|
||||
return &(s.Users[len(s.Users)-1])
|
||||
}
|
||||
|
||||
func (s *Session) VerifySignature(signature string, message []byte) (*User, error) {
|
||||
|
@ -8,12 +8,3 @@ type User struct {
|
||||
Name string
|
||||
PublicKey ed25519.PublicKey
|
||||
}
|
||||
|
||||
func NewUser(name string, publicKey ed25519.PublicKey) User {
|
||||
// ed25519
|
||||
|
||||
return User{
|
||||
Name: name,
|
||||
PublicKey: publicKey,
|
||||
}
|
||||
}
|
||||
|
@ -29,8 +29,14 @@ func CreateUser(c echo.Context) error {
|
||||
if err != nil {
|
||||
return c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
user := game.NewUser(bodyContent.Name, pub)
|
||||
session.AddUser(user)
|
||||
|
||||
return c.JSON(http.StatusOK, user)
|
||||
user := session.AddUser(game.User{
|
||||
Name: bodyContent.Name,
|
||||
PublicKey: pub,
|
||||
})
|
||||
|
||||
return c.JSON(http.StatusOK, ResponseData{
|
||||
Session: session,
|
||||
User: user,
|
||||
})
|
||||
}
|
||||
|
@ -32,5 +32,8 @@ func GuessLetter(c echo.Context) error {
|
||||
return c.String(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, session)
|
||||
return c.JSON(http.StatusOK, ResponseData{
|
||||
User: user,
|
||||
Session: session,
|
||||
})
|
||||
}
|
||||
|
@ -8,6 +8,11 @@ import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type ResponseData struct{
|
||||
Session *game.Session
|
||||
User *game.User
|
||||
}
|
||||
|
||||
func GetData(c echo.Context) (*game.Session, *game.User, error) {
|
||||
session, err := game.GetSession(c.Param("session"))
|
||||
if err != nil {
|
||||
|
@ -28,7 +28,7 @@ class User:
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"User({self.name})"
|
||||
|
||||
|
||||
|
||||
def test_auth(self):
|
||||
r = self.signed_request("/test-auth", {
|
||||
@ -41,7 +41,7 @@ class User:
|
||||
"Guess": letter
|
||||
})
|
||||
print(r.content)
|
||||
|
||||
|
||||
def signed_request(self, endpoint: str, body: dict) -> requests.Response:
|
||||
payload = json.dumps(body).encode("utf-8")
|
||||
|
||||
@ -57,52 +57,64 @@ class User:
|
||||
)
|
||||
|
||||
|
||||
class Session:
|
||||
class Game:
|
||||
def __init__(self) -> None:
|
||||
data = requests.post(BASE_URL + "/session").json()
|
||||
self.name = data["Name"]
|
||||
self.users: List[User] = []
|
||||
self.session_name = input("session: ").strip().lower()
|
||||
|
||||
if self.session_name == "":
|
||||
data = requests.post(BASE_URL + "/session").json()
|
||||
self.session_name = data["Name"]
|
||||
|
||||
print(f"playing with session {self.session_name}")
|
||||
|
||||
self.user_name = input("name: ").strip()
|
||||
|
||||
self.signing_key = SigningKey.generate()
|
||||
r = requests.post(BASE_URL + f"/{self.session_name}/user", json={
|
||||
"Name": self.user_name,
|
||||
"PublicKey": base64.b64encode(self.signing_key.verify_key.__bytes__()).decode("ascii")
|
||||
})
|
||||
|
||||
self.last_turn = r.json()
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Session({self.name})"
|
||||
return f"Game({self.session_name}, {self.user_name})"
|
||||
|
||||
def add_user(self, name: str) -> User:
|
||||
u = User(session=self, name=name)
|
||||
self.users.append(u)
|
||||
return u
|
||||
def print_turn(self, data: dict):
|
||||
print()
|
||||
print(" ".join(c if c != '' else '_' for c in data["Session"]["DiscoveredPhrase"]))
|
||||
print(f"guessed: {','.join(data['Session']['AskedLetters'])}")
|
||||
|
||||
def guess(self):
|
||||
self.print_turn(self.last_turn)
|
||||
self.send_guess(input(f"{self.user_name}: ").strip())
|
||||
|
||||
def send_guess(self, letter: str):
|
||||
data = self.signed_request("/guess", {
|
||||
"Guess": letter
|
||||
}).json()
|
||||
|
||||
self.last_turn = data
|
||||
|
||||
def signed_request(self, endpoint: str, body: dict) -> requests.Response:
|
||||
payload = json.dumps(body).encode("utf-8")
|
||||
|
||||
signature = self.signing_key.sign(payload)
|
||||
|
||||
return requests.post(
|
||||
url=BASE_URL+f"/{self.session_name}"+endpoint,
|
||||
data=payload,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"signature": base64.b64encode(signature.signature).decode("ascii")
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def build_word_dict():
|
||||
lines = [
|
||||
"""package words
|
||||
|
||||
var Words []string = []string{"""
|
||||
]
|
||||
|
||||
with open("/usr/share/dict/words", "r") as f:
|
||||
for l in f.readlines():
|
||||
l = l.strip()
|
||||
if not l.isalpha():
|
||||
continue
|
||||
lines.append(f'\t"{l}",')
|
||||
|
||||
lines.append("}")
|
||||
|
||||
with open("internal/words/dictionary.go", "w") as f:
|
||||
f.write("\n".join(lines))
|
||||
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = Session()
|
||||
print(s)
|
||||
a = s.add_user(name="Hazel_1")
|
||||
b = s.add_user(name="Hazel_2")
|
||||
c = s.add_user(name="Hazel_3")
|
||||
|
||||
a.guess("a")
|
||||
b.guess("e")
|
||||
c.guess("i")
|
||||
a.guess("o")
|
||||
b.guess("u")
|
||||
g = Game()
|
||||
|
||||
while True:
|
||||
g.guess()
|
||||
|
108
python_frontend/test.py
Normal file
108
python_frontend/test.py
Normal file
@ -0,0 +1,108 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List
|
||||
import requests
|
||||
from nacl.signing import SigningKey, VerifyKey
|
||||
import base64
|
||||
|
||||
import json
|
||||
|
||||
|
||||
BASE_URL = "http://localhost:1323/api"
|
||||
|
||||
|
||||
class User:
|
||||
def __init__(self, session: Session, name: str) -> None:
|
||||
self.session = session
|
||||
self.name = name
|
||||
|
||||
self.signing_key = SigningKey.generate()
|
||||
|
||||
r = requests.post(BASE_URL + f"/{self.session.name}/user", json={
|
||||
"Name": name,
|
||||
"PublicKey": base64.b64encode(self.signing_key.verify_key.__bytes__()).decode("ascii")
|
||||
})
|
||||
|
||||
data = r.json()
|
||||
print(json.dumps(data, indent=4))
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"User({self.name})"
|
||||
|
||||
|
||||
def test_auth(self):
|
||||
r = self.signed_request("/test-auth", {
|
||||
"foo": "bar"
|
||||
})
|
||||
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")
|
||||
|
||||
signature = self.signing_key.sign(payload)
|
||||
|
||||
return requests.post(
|
||||
url=BASE_URL+f"/{self.session.name}"+endpoint,
|
||||
data=payload,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"signature": base64.b64encode(signature.signature).decode("ascii")
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class Session:
|
||||
def __init__(self) -> None:
|
||||
data = requests.post(BASE_URL + "/session").json()
|
||||
self.name = data["Name"]
|
||||
self.users: List[User] = []
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"Session({self.name})"
|
||||
|
||||
def add_user(self, name: str) -> User:
|
||||
u = User(session=self, name=name)
|
||||
self.users.append(u)
|
||||
return u
|
||||
|
||||
|
||||
def build_word_dict():
|
||||
lines = [
|
||||
"""package words
|
||||
|
||||
var Words []string = []string{"""
|
||||
]
|
||||
|
||||
with open("/usr/share/dict/words", "r") as f:
|
||||
for l in f.readlines():
|
||||
l = l.strip()
|
||||
if not l.isalpha():
|
||||
continue
|
||||
lines.append(f'\t"{l}",')
|
||||
|
||||
lines.append("}")
|
||||
|
||||
with open("internal/words/dictionary.go", "w") as f:
|
||||
f.write("\n".join(lines))
|
||||
|
||||
exit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = Session()
|
||||
print(s)
|
||||
a = s.add_user(name="Hazel_1")
|
||||
b = s.add_user(name="Hazel_2")
|
||||
c = s.add_user(name="Hazel_3")
|
||||
|
||||
a.guess("a")
|
||||
b.guess("e")
|
||||
c.guess("i")
|
||||
a.guess("o")
|
||||
b.guess("u")
|
Loading…
x
Reference in New Issue
Block a user