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>
function playCard(card) {
let player = {{player}}
window.game_socket.send("playcard "+player+":"+card)
let player = "{{player}}";
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) {
let player = {{player}}
window.game_socket.send("setTrumpColor "+player+":"+color)
let player = "{{player}}"
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>

View File

@ -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);
}

View File

@ -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)