diff --git a/js-cardgame b/js-cardgame index 0115621..4d88b91 160000 --- a/js-cardgame +++ b/js-cardgame @@ -1 +1 @@ -Subproject commit 0115621a3d1081239fbea7a7746d7093dc67524f +Subproject commit 4d88b9198cb335414600aab691bbe1ae5edb590c diff --git a/templates/home/gplayer.html b/templates/home/gplayer.html index 5eeeaea..ccd1377 100644 --- a/templates/home/gplayer.html +++ b/templates/home/gplayer.html @@ -101,6 +101,7 @@ function setupGame(names, hand) //game.play_card(4, 0, "y05"); //game.play_card(5, 0, "g05"); //game.give_trick_to(0); + window.the_game = game; }); }; diff --git a/templates/shared/_layout.html b/templates/shared/_layout.html index 70d78dc..55bd718 100644 --- a/templates/shared/_layout.html +++ b/templates/shared/_layout.html @@ -71,6 +71,13 @@ setupGame(names, window.hand) //alert('game is set up') } + else if (fct == "cardPlayed") { + let player_id = msg.player; + let card_idx = msg.card_idx; + let card = msg.card; + let num_players = msg.num_players; + cardPlayed(player_id, card_idx, card, num_players) + } else { alert('unknown command: '+event.data); } @@ -103,6 +110,12 @@ window.game_socket.send(t); } + function cardPlayed(player, card_idx, card, num_players) { + let myself = "{{player}}"; + let player_idx = ((player + num_players) - myself) % num_players; + window.the_game.play_card(player_idx, card_idx, card) + } + diff --git a/views/player.py b/views/player.py index d31da18..f0a38de 100644 --- a/views/player.py +++ b/views/player.py @@ -48,6 +48,31 @@ async def play_card(player, card): await the_game.send_page_reload() +async def play_card_js(player_id, card_idx): + global playing_lock + async with playing_lock: + try: + isActive = the_game.players_ordered[the_game.active_player].id == player_id + except IndexError: + isActive = False + if isActive: + card = the_game.play_card(player_id, card_idx) + + if the_game.is_round_finished(): + trick_winner, highest_card = the_game.get_trick_winner() + trick_winner.take_trick() + elif the_game.is_trick_finished(): + trick_winner, highest_card = the_game.get_trick_winner() + trick_winner.take_trick() + the_game.next_trick() + + s = card.json_serialise() + msg = {"type": "message", "fct": "cardPlayed", "card": s, "card_idx": card_idx, + "player": player_id, "num_players": len(the_game.players)} + for ws in the_game.websockets: + await ws.send_json(msg) + + @api.route("/player/{player}/play/{card}") async def play(req, resp, player: str, card: str): p = int(player) @@ -99,7 +124,7 @@ async def ws_handler(ws): if fct == 'playcard': p = int(x['player']) c = int(x['card']) - await play_card(p, c) + await play_card_js(p, c) elif fct == 'setTrumpColor': player = x['player'] color = x['color'] diff --git a/wiz_game.py b/wiz_game.py index 4ee8262..8d8401f 100644 --- a/wiz_game.py +++ b/wiz_game.py @@ -203,6 +203,7 @@ class WizGame: self.active_player += 1 if self.is_trick_finished(): self.num_tricks_played += 1 + return c # return card to calling function def set_trump_color(self, trump_color): if self.trump_card.value != 'Z':