Files
tbd-station-14/Content.Client/Launcher/LauncherConnectingGui.xaml.cs
Pieter-Jan Briers 0ecc5e8c96 Throttle people trying to connect to a full server. (#20972)
* Throttle people trying to connect to a full server.

To reduce spam/load on the server and connection logs table.

Players are forced to wait 30 seconds after getting denied for "server full", before they can try connecting again.

This code is an absolute nightmare mess. I tried to re-use the existing code for the redial timer but god everything here sucks so much.

Requires https://github.com/space-wizards/RobustToolbox/pull/4487

* Use new NetDisconnectMessage API instead.

* Add admin.bypass_max_players CVar.

Just something to help with debugging the player cap on dev, I don't expect this to ever be disabled on real servers.
2024-03-14 19:00:47 +11:00

182 lines
5.9 KiB
C#

using System.Linq;
using Content.Client.Stylesheets;
using Content.Shared.CCVar;
using Content.Shared.Dataset;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
using Robust.Shared.Network;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Client.Launcher
{
[GenerateTypedNameReferences]
public sealed partial class LauncherConnectingGui : Control
{
private const float RedialWaitTimeSeconds = 15f;
private readonly LauncherConnecting _state;
private float _waitTime;
// Pressing reconnect will redial instead of simply reconnecting.
private bool _redial;
private readonly IRobustRandom _random;
private readonly IPrototypeManager _prototype;
private readonly IConfigurationManager _cfg;
public LauncherConnectingGui(LauncherConnecting state, IRobustRandom random,
IPrototypeManager prototype, IConfigurationManager config)
{
_state = state;
_random = random;
_prototype = prototype;
_cfg = config;
RobustXamlLoader.Load(this);
LayoutContainer.SetAnchorPreset(this, LayoutContainer.LayoutPreset.Wide);
Stylesheet = IoCManager.Resolve<IStylesheetManager>().SheetSpace;
ChangeLoginTip();
ReconnectButton.OnPressed += ReconnectButtonPressed;
RetryButton.OnPressed += ReconnectButtonPressed;
ExitButton.OnPressed += _ => _state.Exit();
var addr = state.Address;
if (addr != null)
ConnectingAddress.Text = addr;
state.PageChanged += OnPageChanged;
state.ConnectFailReasonChanged += ConnectFailReasonChanged;
state.ConnectionStateChanged += ConnectionStateChanged;
state.ConnectFailed += HandleDisconnectReason;
ConnectionStateChanged(state.ConnectionState);
// Redial flag setup
var edim = IoCManager.Resolve<ExtendedDisconnectInformationManager>();
edim.LastNetDisconnectedArgsChanged += LastNetDisconnectedArgsChanged;
LastNetDisconnectedArgsChanged(edim.LastNetDisconnectedArgs);
}
// Just button, there's only one at once anyways :)
private void ReconnectButtonPressed(BaseButton.ButtonEventArgs args)
{
if (_redial)
{
// Redial shouldn't fail, but if it does, try a reconnect (maybe we're being run from debug)
if (_state.Redial())
return;
}
_state.RetryConnect();
}
private void ConnectFailReasonChanged(string? reason)
{
ConnectFailReason.SetMessage(reason == null
? ""
: Loc.GetString("connecting-fail-reason", ("reason", reason)));
}
private void LastNetDisconnectedArgsChanged(NetDisconnectedArgs? args)
{
HandleDisconnectReason(args);
}
private void HandleDisconnectReason(INetStructuredReason? reason)
{
if (reason == null)
{
_waitTime = 0;
_redial = false;
}
else
{
_redial = reason.RedialFlag;
if (reason.Message.Int32Of("delay") is { } delay)
{
_waitTime = delay;
}
else if (_redial)
{
_waitTime = RedialWaitTimeSeconds;
}
}
}
private void ChangeLoginTip()
{
var tipsDataset = _cfg.GetCVar(CCVars.LoginTipsDataset);
var loginTipsEnabled = _prototype.TryIndex<DatasetPrototype>(tipsDataset, out var tips);
LoginTips.Visible = loginTipsEnabled;
if (!loginTipsEnabled)
{
return;
}
var tipList = tips!.Values;
if (tipList.Count == 0)
return;
var randomIndex = _random.Next(tipList.Count);
var tip = tipList[randomIndex];
LoginTip.SetMessage(tip);
LoginTipTitle.Text = Loc.GetString("connecting-window-tip", ("numberTip", randomIndex));
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
var button = _state.CurrentPage == LauncherConnecting.Page.ConnectFailed
? RetryButton
: ReconnectButton;
_waitTime -= args.DeltaSeconds;
if (_waitTime <= 0)
{
button.Disabled = false;
var key = _redial
? "connecting-redial"
: _state.CurrentPage == LauncherConnecting.Page.ConnectFailed
? "connecting-reconnect"
: "connecting-retry";
button.Text = Loc.GetString(key);
}
else
{
button.Disabled = true;
button.Text = Loc.GetString("connecting-redial-wait", ("time", _waitTime.ToString("00.000")));
}
}
private void OnPageChanged(LauncherConnecting.Page page)
{
ConnectingStatus.Visible = page == LauncherConnecting.Page.Connecting;
ConnectFail.Visible = page == LauncherConnecting.Page.ConnectFailed;
Disconnected.Visible = page == LauncherConnecting.Page.Disconnected;
if (page == LauncherConnecting.Page.Disconnected)
DisconnectReason.Text = _state.LastDisconnectReason;
}
private void ConnectionStateChanged(ClientConnectionState state)
{
ConnectStatus.Text = Loc.GetString($"connecting-state-{state}");
}
}
}