From 9b8390cfb33365675c29a27a9c0177ca0cced302 Mon Sep 17 00:00:00 2001 From: denizk0461 Date: Sun, 20 Jul 2025 18:30:51 +0200 Subject: [PATCH] server now rotates through playing order every round and lets the winning player go first --- cards/card_manager.gd | 29 ++++++------------- chat_panel.gd | 1 - components/web_client.gd | 2 +- server_tcp.py | 60 +++++++++++++++++++++++++++++++--------- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/cards/card_manager.gd b/cards/card_manager.gd index ea7a348..47fea6c 100644 --- a/cards/card_manager.gd +++ b/cards/card_manager.gd @@ -35,25 +35,20 @@ func get_card_texture(card_id: int, style: CardStyle = CardStyle.STICKMAN) -> Te return load("res://cards/textures/" + style_name + "/" + _get_card_name(card_id) + ".webp") func get_winning_card(card_ids: Array[int], trump: int) -> int: - print("ids: " + str(card_ids) + ", trump: " + str(trump)) if card_ids.has(56) or card_ids.has(57) or card_ids.has(58) or card_ids.has(59): # cards contain magician - print("checking magician route") for card_id in card_ids: if full_deck[card_id].card_type == Card.CardType.MAGICIAN: - print("picked magician") return card_id return -1 # should not happen elif not trump == -1: # trump exists, determine winner from this - print("trump route") var largest_trump_card_id: int = -1 var trump_color: Card.CardColor = full_deck[trump].color for card_id in card_ids: if full_deck[card_id].color == trump_color and card_id > largest_trump_card_id: largest_trump_card_id = card_id if not largest_trump_card_id == -1: - print("picked " + str(largest_trump_card_id)) return largest_trump_card_id # no trump played; determine from highest played card @@ -64,16 +59,13 @@ func get_winning_card(card_ids: Array[int], trump: int) -> int: func _get_largest_non_trump_card(card_ids: Array[int]) -> int: var largest_card_id: int = -1 - print("nontrump route") var first_color: Card.CardColor = _get_first_color_card(card_ids) if first_color == Card.CardColor.WHITE: - print("all white") return card_ids[0] # first one wins if all played cards are white else: for card_id in card_ids: if full_deck[card_id].color == first_color and card_id > largest_card_id: largest_card_id = card_id - print("picked " + str(largest_card_id)) return largest_card_id ## Returns the color of the first non-white card played. @@ -93,14 +85,15 @@ func _get_card_name(card_id: int) -> String: elif card_id > 55: return "magician" - elif card_id < 13: - color = "blue" - elif card_id < 26: - color = "green" - elif card_id < 39: - color = "violet" - else: - color = "orange" + match get_card_color(card_id): + Card.CardColor.BLUE: + color = "blue" + Card.CardColor.GREEN: + color = "green" + Card.CardColor.RED: + color = "violet" + Card.CardColor.YELLOW: + color = "orange" match card_id: 0, 13, 26, 39: @@ -129,10 +122,6 @@ func _get_card_name(card_id: int) -> String: suffix = "12" 12, 25, 38, 51: suffix = "13" - 52, 53, 54, 55: - suffix = "joker" - 56, 57, 58, 59: - suffix = "magician" return color + "_" + suffix diff --git a/chat_panel.gd b/chat_panel.gd index a9cdbc3..c2dd3c0 100644 --- a/chat_panel.gd +++ b/chat_panel.gd @@ -43,7 +43,6 @@ func _on_message_input_gui_input(event: InputEvent) -> void: func _on_send_message_button_pressed() -> void: _send_chat_message() - func _on_web_client_connection_status_changed(new_status: WebClient.ConnectionStatus) -> void: if new_status == WebClient.ConnectionStatus.JOINED: message_input.editable = true diff --git a/components/web_client.gd b/components/web_client.gd index 1a87773..2d8f405 100644 --- a/components/web_client.gd +++ b/components/web_client.gd @@ -141,7 +141,7 @@ func _handle_response(response: String) -> void: # Relay message to be displayed in the chat panel chat_panel.put_chat_message(data["client_name"], data["message"]) - "new_round_started": + "round_started": if data["current_round"] == 1: var playing_order: Array[String] = [] playing_order.assign(data["playing_order"]) diff --git a/server_tcp.py b/server_tcp.py index 2be6b74..ffba1f8 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -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":