Files
Billiards/scripts/server_networking.gd
itdominator 93a2be399c refactor: decouple game networking from lobby client
* Replace direct client/server networking references with GameClient/GameServer scenes
* Refactor message bus to support local emission and argument expansion
* Move lobby connection lifecycle handling to message bus events
* Remove multiplayer connection wait helpers and direct lobby client coupling
* Rename Server to ServerNetworking and update lobby client references
* Update scene resources and export configuration
2026-08-09 18:41:18 -05:00

70 lines
1.7 KiB
GDScript

class_name ServerNetworking extends Node
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"
#)