server: added more gameplay commands and determine playing order on start_game command

This commit is contained in:
2025-07-08 10:42:34 +02:00
parent 3122df893a
commit 08773d963c
4 changed files with 113 additions and 10 deletions
+13 -1
View File
@@ -117,6 +117,10 @@ func _handle_response(response: String) -> void:
"new_round_started": "new_round_started":
print(data) print(data)
"client_submitted_bet":
print(data)
"client_played_card":
print(data)
func _handle_join(data) -> void: func _handle_join(data) -> void:
match data["status"]: match data["status"]:
@@ -173,7 +177,15 @@ func start_game() -> void:
_send_message(JSON.stringify({ _send_message(JSON.stringify({
"response": "start_game", "response": "start_game",
"lobby_id": _lobby_id, "lobby_id": _lobby_id,
"cards": CardManager.get_shuffled_cards(_connected_client_ids, 30), "cards": CardManager.get_shuffled_cards(_connected_client_ids, 1),
}))
func start_next_round(round_count: int) -> void:
_send_message(JSON.stringify({
"response": "start_next_round",
"lobby_id": _lobby_id,
"round": round_count,
"cards": CardManager.get_shuffled_cards(_connected_client_ids, round_count),
})) }))
func _send_message(message: String) -> void: func _send_message(message: String) -> void:
+2
View File
@@ -156,7 +156,9 @@ grow_vertical = 2
text = "Start Game!" text = "Start Game!"
[connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_joined_lobby"] [connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_joined_lobby"]
[connection signal="client_joined_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_joined_lobby"]
[connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_left_lobby"] [connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_client_left_lobby"]
[connection signal="client_left_lobby" from="WebClientTCP" to="HBoxContainer/GameUI" method="_on_web_client_tcp_client_left_lobby"]
[connection signal="connected_to_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_lobby"] [connection signal="connected_to_lobby" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_lobby"]
[connection signal="connected_to_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_server"] [connection signal="connected_to_server" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connected_to_server"]
[connection signal="connection_error_occurred" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connection_error_occurred"] [connection signal="connection_error_occurred" from="WebClientTCP" to="HBoxContainer/ClientInterface" method="_on_web_client_tcp_connection_error_occurred"]
+33 -4
View File
@@ -5,15 +5,30 @@ extends Node
@export var start_game_button: Button @export var start_game_button: Button
var _is_host: bool = false var _is_host: bool = false
var _current_player_count: int = 0
var _current_round: int = 0
var _max_rounds: int = 0
func _set_host(is_host: bool) -> void: func _set_host(is_host: bool) -> void:
_is_host = is_host _is_host = is_host
if is_host:
start_game_button.show() func _start_next_round() -> void:
else: _current_round += 1
start_game_button.hide() web_client.start_next_round(_current_round)
func _get_max_rounds(player_count: int) -> int:
match player_count:
3:
return 20
4:
return 15
5:
return 12
_: # 6
return 10
func _on_start_game_button_pressed() -> void: func _on_start_game_button_pressed() -> void:
_current_round = 1
web_client.start_game() web_client.start_game()
func _on_create_lobby_button_pressed() -> void: func _on_create_lobby_button_pressed() -> void:
@@ -21,3 +36,17 @@ func _on_create_lobby_button_pressed() -> void:
func _on_join_lobby_button_pressed() -> void: func _on_join_lobby_button_pressed() -> void:
_set_host(false) _set_host(false)
func _update_player_count(player_count: int) -> void:
_current_player_count = player_count
_max_rounds = _get_max_rounds(player_count)
if _is_host and player_count >= 3:
start_game_button.show()
else:
start_game_button.hide()
func _on_web_client_tcp_client_joined_lobby(client_name: String, connected_client_names: Array[String]) -> void:
_update_player_count(connected_client_names.size())
func _on_web_client_tcp_client_left_lobby(client_name: String, connected_client_names: Array[String]) -> void:
_update_player_count(connected_client_names.size())
+65 -5
View File
@@ -21,7 +21,8 @@ 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 self.client_playing_order = []
self.currently_playing_client = 0 # index of client_playing_order
class Client: class Client:
@@ -171,15 +172,13 @@ class LobbyManager:
#region gameplay #region gameplay
async def spread_cards(self, lobby_id, cards) -> None: async def spread_cards(self, lobby_id, round, cards, playing_order) -> None:
client_ids = self.get_lobby_client_ids(lobby_id) client_ids = self.get_lobby_client_ids(lobby_id)
trump = None trump = None
if cards["trump"]: if cards["trump"]:
trump = cards["trump"] trump = cards["trump"]
self.get_lobby(lobby_id).current_round += 1
for client_id in client_ids: for client_id in client_ids:
client_cards = cards[client_id] client_cards = cards[client_id]
await self.send_message( await self.send_message(
@@ -187,12 +186,38 @@ class LobbyManager:
client_id, client_id,
json.dumps({ json.dumps({
"response": "new_round_started", "response": "new_round_started",
"current_round": self.get_lobby(lobby_id).current_round, "current_round": round,
"cards": client_cards, "cards": client_cards,
"trump": trump, "trump": trump,
"playing_order": playing_order,
}) })
) )
async def spread_bet(self, lobby_id, client_id, bet) -> None:
await self.broadcast_message(
lobby_id,
json.dumps({
"response": "client_played_card",
"client_id": client_id,
"bet": bet,
"next_client": None, # TODO!
})
)
async def spread_played_card(self, lobby_id, client_id, played_card) -> None:
await self.broadcast_message(
lobby_id,
json.dumps({
"response": "client_played_card",
"client_id": client_id,
"card": played_card,
"next_client": None, # TODO!
})
)
def get_next_client(self, lobby_id, client_id) -> str:
pass
#endregion #endregion
#region miscellaneous #region miscellaneous
@@ -358,10 +383,45 @@ async def process_input(message: str, websocket) -> None:
# Start game (host only) # Start game (host only)
elif data_object["response"] == "start_game": elif data_object["response"] == "start_game":
lobby_id = data_object["lobby_id"]
lobby = lobby_manager.get_lobby(lobby_id)
lobby.client_playing_order = list(lobby.connected_clients.keys())
random.shuffle(lobby.client_playing_order)
await lobby_manager.spread_cards( await lobby_manager.spread_cards(
data_object["lobby_id"], data_object["lobby_id"],
1,
data_object["cards"], data_object["cards"],
lobby.client_playing_order,
)
print(f"Lobby {lobby_id} is starting the game, playing order is: {lobby.client_playing_order}")
# Start next round (host only)
elif data_object["response"] == "start_next_round":
lobby_id = data_object["lobby_id"]
await lobby_manager.spread_cards(
lobby_id,
data_object["round"],
data_object["cards"],
None,
)
elif data_object["response"] == "submit_bet":
await lobby_manager.spread_bet(
data_object["lobby_id"],
data_object["client_id"],
data_object["bet"],
)
elif data_object["response"] == "play_card":
await lobby_manager.spread_played_card(
data_object["lobby_id"],
data_object["client_id"],
data_object["played_card"],
) )
if __name__ == '__main__': if __name__ == '__main__':