From ebf7878397c1fc796121dffd475bc6e0d806cc33 Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Sun, 6 Dec 2020 21:21:51 +0100 Subject: [PATCH] websocket comm per json now --- templates/home/player.html | 25 +++++++++++++++++++++---- templates/shared/_layout.html | 3 +++ views/player.py | 30 ++++++++++++++++++++++++------ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/templates/home/player.html b/templates/home/player.html index cb038d7..37eb28a 100644 --- a/templates/home/player.html +++ b/templates/home/player.html @@ -4,13 +4,30 @@ diff --git a/templates/shared/_layout.html b/templates/shared/_layout.html index 6505ef2..c9935a3 100644 --- a/templates/shared/_layout.html +++ b/templates/shared/_layout.html @@ -37,6 +37,9 @@ if (event.data == "reload") { location.reload(true); } + else if (event.data.startsWith("start game: ")) { + let names = "..." + } else { alert('unknown command: '+event.data); } diff --git a/views/player.py b/views/player.py index 12d012b..2e0c864 100644 --- a/views/player.py +++ b/views/player.py @@ -3,6 +3,7 @@ from app_instance import api from wiz_game import the_game from starlette.websockets import WebSocketDisconnect import asyncio +import json playing_lock = asyncio.Lock() @@ -74,14 +75,31 @@ async def websocket(ws): await ws.accept() the_game.websockets.append(ws) try: - cmd = await ws.receive_text() - fct, args = cmd.split() - if fct == "playcard": - p, c = map(int, args.split(':')) + x = await ws.receive_json() + print(x) + fct = x['fct'] + if fct == 'playcard': + print("play card :)") + p = int(x['player']) + c = int(x['card']) await play_card(p, c) - elif fct == "setTrumpColor": - player, color = args.split(':') + elif fct == 'setTrumpColor': + print("trump color :)") + player = x['player'] + color = x['color'] await set_trump_color(player, color) + else: + print("confused :(") + + # cmd = await ws.receive_text() + # fct, args = cmd.split() + # if fct == "playcard": + # p, c = map(int, args.split(':')) + # await play_card(p, c) + # elif fct == "setTrumpColor": + # player, color = args.split(':') + # await set_trump_color(player, color) + except WebSocketDisconnect: the_game.websockets.remove(ws)