Skip to content

Commit

Permalink
Merge pull request #94 from Lan2Play/feature/ConfigureAllowPlayersWit…
Browse files Browse the repository at this point in the history
…houtMatch

Add server config option to switch if players can join
  • Loading branch information
TheR00st3r authored Dec 1, 2023
2 parents 5b425c6 + 43f4687 commit a4dec48
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
19 changes: 13 additions & 6 deletions Docs/source/admin/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,28 @@ 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
'''''''''''''''''''''

.. code-block:: json
{
"none": ""
"locale": "en",
"allow_players_without_match": true
}
Expand Down
21 changes: 11 additions & 10 deletions PugSharp.Config/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,21 @@ public OneOf<Error<string>, 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<ServerConfig>(loadingStream);

if (_ServerConfig == null)
else
{
_Logger.LogError("ServerConfig was deserialized to null");
return new Error<string>("ServerConfig couldn't be deserialized");
using var loadingStream = File.OpenRead(configPath);
_ServerConfig = JsonSerializer.Deserialize<ServerConfig>(loadingStream);

if (_ServerConfig == null)
{
_Logger.LogError("ServerConfig was deserialized to null");
return new Error<string>("ServerConfig couldn't be deserialized");
}
}

using FileStream createStream = File.Create(configPath);
JsonSerializer.Serialize(createStream, _ServerConfig);
return _ServerConfig;
}
catch (Exception ex)
Expand Down
3 changes: 3 additions & 0 deletions PugSharp.Config/ServerConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 12 additions & 5 deletions PugSharp/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

/// <summary>
Expand Down Expand Up @@ -73,9 +74,6 @@ public Application(
PugSharpDirectory = Path.Combine(_CsServer.GameDirectory, "csgo", "PugSharp");
_ConfigProvider.Initialize(Path.Join(PugSharpDirectory, "Config"));




_ = Task.Run(ConfigLoaderTask, _CancellationTokenSource.Token);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a4dec48

Please sign in to comment.