3 Commits

Author SHA1 Message Date
Morbo
c3d53d2c8b Update getters logic 2023-01-29 00:37:48 +03:00
Morbo
d6f046af9b Disable by default 2023-01-28 23:18:41 +03:00
Morbo
bd6c7e669e Add player slots manager 2023-01-28 21:10:35 +03:00
6 changed files with 104 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
using System.Linq;
using Content.Server.Connection;
using Content.Server.Corvax.PlayerSlots;
using Content.Shared.CCVar;
using Content.Shared.Corvax.CCCVars;
using Content.Shared.Corvax.JoinQueue;
@@ -24,13 +25,13 @@ public sealed class JoinQueueManager
private static readonly Counter QueueBypassCount = Metrics.CreateCounter(
"join_queue_bypass_count",
"Amount of players who bypassed queue by privileges.");
private static readonly Histogram QueueTimings = Metrics.CreateHistogram(
"join_queue_timings",
"Timings of players in queue",
new HistogramConfiguration()
{
LabelNames = new[] {"type"},
LabelNames = new[] { "type" },
Buckets = Histogram.ExponentialBuckets(1, 2, 14),
});
@@ -38,17 +39,18 @@ public sealed class JoinQueueManager
[Dependency] private readonly IConnectionManager _connectionManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IServerNetManager _netManager = default!;
[Dependency] private readonly PlayerSlotsManager _slotsManager = default!; // Corvax-PlayerSlots
/// <summary>
/// Queue of active player sessions
/// NOTE: Real Queue class can't delete disconnected users
/// </summary>
private readonly List<IPlayerSession> _queue = new(); // Real Queue class can't delete disconnected users
private readonly List<IPlayerSession> _queue = new();
private bool _isEnabled = false;
public int PlayerInQueueCount => _queue.Count;
public int ActualPlayersCount => _playerManager.PlayerCount - PlayerInQueueCount; // Now it's only real value with actual players count that in game
public void Initialize()
{
_netManager.RegisterNetMessage<MsgQueueUpdate>();
@@ -81,12 +83,12 @@ public sealed class JoinQueueManager
}
var isPrivileged = await _connectionManager.HavePrivilegedJoin(e.Session.UserId);
var currentOnline = _playerManager.PlayerCount - 1; // Do not count current session in general online, because we are still deciding her fate
var currentOnline = _slotsManager.PlayersCount - 1; // Do not count current session in general online, because we are still deciding her fate
var haveFreeSlot = currentOnline < _cfg.GetCVar(CCVars.SoftMaxPlayers);
if (isPrivileged || haveFreeSlot)
{
SendToGame(e.Session);
if (isPrivileged && !haveFreeSlot)
QueueBypassCount.Inc();
@@ -114,10 +116,10 @@ public sealed class JoinQueueManager
/// <param name="connectedTime">Session connected time for histogram metrics</param>
private void ProcessQueue(bool isDisconnect, DateTime connectedTime)
{
var players = ActualPlayersCount;
var players = _slotsManager.PlayersCount;
if (isDisconnect)
players--; // Decrease currently disconnected session but that has not yet been deleted
var haveFreeSlot = players < _cfg.GetCVar(CCVars.SoftMaxPlayers);
var queueContains = _queue.Count > 0;
if (haveFreeSlot && queueContains)

View File

@@ -0,0 +1,74 @@
using System.Linq;
using Content.Server.Administration.Managers;
using Content.Server.Corvax.JoinQueue;
using Content.Server.Corvax.Sponsors;
using Content.Shared.Corvax.CCCVars;
using Robust.Server.Player;
using Robust.Shared.Configuration;
using Robust.Shared.Enums;
namespace Content.Server.Corvax.PlayerSlots;
public sealed class PlayerSlotsManager
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IAdminManager _adminManager = default!;
[Dependency] private readonly SponsorsManager _sponsorsManager = default!; // Corvax-Sponsors
[Dependency] private readonly JoinQueueManager _queueManager = default!; // Corvax-Queue
private bool _ignorePrivileged = false;
private int _privilegedCount = 0;
/// <summary>
/// Value that mostly must be used to determine total players
/// Ignores privileged if enabled
/// </summary>
public int PlayersCount
{
get
{
if (_ignorePrivileged)
return _playerManager.PlayerCount - _privilegedCount;
return _playerManager.PlayerCount;
}
}
/// <summary>
/// The actual number of players currently playing
/// Ignore only that stay in queue
/// </summary>
public int InGamePlayersCount => _playerManager.PlayerCount - _queueManager.PlayerInQueueCount;
/// <summary>
/// Online that must be visible publicly
/// Ignores privileged if enabled and in queue
/// </summary>
public int PublicPlayersCount => PlayersCount - _queueManager.PlayerInQueueCount;
public void Initialize()
{
_cfg.OnValueChanged(CCCVars.IgnorePrivileged, val => _ignorePrivileged = val, true);
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
}
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
_privilegedCount = _playerManager.Sessions
.Where(s =>
{
// Skip session that raised event if it disconnect, because it is still present in the total enumeration of all sessions
var isCurrentSession = s == e.Session;
var isDisconnectEvent = e.NewStatus == SessionStatus.Disconnected;
return !(isCurrentSession && isDisconnectEvent);
})
.Count(s =>
{
var isAdmin = _adminManager.IsAdmin((IPlayerSession)s);
var havePriorityJoin =
_sponsorsManager.TryGetInfo(s.UserId, out var sponsor) && sponsor.HavePriorityJoin;
return isAdmin || havePriorityJoin;
});
}
}

View File

@@ -6,6 +6,7 @@ using Content.Server.Afk;
using Content.Server.Chat.Managers;
using Content.Server.Connection;
using Content.Server.Corvax.JoinQueue;
using Content.Server.Corvax.PlayerSlots;
using Content.Server.Corvax.Sponsors;
using Content.Server.Corvax.TTS;
using Content.Server.Database;
@@ -112,6 +113,7 @@ namespace Content.Server.Entry
IoCManager.Resolve<SponsorsManager>().Initialize(); // Corvax-Sponsors
IoCManager.Resolve<JoinQueueManager>().Initialize(); // Corvax-Queue
IoCManager.Resolve<TTSManager>().Initialize(); // Corvax-TTS
IoCManager.Resolve<PlayerSlotsManager>().Initialize(); // Corvax-PlayerSlots
IoCManager.Resolve<ServerInfoManager>().Initialize();
_voteManager.Initialize();

View File

@@ -1,5 +1,5 @@
using System.Text.Json.Nodes;
using Content.Server.Corvax.JoinQueue;
using Content.Server.Corvax.PlayerSlots;
using Content.Shared.CCVar;
using Robust.Server.ServerStatus;
using Robust.Shared.Configuration;
@@ -23,7 +23,7 @@ namespace Content.Server.GameTicking
/// For access to CVars in status responses.
/// </summary>
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly JoinQueueManager _queueManager = default!; // Corvax-Queue
[Dependency] private readonly PlayerSlotsManager _slots = default!; // Corvax-PlayerSlots
private void InitializeStatusShell()
{
@@ -36,7 +36,7 @@ namespace Content.Server.GameTicking
lock (_statusShellLock)
{
jObject["name"] = _baseServer.ServerName;
jObject["players"] = _queueManager.ActualPlayersCount; // Corvax-Queue
jObject["players"] = _slots.PublicPlayersCount; // Corvax-PlayerSlots
jObject["soft_max_players"] = _cfg.GetCVar(CCVars.SoftMaxPlayers);
jObject["run_level"] = (int) _runLevel;
if (_runLevel >= GameRunLevel.InRound)

View File

@@ -6,6 +6,7 @@ using Content.Server.Afk;
using Content.Server.Chat.Managers;
using Content.Server.Connection;
using Content.Server.Corvax.JoinQueue;
using Content.Server.Corvax.PlayerSlots;
using Content.Server.Corvax.Sponsors;
using Content.Server.Corvax.TTS;
using Content.Server.Database;
@@ -62,6 +63,7 @@ namespace Content.Server.IoC
IoCManager.Register<SponsorsManager>(); // Corvax-Sponsors
IoCManager.Register<JoinQueueManager>(); // Corvax-Queue
IoCManager.Register<TTSManager>(); // Corvax-TTS
IoCManager.Register<PlayerSlotsManager>(); // Corvax-PlayerSlots
IoCManager.Register<ServerInfoManager>();
}
}

View File

@@ -50,7 +50,7 @@ public sealed class CCCVars
/// </summary>
public static readonly CVarDef<bool>
QueueEnabled = CVarDef.Create("queue.enabled", false, CVar.SERVERONLY);
/**
* TTS (Text-To-Speech)
*/
@@ -94,4 +94,15 @@ public sealed class CCCVars
/// </summary>
public static readonly CVarDef<bool> PeacefulRoundEnd =
CVarDef.Create("game.peaceful_end", true, CVar.SERVERONLY);
/*
* Player Slots
*/
/// <summary>
/// Do not count privileged (admins, sponsors) joined players when calculating total occupied slots.
/// This increases the chances of the average player getting on the server.
/// </summary>
public static readonly CVarDef<bool>
IgnorePrivileged = CVarDef.Create("game.ignore_privileged", false, CVar.SERVERONLY);
}