websocket comm per json now

This commit is contained in:
Stefan Rupp 2020-12-06 21:21:51 +01:00
parent c4746d2306
commit ebf7878397
3 changed files with 48 additions and 10 deletions

View File

@ -4,13 +4,30 @@
<script> <script>
function playCard(card) { function playCard(card) {
let player = {{player}} let player = "{{player}}";
window.game_socket.send("playcard "+player+":"+card) let c = card.toString();
var msg = {
type: "message",
fct: "playcard",
player: player,
card: c
};
//window.game_socket.send("playcard "+player+":"+card);
t = JSON.stringify(msg);
window.game_socket.send(t);
} }
function setTrumpColor(color) { function setTrumpColor(color) {
let player = {{player}} let player = "{{player}}"
window.game_socket.send("setTrumpColor "+player+":"+color) var msg = {
type: "message",
fct: "setTrumpColor",
player: player,
color: color
};
//window.game_socket.send("setTrumpColor "+player+":"+color)
t = JSON.stringify(msg);
window.game_socket.send(t);
} }
</script> </script>

View File

@ -37,6 +37,9 @@
if (event.data == "reload") { if (event.data == "reload") {
location.reload(true); location.reload(true);
} }
else if (event.data.startsWith("start game: ")) {
let names = "..."
}
else { else {
alert('unknown command: '+event.data); alert('unknown command: '+event.data);
} }

View File

@ -3,6 +3,7 @@ from app_instance import api
from wiz_game import the_game from wiz_game import the_game
from starlette.websockets import WebSocketDisconnect from starlette.websockets import WebSocketDisconnect
import asyncio import asyncio
import json
playing_lock = asyncio.Lock() playing_lock = asyncio.Lock()
@ -74,14 +75,31 @@ async def websocket(ws):
await ws.accept() await ws.accept()
the_game.websockets.append(ws) the_game.websockets.append(ws)
try: try:
cmd = await ws.receive_text() x = await ws.receive_json()
fct, args = cmd.split() print(x)
if fct == "playcard": fct = x['fct']
p, c = map(int, args.split(':')) if fct == 'playcard':
print("play card :)")
p = int(x['player'])
c = int(x['card'])
await play_card(p, c) await play_card(p, c)
elif fct == "setTrumpColor": elif fct == 'setTrumpColor':
player, color = args.split(':') print("trump color :)")
player = x['player']
color = x['color']
await set_trump_color(player, 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: except WebSocketDisconnect:
the_game.websockets.remove(ws) the_game.websockets.remove(ws)