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 %} {% block content %}
<script>
function playCard(card) {
let player = {{player}}
window.game_socket.send("playcard "+player+":"+card)
}
</script>
<div class="content"> <div class="content">
<h1><span class="font-semi-bold">Gambling Foo</span> <span class="smaller">A RESTful gaming service</span></h1> <h1><span class="font-semi-bold">Gambling Foo</span> <span class="smaller">A RESTful gaming service</span></h1>
<p class="lead"> <p class="lead">
@ -42,7 +49,11 @@
<li> <li>
<strong>>card {{loop.index}} is: {{card}} </strong><br> <strong>>card {{loop.index}} is: {{card}} </strong><br>
{% if playerActive and not choose_trump_color %} {% if playerActive and not choose_trump_color %}
<!--
<a href="/player/{{player}}/play/{{loop.index0}}">play card {{loop.index}}&lt;{{card}}&gt;</a> <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 %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}

View File

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

View File

@ -33,22 +33,14 @@ def index(req, resp):
@api.route("/control/next_hand/") @api.route("/control/next_hand/")
async def start_next_hand(req, resp): async def start_next_hand(req, resp):
the_game.next_hand() the_game.next_hand()
for ws in the_game.websockets: await the_game.send_page_reload()
try:
await ws.send_text("reload")
except Exception as e:
pass
api.redirect(resp, '/control/', status_code=303) api.redirect(resp, '/control/', status_code=303)
@api.route("/control/next_trick/") @api.route("/control/next_trick/")
async def start_next_trick(req, resp): async def start_next_trick(req, resp):
the_game.next_trick() the_game.next_trick()
for ws in the_game.websockets: await the_game.send_page_reload()
try:
await ws.send_text("reload")
except Exception as e:
pass
api.redirect(resp, '/control/', status_code=303) api.redirect(resp, '/control/', status_code=303)
@ -60,11 +52,7 @@ async def undo_last(req, resp):
p, c = the_game.played_cards.pop() p, c = the_game.played_cards.pop()
the_game.active_player -= 1 the_game.active_player -= 1
p.add_card(c) p.add_card(c)
for ws in the_game.websockets: await the_game.send_page_reload()
try:
await ws.send_text("reload")
except Exception as e:
pass
else: else:
if the_game.num_tricks_played > 0: if the_game.num_tricks_played > 0:
the_game.num_tricks_played -= 1; 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] the_game.players_ordered = [p for p, _ in the_game.played_cards]
p, c = the_game.played_cards.pop() p, c = the_game.played_cards.pop()
p.add_card(c) p.add_card(c)
for ws in the_game.websockets: await the_game.send_page_reload()
try:
await ws.send_text("reload")
except Exception as e:
pass
api.redirect(resp, '/control/', status_code=303) 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) 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_card(player, card):
async def play(req, resp, player: str, card: str):
p = int(player)
c = int(card)
global playing_lock global playing_lock
async with playing_lock: async with playing_lock:
try: 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: except IndexError:
isActive = False isActive = False
if isActive: if isActive:
the_game.play_card(p, c) the_game.play_card(player, card)
if the_game.is_hand_finished(): if the_game.is_hand_finished():
trick_winner, highest_card = the_game.get_trick_winner() trick_winner, highest_card = the_game.get_trick_winner()
trick_winner.take_trick() 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, highest_card = the_game.get_trick_winner()
trick_winner.take_trick() trick_winner.take_trick()
the_game.next_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 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.redirect(resp, url, status_code=303)
@api.route("/player/{player}/set_trump/{trump}") @api.route("/player/{player}/set_trump/{trump}")
@ -62,11 +60,7 @@ async def set_trump_color(req, resp, player, trump):
p = int(player) p = int(player)
the_game.set_trump_color(trump) the_game.set_trump_color(trump)
url = '/player/'+player url = '/player/'+player
for ws in the_game.websockets: await the_game.send_page_reload()
try:
await ws.send_text("reload")
except Exception as e:
pass
api.redirect(resp, url, status_code=303) api.redirect(resp, url, status_code=303)
@ -75,7 +69,11 @@ async def websocket(ws):
await ws.accept() await ws.accept()
the_game.websockets.append(ws) the_game.websockets.append(ws)
try: 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: except WebSocketDisconnect:
the_game.websockets.remove(ws) the_game.websockets.remove(ws)

View File

@ -212,6 +212,13 @@ class WizGame:
else: else:
return self.start_player-1 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"] p = ["A", "B", "C", "D", "E"]
the_game = WizGame(p) the_game = WizGame(p)