implement undo function

This commit is contained in:
Stefan Rupp 2020-05-21 18:49:52 +02:00
parent c6def1169f
commit 688641d749
3 changed files with 37 additions and 0 deletions

View File

@ -26,6 +26,8 @@
Winner: {{trick_winner}} with Card {{highest_card}}
<a href="/control/next_hand/">start next hand</a>
{% endif %}
<br>
<a href="/control/undo_last/">undo last card</a>
<!--
<ul>
<li>

View File

@ -51,3 +51,34 @@ async def start_next_trick(req, resp):
pass
api.redirect(resp, '/control/', status_code=303)
@api.route("/control/undo_last/")
async def undo_last(req, resp):
hand_finished = the_game.is_hand_finished()
if the_game.played_cards:
if not hand_finished:
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
else:
if the_game.num_tricks_played > 0:
the_game.num_tricks_played -= 1;
the_game.played_cards = the_game.last_trick;
the_game.active_player = len(the_game.players) - 1
player, _ = the_game.get_trick_winner()
player.tricks_taken -= 1
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
api.redirect(resp, '/control/', status_code=303)

View File

@ -73,6 +73,10 @@ class Player:
def take_trick(self):
self.tricks_taken += 1
def add_card(self, card):
self.cards.append(card)
self.cards = sorted(self.cards, key=attrgetter('color', 'value'))
class WizGame:
def __init__(self, player_names: list):