From 43f468751043f11df28a87edc82353b6e8df4b8e Mon Sep 17 00:00:00 2001 From: TheR00st3r Date: Fri, 1 Dec 2023 23:16:54 +0100 Subject: [PATCH] Add server config option to switch if players can join --- Docs/source/admin/configuration.rst | 19 +++++++++++++------ PugSharp.Config/ConfigProvider.cs | 21 +++++++++++---------- PugSharp.Config/ServerConfig.cs | 3 +++ PugSharp/Application.cs | 17 ++++++++++++----- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Docs/source/admin/configuration.rst b/Docs/source/admin/configuration.rst index 8301bccd..42cba247 100644 --- a/Docs/source/admin/configuration.rst +++ b/Docs/source/admin/configuration.rst @@ -103,13 +103,19 @@ Serverconfig ........................ The Serverconfig defines server wide PugSharp settings for your server. It is loaded automatically when PugSharp is loaded. +Location: /game/csgo/PugSharp/Config/server.json + + + Serverconfig Fields ''''''''''''''''''''' -+--------+---------+---------------------------------------------------------------------------------------+ -| Field | Default | Description | -+========+=========+=======================================================================================+ -| locale | en | This is the language that will be used for the messages that are printed to the users | -+--------+---------+---------------------------------------------------------------------------------------+ ++-----------------------------+---------+---------------------------------------------------------------------------------------+ +| Field | Default | Description | ++=============================+=========+=======================================================================================+ +| locale | en | This is the language that will be used for the messages that are printed to the users | ++-----------------------------+---------+---------------------------------------------------------------------------------------+ +| allow_players_without_match | true | Defines if players can join the server when no match is loaded. | ++-----------------------------+---------+---------------------------------------------------------------------------------------+ Serverconfig Example ''''''''''''''''''''' @@ -117,7 +123,8 @@ Serverconfig Example .. code-block:: json { - "none": "" + "locale": "en", + "allow_players_without_match": true } diff --git a/PugSharp.Config/ConfigProvider.cs b/PugSharp.Config/ConfigProvider.cs index 904ae734..d82419ff 100644 --- a/PugSharp.Config/ConfigProvider.cs +++ b/PugSharp.Config/ConfigProvider.cs @@ -138,20 +138,21 @@ public OneOf, ServerConfig> LoadServerConfig() // Create default config _ServerConfig = new ServerConfig(); - using FileStream createStream = File.Create(configPath); - JsonSerializer.Serialize(createStream, _ServerConfig); - return _ServerConfig; } - - using var loadingStream = File.OpenRead(configPath); - _ServerConfig = JsonSerializer.Deserialize(loadingStream); - - if (_ServerConfig == null) + else { - _Logger.LogError("ServerConfig was deserialized to null"); - return new Error("ServerConfig couldn't be deserialized"); + using var loadingStream = File.OpenRead(configPath); + _ServerConfig = JsonSerializer.Deserialize(loadingStream); + + if (_ServerConfig == null) + { + _Logger.LogError("ServerConfig was deserialized to null"); + return new Error("ServerConfig couldn't be deserialized"); + } } + using FileStream createStream = File.Create(configPath); + JsonSerializer.Serialize(createStream, _ServerConfig); return _ServerConfig; } catch (Exception ex) diff --git a/PugSharp.Config/ServerConfig.cs b/PugSharp.Config/ServerConfig.cs index d3e9201c..09fed966 100644 --- a/PugSharp.Config/ServerConfig.cs +++ b/PugSharp.Config/ServerConfig.cs @@ -6,4 +6,7 @@ public class ServerConfig { [JsonPropertyName("locale")] public string Locale { get; init; } = "en"; + + [JsonPropertyName("allow_players_without_match")] + public bool AllowPlayersWithoutMatch { get; init; } = true; } diff --git a/PugSharp/Application.cs b/PugSharp/Application.cs index 4efa5292..95ec0841 100644 --- a/PugSharp/Application.cs +++ b/PugSharp/Application.cs @@ -45,6 +45,7 @@ public class Application : IApplication private Match.Match? _Match; private bool _DisposedValue; private ConfigCreator _ConfigCreator; + private ServerConfig _ServerConfig; private readonly CurrentRoundState _CurrentRoundState = new(); /// @@ -73,9 +74,6 @@ public Application( PugSharpDirectory = Path.Combine(_CsServer.GameDirectory, "csgo", "PugSharp"); _ConfigProvider.Initialize(Path.Join(PugSharpDirectory, "Config")); - - - _ = Task.Run(ConfigLoaderTask, _CancellationTokenSource.Token); } @@ -85,7 +83,11 @@ public void Initialize(bool hotReload) serverConfigResult.Switch( error => { }, // Do nothing - Error already logged - serverConfig => SetServerCulture(serverConfig.Locale) + serverConfig => + { + _ServerConfig = serverConfig; + SetServerCulture(serverConfig.Locale); + } ); RegisterEventHandlers(); @@ -174,7 +176,7 @@ private HookResult OnPlayerConnectFull(EventPlayerConnectFull eventPlayerConnect if (userId != null && userId.IsValid) { - // // Userid will give you a reference to a CCSPlayerController class + // Userid will give you a reference to a CCSPlayerController class _Logger.LogInformation("Player {playerName} has connected!", userId.PlayerName); if (userId.IsHLTV) @@ -208,6 +210,11 @@ private HookResult OnPlayerConnectFull(EventPlayerConnectFull eventPlayerConnect // do nothing } } + else if (_ServerConfig?.AllowPlayersWithoutMatch == false) + { + eventPlayerConnectFull.Userid.PrintToCenter("Joining without a match is not allowed!"); + eventPlayerConnectFull.Userid.Kick(); + } else { // Do nothign if no match is loaded