Files
OldThink/Content.Server/_White/AuthPanel/AuthPanelSystem.cs
BIGZi0348 dc570dcdb5 temp
2024-12-25 11:44:25 +03:00

218 lines
7.3 KiB
C#

using Content.Server.GameTicking;
using Content.Server.Popups;
using Content.Server.Station.Systems;
using Content.Server._White.ERTRecruitment;
using Content.Server._White.JoinQueue;
using Content.Shared.Access.Systems;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.GameTicking;
using Content.Shared._White.AuthPanel;
using Content.Shared._White.GhostRecruitment;
using Content.Shared.Ghost;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Random;
using Robust.Shared.Timing;
using Robust.Shared.Player;
namespace Content.Server._White.AuthPanel;
public sealed class AuthPanelSystem : EntitySystem
{
[Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly AccessReaderSystem _access = default!;
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly ERTRecruitmentRule _ert = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly GameTicker _ticker = default!;
public Dictionary<AuthPanelAction, HashSet<EntityUid>> Counter = new();
public Dictionary<AuthPanelAction, HashSet<int>> CardIndexes = new();
public string Reason = "";
public static int MaxCount = 1;
public static int DelayNextAction = 10;
public static int EarliestStart = 45;
private TimeSpan? _delay;
public override void Initialize()
{
SubscribeLocalEvent<AuthPanelComponent, AuthPanelButtonPressedMessage>(OnButtonPressed);
SubscribeLocalEvent<AuthPanelComponent, AuthPanelPerformActionEvent>(OnPerformAction);
SubscribeLocalEvent<RecruitedComponent, ERTRecruitedReasonEvent>(OnReason);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRestart);
}
private void OnReason(EntityUid uid, RecruitedComponent component, ERTRecruitedReasonEvent args)
{
args.Reason = Reason;
}
private void OnRestart(RoundRestartCleanupEvent ev)
{
ClearPanel();
}
private void ClearPanel()
{
Counter.Clear();
CardIndexes.Clear();
_delay = null;
}
private void OnPerformAction(EntityUid uid, AuthPanelComponent component, AuthPanelPerformActionEvent args)
{
if (args.Action is AuthPanelAction.ERTRecruit)
{
var query = EntityQueryEnumerator<GhostComponent, ActorComponent>();
var ghostList = new List<EntityUid>();
while (query.MoveNext(out var ghost, out _, out _))
{
ghostList.Add(ghost);
}
// if (_ticker.RoundDuration() < TimeSpan.FromMinutes(EarliestStart))
// {
// var station = _station.GetStationInMap(Transform(uid).MapID);
// if (station != null)
// _ert.DeclineERT(station.Value);
// _adminLogger.Add(LogType.EventStarted, LogImpact.High, $"ERT Declined - Not enough time passed");
// return;
// }
var playerCount = _playerManager.PlayerCount;
//if (playerCount - ghostList.Count > playerCount / 2 && ghostList.Count > 3)
if (true)
{
_gameTicker.AddGameRule(ERTRecruitmentRuleComponent.EventName);
}
else
{
var station = _station.GetStationInMap(Transform(uid).MapID);
if (station != null)
_ert.DeclineERT(station.Value);
_adminLogger.Add(LogType.EventStarted, LogImpact.High, $"ERT Declined - Not enough ghosts");
}
foreach (var entities in Counter.Values)
{
foreach (var entity in entities)
{
_adminLogger.Add(LogType.EventStarted, LogImpact.High,
$"{ToPrettyString(entity):player} just called ERT. Reason: {Reason}");
}
}
}
Timer.Spawn(TimeSpan.FromSeconds(DelayNextAction), () => ClearPanel());
}
private void OnButtonPressed(EntityUid uid, AuthPanelComponent component, AuthPanelButtonPressedMessage args)
{
var access = _access.FindAccessTags(args.Actor);
if (!access.Contains("Command"))
{
_popup.PopupEntity(Loc.GetString("auth-panel-no-access"),
args.Actor, args.Actor);
return;
}
if (string.IsNullOrEmpty(args.Reason))
{
_popup.PopupEntity(Loc.GetString("auth-panel-no-reason"),
args.Actor, args.Actor);
return;
}
if (_delay != null)
{
_popup.PopupEntity(Loc.GetString("auth-panel-wait"),
args.Actor, args.Actor);
return;
}
if (!Counter.TryGetValue(args.Button, out var hashSet))
{
hashSet = new HashSet<EntityUid>();
Counter.Add(args.Button, hashSet);
}
if (hashSet.Count == MaxCount)
return;
if (!CardIndexes.TryGetValue(args.Button, out var cardSet))
{
cardSet = new HashSet<int>();
CardIndexes.Add(args.Button, cardSet);
}
if (cardSet.Contains(access.Count))
{
_popup.PopupEntity(Loc.GetString("auth-panel-used-ID"),
args.Actor, args.Actor);
return;
}
if (!hashSet.Add(args.Actor))
{
_popup.PopupEntity(Loc.GetString("auth-panel-pressed"),
args.Actor, args.Actor);
return;
}
cardSet.Add(access.Count);
_delay = _timing.CurTime + TimeSpan.FromSeconds(5);
Reason = args.Reason;
UpdateUserInterface(args.Button);
_adminLogger.Add(LogType.EventStarted, LogImpact.High, $"{ToPrettyString(args.Actor):player} vote for {args.Button}. Reason: {Reason}");
if (hashSet.Count == MaxCount)
{
var ev = new AuthPanelPerformActionEvent(args.Button);
RaiseLocalEvent(uid, ev);
}
}
public void UpdateUserInterface(AuthPanelAction rawaction)
{
if (!Counter.TryGetValue(rawaction, out var hashSet))
return;
var action = new AuthPanelConfirmationAction(rawaction, hashSet.Count, MaxCount, Reason);
var query = EntityQueryEnumerator<AuthPanelComponent>();
while (query.MoveNext(out var uid, out _))
{
if (!_ui.HasUi(uid, AuthPanelUiKey.Key))
return;
var state = new AuthPanelConfirmationActionState(action);
_ui.SetUiState(uid, AuthPanelUiKey.Key, state);
_appearance.SetData(uid, AuthPanelVisualLayers.Confirm, true);
}
}
public override void Update(float frameTime)
{
if (_delay == null)
return;
if (_timing.CurTime >= _delay)
{
_delay = null;
}
}
}