2024-12-03 21:44:07 +01:00
|
|
|
extends Node
|
|
|
|
|
2024-12-04 21:01:24 +01:00
|
|
|
## Connection Code
|
|
|
|
|
|
|
|
# Connect the signals.
|
|
|
|
func _ready() -> void:
|
|
|
|
if multiplayer.connected_to_server.connect(_on_connected_ok) != OK:
|
|
|
|
Log.error("FAILED to connect the connected_to_server signal to _on_connected_ok!", "Internal Error: Failed to connect signal.")
|
|
|
|
if multiplayer.connection_failed.connect(close_network) != OK:
|
|
|
|
Log.error("FAILED to connect the connection_failed signal to _on_connected_fail!", "Internal Error: Failed to connect signal.")
|
|
|
|
if multiplayer.server_disconnected.connect(close_network) != OK:
|
|
|
|
Log.error("FAILED to connect the server_disconnected signal to _on_server_disconnected!", "Internal Error: Failed to connect signal.")
|
2024-12-03 21:44:07 +01:00
|
|
|
|
|
|
|
# Start the network listener.
|
2024-12-04 21:01:24 +01:00
|
|
|
func start_server() -> Error:
|
2024-12-03 21:44:07 +01:00
|
|
|
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
2024-12-04 21:01:24 +01:00
|
|
|
if peer.create_server(Game.port, Game.max_clients) != OK:
|
|
|
|
Log.warning("Couldn't create the server at port %d!" % Game.port)
|
|
|
|
return FAILED
|
2024-12-03 21:44:07 +01:00
|
|
|
multiplayer.multiplayer_peer = peer
|
2024-12-04 21:01:24 +01:00
|
|
|
Log.info("Created the server at port %d." % Game.port)
|
|
|
|
return OK
|
2024-12-03 21:44:07 +01:00
|
|
|
|
|
|
|
# Connect to a server.
|
2024-12-04 21:01:24 +01:00
|
|
|
func join_server() -> Error:
|
2024-12-03 21:44:07 +01:00
|
|
|
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
2024-12-04 21:01:24 +01:00
|
|
|
if peer.create_client(Game.ip, Game.port) != OK:
|
|
|
|
Log.warning("Couldn't connect to the server at %s:%d!" % [Game.ip, Game.port])
|
|
|
|
return FAILED
|
2024-12-03 21:44:07 +01:00
|
|
|
multiplayer.multiplayer_peer = peer
|
2024-12-04 21:01:24 +01:00
|
|
|
Log.info("Connected to the server at %s:%d." % [Game.ip, Game.port])
|
|
|
|
return OK
|
2024-12-03 21:44:07 +01:00
|
|
|
|
|
|
|
# Close all network connections.
|
|
|
|
func close_network() -> void:
|
|
|
|
multiplayer.multiplayer_peer = null
|
|
|
|
Log.info("Closed all network connections.")
|
2024-12-04 21:01:24 +01:00
|
|
|
|
|
|
|
## Matchmaking Code
|
|
|
|
|
|
|
|
var mutex: Mutex = Mutex.new()
|
|
|
|
var rooms: Dictionary = {}
|
|
|
|
var peers: Dictionary = {}
|
|
|
|
|
|
|
|
func join_room() -> void:
|
|
|
|
if join_server() != OK:
|
|
|
|
Log.warning("Failed to connect to sever.")
|
|
|
|
|
|
|
|
func _on_connected_ok() -> void:
|
|
|
|
if rpc_id(1, "send_roomname", Game.room_name) != OK:
|
|
|
|
Log.warning("Failed to send roomname!")
|
|
|
|
|
|
|
|
@rpc("any_peer", "call_remote", "reliable")
|
|
|
|
func send_roomname(roomname: String) -> void:
|
|
|
|
mutex.lock()
|
|
|
|
if multiplayer.get_unique_id() == 1:
|
|
|
|
if !rooms.has(roomname) and !rooms.has(multiplayer.get_remote_sender_id()):
|
|
|
|
rooms[roomname] = multiplayer.get_remote_sender_id()
|
|
|
|
rooms[multiplayer.get_remote_sender_id()] = roomname
|
|
|
|
peers[roomname] = [multiplayer.get_remote_sender_id()]
|
|
|
|
Log.info("Room %s registered." % roomname)
|
|
|
|
elif peers[roomname] is Array:
|
|
|
|
# For some odd reason, Godot doesn't allow casting peers[roomname] to an Array, but this works?
|
|
|
|
var peer_array: Array = peers[roomname]
|
|
|
|
peer_array.append(multiplayer.get_remote_sender_id())
|
|
|
|
peers[roomname] = peer_array
|
|
|
|
Log.debug("Room %s joined by peer %d." % [roomname, multiplayer.get_remote_sender_id()])
|
|
|
|
else:
|
|
|
|
Log.warning("Peer %d tried to send this client a request meant for the authority!" % multiplayer.get_remote_sender_id())
|
|
|
|
mutex.unlock()
|