server now rotates through playing order every round and lets the winning player go first

This commit is contained in:
2025-07-20 18:30:51 +02:00
parent 5e54ce45e7
commit 9b8390cfb3
4 changed files with 57 additions and 35 deletions
+47 -13
View File
@@ -21,9 +21,11 @@ class Lobby:
self.lobby_id = lobby_id
self.host_client_id = host_client_id
self.connected_clients = {}
# original order, set at the start of the game and not modified thereafter
self.client_playing_order = []
# order that is changed with every round and take, based on client_playing_order
self.current_playing_order = []
self.client_bets = {} # client_id: bet
# self.currently_playing_client = 0 # index of client_playing_order
self.current_round = 0
class Client:
@@ -187,7 +189,7 @@ class LobbyManager:
lobby_id,
client_id,
json.dumps({
"response": "new_round_started",
"response": "round_started",
"current_round": round,
"cards": cards,
"trump": trump,
@@ -253,6 +255,22 @@ class LobbyManager:
return self.get_unique_client_id(lobby_id)
return id
# Shifts the playing order by (current_round - 1) to start with the next player in line.
def shift_playing_order(self, lobby_id, current_round) -> list:
order = self.rotate(self.get_lobby(lobby_id).client_playing_order, current_round - 1)
self.get_lobby(lobby_id).current_playing_order = order
return order
def rotate(self, l, n):
return l[n:] + l[:n]
# Returns a playing order starting at client_id.
def set_playing_order_start(self, lobby_id, client_id) -> list:
order = self.shift_playing_order(lobby_id, self.get_lobby(lobby_id).client_playing_order.index(client_id) + 1)
self.get_lobby(lobby_id).current_playing_order = order
return order
#endregion
lobby_manager = LobbyManager()
@@ -413,16 +431,22 @@ async def process_input(message: str, websocket) -> None:
lobby_manager.get_lobby(data_object["lobby_id"]).current_round = 1
print(f"Lobby {lobby_id} is starting the game, playing order is: {lobby.client_playing_order}")
print(f"Lobby {lobby_id} is starting the game, initial playing order is: {lobby.client_playing_order}")
# Initialise current_playing_order
lobby_manager.shift_playing_order(lobby_id, 1)
await lobby_manager.request_bet(
lobby_id,
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
lobby_manager.get_lobby(lobby_id).current_playing_order[0],
-1,
)
elif data_object["response"] == "start_next_play":
lobby_id = data_object["lobby_id"]
# Playing order is set in response to submit_take_winner
await lobby_manager.broadcast_message(
lobby_id,
json.dumps({
@@ -431,7 +455,7 @@ async def process_input(message: str, websocket) -> None:
)
await lobby_manager.send_message(
lobby_id,
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
lobby_manager.get_lobby(lobby_id).current_playing_order[0],
json.dumps({
"response": "request_card",
}),
@@ -444,17 +468,21 @@ async def process_input(message: str, websocket) -> None:
# Reset bets
lobby_manager.get_lobby(lobby_id).client_bets = {}
lobby_manager.get_lobby(data_object["lobby_id"]).current_round += 1
current_round = lobby_manager.get_lobby(data_object["lobby_id"]).current_round
# Set new playing order
playing_order = lobby_manager.shift_playing_order(lobby_id, current_round)
await lobby_manager.spread_cards(
lobby_id,
data_object["round"],
current_round,
data_object["cards"],
None,
)
await lobby_manager.request_bet(
lobby_id,
lobby_manager.get_lobby(lobby_id).client_playing_order[0],
playing_order[0],
-1,
)
@@ -471,7 +499,7 @@ async def process_input(message: str, websocket) -> None:
bet,
)
last_client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
last_client_index = lobby_manager.get_lobby(lobby_id).current_playing_order.index(client_id)
player_count = lobby_manager.get_lobby_player_count(lobby_id)
if last_client_index == (player_count - 1):
@@ -485,7 +513,7 @@ async def process_input(message: str, websocket) -> None:
print("Sent game start")
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[0]
playing_client_id = lobby_manager.get_lobby(lobby_id).current_playing_order[0]
await lobby_manager.send_message(
lobby_id,
@@ -510,7 +538,7 @@ async def process_input(message: str, websocket) -> None:
await lobby_manager.request_bet(
lobby_id,
lobby_manager.get_lobby(lobby_id).client_playing_order[last_client_index + 1],
lobby_manager.get_lobby(lobby_id).current_playing_order[last_client_index + 1],
disallowed_bet,
)
@@ -523,7 +551,8 @@ async def process_input(message: str, websocket) -> None:
client_id,
data_object["played_card"],
)
playing_order = lobby_manager.get_lobby(lobby_id).client_playing_order
playing_order = lobby_manager.get_lobby(lobby_id).current_playing_order
client_index = playing_order.index(client_id)
if client_index == (len(playing_order) - 1):
# end round
@@ -547,11 +576,16 @@ async def process_input(message: str, websocket) -> None:
)
elif data_object["response"] == "submit_play_winner":
lobby_id = data_object["lobby_id"]
winning_client_id = data_object["client_id"]
await lobby_manager.spread_play_winner(
data_object["lobby_id"],
data_object["client_id"],
lobby_id,
winning_client_id,
)
lobby_manager.set_playing_order_start(lobby_id, winning_client_id)
print(f"DING DING DING we have a winner!!! {data_object['client_id']}")
elif data_object["response"] == "end_game":