diff --git a/cards/card_manager.gd b/cards/card_manager.gd index dab94d4..b137669 100644 --- a/cards/card_manager.gd +++ b/cards/card_manager.gd @@ -7,6 +7,20 @@ enum CardStyle { STICKMAN, } +func _ready() -> void: + # wizard wins + assert(get_winning_card([1, 2, 3, 59], -1) == 59) + assert(get_winning_card([1, 2, 3, 59], 3) == 59) + # trump wins + assert(get_winning_card([1, 14, 2, 3], 4) == 3) + assert(get_winning_card([29, 14, 27, 31], 15) == 14) + assert(get_winning_card([1, 12, 6, 31], 30) == 31) + # joker win + assert(get_winning_card([54, 52, 55, 53], 31) == 54) + # other wins + assert(get_winning_card([14, 15, 18, 13], 16) == 18) + assert(get_winning_card([14, 15, 18, 13], 46) == 18) + func get_shuffled_cards(client_ids: Array[String], cards_to_pick: int) -> Dictionary: # Dictionary[String, Array[int]] var dict: Dictionary = {} var deck: Array[int] = full_deck.keys() @@ -35,20 +49,25 @@ 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 @@ -60,14 +79,16 @@ 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. @@ -82,8 +103,6 @@ func _get_card_name(card_id: int) -> String: var color: String var suffix: String - print("looking up card with id " + str(card_id)) - if card_id < 13: color = "blue" elif card_id < 26: diff --git a/components/web_client_tcp.gd b/components/web_client_tcp.gd index 65e7fb0..e1aff3a 100644 --- a/components/web_client_tcp.gd +++ b/components/web_client_tcp.gd @@ -44,6 +44,7 @@ signal received_win() signal notify_play_card_request() signal received_played_card(client_id: String, card_id: int) signal received_play_winner_request() +signal request_next_play() signal request_next_round() enum ConnectionStatus { @@ -82,7 +83,7 @@ func _process(_delta: float) -> void: if _processed_status == WebSocketPeer.STATE_OPEN: while _socket.get_available_packet_count(): cached_response = _socket.get_packet().get_string_from_utf8() - print(cached_response) + #print(cached_response) #cached_response += (received_data[1] as PackedByteArray).get_string_from_utf8() if cached_response.ends_with("}"): #print(cached_response) @@ -128,14 +129,14 @@ func _handle_response(response: String) -> void: received_chat_message.emit.call_deferred(data["client_name"], data["message"]) "new_round_started": - print(data) + #print(data) if data["current_round"] == 1: var playing_order: Array[String] = [] playing_order.assign(data["playing_order"]) client_order_received.emit.call_deferred(playing_order) var received_cards = data["cards"] - print(received_cards) + #print(received_cards) cards_received.emit.call_deferred(received_cards) trump_received.emit.call_deferred(data["trump"]) "client_submitted_bet": @@ -217,6 +218,12 @@ func start_game() -> void: "cards": CardManager.get_shuffled_cards(_connected_client_ids, 1), })) +func start_next_play() -> void: + _send_message(JSON.stringify({ + "response": "start_next_play", + "lobby_id": _lobby_id, + })) + func start_next_round(round_count: int) -> void: _send_message(JSON.stringify({ "response": "start_next_round", @@ -248,7 +255,8 @@ func submit_play_winner(winning_client_id: String) -> void: "lobby_id": _lobby_id, "client_id": winning_client_id, })) - request_next_round.emit.call_deferred() + request_next_play.emit.call_deferred() + #request_next_round.emit.call_deferred() func _send_message(message: String) -> void: _socket.send_text(message) diff --git a/environment_manager.gd b/environment_manager.gd index ed264dd..0e92478 100644 --- a/environment_manager.gd +++ b/environment_manager.gd @@ -105,6 +105,7 @@ func _on_web_client_tcp_received_play_winner_request() -> void: for play in _plays: if play.card_id == winning_card_id: web_client.submit_play_winner(play.client_id) + _plays.clear() return func _on_web_client_tcp_trump_received(trump_id: int) -> void: diff --git a/game.tscn b/game.tscn index ac6a049..f266a4b 100644 --- a/game.tscn +++ b/game.tscn @@ -293,6 +293,7 @@ transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0.1 [connection signal="received_play_winner_request" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_received_play_winner_request"] [connection signal="received_played_card" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_received_played_card"] [connection signal="received_win" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_received_win"] +[connection signal="request_next_play" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_request_next_play"] [connection signal="request_next_round" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_request_next_round"] [connection signal="trump_received" from="WebClientTCP" to="EnvironmentManager" method="_on_web_client_tcp_trump_received"] [connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ServerInputs/ConnectButton" to="HBoxContainer/ClientInterface" method="_on_connect_button_pressed"] diff --git a/game_client.gd b/game_client.gd index f596b23..23d4cc9 100644 --- a/game_client.gd +++ b/game_client.gd @@ -10,13 +10,23 @@ extends Node var _is_host: bool = false var _current_player_count: int = 0 var _current_round: int = 0 +var _cards_left: int = 0 var _max_rounds: int = 0 func _set_host(is_host: bool) -> void: _is_host = is_host +func _start_next_play() -> void: + _cards_left -= 1 + print("cards left: " + str(_cards_left)) + if _cards_left == 0: + _start_next_round() + else: + web_client.start_next_play() + func _start_next_round() -> void: _current_round += 1 + _cards_left = _current_round web_client.start_next_round(_current_round) func _get_max_rounds(player_count: int) -> int: @@ -32,6 +42,7 @@ func _get_max_rounds(player_count: int) -> int: func _on_start_game_button_pressed() -> void: _current_round = 1 + _cards_left = _current_round web_client.start_game() func _on_create_lobby_button_pressed() -> void: @@ -79,3 +90,6 @@ func _on_web_client_tcp_received_win() -> void: $WinLabel.show() await get_tree().create_timer(1.0).timeout $WinLabel.hide() + +func _on_web_client_tcp_request_next_play() -> void: + _start_next_play() diff --git a/server_tcp.py b/server_tcp.py index 590bbbe..1545d20 100644 --- a/server_tcp.py +++ b/server_tcp.py @@ -232,9 +232,6 @@ class LobbyManager: "client_id": client_id, }), ) - - def get_next_client(self, lobby_id, client_id) -> str: - pass #endregion @@ -421,6 +418,16 @@ async def process_input(message: str, websocket) -> None: -1, ) + elif data_object["response"] == "start_next_play": + lobby_id = data_object["lobby_id"] + await lobby_manager.send_message( + lobby_id, + lobby_manager.get_lobby(lobby_id).client_playing_order[0], + json.dumps({ + "response": "request_card", + }), + ) + # Start next round (host only) elif data_object["response"] == "start_next_round": lobby_id = data_object["lobby_id"]