playing cards per websocket now :)

This commit is contained in:
Stefan Rupp 2020-05-21 19:54:39 +02:00
parent 688641d749
commit 644c28ae75
5 changed files with 40 additions and 38 deletions

View File

@ -2,6 +2,13 @@
{% block content %}
<script>
function playCard(card) {
let player = {{player}}
window.game_socket.send("playcard "+player+":"+card)
}
</script>
<div class="content">
<h1><span class="font-semi-bold">Gambling Foo</span> <span class="smaller">A RESTful gaming service</span></h1>
<p class="lead">
@ -42,7 +49,11 @@
<li>
<strong>>card {{loop.index}} is: {{card}} </strong><br>
{% if playerActive and not choose_trump_color %}
<!--
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}&lt;{{card}}&gt;</a>
<div style="background-color:#D94A38;width:170px;height:80px;margin:20px;padding-top:20px;color:#ffffff;font-weight:bold;font-size:18px;float:left;text-align:center;" onclick="playCard({{loop.index0}})">Play Me</div>
-->
<button onclick="playCard({{loop.index0}})">Play {{card}}</button>
{% endif %}
</li>
{% endfor %}

View File

@ -23,6 +23,7 @@
<![endif]-->
<script>
let socket = new WebSocket("wss://cowiz.struppi.name/ws");
window.game_socket = socket
socket.onopen = function(e) {
//alert("[open] Connection established");

View File

@ -33,22 +33,14 @@ def index(req, resp):
@api.route("/control/next_hand/")
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:
pass
await the_game.send_page_reload()
api.redirect(resp, '/control/', status_code=303)
@api.route("/control/next_trick/")
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:
pass
await the_game.send_page_reload()
api.redirect(resp, '/control/', status_code=303)
@ -60,11 +52,7 @@ async def undo_last(req, resp):
p, c = the_game.played_cards.pop()
the_game.active_player -= 1
p.add_card(c)
for ws in the_game.websockets:
try:
await ws.send_text("reload")
except Exception as e:
pass
await the_game.send_page_reload()
else:
if the_game.num_tricks_played > 0:
the_game.num_tricks_played -= 1;
@ -75,10 +63,7 @@ async def undo_last(req, resp):
the_game.players_ordered = [p for p, _ in the_game.played_cards]
p, c = the_game.played_cards.pop()
p.add_card(c)
for ws in the_game.websockets:
try:
await ws.send_text("reload")
except Exception as e:
pass
await the_game.send_page_reload()
api.redirect(resp, '/control/', status_code=303)

View File

@ -28,19 +28,15 @@ def show(req, resp, player: str):
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):
p = int(player)
c = int(card)
async def play_card(player, card):
global playing_lock
async with playing_lock:
try:
isActive = the_game.players_ordered[the_game.active_player].id == p
isActive = the_game.players_ordered[the_game.active_player].id == player
except IndexError:
isActive = False
if isActive:
the_game.play_card(p, c)
the_game.play_card(player, card)
if the_game.is_hand_finished():
trick_winner, highest_card = the_game.get_trick_winner()
trick_winner.take_trick()
@ -48,13 +44,15 @@ async def play(req, resp, player: str, card: str):
trick_winner, highest_card = the_game.get_trick_winner()
trick_winner.take_trick()
the_game.next_trick()
await the_game.send_page_reload()
@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
for ws in the_game.websockets:
try:
await ws.send_text("reload")
except Exception as e:
pass
api.redirect(resp, url, status_code=303)
@api.route("/player/{player}/set_trump/{trump}")
@ -62,11 +60,7 @@ 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:
pass
await the_game.send_page_reload()
api.redirect(resp, url, status_code=303)
@ -75,7 +69,11 @@ async def websocket(ws):
await ws.accept()
the_game.websockets.append(ws)
try:
foo = await ws.receive_text()
cmd = await ws.receive_text()
fct, args = cmd.split()
if fct == "playcard":
p, c = map(int, args.split(':'))
await play_card(p, c)
except WebSocketDisconnect:
the_game.websockets.remove(ws)

View File

@ -212,6 +212,13 @@ class WizGame:
else:
return self.start_player-1
async def send_page_reload(self):
for ws in self.websockets:
try:
await ws.send_text("reload")
except Exception as e:
pass
p = ["A", "B", "C", "D", "E"]
the_game = WizGame(p)