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
|
# pylint: disable=unused-wildcard-import
|
||||||
from .models import *
|
from .models import *
|
||||||
from starlette.websockets import WebSocket
|
from starlette.websockets import WebSocket
|
||||||
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
hashes = hashids.Hashids("a very good salt yes this is nice ok",
|
hashes = hashids.Hashids("a very good salt yes this is nice ok",
|
||||||
4, "ABCDEFGHJKLMNPQRSTUVXYZ23456789")
|
4, "ABCDEFGHJKLMNPQRSTUVXYZ23456789")
|
||||||
|
|
||||||
app = FastAPI(title="Cards against idiots")
|
app = FastAPI(title="Cards against idiots")
|
||||||
r = redis.Redis(host='redis', port=6379, password='yeet')
|
r = redis.Redis(host='redis', port=6379, password='yeet')
|
||||||
|
app.add_middleware(CORSMiddleware, allow_origins=[
|
||||||
|
'*'], allow_methods=["*"], allow_headers=["*"])
|
||||||
|
|
||||||
# loop = asyncio.get_event_loop()
|
# loop = asyncio.get_event_loop()
|
||||||
# r = aioredis.create_redis("redis://redis", loop=loop)
|
# r = aioredis.create_redis("redis://redis", loop=loop)
|
||||||
cards = {}
|
cards = {}
|
||||||
|
@ -50,10 +54,12 @@ async def ws_room(ws: WebSocket, code: str, kind: str):
|
||||||
while True:
|
while True:
|
||||||
# 1: Message from pub/sub
|
# 1: Message from pub/sub
|
||||||
msg = ps.get_message()
|
msg = ps.get_message()
|
||||||
if msg:
|
if msg and type(msg["data"]) is bytes:
|
||||||
await ws.send_text(str(msg["data"]))
|
await ws.send_bytes(msg["data"].decode("utf-8"))
|
||||||
|
elif msg:
|
||||||
await asyncio.sleep(1)
|
await ws.send_text("1")
|
||||||
|
else:
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
|
||||||
elif kind == "pub": # 2: Message from WS
|
elif kind == "pub": # 2: Message from WS
|
||||||
while True:
|
while True:
|
||||||
|
@ -84,25 +90,28 @@ async def ws_room(ws: WebSocket, code: str, kind: str):
|
||||||
|
|
||||||
r.hset("rooms", room.code, room.json())
|
r.hset("rooms", room.code, room.json())
|
||||||
|
|
||||||
r.publish(code, PubRoom(**room.dict()).json())
|
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()})
|
||||||
await ws.send_text(str(p.uuid))
|
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||||
|
data=PubRoom(**room.dict())).json())
|
||||||
elif msg.msgtype is MessageType.START:
|
elif msg.msgtype is MessageType.START:
|
||||||
pid: str = str(msg.data)
|
pid: str = str(msg.data)
|
||||||
|
|
||||||
if pid == str(room.admin_uuid):
|
if pid == str(room.admin_uuid):
|
||||||
room.started = True
|
room.started = True
|
||||||
|
|
||||||
if not next_round(code):
|
|
||||||
await ws.close()
|
|
||||||
r.hset("rooms", room.code, room.json())
|
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:
|
elif msg.msgtype is MessageType.ANSWER:
|
||||||
answer = AnswerReceived(**json.loads(msg.data))
|
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)
|
room.answers.append(mapped)
|
||||||
r.hset("rooms", room.code, room.json())
|
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:
|
elif msg.msgtype is MessageType.PICK:
|
||||||
answer = AnswerSendingButItHasAUUIDBecauseItIsImportantToCheckIfItWasSentByTheCzar(
|
answer = AnswerSendingButItHasAUUIDBecauseItIsImportantToCheckIfItWasSentByTheCzar(
|
||||||
**json.loads(msg.data))
|
**json.loads(msg.data))
|
||||||
if answer.uuid != room.czar:
|
if str(answer.uuid) != str(room.players[room.czar].uuid):
|
||||||
continue
|
continue
|
||||||
r.publish(code, Message(
|
r.publish(code, Message(
|
||||||
msgtype=MessageType.PICK, data=answer).json())
|
msgtype=MessageType.PICK, data=answer).json())
|
||||||
room.players[answer.index].points += 1
|
room.players[answer.index].points += 1
|
||||||
|
|
||||||
r.hset("rooms", room.code, room.json())
|
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)
|
next_round(code)
|
||||||
else:
|
else:
|
||||||
|
@ -144,12 +154,14 @@ def next_round(code: str):
|
||||||
cardid = random.randrange(0, len(cards["black"]))
|
cardid = random.randrange(0, len(cards["black"]))
|
||||||
room.played_cards.append(cardid)
|
room.played_cards.append(cardid)
|
||||||
room.czar = (room.czar + 1) % len(room.players)
|
room.czar = (room.czar + 1) % len(room.players)
|
||||||
|
|
||||||
r.hset("rooms", room.code, room.json())
|
r.hset("rooms", room.code, room.json())
|
||||||
|
|
||||||
r.publish(code, Message(
|
r.publish(code, Message(
|
||||||
msgtype=MessageType.START,
|
msgtype=MessageType.START,
|
||||||
data=RoundStart(czar=room.czar, card=cards["black"][cardid])).json())
|
data=RoundStart(czar=room.czar, card=cards["black"][cardid])).json())
|
||||||
|
r.publish(code, Message(msgtype=MessageType.ROOM,
|
||||||
|
data=PubRoom(**room.dict())).json())
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ class PubRoom(BaseModel):
|
||||||
played_cards: List[int] = []
|
played_cards: List[int] = []
|
||||||
|
|
||||||
class Answer(BaseModel):
|
class Answer(BaseModel):
|
||||||
text: str
|
text: List[str]
|
||||||
|
|
||||||
class AnswerReceived(Answer):
|
class AnswerReceived(Answer):
|
||||||
uuid: str
|
uuid: str
|
||||||
|
@ -75,6 +75,7 @@ class MessageType(AutoName):
|
||||||
START = auto()
|
START = auto()
|
||||||
ANSWER = auto()
|
ANSWER = auto()
|
||||||
PICK = auto()
|
PICK = auto()
|
||||||
|
ROOM = auto()
|
||||||
|
|
||||||
|
|
||||||
class Message(BaseModel):
|
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