from app_instance import api from wiz_game import the_game from starlette.websockets import WebSocketDisconnect import asyncio @api.route("/player/{player}") def show(req, resp, player: str): p = int(player) name = the_game.players[p].name tcard = the_game.trump_card tcolor = the_game.get_trump_color() #isActive = the_game.is_players_turn(p) try: isActive = the_game.players_ordered[the_game.active_player].id == p print("show p: {} | {}".format(the_game.players_ordered[the_game.active_player].id, p)) except IndexError: isActive = False print("show p: {} | {}".format('None', p)) #active_player = the_game.active_player prev_player = the_game.get_prev_player() if tcard and tcard.value == 'Z' and tcolor == '-': choose_trump_color = True else: choose_trump_color = False resp.content = api.template('home/player.html', player=p, playerActive=isActive, choose_trump_player=prev_player, cards=the_game.players[p].cards, choose_trump_color=choose_trump_color, trump_card=tcard, trump_color=tcolor, played_cards=the_game.played_cards, last_trick=the_game.last_trick) @api.route("/player/{player}/play/{card}") async def play(req, resp, player: str, card: str): lock = asyncio.Lock() p = int(player) c = int(card) async with lock: try: isActive = the_game.players_ordered[the_game.active_player].id == p except IndexError: isActive = False print("show p: {} | {}".format('None', p)) if isActive: the_game.play_card(p, c) if the_game.is_hand_finished(): print("hand finished") trick_winner, highest_card = the_game.get_trick_winner() trick_winner.take_trick() #the_game.next_hand() elif the_game.is_trick_finished(): print("trick finished") trick_winner, highest_card = the_game.get_trick_winner() trick_winner.take_trick() the_game.next_trick() 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}") 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)