2025-06-29 20:34:46 +02:00
|
|
|
class_name WebClientTCP
|
|
|
|
|
extends Node
|
|
|
|
|
|
2025-07-20 13:36:30 +02:00
|
|
|
## Sender tag for system messages displayed in the chat
|
|
|
|
|
const SENDER_SYSTEM: String = "SYSTEM"
|
|
|
|
|
|
2025-07-04 19:59:46 +02:00
|
|
|
#var _tcp := StreamPeerTCP.new()
|
|
|
|
|
var _socket := WebSocketPeer.new()
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-04 19:59:46 +02:00
|
|
|
var _new_status: int = WebSocketPeer.STATE_CLOSED
|
2025-06-29 20:34:46 +02:00
|
|
|
var _processed_status: int = -1
|
|
|
|
|
|
|
|
|
|
var _lobby_id: String
|
|
|
|
|
var _client_id: String
|
|
|
|
|
var _client_name: String
|
|
|
|
|
|
2025-07-04 12:22:48 +02:00
|
|
|
var received_data: Array
|
|
|
|
|
var cached_response: String
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
var _thread: Thread
|
|
|
|
|
var _stop_thread: bool = false
|
|
|
|
|
|
2025-07-18 14:10:39 +02:00
|
|
|
var _is_first_play: bool = false
|
|
|
|
|
|
2025-07-07 22:40:46 +02:00
|
|
|
var _connected_client_ids: Array[String] = []
|
|
|
|
|
var _connected_client_names: Array[String] = []
|
2025-07-06 19:49:08 +02:00
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
signal connected_to_server()
|
|
|
|
|
signal connection_error_occurred()
|
|
|
|
|
signal disconnected_from_server()
|
|
|
|
|
|
|
|
|
|
signal connected_to_lobby(lobby_id: String)
|
|
|
|
|
signal disconnected_from_lobby()
|
2025-07-08 17:40:13 +02:00
|
|
|
signal received_client_id(client_id: String)
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-07 22:11:02 +02:00
|
|
|
signal client_joined_lobby(client_name: String, connected_client_names: Array[String])
|
|
|
|
|
signal client_left_lobby(client_name: String, connected_client_names: Array[String])
|
2025-06-30 22:07:43 +02:00
|
|
|
|
2025-07-07 10:57:39 +02:00
|
|
|
signal received_connection_message(message: String)
|
2025-07-04 11:29:25 +02:00
|
|
|
signal received_chat_message(client_name: String, message: String)
|
|
|
|
|
|
2025-07-02 21:58:19 +02:00
|
|
|
signal connection_status_changed(new_status: ConnectionStatus)
|
|
|
|
|
|
2025-07-16 09:45:44 +02:00
|
|
|
# This signal is messy as fuck
|
|
|
|
|
signal client_order_received(client_ids: Array[String], client_ids_playing_order: Array[String], client_names: Array[String])
|
2025-07-16 10:20:45 +02:00
|
|
|
|
2025-07-16 11:59:52 +02:00
|
|
|
signal client_played_card(client_id: String, card_id: int)
|
2025-07-16 10:20:45 +02:00
|
|
|
signal received_expected_bet(client_id: String, expected_bet: int)
|
|
|
|
|
signal new_round_started()
|
2025-07-15 11:31:01 +02:00
|
|
|
signal cards_received(cards: Dictionary)
|
2025-07-15 14:42:06 +02:00
|
|
|
signal trump_received(trump_id: int)
|
2025-07-18 12:53:43 +02:00
|
|
|
signal received_bet_request(disallowed_bet: int, current_round: int)
|
2025-07-15 14:42:06 +02:00
|
|
|
signal received_win()
|
2025-07-16 10:20:45 +02:00
|
|
|
signal received_winning_client(client_id: String)
|
2025-07-15 14:42:06 +02:00
|
|
|
signal notify_play_card_request()
|
2025-07-18 14:10:39 +02:00
|
|
|
signal reset_first_card()
|
|
|
|
|
signal received_first_card(card_id: int)
|
2025-07-15 14:42:06 +02:00
|
|
|
signal received_played_card(client_id: String, card_id: int)
|
2025-07-15 12:11:50 +02:00
|
|
|
signal received_play_winner_request()
|
2025-07-16 11:15:05 +02:00
|
|
|
signal received_end_game_request()
|
2025-07-15 19:38:27 +02:00
|
|
|
signal request_next_play()
|
2025-07-15 12:11:50 +02:00
|
|
|
signal request_next_round()
|
|
|
|
|
|
2025-07-20 13:36:30 +02:00
|
|
|
# new signals
|
|
|
|
|
## Emitted when a message that needs to be displayed to the player has been received.
|
|
|
|
|
signal user_message_received(sender: String, message: String)
|
|
|
|
|
|
2025-07-02 21:58:19 +02:00
|
|
|
enum ConnectionStatus {
|
|
|
|
|
DISCONNECTED,
|
|
|
|
|
ATTEMPTING_CONNECTION,
|
|
|
|
|
LOBBYLESS, # == connected
|
|
|
|
|
ATTEMPTING_JOIN,
|
|
|
|
|
JOINED,
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-04 11:41:57 +02:00
|
|
|
func _process(_delta: float) -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.poll()
|
|
|
|
|
_new_status = _socket.get_ready_state()
|
2025-06-29 20:34:46 +02:00
|
|
|
if not _processed_status == _new_status:
|
|
|
|
|
match _new_status:
|
2025-07-04 19:59:46 +02:00
|
|
|
WebSocketPeer.STATE_OPEN:
|
|
|
|
|
print("open")
|
2025-07-04 12:22:48 +02:00
|
|
|
_stop_thread = false
|
2025-07-06 19:49:08 +02:00
|
|
|
#_thread = Thread.new()
|
|
|
|
|
#_thread.start(_listen_for_data)
|
2025-06-29 20:34:46 +02:00
|
|
|
connected_to_server.emit()
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit(ConnectionStatus.LOBBYLESS)
|
2025-07-04 19:59:46 +02:00
|
|
|
WebSocketPeer.STATE_CONNECTING:
|
|
|
|
|
print("connecting")
|
|
|
|
|
connection_status_changed.emit(ConnectionStatus.ATTEMPTING_CONNECTION)
|
|
|
|
|
WebSocketPeer.STATE_CLOSING:
|
|
|
|
|
print("closing")
|
|
|
|
|
pass
|
|
|
|
|
WebSocketPeer.STATE_CLOSED:
|
|
|
|
|
print("closed")
|
2025-07-06 19:49:08 +02:00
|
|
|
#_clean_up_thread()
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
2025-07-04 19:59:46 +02:00
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
_processed_status = _new_status
|
2025-07-06 19:49:08 +02:00
|
|
|
|
|
|
|
|
if _processed_status == WebSocketPeer.STATE_OPEN:
|
|
|
|
|
while _socket.get_available_packet_count():
|
2025-07-04 19:59:46 +02:00
|
|
|
cached_response = _socket.get_packet().get_string_from_utf8()
|
2025-07-15 19:38:27 +02:00
|
|
|
#print(cached_response)
|
2025-07-04 19:59:46 +02:00
|
|
|
#cached_response += (received_data[1] as PackedByteArray).get_string_from_utf8()
|
|
|
|
|
if cached_response.ends_with("}"):
|
|
|
|
|
#print(cached_response)
|
|
|
|
|
_handle_response(cached_response)
|
|
|
|
|
cached_response = ""
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-06 19:49:08 +02:00
|
|
|
func _listen_for_data() -> void:
|
|
|
|
|
while true:
|
|
|
|
|
if _socket.get_available_packet_count():
|
|
|
|
|
cached_response = _socket.get_packet().get_string_from_utf8()
|
|
|
|
|
print(cached_response)
|
|
|
|
|
if cached_response.ends_with("}"):
|
|
|
|
|
_handle_response(cached_response)
|
|
|
|
|
cached_response = ""
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _handle_response(response: String) -> void:
|
|
|
|
|
var data = JSON.parse_string(response)
|
|
|
|
|
match data["response"]:
|
|
|
|
|
"create_lobby":
|
|
|
|
|
if data["status"] == "ok":
|
2025-07-04 10:57:24 +02:00
|
|
|
_lobby_id = data["lobby_id"]
|
|
|
|
|
_client_id = data["client_id"]
|
2025-07-15 11:05:19 +02:00
|
|
|
received_client_id.emit.call_deferred(_client_id)
|
2025-06-29 20:34:46 +02:00
|
|
|
connected_to_lobby.emit.call_deferred(_lobby_id)
|
2025-07-02 21:58:19 +02:00
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
2025-07-07 10:57:39 +02:00
|
|
|
"join_lobby_response":
|
|
|
|
|
_handle_join(data)
|
2025-06-30 22:07:43 +02:00
|
|
|
"client_joined_lobby":
|
2025-07-07 22:40:46 +02:00
|
|
|
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)
|
2025-07-02 22:16:52 +02:00
|
|
|
"leave_lobby_request":
|
|
|
|
|
leave_lobby()
|
|
|
|
|
"leave_lobby":
|
|
|
|
|
if data["status"] == "ok":
|
2025-07-04 11:41:57 +02:00
|
|
|
client_left_lobby.emit.call_deferred(data["client_name"])
|
|
|
|
|
"client_left_lobby":
|
2025-07-07 22:40:46 +02:00
|
|
|
_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)
|
2025-07-20 13:36:30 +02:00
|
|
|
|
|
|
|
|
## A chat message has been received
|
2025-07-04 11:29:25 +02:00
|
|
|
"receive_chat_message":
|
2025-07-20 13:36:30 +02:00
|
|
|
# Relay message to be displayed in the chat panel
|
|
|
|
|
user_message_received.emit.call_deferred(data["client_name"], data["message"])
|
2025-07-07 22:40:46 +02:00
|
|
|
|
|
|
|
|
"new_round_started":
|
2025-07-08 17:40:13 +02:00
|
|
|
if data["current_round"] == 1:
|
|
|
|
|
var playing_order: Array[String] = []
|
|
|
|
|
playing_order.assign(data["playing_order"])
|
2025-07-16 09:45:44 +02:00
|
|
|
client_order_received.emit.call_deferred(playing_order, _connected_client_ids, _connected_client_names)
|
2025-07-08 17:40:13 +02:00
|
|
|
|
2025-07-15 11:31:01 +02:00
|
|
|
var received_cards = data["cards"]
|
2025-07-16 10:20:45 +02:00
|
|
|
new_round_started.emit.call_deferred()
|
2025-07-08 17:40:13 +02:00
|
|
|
cards_received.emit.call_deferred(received_cards)
|
2025-07-15 14:42:06 +02:00
|
|
|
trump_received.emit.call_deferred(data["trump"])
|
2025-07-08 10:42:34 +02:00
|
|
|
"client_submitted_bet":
|
2025-07-16 10:20:45 +02:00
|
|
|
received_expected_bet.emit.call_deferred(
|
|
|
|
|
data["client_id"],
|
|
|
|
|
data["bet"],
|
|
|
|
|
)
|
2025-07-09 16:48:45 +02:00
|
|
|
"request_bet":
|
2025-07-18 12:53:43 +02:00
|
|
|
received_bet_request.emit.call_deferred(data["disallowed_bet"], data["round"])
|
2025-07-09 16:48:45 +02:00
|
|
|
"begin_playing_cards":
|
2025-07-15 14:42:06 +02:00
|
|
|
# all players know it's time to play
|
2025-07-09 16:48:45 +02:00
|
|
|
print(data)
|
2025-07-18 14:10:39 +02:00
|
|
|
_is_first_play = true
|
|
|
|
|
reset_first_card.emit.call_deferred()
|
2025-07-09 16:48:45 +02:00
|
|
|
"request_card":
|
2025-07-15 14:42:06 +02:00
|
|
|
# individual player asked to play card
|
|
|
|
|
notify_play_card_request.emit.call_deferred()
|
2025-07-18 14:10:39 +02:00
|
|
|
"begin_next_play":
|
|
|
|
|
print("beginning next play!")
|
|
|
|
|
_is_first_play = true
|
|
|
|
|
reset_first_card.emit.call_deferred()
|
2025-07-09 16:48:45 +02:00
|
|
|
"play_card":
|
2025-07-08 10:42:34 +02:00
|
|
|
print(data)
|
2025-07-16 11:59:52 +02:00
|
|
|
#client_played_card.emit.call_deferred(data["client_id"], data["played_card"])
|
2025-07-15 12:11:50 +02:00
|
|
|
"determine_play_winner_request": # single card go-around
|
|
|
|
|
received_play_winner_request.emit.call_deferred()
|
2025-07-16 11:15:05 +02:00
|
|
|
"end_game_request":
|
|
|
|
|
received_end_game_request.emit.call_deferred()
|
2025-07-15 14:42:06 +02:00
|
|
|
"client_played_card":
|
2025-07-18 14:10:39 +02:00
|
|
|
print("RECEIVED FOLLOWING CARD ID: " + str(data["played_card"]))
|
|
|
|
|
if _is_first_play:
|
|
|
|
|
print("set first card")
|
|
|
|
|
received_first_card.emit.call_deferred(data["played_card"])
|
|
|
|
|
if not CardManager.get_card_color(data["played_card"]) == Card.CardColor.WHITE:
|
|
|
|
|
_is_first_play = false
|
2025-07-15 14:42:06 +02:00
|
|
|
received_played_card.emit.call_deferred(data["client_id"], data["played_card"])
|
2025-07-15 12:11:50 +02:00
|
|
|
"client_won_play":
|
2025-07-15 14:42:06 +02:00
|
|
|
if data["client_id"] == _client_id:
|
|
|
|
|
received_win.emit.call_deferred()
|
2025-07-16 10:20:45 +02:00
|
|
|
received_winning_client.emit.call_deferred(data["client_id"])
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-07 10:57:39 +02:00
|
|
|
func _handle_join(data) -> void:
|
|
|
|
|
match data["status"]:
|
|
|
|
|
"lobby_not_found":
|
|
|
|
|
received_connection_message.emit.call_deferred("Could not join; lobby does not exist")
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
"lobby_full":
|
|
|
|
|
received_connection_message.emit.call_deferred("Could not join; lobby is full")
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
"ok":
|
|
|
|
|
_client_id = data["client_id"]
|
|
|
|
|
connected_to_lobby.emit.call_deferred(_lobby_id)
|
2025-07-08 17:40:13 +02:00
|
|
|
received_client_id.emit.call_deferred(_client_id)
|
2025-07-07 10:57:39 +02:00
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.JOINED)
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _exit_tree() -> void:
|
|
|
|
|
disconnect_from_server()
|
|
|
|
|
|
2025-07-06 19:49:08 +02:00
|
|
|
func connect_to_server(text: String) -> void:
|
|
|
|
|
var err = _socket.connect_to_url(text)
|
2025-07-04 19:59:46 +02:00
|
|
|
if err != OK:
|
|
|
|
|
print("didn't work!")
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func disconnect_from_server() -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.close()
|
2025-07-04 12:22:48 +02:00
|
|
|
disconnected_from_server.emit()
|
|
|
|
|
connection_status_changed.emit(ConnectionStatus.DISCONNECTED)
|
|
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func create_lobby(client_name: String) -> void:
|
|
|
|
|
_client_name = client_name
|
|
|
|
|
|
2025-07-04 10:57:24 +02:00
|
|
|
_send_message(TCPMessages.create_lobby(client_name))
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func join_lobby(lobby_id: String, client_name: String) -> void:
|
|
|
|
|
_lobby_id = lobby_id
|
|
|
|
|
_client_name = client_name
|
|
|
|
|
|
2025-07-04 10:57:24 +02:00
|
|
|
_send_message(TCPMessages.join_lobby(_lobby_id, client_name))
|
2025-07-07 10:57:39 +02:00
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.ATTEMPTING_JOIN)
|
2025-06-29 20:34:46 +02:00
|
|
|
|
|
|
|
|
func leave_lobby() -> void:
|
|
|
|
|
_send_message(TCPMessages.leave_lobby(_lobby_id, _client_id))
|
|
|
|
|
|
|
|
|
|
_lobby_id = ""
|
|
|
|
|
_client_id = ""
|
|
|
|
|
_client_name = ""
|
2025-06-30 22:07:43 +02:00
|
|
|
|
2025-07-04 11:29:25 +02:00
|
|
|
disconnected_from_lobby.emit.call_deferred()
|
|
|
|
|
connection_status_changed.emit.call_deferred(ConnectionStatus.LOBBYLESS)
|
|
|
|
|
|
|
|
|
|
func send_chat_message(message: String) -> void:
|
2025-07-20 14:11:46 +02:00
|
|
|
#_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|
|
|
|
|
pass
|
2025-06-29 20:34:46 +02:00
|
|
|
|
2025-07-07 22:40:46 +02:00
|
|
|
func start_game() -> void:
|
|
|
|
|
_send_message(JSON.stringify({
|
|
|
|
|
"response": "start_game",
|
|
|
|
|
"lobby_id": _lobby_id,
|
2025-07-08 10:42:34 +02:00
|
|
|
"cards": CardManager.get_shuffled_cards(_connected_client_ids, 1),
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-15 19:38:27 +02:00
|
|
|
func start_next_play() -> void:
|
|
|
|
|
_send_message(JSON.stringify({
|
|
|
|
|
"response": "start_next_play",
|
|
|
|
|
"lobby_id": _lobby_id,
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-08 10:42:34 +02:00
|
|
|
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),
|
2025-07-07 22:40:46 +02:00
|
|
|
}))
|
|
|
|
|
|
2025-07-16 11:15:05 +02:00
|
|
|
func end_game() -> void:
|
|
|
|
|
_send_message(JSON.stringify({
|
|
|
|
|
"response": "end_game",
|
|
|
|
|
"lobby_id": _lobby_id,
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-15 14:42:06 +02:00
|
|
|
func play_card(card_id: int) -> void:
|
|
|
|
|
_send_message(JSON.stringify({
|
|
|
|
|
"response": "play_card",
|
|
|
|
|
"lobby_id": _lobby_id,
|
|
|
|
|
"client_id": _client_id,
|
|
|
|
|
"played_card": card_id,
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-09 16:48:45 +02:00
|
|
|
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,
|
|
|
|
|
}))
|
|
|
|
|
|
2025-07-15 14:42:06 +02:00
|
|
|
func submit_play_winner(winning_client_id: String) -> void:
|
2025-07-15 12:11:50 +02:00
|
|
|
_send_message(JSON.stringify({
|
|
|
|
|
"response": "submit_play_winner",
|
|
|
|
|
"lobby_id": _lobby_id,
|
|
|
|
|
"client_id": winning_client_id,
|
|
|
|
|
}))
|
2025-07-15 19:38:27 +02:00
|
|
|
request_next_play.emit.call_deferred()
|
|
|
|
|
#request_next_round.emit.call_deferred()
|
2025-07-15 12:11:50 +02:00
|
|
|
|
2025-06-29 20:34:46 +02:00
|
|
|
func _send_message(message: String) -> void:
|
2025-07-04 19:59:46 +02:00
|
|
|
_socket.send_text(message)
|
2025-07-20 14:11:46 +02:00
|
|
|
|
|
|
|
|
func _on_chat_panel_message_sent(message: String) -> void:
|
|
|
|
|
_send_message(TCPMessages.send_chat_message(_lobby_id, _client_id, message))
|