added commands for requesting and receiving bets and starting rounds
This commit is contained in:
@@ -131,7 +131,13 @@ func _handle_response(response: String) -> void:
|
|||||||
cards_received.emit.call_deferred(received_cards)
|
cards_received.emit.call_deferred(received_cards)
|
||||||
"client_submitted_bet":
|
"client_submitted_bet":
|
||||||
print(data)
|
print(data)
|
||||||
"client_played_card":
|
"request_bet":
|
||||||
|
print(data)
|
||||||
|
"begin_playing_cards":
|
||||||
|
print(data)
|
||||||
|
"request_card":
|
||||||
|
print(data)
|
||||||
|
"play_card":
|
||||||
print(data)
|
print(data)
|
||||||
|
|
||||||
func _handle_join(data) -> void:
|
func _handle_join(data) -> void:
|
||||||
@@ -201,5 +207,14 @@ func start_next_round(round_count: int) -> void:
|
|||||||
"cards": CardManager.get_shuffled_cards(_connected_client_ids, round_count),
|
"cards": CardManager.get_shuffled_cards(_connected_client_ids, round_count),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
func submit_bet(bet: int, current_round: int) -> void:
|
||||||
|
_send_message(JSON.stringify({
|
||||||
|
"response": "submit_bet",
|
||||||
|
"lobby_id": _lobby_id,
|
||||||
|
"client_id": _client_id,
|
||||||
|
"round": current_round,
|
||||||
|
"bet": bet,
|
||||||
|
}))
|
||||||
|
|
||||||
func _send_message(message: String) -> void:
|
func _send_message(message: String) -> void:
|
||||||
_socket.send_text(message)
|
_socket.send_text(message)
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ 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", "start_game_button", "play_card_button")]
|
[node name="GameUI" type="Control" parent="HBoxContainer" node_paths=PackedStringArray("web_client", "start_game_button", "play_card_button", "bet_container", "bet_input")]
|
||||||
layout_mode = 2
|
layout_mode = 2
|
||||||
size_flags_horizontal = 3
|
size_flags_horizontal = 3
|
||||||
mouse_filter = 2
|
mouse_filter = 2
|
||||||
@@ -150,6 +150,8 @@ script = ExtResource("3_feb5d")
|
|||||||
web_client = NodePath("../../WebClientTCP")
|
web_client = NodePath("../../WebClientTCP")
|
||||||
start_game_button = NodePath("StartGameButton")
|
start_game_button = NodePath("StartGameButton")
|
||||||
play_card_button = NodePath("PlayCardButton")
|
play_card_button = NodePath("PlayCardButton")
|
||||||
|
bet_container = NodePath("BetContainer")
|
||||||
|
bet_input = NodePath("BetContainer/BetInput")
|
||||||
|
|
||||||
[node name="StartGameButton" type="Button" parent="HBoxContainer/GameUI"]
|
[node name="StartGameButton" type="Button" parent="HBoxContainer/GameUI"]
|
||||||
visible = false
|
visible = false
|
||||||
@@ -182,6 +184,20 @@ grow_vertical = 0
|
|||||||
disabled = true
|
disabled = true
|
||||||
text = "Play Card"
|
text = "Play Card"
|
||||||
|
|
||||||
|
[node name="BetContainer" type="VBoxContainer" parent="HBoxContainer/GameUI"]
|
||||||
|
layout_mode = 0
|
||||||
|
offset_left = 218.0
|
||||||
|
offset_top = 833.0
|
||||||
|
offset_right = 445.0
|
||||||
|
offset_bottom = 899.0
|
||||||
|
|
||||||
|
[node name="BetInput" type="LineEdit" parent="HBoxContainer/GameUI/BetContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
|
||||||
|
[node name="SubmitBetButton" type="Button" parent="HBoxContainer/GameUI/BetContainer"]
|
||||||
|
layout_mode = 2
|
||||||
|
text = "submit bet"
|
||||||
|
|
||||||
[node name="EnvironmentManager" type="Node3D" parent="." node_paths=PackedStringArray("own_player_hand")]
|
[node name="EnvironmentManager" type="Node3D" parent="." node_paths=PackedStringArray("own_player_hand")]
|
||||||
script = ExtResource("4_7jktm")
|
script = ExtResource("4_7jktm")
|
||||||
own_player_hand = NodePath("PlayerHand")
|
own_player_hand = NodePath("PlayerHand")
|
||||||
@@ -229,4 +245,5 @@ mesh = SubResource("BoxMesh_fc0e3")
|
|||||||
[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ChatContainer/SendMessageButton" to="HBoxContainer/ClientInterface" method="_on_send_message_button_pressed"]
|
[connection signal="pressed" from="HBoxContainer/ClientInterface/MarginContainer/VBoxContainer/ChatContainer/SendMessageButton" to="HBoxContainer/ClientInterface" method="_on_send_message_button_pressed"]
|
||||||
[connection signal="pressed" from="HBoxContainer/GameUI/StartGameButton" to="HBoxContainer/GameUI" method="_on_start_game_button_pressed"]
|
[connection signal="pressed" from="HBoxContainer/GameUI/StartGameButton" to="HBoxContainer/GameUI" method="_on_start_game_button_pressed"]
|
||||||
[connection signal="pressed" from="HBoxContainer/GameUI/PlayCardButton" to="EnvironmentManager/PlayerHand" method="_on_play_card_button_pressed"]
|
[connection signal="pressed" from="HBoxContainer/GameUI/PlayCardButton" to="EnvironmentManager/PlayerHand" method="_on_play_card_button_pressed"]
|
||||||
|
[connection signal="pressed" from="HBoxContainer/GameUI/BetContainer/SubmitBetButton" to="HBoxContainer/GameUI" method="_on_submit_bet_button_pressed"]
|
||||||
[connection signal="card_selected" from="EnvironmentManager/PlayerHand" to="HBoxContainer/GameUI" method="_on_player_hand_card_selected"]
|
[connection signal="card_selected" from="EnvironmentManager/PlayerHand" to="HBoxContainer/GameUI" method="_on_player_hand_card_selected"]
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ extends Node
|
|||||||
@export var web_client: WebClientTCP
|
@export var web_client: WebClientTCP
|
||||||
@export var start_game_button: Button
|
@export var start_game_button: Button
|
||||||
@export var play_card_button: Button
|
@export var play_card_button: Button
|
||||||
|
@export var bet_container: Control
|
||||||
|
@export var bet_input: LineEdit
|
||||||
|
|
||||||
var _is_host: bool = false
|
var _is_host: bool = false
|
||||||
var _current_player_count: int = 0
|
var _current_player_count: int = 0
|
||||||
@@ -54,3 +56,6 @@ func _on_web_client_tcp_client_left_lobby(client_name: String, connected_client_
|
|||||||
|
|
||||||
func _on_player_hand_card_selected(card_id: int) -> void:
|
func _on_player_hand_card_selected(card_id: int) -> void:
|
||||||
play_card_button.disabled = card_id == -1
|
play_card_button.disabled = card_id == -1
|
||||||
|
|
||||||
|
func _on_submit_bet_button_pressed() -> void:
|
||||||
|
web_client.submit_bet(int(bet_input.text.strip_edges()), _current_round)
|
||||||
|
|||||||
+68
-9
@@ -22,7 +22,8 @@ class Lobby:
|
|||||||
self.host_client_id = host_client_id
|
self.host_client_id = host_client_id
|
||||||
self.connected_clients = {}
|
self.connected_clients = {}
|
||||||
self.client_playing_order = []
|
self.client_playing_order = []
|
||||||
self.currently_playing_client = 0 # index of client_playing_order
|
self.client_bets = {} # client_id: bet
|
||||||
|
# self.currently_playing_client = 0 # index of client_playing_order
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
|
|
||||||
@@ -197,10 +198,9 @@ class LobbyManager:
|
|||||||
await self.broadcast_message(
|
await self.broadcast_message(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
json.dumps({
|
json.dumps({
|
||||||
"response": "client_played_card",
|
"response": "client_submitted_bet",
|
||||||
"client_id": client_id,
|
"client_id": client_id,
|
||||||
"bet": bet,
|
"bet": bet,
|
||||||
"next_client": None, # TODO!
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -211,7 +211,6 @@ class LobbyManager:
|
|||||||
"response": "client_played_card",
|
"response": "client_played_card",
|
||||||
"client_id": client_id,
|
"client_id": client_id,
|
||||||
"card": played_card,
|
"card": played_card,
|
||||||
"next_client": None, # TODO!
|
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -401,6 +400,9 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
elif data_object["response"] == "start_next_round":
|
elif data_object["response"] == "start_next_round":
|
||||||
lobby_id = data_object["lobby_id"]
|
lobby_id = data_object["lobby_id"]
|
||||||
|
|
||||||
|
# Reset bets
|
||||||
|
lobby_manager.get_lobby(lobby_id).client_bets = {}
|
||||||
|
|
||||||
await lobby_manager.spread_cards(
|
await lobby_manager.spread_cards(
|
||||||
lobby_id,
|
lobby_id,
|
||||||
data_object["round"],
|
data_object["round"],
|
||||||
@@ -409,21 +411,78 @@ async def process_input(message: str, websocket) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
elif data_object["response"] == "submit_bet":
|
elif data_object["response"] == "submit_bet":
|
||||||
|
lobby_id = data_object["lobby_id"]
|
||||||
|
client_id = data_object["client_id"]
|
||||||
|
bet = data_object["bet"]
|
||||||
|
|
||||||
|
lobby_manager.get_lobby(lobby_id).client_bets[client_id] = bet
|
||||||
|
|
||||||
await lobby_manager.spread_bet(
|
await lobby_manager.spread_bet(
|
||||||
data_object["lobby_id"],
|
lobby_id,
|
||||||
data_object["client_id"],
|
client_id,
|
||||||
data_object["bet"],
|
bet,
|
||||||
|
)
|
||||||
|
|
||||||
|
last_client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
|
||||||
|
player_count = lobby_manager.get_lobby_player_count(lobby_id)
|
||||||
|
|
||||||
|
if last_client_index == (player_count - 1):
|
||||||
|
# all have bet; start game
|
||||||
|
await lobby_manager.broadcast_message(
|
||||||
|
lobby_id,
|
||||||
|
json.dumps({
|
||||||
|
"response": "begin_playing_cards",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[0]
|
||||||
|
|
||||||
|
await lobby_manager.send_message(
|
||||||
|
lobby_id,
|
||||||
|
playing_client_id,
|
||||||
|
json.dumps({
|
||||||
|
"response": "request_card",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
# bets are not finished; continue asking for bets
|
||||||
|
disallowed_bet = -1
|
||||||
|
if last_client_index == (player_count - 2):
|
||||||
|
# last client to bet; limit bet options
|
||||||
|
total_bet = sum(lobby_manager.get_lobby(lobby_id).client_bets.values())
|
||||||
|
disallowed_bet = total_bet - data_object["round"]
|
||||||
|
|
||||||
|
await lobby_manager.send_message(
|
||||||
|
lobby_id,
|
||||||
|
lobby_manager.get_lobby(lobby_id).client_playing_order[last_client_index + 1],
|
||||||
|
json.dumps({
|
||||||
|
"response": "request_bet",
|
||||||
|
"disallowed_bet": disallowed_bet,
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
elif data_object["response"] == "play_card":
|
elif data_object["response"] == "play_card":
|
||||||
|
lobby_id = data_object["lobby_id"]
|
||||||
|
client_id = data_object["client_id"]
|
||||||
|
|
||||||
await lobby_manager.spread_played_card(
|
await lobby_manager.spread_played_card(
|
||||||
data_object["lobby_id"],
|
lobby_id,
|
||||||
data_object["client_id"],
|
client_id,
|
||||||
data_object["played_card"],
|
data_object["played_card"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
client_index = lobby_manager.get_lobby(lobby_id).client_playing_order.index(client_id)
|
||||||
|
if not client_index == (len(lobby_manager.get_lobby(lobby_id).client_playing_order) - 1):
|
||||||
|
playing_client_id = lobby_manager.get_lobby(lobby_id).client_playing_order[client_index + 1]
|
||||||
|
|
||||||
|
await lobby_manager.send_message(
|
||||||
|
lobby_id,
|
||||||
|
playing_client_id,
|
||||||
|
json.dumps({
|
||||||
|
"response": "request_card",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_local = False
|
run_local = False
|
||||||
# Run locally without SSL certificate
|
# Run locally without SSL certificate
|
||||||
|
|||||||
Reference in New Issue
Block a user