Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add server config option to switch if players can join #94

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 +45,14 @@
private Match.Match? _Match;
private bool _DisposedValue;
private ConfigCreator _ConfigCreator;
private ServerConfig _ServerConfig;
private readonly CurrentRoundState _CurrentRoundState = new();

/// <summary>
/// Create instance of Application
/// </summary>
#pragma warning disable S107 // Methods should not have too many parameters
public Application(

Check warning on line 55 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Non-nullable field '_ConfigCreator' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 55 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Non-nullable field '_ServerConfig' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
ILogger<Application> logger,
IBasePlugin plugin,
ICsServer csServer,
Expand All @@ -73,9 +74,6 @@
PugSharpDirectory = Path.Combine(_CsServer.GameDirectory, "csgo", "PugSharp");
_ConfigProvider.Initialize(Path.Join(PugSharpDirectory, "Config"));




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

Expand All @@ -85,7 +83,11 @@

serverConfigResult.Switch(
error => { }, // Do nothing - Error already logged
serverConfig => SetServerCulture(serverConfig.Locale)
serverConfig =>
{
_ServerConfig = serverConfig;
SetServerCulture(serverConfig.Locale);
}
);

RegisterEventHandlers();
Expand All @@ -105,7 +107,7 @@
var results = command.CommandCallBack(args);
foreach (var result in results)
{
// TODO Translation?

Check warning on line 110 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
c.ReplyToCommand(result);
}
}, c, p, c.GetCommandString, c.ArgString);
Expand Down Expand Up @@ -174,7 +176,7 @@

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 @@
// 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 Expand Up @@ -318,7 +325,7 @@
}
}

// TODO Add Round Events to RoundService?

Check warning on line 328 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
private HookResult OnRoundPreStart(EventRoundPrestart eventRoundPrestart, GameEventInfo info)
{
_Logger.LogInformation("OnRoundPreStart called");
Expand Down Expand Up @@ -555,7 +562,7 @@
{
int maxMoneyValue = 16000;
//try
//{

Check warning on line 565 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Remove this commented out code. (https://rules.sonarsource.com/csharp/RSPEC-125)
// // Use value from server if possible
// var maxMoneyCvar = ConVar.Find("mp_maxmoney");
// if (maxMoneyCvar != null)
Expand Down Expand Up @@ -588,7 +595,7 @@
var killedByBomb = eventPlayerDeath.Weapon.Equals("planted_c4", StringComparison.OrdinalIgnoreCase);
var isSuicide = (attacker == victim) && !killedByBomb;
var isHeadshot = eventPlayerDeath.Headshot;
var isClutcher = false; // TODO

Check warning on line 598 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

var victimStats = _CurrentRoundState.GetPlayerRoundStats(victim.SteamID, victim.PlayerName);

Expand Down Expand Up @@ -654,7 +661,7 @@
break;
}

var hasClutched = false; // TODO

Check warning on line 664 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)

if (hasClutched)
{
Expand Down Expand Up @@ -1491,7 +1498,7 @@
}

#pragma warning disable MA0003 // Add parameter name to improve readability
p.Pawn.Value.CommitSuicide(true, true);

Check warning on line 1501 in PugSharp/Application.cs

View workflow job for this annotation

GitHub Actions / test_linux_x64

Dereference of a possibly null reference.
#pragma warning restore MA0003 // Add parameter name to improve readability
},
command,
Expand Down
Loading