added commands for requesting and receiving bets and starting rounds
This commit is contained in:
+69
-10
@@ -22,7 +22,8 @@ class Lobby:
|
||||
self.host_client_id = host_client_id
|
||||
self.connected_clients = {}
|
||||
self.client_playing_order = []
|
||||
self.currently_playing_client = 0 # index of client_playing_order
|
||||
self.client_bets = {} # client_id: bet
|
||||
# self.currently_playing_client = 0 # index of client_playing_order
|
||||
|
||||
class Client:
|
||||
|
||||
@@ -197,10 +198,9 @@ class LobbyManager:
|
||||
await self.broadcast_message(
|
||||
lobby_id,
|
||||
json.dumps({
|
||||
"response": "client_played_card",
|
||||
"response": "client_submitted_bet",
|
||||
"client_id": client_id,
|
||||
"bet": bet,
|
||||
"next_client": None, # TODO!
|
||||
})
|
||||
)
|
||||
|
||||
@@ -211,7 +211,6 @@ class LobbyManager:
|
||||
"response": "client_played_card",
|
||||
"client_id": client_id,
|
||||
"card": played_card,
|
||||
"next_client": None, # TODO!
|
||||
})
|
||||
)
|
||||
|
||||
@@ -401,28 +400,88 @@ async def process_input(message: str, websocket) -> None:
|
||||
elif data_object["response"] == "start_next_round":
|
||||
lobby_id = data_object["lobby_id"]
|
||||
|
||||
# Reset bets
|
||||
lobby_manager.get_lobby(lobby_id).client_bets = {}
|
||||
|
||||
await lobby_manager.spread_cards(
|
||||
lobby_id,
|
||||
data_object["round"],
|
||||
data_object["cards"],
|
||||
None,
|
||||
)
|
||||
)
|
||||
|
||||
elif data_object["response"] == "submit_bet":
|
||||
lobby_id = data_object["lobby_id"]
|
||||
client_id = data_object["client_id"]
|
||||
bet = data_object["bet"]
|
||||
|
||||
lobby_manager.get_lobby(lobby_id).client_bets[client_id] = bet
|
||||
|
||||
await lobby_manager.spread_bet(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
data_object["bet"],
|
||||
lobby_id,
|
||||
client_id,
|
||||
bet,
|
||||
)
|
||||
|
||||
last_client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
|
||||
player_count = lobby_manager.get_lobby_player_count(lobby_id)
|
||||
|
||||
if last_client_index == (player_count - 1):
|
||||
# all have bet; start game
|
||||
await lobby_manager.broadcast_message(
|
||||
lobby_id,
|
||||
json.dumps({
|
||||
"response": "begin_playing_cards",
|
||||
}),
|
||||
)
|
||||
|
||||
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[0]
|
||||
|
||||
await lobby_manager.send_message(
|
||||
lobby_id,
|
||||
playing_client_id,
|
||||
json.dumps({
|
||||
"response": "request_card",
|
||||
}),
|
||||
)
|
||||
else:
|
||||
# bets are not finished; continue asking for bets
|
||||
disallowed_bet = -1
|
||||
if last_client_index == (player_count - 2):
|
||||
# last client to bet; limit bet options
|
||||
total_bet = sum(lobby_manager.get_lobby(lobby_id).client_bets.values())
|
||||
disallowed_bet = total_bet - data_object["round"]
|
||||
|
||||
await lobby_manager.send_message(
|
||||
lobby_id,
|
||||
lobby_manager.get_lobby(lobby_id).client_playing_order[last_client_index + 1],
|
||||
json.dumps({
|
||||
"response": "request_bet",
|
||||
"disallowed_bet": disallowed_bet,
|
||||
}),
|
||||
)
|
||||
|
||||
elif data_object["response"] == "play_card":
|
||||
lobby_id = data_object["lobby_id"]
|
||||
client_id = data_object["client_id"]
|
||||
|
||||
await lobby_manager.spread_played_card(
|
||||
data_object["lobby_id"],
|
||||
data_object["client_id"],
|
||||
lobby_id,
|
||||
client_id,
|
||||
data_object["played_card"],
|
||||
)
|
||||
|
||||
client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
|
||||
if not client_index == (len(lobby_manager.get_lobby(lobby_id).client_playing_order) - 1):
|
||||
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[client_index + 1]
|
||||
|
||||
await lobby_manager.send_message(
|
||||
lobby_id,
|
||||
playing_client_id,
|
||||
json.dumps({
|
||||
"response": "request_card",
|
||||
}),
|
||||
)
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_local = False
|
||||
|
||||
Reference in New Issue
Block a user