feat: init server

pull/2/head
Евгений Сугоняко 2024-05-03 20:17:30 +03:00
parent d5844ae54b
commit 0914c2fc50
1 changed files with 77 additions and 1 deletions

View File

@ -3,8 +3,84 @@ class_name SERVER extends Node
Master server controller
"""
# Debug
var debug : bool = true
# Cmd arguments
var arguments = {}
# Helpers
var FileManager := File_Manager.new()
var ServerPeer := ENetMultiplayerPeer.new()
# Properties
var settings : Dictionary = {
Port = 3300,
Max_clients = 2048,
MakeLogs = false
}
var response : Dictionary = {
status = STATUS.OK,
message = '',
data = []
}
# Constants
const STATUS : Dictionary = {
OK = 200,
ERROR = 500,
WARNING = 300
}
#--------------------------------------------------------------------------------------------------#
# Initialization
func _init():
set_editor_description("Master server controller")
# Set priorities
set_process_mode(PROCESS_MODE_ALWAYS)
set_process_priority(0)
set_physics_process(0)
# IF in debug mode
if debug:
print("Debug mode ON")
FileManager.set_base_dir("res://")
func _ready():
# Get cmd arguments
for argument in OS.get_cmdline_args():
if argument.find("=") > -1:
var key_value = argument.split("=")
arguments[key_value[0].lstrip("--")] = key_value[1]
else:
arguments[argument.lstrip("--")] = ""
# Start server
self.start_server()
# Exit
func _exit_tree():
multiplayer.multiplayer_peer = null
ServerPeer.close()
FileManager.add_to_log("EXIT | Status: 200 | Message: Server stoped\n")
#--------------------------------------------------------------------------------------------------#
# Methods
func start_server() -> void:
if 'port' in arguments:
settings.Port = int(arguments['port'])
if 'max_clients' in arguments:
settings.Max_clients = int(arguments['max_clients'])
if 'log' in arguments:
settings.MakeLogs = true
var peer_status = ServerPeer.create_server(settings.Port, settings.Max_clients)
if peer_status == Error.OK:
response.status = STATUS.OK
response.message = "Server is started on port: %s for max %s clients" % [settings.Port, settings.Max_clients]
multiplayer.multiplayer_peer = ServerPeer
else:
ServerPeer.close()
response.status = STATUS.ERROR
response.message = error_string(peer_status)
FileManager.add_to_log(error_string(peer_status) + " | Status: %s | Message: %s" % [response.status, response.message])