built in websockets for auto-reload

This commit is contained in:
2020-04-19 17:14:25 +02:00
parent 38102ce778
commit 72e968274b
5 changed files with 73 additions and 9 deletions

View File

@ -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)

View File

@ -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)