cowiz20/views/player.py
2021-02-03 14:16:05 +01:00

154 lines
4.9 KiB
Python

from app_instance import api
from wiz_game import the_game
from starlette.websockets import WebSocketDisconnect
import asyncio
import json
playing_lock = asyncio.Lock()
@api.route("/player/{player}")
def show(req, resp, player: str):
p = int(player)
tcard = the_game.trump_card
tcolor = the_game.get_trump_color()
try:
isActive = the_game.players_ordered[the_game.active_player].id == p
except IndexError:
isActive = False
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)
async def play_card(player, card):
global playing_lock
async with playing_lock:
try:
isActive = the_game.players_ordered[the_game.active_player].id == player
except IndexError:
isActive = False
if isActive:
the_game.play_card(player, card)
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()
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:
try:
await ws.send_json(msg)
except Exception:
the_game.websockets.remove(ws)
@api.route("/player/{player}/play/{card}")
async def play(req, resp, player: str, card: str):
p = int(player)
c = int(card)
play_card(p, c)
url = '/player/'+player
api.redirect(resp, url, status_code=303)
@api.route("/player/{player}/set_trump/{trump}")
async def set_trump_color_rest(req, resp, player, trump):
p = int(player)
the_game.set_trump_color(trump)
url = '/player/'+player
await the_game.send_page_reload()
api.redirect(resp, url, status_code=303)
async def set_trump_color(player, color):
p = int(player)
if p == the_game.players_ordered[-1].id:
the_game.set_trump_color(color)
await the_game.send_page_reload()
async def initGame(player, ws):
p = int(player)
players = the_game.players[p:] + the_game.players[:p]
n = [x.name for x in players]
msg = {"type": "message", "fct": "startGame", "names": n}
await ws.send_json(msg)
async def getHand(player, ws):
p = int(player)
c = the_game.players[p].cards
s = [x.json_serialise() for x in c]
msg = {"type": "message", "fct": "getHand", "cards": s}
await ws.send_json(msg)
async def ws_handler(ws):
await ws.accept()
the_game.websockets.append(ws)
while True:
try:
x = await ws.receive_json()
fct = x['fct']
if fct == 'playcard':
p = int(x['player'])
c = int(x['card'])
await play_card_js(p, c)
elif fct == 'setTrumpColor':
player = x['player']
color = x['color']
await set_trump_color(player, color)
elif fct == "requestInit":
player = x['player']
await initGame(player, ws);
elif fct == "getHand":
player = x['player']
await getHand(player, ws)
else:
print("confused :(")
except WebSocketDisconnect:
the_game.websockets.remove(ws)
break;
@api.route('/ws', websocket=True)
async def websocket(ws):
task = asyncio.create_task(ws_handler(ws))
the_game.ws_tasks.append(task)
await task;