mirror of
https://github.com/JohnBreaux/Boat-Battle.git
synced 2024-11-15 05:25:57 +00:00
Lobby: Return to Host/Connect menu when connection to server unexpectedly closes
This commit is contained in:
parent
ceb8df329a
commit
bfd005d01c
@ -1,7 +1,7 @@
|
|||||||
extends Control
|
extends Control
|
||||||
# Ignore discarded return values
|
# Ignore discarded return values
|
||||||
# warning-ignore:return_value_discarded
|
# warning-ignore:return_value_discarded
|
||||||
onready var text = find_node("Player List")
|
onready var player_list = find_node("Player List")
|
||||||
onready var ip = find_node("IP Address")
|
onready var ip = find_node("IP Address")
|
||||||
|
|
||||||
# TODO: Write a function to update Player List with the list of attached players
|
# TODO: Write a function to update Player List with the list of attached players
|
||||||
@ -11,7 +11,7 @@ func _on_peers_updated():
|
|||||||
for peer in Net.peer_info:
|
for peer in Net.peer_info:
|
||||||
connected_peers += ("%s\n" % Net.peer_info[peer]["name"])
|
connected_peers += ("%s\n" % Net.peer_info[peer]["name"])
|
||||||
pass
|
pass
|
||||||
text.text = connected_peers.rsplit("\n", true, 1)[0].c_unescape()
|
player_list.text = connected_peers.rsplit("\n", true, 1)[0].c_unescape()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func set_IP_Address_text(show):
|
func set_IP_Address_text(show):
|
||||||
@ -23,6 +23,7 @@ func set_IP_Address_text(show):
|
|||||||
|
|
||||||
func _ready():
|
func _ready():
|
||||||
Net.connect("peers_updated", self, "_on_peers_updated")
|
Net.connect("peers_updated", self, "_on_peers_updated")
|
||||||
|
Net.connect("disconnected", self, "_on_Net_disconnected")
|
||||||
_on_peers_updated()
|
_on_peers_updated()
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -47,12 +48,18 @@ func _on_Host_Button_pressed():
|
|||||||
# Disconnect from (or stop hosting) a game
|
# Disconnect from (or stop hosting) a game
|
||||||
# Shows the host/connect buttons
|
# Shows the host/connect buttons
|
||||||
func _on_Disconnect_Button_pressed():
|
func _on_Disconnect_Button_pressed():
|
||||||
# Hide "Connected Options"
|
|
||||||
show_Connected_Options(false)
|
|
||||||
# Show the host IP address
|
|
||||||
set_IP_Address_text(false)
|
|
||||||
# Disconnect
|
# Disconnect
|
||||||
Net.disconnect_host()
|
Net.disconnect_host()
|
||||||
|
# Hide "Connected Options"
|
||||||
|
show_Connected_Options(false)
|
||||||
|
# Hide the host IP address
|
||||||
|
set_IP_Address_text(false)
|
||||||
|
|
||||||
|
func _on_Net_disconnected():
|
||||||
|
# Hide "Connected Options"
|
||||||
|
show_Connected_Options(false)
|
||||||
|
# Hide the host IP address
|
||||||
|
set_IP_Address_text(false)
|
||||||
|
|
||||||
func _on_Change_Name_Button_pressed():
|
func _on_Change_Name_Button_pressed():
|
||||||
# Show the Change Name dialogue
|
# Show the Change Name dialogue
|
||||||
@ -76,17 +83,30 @@ func _on_Connect_to_Game_confirmed():
|
|||||||
# Show "Connected Options"
|
# Show "Connected Options"
|
||||||
show_Connected_Options(true)
|
show_Connected_Options(true)
|
||||||
|
|
||||||
func _on_Change_Name_confirmed():
|
|
||||||
# Get the new name
|
|
||||||
var name = find_node("Name Entry").text.split("\n")[0]
|
|
||||||
# Set it as the name
|
|
||||||
Net.change_name(name)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func _on_Exit_Lobby_pressed():
|
func _on_Exit_Lobby_pressed():
|
||||||
# Disconnect
|
# Disconnect
|
||||||
if Net.connected:
|
if Net.connected:
|
||||||
Net.disconnect_host()
|
Net.disconnect_host()
|
||||||
# Close Lobby menu
|
# Close Lobby menu
|
||||||
queue_free()
|
queue_free()
|
||||||
|
|
||||||
|
|
||||||
|
func _on_IP_and_Port_Entry_text_entered(text):
|
||||||
|
# Split it into IP and Port segments
|
||||||
|
var ip_port = text.split(":")
|
||||||
|
# If text exists and contains valid IP address
|
||||||
|
if ip_port.size() > 0 and ip_port[0].is_valid_ip_address():
|
||||||
|
# Connect to host
|
||||||
|
var connected = Net.callv("connect_host", ip_port)
|
||||||
|
if connected == OK:
|
||||||
|
# Show "Connected Options"
|
||||||
|
show_Connected_Options(true)
|
||||||
|
# Hide the popup
|
||||||
|
find_node("Connect to Game").hide()
|
||||||
|
|
||||||
|
|
||||||
|
func _on_Name_Entry_text_entered(text):
|
||||||
|
# Change the name
|
||||||
|
Net.change_name(text)
|
||||||
|
# Hide the popup
|
||||||
|
find_node("Change Name").hide()
|
||||||
|
@ -10,15 +10,15 @@ const LOCALHOST = "127.0.0.1"
|
|||||||
# Mail types:
|
# Mail types:
|
||||||
# 1: REQUEST: Message is a request for information
|
# 1: REQUEST: Message is a request for information
|
||||||
# 0: REPLY: Message is a reply
|
# 0: REPLY: Message is a reply
|
||||||
enum {REPLY, REQUEST}
|
enum {REPLY, REQUEST, READY, ACK}
|
||||||
|
|
||||||
# Signals
|
# Signals
|
||||||
# incoming(mail): Sent when there's an incoming message
|
# incoming(mail): Sent when there's an incoming message
|
||||||
signal incoming
|
signal incoming
|
||||||
# peers_updated(): Sent when the peer list is updated
|
# peers_updated(): Sent when the peer list is updated
|
||||||
signal peers_updated
|
signal peers_updated
|
||||||
#
|
# disconnected(): Sent when unexpectedly disconnected
|
||||||
|
signal disconnected
|
||||||
# Variables
|
# Variables
|
||||||
# inbox: Array: Message history
|
# inbox: Array: Message history
|
||||||
var inbox = []
|
var inbox = []
|
||||||
@ -33,8 +33,9 @@ var local_info = {"name": ""}
|
|||||||
|
|
||||||
# Network -- handles server and client setup, and facilitates communication between the two
|
# Network -- handles server and client setup, and facilitates communication between the two
|
||||||
# receive: Receive a message (called by sender's `send` function)
|
# receive: Receive a message (called by sender's `send` function)
|
||||||
# item: The message received from the sender (implicitly JSON-decoded by JSONRPC)
|
# mail: The message received from the sender (implicitly JSON-decoded by JSONRPC)
|
||||||
sync func receive(mail):
|
# mail_type: Type of mail (see "Mail Types" enum above)
|
||||||
|
remote func receive(mail):
|
||||||
# Get the sender's ID and force letter to be properly addressed
|
# Get the sender's ID and force letter to be properly addressed
|
||||||
mail[0] = get_tree().get_rpc_sender_id()
|
mail[0] = get_tree().get_rpc_sender_id()
|
||||||
# Add the mail to the inbox (so it can be read back later if necessary
|
# Add the mail to the inbox (so it can be read back later if necessary
|
||||||
@ -45,10 +46,12 @@ sync func receive(mail):
|
|||||||
# send: Send a message
|
# send: Send a message
|
||||||
# id: Peer ID of the recipient
|
# id: Peer ID of the recipient
|
||||||
# mail: Variant of a json-encodable type (non-Object) to send
|
# mail: Variant of a json-encodable type (non-Object) to send
|
||||||
func send(id, mail):
|
# mail_type: Type of mail (see "Mail Types" enum above)
|
||||||
|
func send(id, mail, mail_type = REPLY):
|
||||||
# Make the recipient receive the mail
|
# Make the recipient receive the mail
|
||||||
rpc_id(id, "receive", to_json(mail))
|
rpc_id(id, "receive", to_json([-1, mail, mail_type]))
|
||||||
|
|
||||||
|
# Host
|
||||||
# start_host: Host the game
|
# start_host: Host the game
|
||||||
# port: TCP port
|
# port: TCP port
|
||||||
# max_players: Largest number of players allowed to connect at a time
|
# max_players: Largest number of players allowed to connect at a time
|
||||||
@ -67,12 +70,20 @@ func start_host(port = DEFAULT_PORT, max_players = 2):
|
|||||||
connected = true
|
connected = true
|
||||||
hosting = true
|
hosting = true
|
||||||
|
|
||||||
|
# accept_guests:
|
||||||
|
# Select whether to accept new guests
|
||||||
|
func accept_guests(accept:bool):
|
||||||
|
if hosting:
|
||||||
|
multiplayer.refuse_new_network_connections = not accept
|
||||||
|
|
||||||
|
# Guest
|
||||||
# connect_host: Connect to a host
|
# connect_host: Connect to a host
|
||||||
func connect_host(ip = LOCALHOST, port = DEFAULT_PORT):
|
func connect_host(ip = LOCALHOST, port = DEFAULT_PORT):
|
||||||
get_hostname()
|
get_hostname()
|
||||||
var peer = NetworkedMultiplayerENet.new()
|
var peer = NetworkedMultiplayerENet.new()
|
||||||
peer.create_client(ip, port)
|
var ret = peer.create_client(ip, port)
|
||||||
get_tree().network_peer = peer
|
get_tree().network_peer = peer
|
||||||
|
return ret
|
||||||
|
|
||||||
# disconnect_host
|
# disconnect_host
|
||||||
func disconnect_host():
|
func disconnect_host():
|
||||||
@ -99,6 +110,7 @@ func change_name(name):
|
|||||||
rpc("register_peer", local_info)
|
rpc("register_peer", local_info)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Helper Functions
|
||||||
# get_hostname: Asks the host machine to provide its hostname,
|
# get_hostname: Asks the host machine to provide its hostname,
|
||||||
# and if the peer name isn't set, set it to the hostname
|
# and if the peer name isn't set, set it to the hostname
|
||||||
func get_hostname():
|
func get_hostname():
|
||||||
@ -125,15 +137,19 @@ func _ready():
|
|||||||
_trash = get_tree().connect("server_disconnected", self, "_host_disconnected")
|
_trash = get_tree().connect("server_disconnected", self, "_host_disconnected")
|
||||||
_trash = get_tree().connect("connection_failed", self, "_connection_fail" )
|
_trash = get_tree().connect("connection_failed", self, "_connection_fail" )
|
||||||
|
|
||||||
|
# Signal Handlers
|
||||||
func _peer_connected(id):
|
func _peer_connected(id):
|
||||||
# Send peer info to remote peer
|
# Send peer info to remote peer
|
||||||
rpc_id(id, "register_peer", local_info)
|
rpc_id(id, "register_peer", local_info)
|
||||||
|
if hosting and peer_info.size() >= 2:
|
||||||
|
pass
|
||||||
pass
|
pass
|
||||||
|
|
||||||
func _peer_disconnected(id):
|
func _peer_disconnected(id):
|
||||||
# Unregister the peer locally
|
# Unregister the peer locally
|
||||||
unregister_peer(id)
|
unregister_peer(id)
|
||||||
|
if hosting and peer_info.size() < 2:
|
||||||
|
pass
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@ -147,6 +163,8 @@ func _host_connected():
|
|||||||
func _host_disconnected():
|
func _host_disconnected():
|
||||||
# Ensure host is disconnected
|
# Ensure host is disconnected
|
||||||
disconnect_host()
|
disconnect_host()
|
||||||
|
# Send disconnection message to listeners
|
||||||
|
emit_signal("disconnected")
|
||||||
|
|
||||||
func _connection_fail():
|
func _connection_fail():
|
||||||
# Ensure Net state is clear
|
# Ensure Net state is clear
|
||||||
|
Loading…
Reference in New Issue
Block a user