request game init from server

This commit is contained in:
Stefan Rupp 2020-12-06 22:28:45 +01:00
parent abccd2e51a
commit bcfc488c2e
4 changed files with 53 additions and 17 deletions

View File

@ -4,13 +4,28 @@
<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
};
let 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
};
let t = JSON.stringify(msg);
window.game_socket.send(t);
} }
</script> </script>
@ -26,7 +41,8 @@
let game = null; let game = null;
window.onload = function() //window.onload = function()
function setupGame(names)
{ {
PIXI.AbstractRenderer.autoDensity = true; PIXI.AbstractRenderer.autoDensity = true;
@ -73,15 +89,16 @@ window.onload = function()
loader.load(function(loader, resources) loader.load(function(loader, resources)
{ {
game = new Game(app.stage, resources); game = new Game(app.stage, resources);
game.init(['Hubert', 'Struppi', 'Patrice', 'Steffi', "Max", "Renate"]); //game.init(['Hubert', 'Struppi', 'Patrice', 'Steffi', "Max", "Renate"]);
game.init(names)
game.give_round(["z3", "b04", "r08", "g12", "y13", "r02", "b05", "b02", "b03", "g03", "g05"]); game.give_round(["z3", "b04", "r08", "g12", "y13", "r02", "b05", "b02", "b03", "g03", "g05"]);
game.play_card(0, 0, "z3"); //game.play_card(0, 0, "z3");
game.play_card(1, 0, "y13"); //game.play_card(1, 0, "y13");
game.play_card(2, 0, "b02"); //game.play_card(2, 0, "b02");
game.play_card(3, 0, "r02"); //game.play_card(3, 0, "r02");
game.play_card(4, 0, "y05"); //game.play_card(4, 0, "y05");
game.play_card(5, 0, "g05"); //game.play_card(5, 0, "g05");
game.give_trick_to(0); //game.give_trick_to(0);
}); });
}; };

View File

@ -30,16 +30,23 @@
//alert("[open] Connection established"); //alert("[open] Connection established");
//alert("Sending to server"); //alert("Sending to server");
//socket.send("My name is John"); //socket.send("My name is John");
var msg = {
type: "message",
fct: "requestInit",
};
let t = JSON.stringify(msg);
window.game_socket.send(t);
}; };
socket.onmessage = function(event) { socket.onmessage = function(event) {
let x = JSON.parse(event.data); let msg = JSON.parse(event.data);
let fct = x.fct; let fct = msg.fct;
if (fct == "reload") { if (fct == "reload") {
location.reload(true); location.reload(true);
} }
else if (fct == "startGame") { else if (fct == "startGame") {
let names = "..." let names = msg.names
setupGame(names)
} }
else { else {
alert('unknown command: '+event.data); alert('unknown command: '+event.data);

View File

@ -56,6 +56,7 @@ async def play(req, resp, player: str, card: str):
url = '/player/'+player url = '/player/'+player
api.redirect(resp, url, status_code=303) api.redirect(resp, url, status_code=303)
@api.route("/player/{player}/set_trump/{trump}") @api.route("/player/{player}/set_trump/{trump}")
async def set_trump_color_rest(req, resp, player, trump): async def set_trump_color_rest(req, resp, player, trump):
p = int(player) p = int(player)
@ -64,12 +65,21 @@ async def set_trump_color_rest(req, resp, player, trump):
await the_game.send_page_reload() await the_game.send_page_reload()
api.redirect(resp, url, status_code=303) api.redirect(resp, url, status_code=303)
async def set_trump_color(player, color): async def set_trump_color(player, color):
p = int(player) p = int(player)
if p == the_game.players_ordered[-1].id: if p == the_game.players_ordered[-1].id:
the_game.set_trump_color(color) the_game.set_trump_color(color)
await the_game.send_page_reload() await the_game.send_page_reload()
async def initGame(ws):
p = the_game.players
n = [x.name for x in p]
msg = {"type": "message", "fct": "startGame", "names": n}
await ws.send_json(msg)
@api.route('/ws', websocket=True) @api.route('/ws', websocket=True)
async def websocket(ws): async def websocket(ws):
await ws.accept() await ws.accept()
@ -85,6 +95,8 @@ async def websocket(ws):
player = x['player'] player = x['player']
color = x['color'] color = x['color']
await set_trump_color(player, color) await set_trump_color(player, color)
elif fct == "requestInit":
await initGame(ws);
else: else:
print("confused :(") print("confused :(")

View File

@ -220,6 +220,6 @@ class WizGame:
pass pass
p = ["A", "B", "C", "D", "E"] p = ["egg", "spam", "ham", "pups", "kekse"]
the_game = WizGame(p) the_game = WizGame(p)