prevent double-play through race condition

This commit is contained in:
Stefan Rupp 2020-05-16 19:32:57 +02:00
parent fb638a60dc
commit 7ea63ce623
2 changed files with 22 additions and 13 deletions

View File

@ -2,7 +2,7 @@ from app_instance import api
from wiz_game import the_game from wiz_game import the_game
from starlette.websockets import WebSocketDisconnect from starlette.websockets import WebSocketDisconnect
import asyncio
@api.route("/player/{player}") @api.route("/player/{player}")
def show(req, resp, player: str): def show(req, resp, player: str):
@ -31,19 +31,28 @@ def show(req, resp, player: str):
@api.route("/player/{player}/play/{card}") @api.route("/player/{player}/play/{card}")
async def play(req, resp, player: str, card: str): async def play(req, resp, player: str, card: str):
lock = asyncio.Lock()
p = int(player) p = int(player)
c = int(card) c = int(card)
the_game.play_card(p, c)
if the_game.is_hand_finished(): async with lock:
print("hand finished") try:
trick_winner, highest_card = the_game.get_trick_winner() isActive = the_game.players_ordered[the_game.active_player].id == p
trick_winner.take_trick() except IndexError:
#the_game.next_hand() isActive = False
elif the_game.is_trick_finished(): print("show p: {} | {}".format('None', p))
print("trick finished") if isActive:
trick_winner, highest_card = the_game.get_trick_winner() the_game.play_card(p, c)
trick_winner.take_trick() if the_game.is_hand_finished():
the_game.next_trick() print("hand finished")
trick_winner, highest_card = the_game.get_trick_winner()
trick_winner.take_trick()
#the_game.next_hand()
elif the_game.is_trick_finished():
print("trick finished")
trick_winner, highest_card = the_game.get_trick_winner()
trick_winner.take_trick()
the_game.next_trick()
url = '/player/'+player url = '/player/'+player
for ws in the_game.websockets: for ws in the_game.websockets:

View File

@ -209,6 +209,6 @@ class WizGame:
return self.start_player-1 return self.start_player-1
p = ["Astrid", "Stephan", "Patrice", "struppi"] p = ["A", "B", "C", "D", "E"]
the_game = WizGame(p) the_game = WizGame(p)