From 72e968274baa7250c527bba3f30cbb92eac20f5d Mon Sep 17 00:00:00 2001 From: Stefan Rupp Date: Sun, 19 Apr 2020 17:14:25 +0200 Subject: [PATCH] built in websockets for auto-reload --- server.py | 3 ++- templates/shared/_layout.html | 28 ++++++++++++++++++++++++++++ views/control.py | 23 +++++++++++++++++------ views/player.py | 26 ++++++++++++++++++++++++-- wiz_game.py | 2 ++ 5 files changed, 73 insertions(+), 9 deletions(-) diff --git a/server.py b/server.py index 1edee1e..e50abd0 100755 --- a/server.py +++ b/server.py @@ -14,7 +14,8 @@ def main(): random.seed() wiz_game.the_game.start_game() - api.run(port=8001, address="127.0.0.1") + #api.run(port=8001, address="127.0.0.1") + api.run(port=8001, address="192.168.42.1") exit(0) diff --git a/templates/shared/_layout.html b/templates/shared/_layout.html index a66260f..a825c91 100644 --- a/templates/shared/_layout.html +++ b/templates/shared/_layout.html @@ -21,6 +21,34 @@ + diff --git a/views/control.py b/views/control.py index 81c2694..6c8fc7a 100644 --- a/views/control.py +++ b/views/control.py @@ -32,16 +32,27 @@ def index(req, resp): ) @api.route("/control/next_hand/") -def start_next_hand(req, resp): +async def start_next_hand(req, resp): the_game.next_hand() + for ws in the_game.websockets: + try: + await ws.send_text("reload") + except Exception as e: + print("ws: got {}".format(e)) api.redirect(resp, '/control/', status_code=303) + @api.route("/control/next_trick/") -def start_next_trick(req, resp): +async def start_next_trick(req, resp): the_game.next_trick() + for ws in the_game.websockets: + try: + await ws.send_text("reload") + except Exception as e: + print("ws: got {}".format(e)) api.redirect(resp, '/control/', status_code=303) -@api.route("/control/deal/{cards}") -def deal(req, resp, cards): - the_game.deal_cards(int(cards)) - api.redirect(resp, '/control/', status_code=303) +#@api.route("/control/deal/{cards}") +#def deal(req, resp, cards): +# the_game.deal_cards(int(cards)) +# api.redirect(resp, '/control/', status_code=303) diff --git a/views/player.py b/views/player.py index c8fe823..d89b1a5 100644 --- a/views/player.py +++ b/views/player.py @@ -1,6 +1,7 @@ from app_instance import api from wiz_game import the_game +from starlette.websockets import WebSocketDisconnect @api.route("/player/{player}") @@ -29,16 +30,37 @@ def show(req, resp, player: str): @api.route("/player/{player}/play/{card}") -def play(req, resp, player: str, card: str): +async def play(req, resp, player: str, card: str): p = int(player) c = int(card) the_game.play_card(p, c) url = '/player/'+player + for ws in the_game.websockets: + try: + await ws.send_text("reload") + except Exception as e: + print("ws: got {}".format(e)) api.redirect(resp, url, status_code=303) @api.route("/player/{player}/set_trump/{trump}") -def set_trump_color(req, resp, player, trump): +async def set_trump_color(req, resp, player, trump): p = int(player) the_game.set_trump_color(trump) url = '/player/'+player + for ws in the_game.websockets: + try: + await ws.send_text("reload") + except Exception as e: + print("ws: got {}".format(e)) api.redirect(resp, url, status_code=303) + + +@api.route('/ws', websocket=True) +async def websocket(ws): + await ws.accept() + the_game.websockets.append(ws) + try: + foo = await ws.receive_text() + except WebSocketDisconnect: + the_game.websockets.remove(ws) + diff --git a/wiz_game.py b/wiz_game.py index 51b97c2..b607aa5 100644 --- a/wiz_game.py +++ b/wiz_game.py @@ -81,6 +81,7 @@ class WizGame: self.num_tricks_played = 0 self.played_cards = list() self.players_ordered = self.players + self.websockets = [] def start_game(self): self.create_deck() @@ -90,6 +91,7 @@ class WizGame: self.deal_cards(1) self.played_cards = list() self.players_ordered = self.players + self.websockets = [] def create_deck(self): self.card_deck = []