2020-04-05 16:00:48 +02:00
|
|
|
from app_instance import api
|
|
|
|
|
|
|
|
from wiz_game import the_game
|
2020-04-19 17:14:25 +02:00
|
|
|
from starlette.websockets import WebSocketDisconnect
|
2020-05-16 19:32:57 +02:00
|
|
|
import asyncio
|
2020-12-06 21:21:51 +01:00
|
|
|
import json
|
2020-04-05 16:00:48 +02:00
|
|
|
|
2020-05-18 21:13:51 +02:00
|
|
|
|
|
|
|
playing_lock = asyncio.Lock()
|
|
|
|
|
|
|
|
|
2020-04-05 16:00:48 +02:00
|
|
|
@api.route("/player/{player}")
|
2020-04-12 15:27:05 +02:00
|
|
|
def show(req, resp, player: str):
|
2020-04-05 16:00:48 +02:00
|
|
|
p = int(player)
|
2020-04-05 23:27:33 +02:00
|
|
|
tcard = the_game.trump_card
|
|
|
|
tcolor = the_game.get_trump_color()
|
2020-04-13 22:07:38 +02:00
|
|
|
try:
|
|
|
|
isActive = the_game.players_ordered[the_game.active_player].id == p
|
|
|
|
except IndexError:
|
|
|
|
isActive = False
|
|
|
|
|
2020-04-13 16:14:36 +02:00
|
|
|
prev_player = the_game.get_prev_player()
|
2020-04-19 00:38:57 +02:00
|
|
|
if tcard and tcard.value == 'Z' and tcolor == '-':
|
2020-04-13 16:14:36 +02:00
|
|
|
choose_trump_color = True
|
|
|
|
else:
|
|
|
|
choose_trump_color = False
|
2020-04-13 22:07:38 +02:00
|
|
|
resp.content = api.template('home/player.html', player=p, playerActive=isActive, choose_trump_player=prev_player,
|
2020-04-13 16:14:36 +02:00
|
|
|
cards=the_game.players[p].cards, choose_trump_color=choose_trump_color,
|
2020-04-25 22:00:23 +02:00
|
|
|
trump_card=tcard, trump_color=tcolor, played_cards=the_game.played_cards, last_trick=the_game.last_trick)
|
2020-04-06 22:42:31 +02:00
|
|
|
|
|
|
|
|
2020-05-21 19:54:39 +02:00
|
|
|
async def play_card(player, card):
|
2020-05-18 21:13:51 +02:00
|
|
|
global playing_lock
|
|
|
|
async with playing_lock:
|
2020-05-16 19:32:57 +02:00
|
|
|
try:
|
2020-05-21 19:54:39 +02:00
|
|
|
isActive = the_game.players_ordered[the_game.active_player].id == player
|
2020-05-16 19:32:57 +02:00
|
|
|
except IndexError:
|
|
|
|
isActive = False
|
|
|
|
if isActive:
|
2020-05-21 19:54:39 +02:00
|
|
|
the_game.play_card(player, card)
|
2020-10-11 18:10:05 +02:00
|
|
|
if the_game.is_round_finished():
|
2020-05-16 19:32:57 +02:00
|
|
|
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()
|
2020-05-21 19:54:39 +02:00
|
|
|
await the_game.send_page_reload()
|
|
|
|
|
2020-04-25 22:00:23 +02:00
|
|
|
|
2020-05-21 19:54:39 +02:00
|
|
|
@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)
|
2020-04-07 21:13:23 +02:00
|
|
|
url = '/player/'+player
|
|
|
|
api.redirect(resp, url, status_code=303)
|
2020-04-06 22:42:31 +02:00
|
|
|
|
2020-12-06 22:28:45 +01:00
|
|
|
|
2020-04-13 16:14:36 +02:00
|
|
|
@api.route("/player/{player}/set_trump/{trump}")
|
2020-05-22 15:09:45 +02:00
|
|
|
async def set_trump_color_rest(req, resp, player, trump):
|
2020-04-13 16:14:36 +02:00
|
|
|
p = int(player)
|
|
|
|
the_game.set_trump_color(trump)
|
|
|
|
url = '/player/'+player
|
2020-05-21 19:54:39 +02:00
|
|
|
await the_game.send_page_reload()
|
2020-04-13 16:14:36 +02:00
|
|
|
api.redirect(resp, url, status_code=303)
|
2020-04-19 17:14:25 +02:00
|
|
|
|
2020-12-06 22:28:45 +01:00
|
|
|
|
2020-05-22 15:09:45 +02:00
|
|
|
async def set_trump_color(player, color):
|
|
|
|
p = int(player)
|
2020-05-22 15:29:31 +02:00
|
|
|
if p == the_game.players_ordered[-1].id:
|
2020-05-22 15:09:45 +02:00
|
|
|
the_game.set_trump_color(color)
|
|
|
|
await the_game.send_page_reload()
|
2020-04-19 17:14:25 +02:00
|
|
|
|
2020-12-06 22:28:45 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-04-19 17:14:25 +02:00
|
|
|
@api.route('/ws', websocket=True)
|
|
|
|
async def websocket(ws):
|
|
|
|
await ws.accept()
|
|
|
|
the_game.websockets.append(ws)
|
|
|
|
try:
|
2020-12-06 21:21:51 +01:00
|
|
|
x = await ws.receive_json()
|
|
|
|
fct = x['fct']
|
|
|
|
if fct == 'playcard':
|
|
|
|
p = int(x['player'])
|
|
|
|
c = int(x['card'])
|
2020-05-21 19:54:39 +02:00
|
|
|
await play_card(p, c)
|
2020-12-06 21:21:51 +01:00
|
|
|
elif fct == 'setTrumpColor':
|
|
|
|
player = x['player']
|
|
|
|
color = x['color']
|
2020-05-22 15:09:45 +02:00
|
|
|
await set_trump_color(player, color)
|
2020-12-06 22:28:45 +01:00
|
|
|
elif fct == "requestInit":
|
2020-12-11 17:52:19 +01:00
|
|
|
print("before init")
|
2020-12-06 22:28:45 +01:00
|
|
|
await initGame(ws);
|
2020-12-11 17:52:19 +01:00
|
|
|
print("after init")
|
2020-12-06 21:21:51 +01:00
|
|
|
else:
|
|
|
|
print("confused :(")
|
|
|
|
|
2020-04-19 17:14:25 +02:00
|
|
|
except WebSocketDisconnect:
|
|
|
|
the_game.websockets.remove(ws)
|
|
|
|
|