Merge pull request 'Init server' (#2) from develop into production

Reviewed-on: #2
pull/10/head
Евгений Сугоняко 2024-05-03 17:37:50 +00:00
commit a8ad63c676
7 changed files with 150 additions and 14 deletions

View File

@ -1,2 +1,22 @@
# GameServer
## Server start arguments
Set server port (by default 3300):
```bash
CashFlow_Server -port=3300
```
Set max clients (by default 2048)
```bash
CashFlow_Server -max_clients=2048
```
Write server logs to file server.log
```bash
CashFlow_Server -log
```
All arguments
```bash
CashFlow_Server -port=3300 -max_clients=2048 -log
```

View File

@ -1,9 +0,0 @@
[gd_scene format=3 uid="uid://crkephruijbve"]
[node name="MainControl" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2

6
data/scenes/server.tscn Normal file
View File

@ -0,0 +1,6 @@
[gd_scene format=3 uid="uid://crkephruijbve"]
[node name="MainServerNode" type="Node"]
process_mode = 3
process_priority = 1
process_physics_priority = 1

View File

@ -0,0 +1,37 @@
class_name File_Manager extends Object
"""
File manager
"""
var base_dir = OS.get_executable_path().get_base_dir()
var LOG_FILE = base_dir.path_join("server.log")
#--------------------------------------------------------------------------------------------------#
# Methods
func set_base_dir(dir) -> void:
base_dir = dir
LOG_FILE = base_dir.path_join("server.log")
func add_to_log(_text : String) -> void:
if Server.settings.MakeLogs:
__create_file_is_not_exist(LOG_FILE)
var file = FileAccess.open(LOG_FILE, FileAccess.READ_WRITE)
file.store_line(
file.get_as_text() +
'[ %s %s ] %s' % [
Time.get_date_string_from_system(),
Time.get_time_string_from_system(),
_text
]
)
file.close()
#--------------------------------------------------------------------------------------------------#
# Functions
func __create_file_is_not_exist(_file) -> void:
if !FileAccess.file_exists(_file):
var file := FileAccess.open(_file, FileAccess.WRITE)
file.close()

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])

Binary file not shown.

Before

Width:  |  Height:  |  Size: 37 KiB

View File

@ -15,20 +15,22 @@ config/name_localized={
"en_US": "Cashflow Game Server"
}
config/version="0.0.0.001"
run/main_scene="res://data/scenes/main_manu.tscn"
config/use_custom_user_dir=true
config/custom_user_dir_name="data"
run/main_scene="res://data/scenes/server.tscn"
config/features=PackedStringArray("4.2", "GL Compatibility")
boot_splash/bg_color=Color(0, 0, 0, 1)
boot_splash/image="res://game-server.png"
config/icon="res://game-server.png"
config/windows_native_icon="res://game-server.ico"
boot_splash/minimum_display_time=5
[autoload]
Server="*res://data/scripts/objects/Server/SERVER.gd"
[debug]
file_logging/enable_file_logging=true
file_logging/log_path="user://logs/server.log"
[display]
window/size/viewport_width=800
@ -42,6 +44,10 @@ window/ios/hide_home_indicator=false
window/ios/hide_status_bar=false
window/ios/suppress_ui_gesture=false
[editor]
run/main_run_args="-log"
[gui]
theme/custom_font="res://data/styles/fonts/Roboto-Regular.ttf"