Files
Billiards/scripts/server_networking.gd

70 lines
1.7 KiB
GDScript3
Raw Normal View History

class_name ServerNetworking extends Node
2026-08-07 02:50:54 -05:00
var peer: ENetMultiplayerPeer
func _ready() -> void:
pass
func start_server(address: String = "0.0.0.0", port: int = 8080) -> void:
if peer: return
push_warning("Server Starting... Address: ", address, " Port: ", port)
peer = ENetMultiplayerPeer.new()
peer.set_bind_ip(address)
var state = peer.create_server(port)
match state:
OK:
push_warning("Server Started: ", state)
multiplayer.multiplayer_peer = peer
multiplayer.peer_connected.connect(server_client_connected)
multiplayer.peer_disconnected.connect(server_client_disconnected)
ERR_ALREADY_IN_USE:
peer.close()
peer = null
start_server(address, port)
ERR_CANT_CREATE:
push_warning("Couldn't create server...")
func close_connection() -> void:
push_warning("Server Closed...")
multiplayer.peer_connected.disconnect(server_client_connected)
multiplayer.peer_disconnected.disconnect(server_client_disconnected)
peer.close()
peer = null
func server_client_connected(id: int) -> void:
push_warning("Client Connected... ID: ", id)
if not Globals.game_data.client1:
Globals.game_data.client1 = id
elif not Globals.game_data.client2:
Globals.game_data.client2 = id
if Globals.game_data.client1 and Globals.game_data.client2:
Globals.multiplayer_data.init_match()
# TODO: Add watch match feature...
func server_client_disconnected(id: int) -> void:
push_warning("Client Disconnected... ID: ", id)
# TODO: Fix call structure
#if id == Globals.game_data.client1:
#LobbyNetworking.rpc_id(
#Globals.game_data.client1,
#"go_back_to_match_screen"
#)
#elif id == Globals.game_data.client2:
#LobbyNetworking.rpc_id(
#Globals.game_data.client2,
#"go_back_to_match_screen"
#)