This repository has been archived on 2026-05-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
magician/game_client.gd
T

99 lines
2.5 KiB
GDScript
Raw Normal View History

class_name GameClient
extends Node
@export var web_client: WebClientTCP
@export var start_game_button: Button
@export var play_card_button: Button
@export var bet_container: Control
@export var bet_input: LineEdit
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
if _cards_left == 0:
_start_next_round()
else:
web_client.start_next_play()
func _start_next_round() -> void:
2025-07-16 09:35:32 +02:00
if _current_round == _max_rounds:
web_client.end_game()
2025-07-16 09:35:32 +02:00
# end game
else:
_current_round += 1
_cards_left = _current_round
web_client.start_next_round(_current_round)
func _get_max_rounds(player_count: int) -> int:
match player_count:
3:
return 3
4:
return 15
5:
return 12
_: # 6
return 10
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:
_set_host(true)
func _on_join_lobby_button_pressed() -> void:
_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())
func _on_player_hand_card_selected(card_id: int) -> void:
play_card_button.disabled = card_id == -1
func _on_submit_bet_button_pressed() -> void:
bet_container.hide()
web_client.submit_bet(int(bet_input.text.strip_edges()), _current_round)
func _on_web_client_tcp_received_bet_request(disallowed_bet: int) -> void:
bet_input.text = ""
bet_container.show()
func _on_web_client_tcp_request_next_round() -> void:
_start_next_round()
func _on_player_hand_0_card_played(card_id: int) -> void:
web_client.play_card(card_id)
$Label.hide()
func _on_web_client_tcp_notify_play_card_request() -> void:
$Label.show()
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()