It works!
This commit is contained in:
parent
1d95646a96
commit
57cd0544f4
42
app/main.py
42
app/main.py
|
@ -12,12 +12,16 @@ from typing import List, Dict
|
|||
# pylint: disable=unused-wildcard-import
|
||||
from .models import *
|
||||
from starlette.websockets import WebSocket
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
hashes = hashids.Hashids("a very good salt yes this is nice ok",
|
||||
4, "ABCDEFGHJKLMNPQRSTUVXYZ23456789")
|
||||
|
||||
app = FastAPI(title="Cards against idiots")
|
||||
r = redis.Redis(host='redis', port=6379, password='yeet')
|
||||
app.add_middleware(CORSMiddleware, allow_origins=[
|
||||
'*'], allow_methods=["*"], allow_headers=["*"])
|
||||
|
||||
# loop = asyncio.get_event_loop()
|
||||
# r = aioredis.create_redis("redis://redis", loop=loop)
|
||||
cards = {}
|
||||
|
@ -50,10 +54,12 @@ async def ws_room(ws: WebSocket, code: str, kind: str):
|
|||
while True:
|
||||
# 1: Message from pub/sub
|
||||
msg = ps.get_message()
|
||||
if msg:
|
||||
await ws.send_text(str(msg["data"]))
|
||||
|
||||
await asyncio.sleep(1)
|
||||
if msg and type(msg["data"]) is bytes:
|
||||
await ws.send_bytes(msg["data"].decode("utf-8"))
|
||||
elif msg:
|
||||
await ws.send_text("1")
|
||||
else:
|
||||
await asyncio.sleep(0)
|
||||
|
||||
elif kind == "pub": # 2: Message from WS
|
||||
while True:
|
||||
|
@ -84,25 +90,28 @@ async def ws_room(ws: WebSocket, code: str, kind: str):
|
|||
|
||||
r.hset("rooms", room.code, room.json())
|
||||
|
||||
r.publish(code, PubRoom(**room.dict()).json())
|
||||
|
||||
await ws.send_text(str(p.uuid))
|
||||
await ws.send_json({"msgtype": "JOIN", "data": {"owner": room.admin_uuid == p.uuid, "you": len(room.players)-1, "uuid": str(p.uuid)}})
|
||||
await ws.send_json({"msgtype": "ROOM", "data": PubRoom(**room.dict()).dict()})
|
||||
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||
data=PubRoom(**room.dict())).json())
|
||||
elif msg.msgtype is MessageType.START:
|
||||
pid: str = str(msg.data)
|
||||
|
||||
if pid == str(room.admin_uuid):
|
||||
room.started = True
|
||||
|
||||
if not next_round(code):
|
||||
await ws.close()
|
||||
r.hset("rooms", room.code, room.json())
|
||||
|
||||
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||
data=PubRoom(**room.dict())).json())
|
||||
if room.started and not next_round(code):
|
||||
await ws.close()
|
||||
|
||||
elif msg.msgtype is MessageType.ANSWER:
|
||||
answer = AnswerReceived(**json.loads(msg.data))
|
||||
|
||||
mapper = {p.uuid: i for i, p in enumerate(room.players)}
|
||||
mapper = {str(p.uuid): i for i, p in enumerate(room.players)}
|
||||
|
||||
mapped = AnswerSending(**answer, index=mapper[answer.uuid])
|
||||
mapped = AnswerSending(**answer.dict(), index=mapper[str(answer.uuid)])
|
||||
room.answers.append(mapped)
|
||||
r.hset("rooms", room.code, room.json())
|
||||
|
||||
|
@ -113,14 +122,15 @@ async def ws_room(ws: WebSocket, code: str, kind: str):
|
|||
elif msg.msgtype is MessageType.PICK:
|
||||
answer = AnswerSendingButItHasAUUIDBecauseItIsImportantToCheckIfItWasSentByTheCzar(
|
||||
**json.loads(msg.data))
|
||||
if answer.uuid != room.czar:
|
||||
if str(answer.uuid) != str(room.players[room.czar].uuid):
|
||||
continue
|
||||
r.publish(code, Message(
|
||||
msgtype=MessageType.PICK, data=answer).json())
|
||||
room.players[answer.index].points += 1
|
||||
|
||||
r.hset("rooms", room.code, room.json())
|
||||
r.publish(code, PubRoom(**room.dict()).json())
|
||||
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||
data=PubRoom(**room.dict())).json())
|
||||
|
||||
next_round(code)
|
||||
else:
|
||||
|
@ -144,12 +154,14 @@ def next_round(code: str):
|
|||
cardid = random.randrange(0, len(cards["black"]))
|
||||
room.played_cards.append(cardid)
|
||||
room.czar = (room.czar + 1) % len(room.players)
|
||||
|
||||
r.hset("rooms", room.code, room.json())
|
||||
|
||||
r.publish(code, Message(
|
||||
msgtype=MessageType.START,
|
||||
data=RoundStart(czar=room.czar, card=cards["black"][cardid])).json())
|
||||
|
||||
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||
data=PubRoom(**room.dict())).json())
|
||||
return True
|
||||
|
||||
|
||||
|
|
|
@ -42,7 +42,7 @@ class PubRoom(BaseModel):
|
|||
played_cards: List[int] = []
|
||||
|
||||
class Answer(BaseModel):
|
||||
text: str
|
||||
text: List[str]
|
||||
|
||||
class AnswerReceived(Answer):
|
||||
uuid: str
|
||||
|
@ -75,6 +75,7 @@ class MessageType(AutoName):
|
|||
START = auto()
|
||||
ANSWER = auto()
|
||||
PICK = auto()
|
||||
ROOM = auto()
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
|
|
5166
cards.json
5166
cards.json
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue