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]
|
||||
|
||||
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 deck: Array[int] = full_deck.keys()
|
||||
deck.shuffle()
|
||||
var card_index = 0
|
||||
for i in player_count:
|
||||
for client_id in client_ids:
|
||||
var player_deck: Array[int] = []
|
||||
for j in cards_to_pick:
|
||||
player_deck.append(deck[card_index])
|
||||
card_index += 1
|
||||
dict[i] = player_deck
|
||||
dict[client_id] = player_deck
|
||||
|
||||
# Add trump card, if there are cards left in the deck
|
||||
if card_index == 60:
|
||||
|
||||
@@ -17,7 +17,8 @@ var cached_response: String
|
||||
var _thread: Thread
|
||||
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 connection_error_occurred()
|
||||
@@ -98,20 +99,24 @@ func _handle_response(response: String) -> void:
|
||||
"join_lobby_response":
|
||||
_handle_join(data)
|
||||
"client_joined_lobby":
|
||||
_currently_connected_client_names.assign(data["connected_client_names"])
|
||||
client_joined_lobby.emit.call_deferred(data["new_client_name"], _currently_connected_client_names)
|
||||
print(_currently_connected_client_names)
|
||||
print(data["connected_client_ids"])
|
||||
_connected_client_ids.assign(data["connected_client_ids"])
|
||||
_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()
|
||||
"leave_lobby":
|
||||
if data["status"] == "ok":
|
||||
client_left_lobby.emit.call_deferred(data["client_name"])
|
||||
"client_left_lobby":
|
||||
_currently_connected_client_names.assign(data["connected_client_names"])
|
||||
print(_currently_connected_client_names)
|
||||
client_left_lobby.emit.call_deferred(data["client_name"], _currently_connected_client_names)
|
||||
_connected_client_ids.assign(data["connected_client_ids"])
|
||||
_connected_client_names.assign(data["connected_client_names"])
|
||||
client_left_lobby.emit.call_deferred(data["client_name"], _connected_client_names)
|
||||
"receive_chat_message":
|
||||
received_chat_message.emit.call_deferred(data["client_name"], data["message"])
|
||||
|
||||
"new_round_started":
|
||||
print(data)
|
||||
|
||||
func _handle_join(data) -> void:
|
||||
match data["status"]:
|
||||
@@ -164,5 +169,12 @@ func leave_lobby() -> void:
|
||||
func send_chat_message(message: String) -> void:
|
||||
_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:
|
||||
_socket.send_text(message)
|
||||
|
||||
@@ -132,13 +132,15 @@ size_flags_horizontal = 3
|
||||
layout_mode = 2
|
||||
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
|
||||
size_flags_horizontal = 3
|
||||
script = ExtResource("3_feb5d")
|
||||
web_client = NodePath("../../WebClientTCP")
|
||||
start_game_button = NodePath("StartGameButton")
|
||||
|
||||
[node name="StartGameButton" type="Button" parent="HBoxContainer/GameUI"]
|
||||
visible = false
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
|
||||
+11
-3
@@ -2,14 +2,22 @@ class_name GameClient
|
||||
extends Node
|
||||
|
||||
@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:
|
||||
web_client.start_game()
|
||||
|
||||
func _on_create_lobby_button_pressed() -> void:
|
||||
is_host = true
|
||||
_set_host(true)
|
||||
|
||||
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.host_client_id = host_client_id
|
||||
self.connected_clients = {}
|
||||
self.current_round = 0
|
||||
|
||||
class Client:
|
||||
|
||||
@@ -68,6 +69,7 @@ class LobbyManager:
|
||||
"response": "client_joined_lobby",
|
||||
"new_client_id": client_id,
|
||||
"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),
|
||||
})
|
||||
)
|
||||
@@ -90,6 +92,7 @@ class LobbyManager:
|
||||
"response": "client_left_lobby",
|
||||
"status": "ok",
|
||||
"client_name": client_name,
|
||||
"connected_client_ids": self.get_lobby_client_ids(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:
|
||||
names.append(lobby.connected_clients[client_id].client_name)
|
||||
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
|
||||
def client_id_exists_in_lobby(self, lobby_id, client_id) -> bool:
|
||||
@@ -163,6 +169,32 @@ class LobbyManager:
|
||||
|
||||
#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
|
||||
|
||||
# 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}")
|
||||
|
||||
# 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__':
|
||||
run_local = False
|
||||
|
||||
Reference in New Issue
Block a user