From 41e776924930176d10d5753d49f5cb5493d8884f Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Thu, 9 Dec 2021 21:53:43 -0600 Subject: [PATCH] Leaving works, some more stuff around waiting for peering to finish setting up? --- screens/multiplayer_lobby.tscn | 4 +-- scripts/global/signaller_client.gd | 2 +- scripts/global/webrtc_negotiator.gd | 31 +++++++++++++++++-- scripts/objects/peer.gd | 3 +- scripts/screens/multiplayer_lobby.gd | 45 +++++++++++++++++++++++----- 5 files changed, 72 insertions(+), 13 deletions(-) diff --git a/screens/multiplayer_lobby.tscn b/screens/multiplayer_lobby.tscn index 85153bb..c4ec001 100644 --- a/screens/multiplayer_lobby.tscn +++ b/screens/multiplayer_lobby.tscn @@ -176,7 +176,7 @@ size_flags_vertical = 3 clear_button_enabled = true placeholder_text = "Compose a chat message here, press Enter to send" -[node name="Button" type="Button" parent="v/body/v/h"] +[node name="send" type="Button" parent="v/body/v/h"] margin_left = 678.0 margin_right = 786.0 margin_bottom = 31.0 @@ -188,4 +188,4 @@ text = "Send Message" [connection signal="pressed" from="v/head/start" to="." method="_on_start_pressed"] [connection signal="toggled" from="v/head/ready_up" to="." method="_on_ready_up_toggled"] [connection signal="text_entered" from="v/body/v/h/chat" to="." method="_on_TextEdit_text_entered"] -[connection signal="pressed" from="v/body/v/h/Button" to="." method="_on_Button_pressed"] +[connection signal="pressed" from="v/body/v/h/send" to="." method="_on_Button_pressed"] diff --git a/scripts/global/signaller_client.gd b/scripts/global/signaller_client.gd index 06ade4a..72f7d60 100644 --- a/scripts/global/signaller_client.gd +++ b/scripts/global/signaller_client.gd @@ -156,7 +156,7 @@ func _process_message(data: Dictionary): emit_signal("peer_data", [data] if data.has("id") else data["data"]) "peer_left": print("Peer Left: %s" % data) - emit_signal("peer_left", data) + emit_signal("peer_left", data["data"]) "candidate": print("Candidate received - Data: %s" % JSON.print(data)) diff --git a/scripts/global/webrtc_negotiator.gd b/scripts/global/webrtc_negotiator.gd index b2645da..32d7f35 100644 --- a/scripts/global/webrtc_negotiator.gd +++ b/scripts/global/webrtc_negotiator.gd @@ -4,6 +4,11 @@ extends Node This module sets up WebRTC peer connections. """ +signal peer_connected(id) +signal peer_disconnected(id) +signal connection_succeeded() +signal signaller_disconnected() + onready var webrtc = WebRTCMultiplayer.new() var signaller_client = null @@ -15,17 +20,39 @@ func _init(_ice_servers, _signaller_client): print("Initializing WebRTCNegotiator with ICE servers: %s" % ice_servers) func _ready(): + signaller_client.connect("disconnected", self, "_signaller_disconnected") + signaller_client.connect("offer_received", self, "_offer_received") signaller_client.connect("answer_received", self, "_answer_received") signaller_client.connect("candidate_received", self, "_candidate_received") - # signaller_client.connect("websocket_disconnected", self, "disconnected") signaller_client.connect("peer_init", self, "_peer_id_set") signaller_client.connect("peer_data", self, "_peer_data") - # webrtc.connect("peer_disconnected", self, "peer_disconnected") + + webrtc.connect("connection_succeeded", self, "_connection_succeeded") + webrtc.connect("peer_connected", self, "_peer_connected") + webrtc.connect("peer_disconnected", self, "_peer_disconnected") + # connect("lobby_sealed", self, "lobby_sealed") + add_child(self.signaller_client) +func _peer_connected(peer_id): + print("FULLY CONNECTED PEER: %s" % peer_id) + emit_signal("peer_connected", peer_id) + +func _peer_disconnected(peer_id): + print("FULLY CONNECTED PEER DISCONNECTING: %s" % peer_id) + emit_signal("peer_disconnected") + +func _connection_succeeded(): + print("FULLY CONNECTED") + emit_signal("connection_succeeded") + +func _signaller_disconnected(): + print("SIGNALLER DISCONNECTED") + emit_signal("signaller_disconnected") + func close(): webrtc.close() signaller_client.close() diff --git a/scripts/objects/peer.gd b/scripts/objects/peer.gd index ef36efc..652f2ba 100644 --- a/scripts/objects/peer.gd +++ b/scripts/objects/peer.gd @@ -6,6 +6,7 @@ onready var ready_tex: Texture = load("res://assets/img/check.png") onready var not_ready_tex: Texture = load("res://assets/img/cross.png") export(String) var id = "Some UUID" +export(String) var peerId = "Some UUID" export(String) var display_name = "Lobby" setget set_display_name export(bool) var ready = false setget set_ready @@ -17,7 +18,7 @@ func _update(): $TextureRect.texture = ready_tex if ready else not_ready_tex $TextureRect.hint_tooltip = "Ready" if ready else "Not Ready" -const keys = ["id", "display_name", "ready"] +const keys = ["id", "display_name", "ready", "peerId"] func set_with_dict(dict): if dict.has("name"): self.display_name = dict["name"] for k in keys: diff --git a/scripts/screens/multiplayer_lobby.gd b/scripts/screens/multiplayer_lobby.gd index f243419..a68f28c 100644 --- a/scripts/screens/multiplayer_lobby.gd +++ b/scripts/screens/multiplayer_lobby.gd @@ -5,6 +5,7 @@ onready var lobby_name = $v/head/lobby_info onready var peers_list_label = $v/body/peers/label onready var chat = $v/body/v/messages onready var chat_edit = $v/body/v/h/chat +onready var send_button = $v/body/v/h/send onready var auto_scroll_chk = $v/body/v/chat_head/auto_scroll onready var max_players = $v/head/max_players onready var ready_up = $v/head/ready_up @@ -13,6 +14,7 @@ onready var start = $v/head/start onready var peer = preload("res://objects/peer.tscn") onready var peers = {} +onready var peers_id_mappings = {} onready var is_host = false func _ready(): @@ -22,6 +24,7 @@ func _ready(): Global.signaller_client.connect("lobby_data", self, "_lobby_data") Global.signaller_client.connect("disconnected", self, "_signaller_disconnected") + Global.negotiator.connect("connection_succeeded", self, "_connection_succeeded") # lobby_name.text = "%s" % Global.signaller_client.lobby_name @@ -30,14 +33,24 @@ func _ready(): # hide/show controls depending on whether or not we're the host is_host = Global.signaller_client.is_host() - if is_host: ready_up.queue_free() + if is_host: + ready_up.queue_free() else: lobby_name.editable = false max_players.editable = false start.queue_free() lock.queue_free() + + # until we fully connect + ready_up.disabled = true + chat_edit.disabled = true + send_button.disabled = true print("Setting up lobby... %s %s" % [Global.signaller_client.peer_id, is_host]) +func _connection_succeeded(): + ready_up.disabled = false + send_button.disabled = false + func _lobby_update(l): for u in l: if !is_host: @@ -56,9 +69,11 @@ func _peer_data(peers): func _add_peer(id, peer_data): call_deferred("update_player_count") var new_peer = peer.instance() + if peer_data.peerId == 1: peer_data.ready = true new_peer.set_with_dict(peer_data) peers_grid.add_child(new_peer) peers[id] = new_peer + peers_id_mappings[int(peer_data.peerId)] = id add_chat("> %s joined the lobby" % new_peer.display_name) func _update_peer(id, peer_data): @@ -67,15 +82,24 @@ func _update_peer(id, peer_data): add_chat("! %s is %s" % [peers[id].display_name, update_text]) peers[id].set_with_dict(peer_data) -func _peer_left(ids: Array): +func _peer_left(leavers): call_deferred("update_player_count") - for data in ids: - var id = data["id"] + for data in leavers: + var id = data.id if peers.has(id): + var peer_id = peers[id].peerId call_deferred("add_chat", "< %s left the lobby" % peers[id].display_name) peers[id].queue_free() peers.erase(id) + peers_id_mappings.erase(peer_id) +func peer_by_peer_id(peer_id): + if peers_id_mappings.has(peer_id): + var p = peers_id_mappings[peer_id] + if peers.has(p): + return peers[p] + return null + remotesync func add_chat(message): if auto_scroll_chk.pressed: call_deferred("scroll_chat_to_bottom") else: @@ -104,7 +128,6 @@ func update_can_start(): if !peer.ready: can_start = false break - print(can_start) if can_start: add_chat("! All players ready - game may now be started") start.disabled = !can_start @@ -114,11 +137,19 @@ func _on_start_pressed(): remotesync func start_game(): Global.goto_game() +remotesync func set_ready(ready): + var from = get_tree().get_rpc_sender_id() + print(from) + peer_by_peer_id(from).ready = ready + +func _on_TextEdit_text_entered(_new_text): + if !send_button.disabled: + send_chat_message() + func _lobby_left(_id): Global.lobby_browser() func _on_leave_button_pressed(): Global.lobby_browser() -func _on_TextEdit_text_entered(_new_text): send_chat_message() func update_player_count(): peers_list_label.text = "Players: %d" % peers.size() func _on_Button_pressed(): send_chat_message() -func _on_ready_up_toggled(button_pressed: bool): Global.signaller_client.set_ready(button_pressed) +func _on_ready_up_toggled(button_pressed: bool): rpc("set_ready", button_pressed) func _on_lobby_info_text_changed(new_text: String): Global.signaller_client.set_lobby_name(new_text) func _signaller_disconnected(): Global.main_menu()