host client can now start round and send out shuffled cards, server can spread them to all connected clients
This commit is contained in:
@@ -2,17 +2,17 @@ extends Node
|
|||||||
|
|
||||||
@export var full_deck: Dictionary[int, Card]
|
@export var full_deck: Dictionary[int, Card]
|
||||||
|
|
||||||
func get_shuffled_cards(player_count: int, cards_to_pick: int) -> Dictionary: # Dictionary[String, Array[int]]
|
func get_shuffled_cards(client_ids: Array[String], cards_to_pick: int) -> Dictionary: # Dictionary[String, Array[int]]
|
||||||
var dict: Dictionary = {}
|
var dict: Dictionary = {}
|
||||||
var deck: Array[int] = full_deck.keys()
|
var deck: Array[int] = full_deck.keys()
|
||||||
deck.shuffle()
|
deck.shuffle()
|
||||||
var card_index = 0
|
var card_index = 0
|
||||||
for i in player_count:
|
for client_id in client_ids:
|
||||||
var player_deck: Array[int] = []
|
var player_deck: Array[int] = []
|
||||||
for j in cards_to_pick:
|
for j in cards_to_pick:
|
||||||
player_deck.append(deck[card_index])
|
player_deck.append(deck[card_index])
|
||||||
card_index += 1
|
card_index += 1
|
||||||
dict[i] = player_deck
|
dict[client_id] = player_deck
|
||||||
|
|
||||||
# Add trump card, if there are cards left in the deck
|
# Add trump card, if there are cards left in the deck
|
||||||
if card_index == 60:
|
if card_index == 60:
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ var cached_response: String
|
|||||||
var _thread: Thread
|
var _thread: Thread
|
||||||
var _stop_thread: bool = false
|
var _stop_thread: bool = false
|
||||||
|
|
||||||
var _currently_connected_client_names: Array[String] = []
|
var _connected_client_ids: Array[String] = []
|
||||||
|
var _connected_client_names: Array[String] = []
|
||||||
|
|
||||||
signal connected_to_server()
|
signal connected_to_server()
|
||||||
signal connection_error_occurred()
|
signal connection_error_occurred()
|
||||||
@@ -98,20 +99,24 @@ func _handle_response(response: String) -> void:
|
|||||||
"join_lobby_response":
|
"join_lobby_response":
|
||||||
_handle_join(data)
|
_handle_join(data)
|
||||||
"client_joined_lobby":
|
"client_joined_lobby":
|
||||||
_currently_connected_client_names.assign(data["connected_client_names"])
|
print(data["connected_client_ids"])
|
||||||
client_joined_lobby.emit.call_deferred(data["new_client_name"], _currently_connected_client_names)
|
_connected_client_ids.assign(data["connected_client_ids"])
|
||||||
print(_currently_connected_client_names)
|
_connected_client_names.assign(data["connected_client_names"])
|
||||||
|
client_joined_lobby.emit.call_deferred(data["new_client_name"], _connected_client_names)
|
||||||
"leave_lobby_request":
|
"leave_lobby_request":
|
||||||
leave_lobby()
|
leave_lobby()
|
||||||
"leave_lobby":
|
"leave_lobby":
|
||||||
if data["status"] == "ok":
|
if data["status"] == "ok":
|
||||||
client_left_lobby.emit.call_deferred(data["client_name"])
|
client_left_lobby.emit.call_deferred(data["client_name"])
|
||||||
"client_left_lobby":
|
"client_left_lobby":
|
||||||
_currently_connected_client_names.assign(data["connected_client_names"])
|
_connected_client_ids.assign(data["connected_client_ids"])
|
||||||
print(_currently_connected_client_names)
|
_connected_client_names.assign(data["connected_client_names"])
|
||||||
client_left_lobby.emit.call_deferred(data["client_name"], _currently_connected_client_names)
|
client_left_lobby.emit.call_deferred(data["client_name"], _connected_client_names)
|
||||||
"receive_chat_message":
|
"receive_chat_message":
|
||||||
received_chat_message.emit.call_deferred(data["client_name"], data["message"])
|
received_chat_message.emit.call_deferred(data["client_name"], data["message"])
|
||||||
|
|
||||||
|
"new_round_started":
|
||||||
|
print(data)
|
||||||
|
|
||||||
func _handle_join(data) -> void:
|
func _handle_join(data) -> void:
|
||||||
match data["status"]:
|
match data["status"]:
|
||||||
@@ -164,5 +169,12 @@ func leave_lobby() -> void:
|
|||||||
func send_chat_message(message: String) -> void:
|
func send_chat_message(message: String) -> void:
|
||||||
_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|
_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|
||||||
|
|
||||||
|
func start_game() -> void:
|
||||||
|
_send_message(JSON.stringify({
|
||||||
|
"response": "start_game",
|
||||||
|
"lobby_id": _lobby_id,
|
||||||
|
"cards": CardManager.get_shuffled_cards(_connected_client_ids, 30),
|
||||||
|
}))
|
||||||
|
|
||||||
func _send_message(message: String) -> void:
|
func _send_message(message: String) -> void:
|
||||||
_socket.send_text(message)
|
_socket.send_text(message)
|
||||||
|
|||||||
@@ -132,13 +132,15 @@ size_flags_horizontal = 3
|
|||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
text = "Send"
|
text = "Send"
|
||||||
|
|
||||||
[node name="GameUI" type="Control" parent="HBoxContainer" node_paths=PackedStringArray("web_client")]
|
[node name="GameUI" type="Control" parent="HBoxContainer" node_paths=PackedStringArray("web_client", "start_game_button")]
|
||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
size_flags_horizontal = 3
|
size_flags_horizontal = 3
|
||||||
script = ExtResource("3_feb5d")
|
script = ExtResource("3_feb5d")
|
||||||
web_client = NodePath("../../WebClientTCP")
|
web_client = NodePath("../../WebClientTCP")
|
||||||
|
start_game_button = NodePath("StartGameButton")
|
||||||
|
|
||||||
[node name="StartGameButton" type="Button" parent="HBoxContainer/GameUI"]
|
[node name="StartGameButton" type="Button" parent="HBoxContainer/GameUI"]
|
||||||
|
visible = false
|
||||||
layout_mode = 1
|
layout_mode = 1
|
||||||
anchors_preset = 8
|
anchors_preset = 8
|
||||||
anchor_left = 0.5
|
anchor_left = 0.5
|
||||||
|
|||||||
+11
-3
@@ -2,14 +2,22 @@ class_name GameClient
|
|||||||
extends Node
|
extends Node
|
||||||
|
|
||||||
@export var web_client: WebClientTCP
|
@export var web_client: WebClientTCP
|
||||||
|
@export var start_game_button: Button
|
||||||
|
|
||||||
var is_host: bool = false
|
var _is_host: bool = false
|
||||||
|
|
||||||
|
func _set_host(is_host: bool) -> void:
|
||||||
|
_is_host = is_host
|
||||||
|
if is_host:
|
||||||
|
start_game_button.show()
|
||||||
|
else:
|
||||||
|
start_game_button.hide()
|
||||||
|
|
||||||
func _on_start_game_button_pressed() -> void:
|
func _on_start_game_button_pressed() -> void:
|
||||||
web_client.start_game()
|
web_client.start_game()
|
||||||
|
|
||||||
func _on_create_lobby_button_pressed() -> void:
|
func _on_create_lobby_button_pressed() -> void:
|
||||||
is_host = true
|
_set_host(true)
|
||||||
|
|
||||||
func _on_join_lobby_button_pressed() -> void:
|
func _on_join_lobby_button_pressed() -> void:
|
||||||
is_host = false
|
_set_host(false)
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ class Lobby:
|
|||||||
self.lobby_id = lobby_id
|
self.lobby_id = lobby_id
|
||||||
self.host_client_id = host_client_id
|
self.host_client_id = host_client_id
|
||||||
self.connected_clients = {}
|
self.connected_clients = {}
|
||||||
|
self.current_round = 0
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
|
|
||||||
@@ -68,6 +69,7 @@ class LobbyManager:
|
|||||||
"response": "client_joined_lobby",
|
"response": "client_joined_lobby",
|
||||||
"new_client_id": client_id,
|
"new_client_id": client_id,
|
||||||
"new_client_name": client_name,
|
"new_client_name": client_name,
|
||||||
|
"connected_client_ids": self.get_lobby_client_ids(lobby_id),
|
||||||
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -90,6 +92,7 @@ class LobbyManager:
|
|||||||
"response": "client_left_lobby",
|
"response": "client_left_lobby",
|
||||||
"status": "ok",
|
"status": "ok",
|
||||||
"client_name": client_name,
|
"client_name": client_name,
|
||||||
|
"connected_client_ids": self.get_lobby_client_ids(lobby_id),
|
||||||
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
"connected_client_names": self.get_lobby_client_names(lobby_id),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
@@ -136,6 +139,9 @@ class LobbyManager:
|
|||||||
for client_id in lobby.connected_clients:
|
for client_id in lobby.connected_clients:
|
||||||
names.append(lobby.connected_clients[client_id].client_name)
|
names.append(lobby.connected_clients[client_id].client_name)
|
||||||
return names
|
return names
|
||||||
|
|
||||||
|
def get_lobby_client_ids(self, lobby_id) -> list[str]:
|
||||||
|
return list(self.get_lobby(lobby_id).connected_clients.keys())
|
||||||
|
|
||||||
# Checks if a client is connected to a given lobby under the given ID
|
# Checks if a client is connected to a given lobby under the given ID
|
||||||
def client_id_exists_in_lobby(self, lobby_id, client_id) -> bool:
|
def client_id_exists_in_lobby(self, lobby_id, client_id) -> bool:
|
||||||
@@ -163,6 +169,32 @@ class LobbyManager:
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region gameplay
|
||||||
|
|
||||||
|
async def spread_cards(self, lobby_id, cards) -> None:
|
||||||
|
client_ids = self.get_lobby_client_ids(lobby_id)
|
||||||
|
|
||||||
|
trump = None
|
||||||
|
if cards["trump"]:
|
||||||
|
trump = cards["trump"]
|
||||||
|
|
||||||
|
self.get_lobby(lobby_id).current_round += 1
|
||||||
|
|
||||||
|
for client_id in client_ids:
|
||||||
|
client_cards = cards[client_id]
|
||||||
|
await self.send_message(
|
||||||
|
lobby_id,
|
||||||
|
client_id,
|
||||||
|
json.dumps({
|
||||||
|
"response": "new_round_started",
|
||||||
|
"current_round": self.get_lobby(lobby_id).current_round,
|
||||||
|
"cards": client_cards,
|
||||||
|
"trump": trump,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region miscellaneous
|
#region miscellaneous
|
||||||
|
|
||||||
# Generates an ID and ensures uniqueness among all lobbies.
|
# Generates an ID and ensures uniqueness among all lobbies.
|
||||||
@@ -323,6 +355,14 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
print(f"Relayed chat message by {client_name}")
|
print(f"Relayed chat message by {client_name}")
|
||||||
|
|
||||||
|
# Start game (host only)
|
||||||
|
elif data_object["response"] == "start_game":
|
||||||
|
|
||||||
|
await lobby_manager.spread_cards(
|
||||||
|
data_object["lobby_id"],
|
||||||
|
data_object["cards"],
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_local = False
|
run_local = False
|
||||||
|
|||||||
Reference in New Issue
Block a user