fish-station/Content.Client/Administration/UI/BanPanel/BanPanel.xaml.cs
Vigers Ray 2da8b9f545 Merge remote-tracking branch 'space-wizards/master'
# Conflicts:
#	.github/CODEOWNERS
#	.github/PULL_REQUEST_TEMPLATE.md
#	Content.Client/Changelog/ChangelogTab.xaml.cs
#	Content.Client/Chemistry/UI/ChemMasterWindow.xaml
#	Content.Client/Entry/EntryPoint.cs
#	Content.Client/GameTicking/Managers/ClientGameTicker.cs
#	Content.Client/Launcher/LauncherConnectingGui.xaml
#	Content.Client/Lobby/LobbyState.cs
#	Content.Client/Lobby/UI/LobbyGui.xaml
#	Content.Client/Paper/UI/PaperWindow.xaml.cs
#	Content.Client/StationRecords/GeneralStationRecordConsoleWindow.xaml.cs
#	Content.Client/Stylesheets/StyleNano.cs
#	Content.Client/UserInterface/Screens/SeparatedChatGameScreen.xaml
#	Content.Client/UserInterface/Systems/Ghost/Widgets/GhostGui.xaml.cs
#	Content.Client/UserInterface/Systems/MenuBar/Widgets/GameTopMenuBar.xaml
#	Content.IntegrationTests/Tests/Destructible/DestructibleDamageGroupTest.cs
#	Content.IntegrationTests/Tests/PostMapInitTest.cs
#	Content.IntegrationTests/Tests/Power/StationPowerTests.cs
#	Content.Packaging/ServerPackaging.cs
#	Content.Server/Administration/Systems/AdminVerbSystem.Smites.cs
#	Content.Server/Administration/Systems/AdminVerbSystem.Tools.cs
#	Content.Server/Atmos/TileAtmosphere.cs
#	Content.Server/Bible/BibleSystem.cs
#	Content.Server/Chat/Systems/ChatSystem.cs
#	Content.Server/Content.Server.csproj
#	Content.Server/Discord/DiscordLink/DiscordLink.cs
#	Content.Server/GameTicking/GameTicker.LobbyBackground.cs
#	Content.Server/GameTicking/GameTicker.Spawning.cs
#	Content.Server/Ghost/GhostSystem.cs
#	Content.Server/Kitchen/Components/MicrowaveComponent.cs
#	Content.Server/Kitchen/EntitySystems/MicrowaveSystem.cs
#	Content.Server/Mech/Systems/MechSystem.cs
#	Content.Server/Medical/CrewMonitoring/CrewMonitoringConsoleSystem.cs
#	Content.Server/Medical/DefibrillatorSystem.cs
#	Content.Server/Medical/HealthAnalyzerSystem.cs
#	Content.Server/Radio/EntitySystems/HeadsetSystem.cs
#	Content.Server/Remotes/DoorRemoteSystem.cs
#	Content.Server/RoundEnd/RoundEndSystem.cs
#	Content.Server/Shuttles/Systems/DockingSystem.Shuttle.cs
#	Content.Server/Silicons/Borgs/BorgSystem.Transponder.cs
#	Content.Server/Tips/TipsSystem.cs
#	Content.Server/Traitor/Uplink/UplinkSystem.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.cs
#	Content.Server/Zombies/ZombieSystem.Transform.cs
#	Content.Server/Zombies/ZombieSystem.cs
#	Content.Shared/Bed/BedSystem.cs
#	Content.Shared/Blocking/BlockingSystem.User.cs
#	Content.Shared/Buckle/SharedBuckleSystem.Buckle.cs
#	Content.Shared/Chat/SharedSuicideSystem.cs
#	Content.Shared/Chemistry/Components/HyposprayComponent.cs
#	Content.Shared/Chemistry/EntitySystems/HypospraySystem.cs
#	Content.Shared/Chemistry/EntitySystems/ScoopableSolutionSystem.cs
#	Content.Shared/Chemistry/EntitySystems/SolutionTransferSystem.cs
#	Content.Shared/Chemistry/Events/HyposprayEvents.cs
#	Content.Shared/Content.Shared.csproj
#	Content.Shared/Damage/Systems/DamageableSystem.cs
#	Content.Shared/Friction/TileFrictionController.cs
#	Content.Shared/GameTicking/SharedGameTicker.cs
#	Content.Shared/Interaction/InteractHand.cs
#	Content.Shared/Lock/LockSystem.cs
#	Content.Shared/Medical/Healing/HealingSystem.cs
#	Content.Shared/Mobs/Systems/MobStateSystem.Subscribers.cs
#	Content.Shared/Nutrition/EntitySystems/HungerSystem.cs
#	Content.Shared/Silicons/StationAi/SharedStationAiSystem.Airlock.cs
#	Content.Shared/Throwing/ThrowingSystem.cs
#	Content.Shared/Trigger/Systems/TriggerOnHitscanSystem.cs
#	Content.Shared/Weapons/Hitscan/Systems/HitscanBasicDamageSystem.cs
#	Content.Shared/Weapons/Melee/SharedMeleeWeaponSystem.cs
#	Content.Shared/Weapons/Ranged/Components/BatteryWeaponFireModesComponent.cs
#	Content.Shared/Weapons/Ranged/Components/HitscanBatteryAmmoProviderComponent.cs
#	Content.Shared/Weapons/Ranged/Systems/BatteryWeaponFireModesSystem.cs
#	Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.Ballistic.cs
#	Content.Shared/Weapons/Ranged/Systems/SharedGunSystem.Battery.cs
#	Content.Shared/Zombies/ZombieComponent.cs
#	Content.Shared/_Sunrise/Lobby/LobbyBackgroundPrototype.cs
#	README.md
#	Resources/Locale/en-US/_strings/borg/borg.ftl
#	Resources/Locale/en-US/_strings/chemistry/components/hypospray-component.ftl
#	Resources/Locale/en-US/_strings/chemistry/components/injector-component.ftl
#	Resources/Locale/en-US/_strings/commands/pauseatmos-command.ftl
#	Resources/Locale/en-US/_strings/commands/quick-inspect-command.ftl
#	Resources/Locale/en-US/_strings/commands/show-wallmounts-command.ftl
#	Resources/Locale/en-US/_strings/commands/substepatmos-command.ftl
#	Resources/Locale/en-US/_strings/fluids/components/equip-spray-component.ftl
#	Resources/Locale/en-US/_strings/game-ticking/game-presets/preset-revolutionary.ftl
#	Resources/Locale/en-US/_strings/job/job-names.ftl
#	Resources/Locale/en-US/_strings/lobby/lobby-state-background.ftl
#	Resources/Locale/en-US/_strings/reagents/meta/medicine.ftl
#	Resources/Locale/en-US/_strings/reagents/meta/physical-desc.ftl
#	Resources/Locale/en-US/_strings/silicons/borg-module.ftl
#	Resources/Locale/en-US/_strings/store/changeling-catalog.ftl
#	Resources/Locale/en-US/_strings/store/currency.ftl
#	Resources/Locale/en-US/_strings/store/store.ftl
#	Resources/Locale/en-US/_strings/store/uplink-catalog.ftl
#	Resources/Prototypes/Body/Organs/slime.yml
#	Resources/Prototypes/Catalog/Fills/Backpacks/backpack.yml
#	Resources/Prototypes/Catalog/Fills/Backpacks/duffelbag.yml
#	Resources/Prototypes/Catalog/Fills/Boxes/ammunition.yml
#	Resources/Prototypes/Catalog/Fills/Boxes/emergency.yml
#	Resources/Prototypes/Catalog/Fills/Crates/antag.yml
#	Resources/Prototypes/Catalog/Fills/Crates/armory.yml
#	Resources/Prototypes/Catalog/Fills/Crates/botany.yml
#	Resources/Prototypes/Catalog/Fills/Crates/fun.yml
#	Resources/Prototypes/Catalog/Fills/Crates/medical.yml
#	Resources/Prototypes/Catalog/Fills/Crates/salvage.yml
#	Resources/Prototypes/Catalog/Fills/Crates/security.yml
#	Resources/Prototypes/Catalog/Fills/Crates/service.yml
#	Resources/Prototypes/Catalog/Fills/Crates/syndicate.yml
#	Resources/Prototypes/Catalog/Fills/Items/belt.yml
#	Resources/Prototypes/Catalog/Fills/Items/briefcases.yml
#	Resources/Prototypes/Catalog/Fills/Items/firstaidkits.yml
#	Resources/Prototypes/Catalog/Fills/Items/toolboxes.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/dressers.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/heads.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/security.yml
#	Resources/Prototypes/Catalog/VendingMachines/Inventories/theater.yml
#	Resources/Prototypes/Catalog/uplink_catalog.yml
#	Resources/Prototypes/Entities/Clothing/Back/backpacks.yml
#	Resources/Prototypes/Entities/Clothing/Shoes/magboots.yml
#	Resources/Prototypes/Entities/Debugging/debug_sweps.yml
#	Resources/Prototypes/Entities/Debugging/stress_test.yml
#	Resources/Prototypes/Entities/Markers/Spawners/Random/anomaly.yml
#	Resources/Prototypes/Entities/Markers/Spawners/Random/toy.yml
#	Resources/Prototypes/Entities/Mobs/Cyborgs/base_borg_chassis.yml
#	Resources/Prototypes/Entities/Mobs/Cyborgs/xenoborgs.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/animals.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/carp.yml
#	Resources/Prototypes/Entities/Objects/Devices/base_handheld.yml
#	Resources/Prototypes/Entities/Objects/Fun/Instruments/instruments_misc.yml
#	Resources/Prototypes/Entities/Objects/Fun/dice_bag.yml
#	Resources/Prototypes/Entities/Objects/Fun/snap_pops.yml
#	Resources/Prototypes/Entities/Objects/Materials/Sheets/other.yml
#	Resources/Prototypes/Entities/Objects/Materials/parts.yml
#	Resources/Prototypes/Entities/Objects/Misc/identification_cards.yml
#	Resources/Prototypes/Entities/Objects/Misc/subdermal_implants.yml
#	Resources/Prototypes/Entities/Objects/Shields/shields.yml
#	Resources/Prototypes/Entities/Objects/Specific/Chapel/bibles.yml
#	Resources/Prototypes/Entities/Objects/Specific/Janitorial/janitor.yml
#	Resources/Prototypes/Entities/Objects/Specific/Medical/hypospray.yml
#	Resources/Prototypes/Entities/Objects/Specific/Medical/surgery.yml
#	Resources/Prototypes/Entities/Objects/Specific/Robotics/borg_modules.yml
#	Resources/Prototypes/Entities/Objects/Specific/Robotics/mmi.yml
#	Resources/Prototypes/Entities/Objects/Specific/Service/barber.yml
#	Resources/Prototypes/Entities/Objects/Tools/glowstick.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Boxes/antimateriel.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Boxes/magnum.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/heavy_rifle.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/light_rifle.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/magnum.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/pistol.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/rifle.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Cartridges/shotgun.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/light_rifle.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/magnum.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/pistol.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Magazines/rifle.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/grenade.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/SpeedLoaders/magnum.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/SpeedLoaders/pistol.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/SpeedLoaders/rifle_light.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/explosives.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Battery/battery_guns.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/LMGs/lmgs.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Pistols/pistols.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/hitscan.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Rifles/rifles.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/SMGs/smgs.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/knife.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Throwable/grenades.yml
#	Resources/Prototypes/Entities/Structures/Furniture/dresser.yml
#	Resources/Prototypes/Entities/Structures/Machines/lathe.yml
#	Resources/Prototypes/Entities/Structures/Shuttles/cannons.yml
#	Resources/Prototypes/Entities/Structures/Walls/walls.yml
#	Resources/Prototypes/GameRules/roundstart.yml
#	Resources/Prototypes/GameRules/unknown_shuttles.yml
#	Resources/Prototypes/InventoryTemplates/aghost_inventory_template.yml
#	Resources/Prototypes/Loadouts/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Loadouts/loadout_groups.yml
#	Resources/Prototypes/Loadouts/role_loadouts.yml
#	Resources/Prototypes/Maps/Pools/default.yml
#	Resources/Prototypes/Maps/amber.yml
#	Resources/Prototypes/Maps/oasis.yml
#	Resources/Prototypes/Maps/reach.yml
#	Resources/Prototypes/Objectives/objectiveGroups.yml
#	Resources/Prototypes/Reagents/Consumable/Drink/alcohol.yml
#	Resources/Prototypes/Reagents/medicine.yml
#	Resources/Prototypes/Reagents/narcotics.yml
#	Resources/Prototypes/Reagents/toxins.yml
#	Resources/Prototypes/Recipes/Reactions/medicine.yml
#	Resources/Prototypes/Roles/Jobs/Security/head_of_security.yml
#	Resources/Prototypes/Roles/Jobs/Security/security_cadet.yml
#	Resources/Prototypes/Roles/Jobs/Security/security_officer.yml
#	Resources/Prototypes/Roles/Jobs/Security/warden.yml
#	Resources/Prototypes/Roles/Jobs/Wildcards/zookeeper.yml
#	Resources/Prototypes/Roles/play_time_trackers.yml
#	Resources/Prototypes/Shuttles/shuttle_incoming_event.yml
#	Resources/Prototypes/Species/species_weights.yml
#	Resources/Prototypes/Store/presets.yml
#	Resources/Prototypes/ai_factions.yml
#	Resources/Prototypes/borg_types.yml
#	Resources/Prototypes/explosion.yml
#	Resources/Prototypes/game_presets.yml
#	Resources/Prototypes/lobbyscreens.yml
#	Resources/Prototypes/silicon-laws.yml
#	Resources/Textures/Clothing/Back/Duffels/syndicate.rsi/icon-med.png
#	Resources/Textures/Clothing/Back/Duffels/syndicate.rsi/meta.json
#	Resources/Textures/Objects/Specific/Mech/mecha.rsi/meta.json
#	Resources/Textures/Objects/Specific/Mech/mecha.rsi/ripley-broken.png
#	Resources/Textures/Objects/Specific/Mech/mecha.rsi/ripley-empty.png
#	Resources/Textures/Objects/Specific/Mech/mecha.rsi/ripley-open.png
#	Resources/Textures/Objects/Specific/Mech/mecha.rsi/ripley.png
#	Resources/Textures/Objects/Specific/Mech/ripley_construction.rsi/ripley_chassis.png
#	Resources/Textures/Objects/Specific/Mech/ripley_construction.rsi/ripley_harness+o.png
#	Resources/Textures/Objects/Specific/Mech/ripley_construction.rsi/ripley_harness.png
#	Resources/Textures/Objects/Specific/Medical/hypospray.rsi/meta.json
#	Resources/Textures/Objects/Specific/Medical/medical.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Guns/Projectiles/projectiles.rsi/meta.json
#	Resources/Textures/Objects/Weapons/Guns/Projectiles/projectiles2.rsi/meta.json
#	Resources/Textures/Structures/Storage/canister.rsi/meta.json
#	Resources/Textures/Structures/Storage/tanks.rsi/meta.json
#	Resources/migration.yml
#	RobustToolbox
#	SECURITY.md
#	SpaceStation14.sln
2025-12-26 04:19:27 +01:00

671 lines
24 KiB
C#

using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Numerics;
using Content.Client.Administration.UI.CustomControls;
using Content.Shared.Administration;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Content.Shared.Roles;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Administration.UI.BanPanel;
[GenerateTypedNameReferences]
public sealed partial class BanPanel : DefaultWindow
{
public event Action<Ban>? BanSubmitted;
public event Action<string>? PlayerChanged;
private string? PlayerUsername { get; set; }
private (IPAddress, int)? IpAddress { get; set; }
private ImmutableTypedHwid? Hwid { get; set; }
private double TimeEntered { get; set; }
private uint Multiplier { get; set; }
private bool HasBanFlag { get; set; }
private TimeSpan? ButtonResetOn { get; set; }
// This is less efficient than just holding a reference to the root control and enumerating children, but you
// have to know how the controls are nested, which makes the code more complicated.
// Role group name -> the role buttons themselves.
private readonly Dictionary<string, List<(Button, IPrototype)>> _roleCheckboxes = new();
private readonly ISawmill _banPanelSawmill;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly ILogManager _logManager = default!;
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
private const string ExpandedArrow = "▼";
private const string ContractedArrow = "▶";
private readonly string _antagCategory = Loc.GetString("ban-panel-role-selection-antag");
private readonly string _antagAllSelection = Loc.GetString("ban-panel-role-selection-antag-all-option");
private enum TabNumbers
{
BasicInfo,
//Text,
Players,
Roles
}
private enum Multipliers
{
Minutes,
Hours,
Days,
Weeks,
Months,
Years,
Permanent
}
private enum Types
{
None,
Server,
Role
}
public BanPanel()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_banPanelSawmill = _logManager.GetSawmill("admin.banpanel");
PlayerList.OnSelectionChanged += OnPlayerSelectionChanged;
PlayerNameLine.OnFocusExit += _ => OnPlayerNameChanged();
PlayerCheckbox.OnPressed += _ =>
{
PlayerNameLine.Editable = PlayerCheckbox.Pressed;
PlayerNameLine.ModulateSelfOverride = null;
};
TimeLine.OnTextChanged += OnMinutesChanged;
MultiplierOption.OnItemSelected += args =>
{
MultiplierOption.SelectId(args.Id);
OnMultiplierChanged();
};
IpLine.OnFocusExit += _ => OnIpChanged();
IpCheckbox.OnPressed += _ =>
{
IpLine.Editable = IpCheckbox.Pressed;
OnIpChanged();
};
HwidLine.OnFocusExit += _ => OnHwidChanged();
HwidCheckbox.OnPressed += _ =>
{
HwidLine.Editable = HwidCheckbox.Pressed;
OnHwidChanged();
};
TypeOption.OnItemSelected += args =>
{
TypeOption.SelectId(args.Id);
OnTypeChanged();
};
LastConnCheckbox.OnPressed += _ =>
{
IpLine.ModulateSelfOverride = null;
HwidLine.ModulateSelfOverride = null;
OnIpChanged();
OnHwidChanged();
};
SubmitButton.OnPressed += SubmitButtonOnOnPressed;
IpCheckbox.Pressed = _cfg.GetCVar(CCVars.ServerBanIpBanDefault);
HwidCheckbox.Pressed = _cfg.GetCVar(CCVars.ServerBanHwidBanDefault);
LastConnCheckbox.Pressed = _cfg.GetCVar(CCVars.ServerBanUseLastDetails);
EraseCheckbox.Pressed = _cfg.GetCVar(CCVars.ServerBanErasePlayer);
SeverityOption.AddItem(Loc.GetString("admin-note-editor-severity-none"), (int) NoteSeverity.None);
SeverityOption.AddItem(Loc.GetString("admin-note-editor-severity-low"), (int) NoteSeverity.Minor);
SeverityOption.AddItem(Loc.GetString("admin-note-editor-severity-medium"), (int) NoteSeverity.Medium);
SeverityOption.AddItem(Loc.GetString("admin-note-editor-severity-high"), (int) NoteSeverity.High);
SeverityOption.SelectId((int) NoteSeverity.Medium);
SeverityOption.OnItemSelected += args => SeverityOption.SelectId(args.Id);
MultiplierOption.AddItem(Loc.GetString("ban-panel-minutes"), (int) Multipliers.Minutes);
MultiplierOption.AddItem(Loc.GetString("ban-panel-hours"), (int) Multipliers.Hours);
MultiplierOption.AddItem(Loc.GetString("ban-panel-days"), (int) Multipliers.Days);
MultiplierOption.AddItem(Loc.GetString("ban-panel-weeks"), (int) Multipliers.Weeks);
MultiplierOption.AddItem(Loc.GetString("ban-panel-months"), (int) Multipliers.Months);
MultiplierOption.AddItem(Loc.GetString("ban-panel-years"), (int) Multipliers.Years);
MultiplierOption.AddItem(Loc.GetString("ban-panel-permanent"), (int) Multipliers.Permanent);
MultiplierOption.SelectId((int) Multipliers.Minutes);
OnMultiplierChanged();
Tabs.SetTabTitle((int) TabNumbers.BasicInfo, Loc.GetString("ban-panel-tabs-basic"));
//Tabs.SetTabTitle((int) TabNumbers.Text, Loc.GetString("ban-panel-tabs-reason"));
Tabs.SetTabTitle((int) TabNumbers.Players, Loc.GetString("ban-panel-tabs-players"));
Tabs.SetTabTitle((int) TabNumbers.Roles, Loc.GetString("ban-panel-tabs-role"));
Tabs.SetTabVisible((int) TabNumbers.Roles, false);
TypeOption.AddItem(Loc.GetString("ban-panel-select"), (int) Types.None);
TypeOption.AddItem(Loc.GetString("ban-panel-server"), (int) Types.Server);
TypeOption.AddItem(Loc.GetString("ban-panel-role"), (int) Types.Role);
ReasonTextEdit.Placeholder = new Rope.Leaf(Loc.GetString("ban-panel-reason"));
var departmentJobs = _protoMan.EnumeratePrototypes<DepartmentPrototype>()
.OrderBy(x => x.Weight);
foreach (var proto in departmentJobs)
{
var roles = proto.Roles.Select(x => _protoMan.Index(x))
.OrderBy(x => x.ID);
CreateRoleGroup(proto.ID, proto.Color, roles);
}
var antagRoles = _protoMan.EnumeratePrototypes<AntagPrototype>()
.OrderBy(x => x.ID);
CreateRoleGroup(AntagPrototype.GroupName, AntagPrototype.GroupColor, antagRoles);
}
/// <summary>
/// Creates a "Role group" which stores information and logic for one "group" of roll bans.
/// For example, all antags are one group, logi is a group, medical is a group, etc...
/// </summary>
private void CreateRoleGroup<T>(string groupName, Color color, IEnumerable<T> roles) where T : class, IPrototype
{
var outerContainer = new BoxContainer
{
Name = $"{groupName}GroupOuterBox",
HorizontalExpand = true,
VerticalExpand = true,
Orientation = BoxContainer.LayoutOrientation.Vertical,
Margin = new Thickness(4),
};
// Stores stuff like ban all and expand buttons.
var roleGroupHeader = new BoxContainer
{
Orientation = BoxContainer.LayoutOrientation.Horizontal,
};
// Stores the role checkboxes themselves.
var innerContainer = new GridContainer
{
Name = $"{groupName}GroupInnerBox",
HorizontalExpand = true,
Columns = 2,
Visible = false,
Margin = new Thickness(15, 5, 0, 5),
};
var roleGroupCheckbox = CreateRoleGroupHeader(groupName, roleGroupHeader, color, innerContainer);
outerContainer.AddChild(roleGroupHeader);
// Add the roles themselves
foreach (var role in roles)
{
AddRoleCheckbox(groupName, role.ID, innerContainer, roleGroupCheckbox);
}
outerContainer.AddChild(innerContainer);
RolesContainer.AddChild(new PanelContainer
{
PanelOverride = new StyleBoxFlat
{
BackgroundColor = color
}
});
RolesContainer.AddChild(outerContainer);
RolesContainer.AddChild(new HSeparator());
}
private Button CreateRoleGroupHeader(string groupName, BoxContainer header, Color color, GridContainer innerContainer)
{
var roleGroupCheckbox = new Button
{
Name = $"{groupName}GroupCheckbox",
Text = Loc.GetString("role-bans-ban-group"),
Margin = new Thickness(0, 0, 5, 0),
ToggleMode = true,
};
// When this is toggled, toggle all buttons in this group so they match.
roleGroupCheckbox.OnToggled += args =>
{
foreach (var role in _roleCheckboxes[groupName])
{
role.Item1.Pressed = args.Pressed;
}
if (args.Pressed)
{
if (!Enum.TryParse(_cfg.GetCVar(CCVars.DepartmentBanDefaultSeverity), true, out NoteSeverity newSeverity))
{
_banPanelSawmill
.Warning("Departmental role ban severity could not be parsed from config!");
return;
}
SeverityOption.SelectId((int)newSeverity);
}
else
{
foreach (var roleButtons in _roleCheckboxes.Values)
{
foreach (var button in roleButtons)
{
if (button.Item1.Pressed)
return;
}
}
if (!Enum.TryParse(_cfg.GetCVar(CCVars.RoleBanDefaultSeverity), true, out NoteSeverity newSeverity))
{
_banPanelSawmill
.Warning("Role ban severity could not be parsed from config!");
return;
}
SeverityOption.SelectId((int)newSeverity);
}
};
var hideButton = new Button
{
Text = Loc.GetString("role-bans-expand-roles") + " " + ContractedArrow,
ToggleMode = true,
};
hideButton.OnPressed += args =>
{
innerContainer.Visible = args.Button.Pressed;
((Button)args.Button).Text = args.Button.Pressed
? Loc.GetString("role-bans-contract-roles") + " " + ExpandedArrow
: Loc.GetString("role-bans-expand-roles") + " " + ContractedArrow;
};
header.AddChild(new Label
{
Text = groupName,
Modulate = color,
Margin = new Thickness(0, 0, 5, 0),
});
header.AddChild(roleGroupCheckbox);
header.AddChild(hideButton);
return roleGroupCheckbox;
}
/// <summary>
/// Adds a toggle button specifically for one "role" in a "group"
/// E.g. it would add the Chief Medical Officer "role" into the "Medical" group.
/// </summary>
private void AddRoleCheckbox(string group, string role, GridContainer roleGroupInnerContainer, Button roleGroupCheckbox)
{
var roleCheckboxContainer = new BoxContainer();
var roleToggleButton = new Button
{
Name = role,
Text = role,
ToggleMode = true,
};
roleToggleButton.OnToggled += args =>
{
// Checks the role group checkbox if all the children are pressed
if (args.Pressed && _roleCheckboxes[group].All(e => e.Item1.Pressed))
roleGroupCheckbox.Pressed = args.Pressed;
else
roleGroupCheckbox.Pressed = false;
};
IPrototype rolePrototype;
if (_protoMan.TryIndex<JobPrototype>(role, out var jobPrototype))
rolePrototype = jobPrototype;
else if (_protoMan.TryIndex<AntagPrototype>(role, out var antagPrototype))
rolePrototype = antagPrototype;
else
{
_banPanelSawmill.Error($"Adding a role checkbox for role {role}: role is not a JobPrototype or AntagPrototype.");
return;
}
// This is adding the icon before the role name
// TODO: This should not be using raw strings for prototypes as it means it won't be validated at all.
// // I know the ban manager is doing the same thing, but that should not leak into UI code.
if (jobPrototype is not null && _protoMan.TryIndex(jobPrototype.Icon, out var iconProto))
{
var jobIconTexture = new TextureRect
{
Texture = _entMan.System<SpriteSystem>().Frame0(iconProto.Icon),
TextureScale = new Vector2(2.5f, 2.5f),
Stretch = TextureRect.StretchMode.KeepCentered,
Margin = new Thickness(5, 0, 0, 0),
};
roleCheckboxContainer.AddChild(jobIconTexture);
}
roleCheckboxContainer.AddChild(roleToggleButton);
roleGroupInnerContainer.AddChild(roleCheckboxContainer);
_roleCheckboxes.TryAdd(group, []);
_roleCheckboxes[group].Add((roleToggleButton, rolePrototype));
}
public void UpdateBanFlag(bool newFlag)
{
HasBanFlag = newFlag;
SubmitButton.Visible = HasBanFlag;
ModulateSelfOverride = HasBanFlag ? Color.Red : null;
}
public void UpdatePlayerData(string playerName)
{
if (string.IsNullOrEmpty(playerName))
{
PlayerNameLine.ModulateSelfOverride = Color.Red;
ErrorLevel |= ErrorLevelEnum.PlayerName;
UpdateSubmitEnabled();
return;
}
PlayerNameLine.ModulateSelfOverride = null;
ErrorLevel &= ~ErrorLevelEnum.PlayerName;
UpdateSubmitEnabled();
PlayerUsername = playerName;
PlayerNameLine.Text = playerName;
}
[Flags]
private enum ErrorLevelEnum : byte
{
None = 0,
Minutes = 1 << 0,
PlayerName = 1 << 1,
IpAddress = 1 << 2,
Hwid = 1 << 3,
}
private ErrorLevelEnum ErrorLevel { get; set; }
private void OnMinutesChanged(LineEdit.LineEditEventArgs args)
{
TimeLine.Text = args.Text;
if (!double.TryParse(args.Text, out var result))
{
ExpiresLabel.Text = Loc.GetString("ban-panel-expiry-error");
ErrorLevel |= ErrorLevelEnum.Minutes;
TimeLine.ModulateSelfOverride = Color.Red;
UpdateSubmitEnabled();
return;
}
ErrorLevel &= ~ErrorLevelEnum.Minutes;
TimeLine.ModulateSelfOverride = null;
TimeEntered = result;
UpdateSubmitEnabled();
UpdateExpiresLabel();
}
private void OnMultiplierChanged()
{
TimeLine.Editable = MultiplierOption.SelectedId != (int) Multipliers.Permanent;
Multiplier = MultiplierOption.SelectedId switch
{
(int) Multipliers.Minutes => 1,
(int) Multipliers.Hours => 60,
(int) Multipliers.Days => 60 * 24,
(int) Multipliers.Weeks => 60 * 24 * 7,
(int) Multipliers.Months => 60 * 24 * 30,
(int) Multipliers.Years => 60 * 24 * 365,
(int) Multipliers.Permanent => 0,
_ => throw new ArgumentOutOfRangeException(nameof(MultiplierOption.SelectedId), "Multiplier out of range")
};
UpdateExpiresLabel();
}
private void UpdateExpiresLabel()
{
var minutes = (uint) (TimeEntered * Multiplier);
ExpiresLabel.Text = minutes == 0
? $"{Loc.GetString("admin-note-editor-expiry-label")} {Loc.GetString("server-ban-string-never")}"
: $"{Loc.GetString("admin-note-editor-expiry-label")} {DateTime.Now + TimeSpan.FromMinutes(minutes):yyyy/MM/dd HH:mm:ss}";
}
private void OnIpChanged()
{
if (LastConnCheckbox.Pressed && IpAddress is null || !IpCheckbox.Pressed)
{
IpAddress = null;
ErrorLevel &= ~ErrorLevelEnum.IpAddress;
IpLine.ModulateSelfOverride = null;
UpdateSubmitEnabled();
return;
}
var ip = IpLine.Text;
var hid = "0";
if (ip.Contains('/'))
{
var split = ip.Split('/');
ip = split[0];
hid = split[1];
}
if (!IPAddress.TryParse(ip, out var parsedIp) || !byte.TryParse(hid, out var hidInt) || hidInt > 128 || hidInt > 32 && parsedIp.AddressFamily == AddressFamily.InterNetwork)
{
ErrorLevel |= ErrorLevelEnum.IpAddress;
IpLine.ModulateSelfOverride = Color.Red;
UpdateSubmitEnabled();
return;
}
if (hidInt == 0)
hidInt = (byte) (parsedIp.AddressFamily == AddressFamily.InterNetworkV6 ? 128 : 32);
IpAddress = (parsedIp, hidInt);
ErrorLevel &= ~ErrorLevelEnum.IpAddress;
IpLine.ModulateSelfOverride = null;
UpdateSubmitEnabled();
}
private void OnHwidChanged()
{
var hwidString = HwidLine.Text;
ImmutableTypedHwid? hwid = null;
if (HwidCheckbox.Pressed && !(string.IsNullOrEmpty(hwidString) && LastConnCheckbox.Pressed) && !ImmutableTypedHwid.TryParse(hwidString, out hwid))
{
ErrorLevel |= ErrorLevelEnum.Hwid;
HwidLine.ModulateSelfOverride = Color.Red;
UpdateSubmitEnabled();
return;
}
ErrorLevel &= ~ErrorLevelEnum.Hwid;
HwidLine.ModulateSelfOverride = null;
UpdateSubmitEnabled();
if (LastConnCheckbox.Pressed || !HwidCheckbox.Pressed)
{
Hwid = null;
return;
}
Hwid = hwid;
}
private void OnTypeChanged()
{
TypeOption.ModulateSelfOverride = null;
Tabs.SetTabVisible((int) TabNumbers.Roles, TypeOption.SelectedId == (int) Types.Role);
NoteSeverity? newSeverity = null;
switch (TypeOption.SelectedId)
{
case (int)Types.Server:
if (Enum.TryParse(_cfg.GetCVar(CCVars.ServerBanDefaultSeverity), true, out NoteSeverity serverSeverity))
newSeverity = serverSeverity;
else
{
_banPanelSawmill
.Warning("Server ban severity could not be parsed from config!");
}
break;
case (int) Types.Role:
if (Enum.TryParse(_cfg.GetCVar(CCVars.RoleBanDefaultSeverity), true, out NoteSeverity roleSeverity))
{
newSeverity = roleSeverity;
}
else
{
_banPanelSawmill
.Warning("Role ban severity could not be parsed from config!");
}
break;
}
if (newSeverity != null)
SeverityOption.SelectId((int) newSeverity.Value);
}
private void UpdateSubmitEnabled()
{
SubmitButton.Disabled = ErrorLevel != ErrorLevelEnum.None;
}
private void OnPlayerNameChanged()
{
if (PlayerUsername == PlayerNameLine.Text)
return;
PlayerUsername = PlayerNameLine.Text;
if (!PlayerCheckbox.Pressed)
return;
if (string.IsNullOrWhiteSpace(PlayerUsername))
ErrorLevel |= ErrorLevelEnum.PlayerName;
else
ErrorLevel &= ~ErrorLevelEnum.PlayerName;
UpdateSubmitEnabled();
PlayerChanged?.Invoke(PlayerUsername);
}
public void OnPlayerSelectionChanged(PlayerInfo? player)
{
PlayerNameLine.Text = player?.Username ?? string.Empty;
OnPlayerNameChanged();
}
private void ResetTextEditor(GUIBoundKeyEventArgs _)
{
ReasonTextEdit.ModulateSelfOverride = null;
ReasonTextEdit.OnKeyBindDown -= ResetTextEditor;
}
private void SubmitButtonOnOnPressed(BaseButton.ButtonEventArgs obj)
{
ProtoId<JobPrototype>[]? jobs = null;
ProtoId<AntagPrototype>[]? antags = null;
if (TypeOption.SelectedId == (int) Types.Role)
{
var jobList = new List<ProtoId<JobPrototype>>();
var antagList = new List<ProtoId<AntagPrototype>>();
if (_roleCheckboxes.Count == 0)
throw new DebugAssertException("RoleCheckboxes was empty");
foreach (var button in _roleCheckboxes.Values.SelectMany(departmentButtons => departmentButtons))
{
if (button.Item1 is { Pressed: true, Name: not null })
{
switch (button.Item2)
{
case JobPrototype:
jobList.Add(button.Item2.ID);
break;
case AntagPrototype:
antagList.Add(button.Item2.ID);
break;
}
}
}
if (jobList.Count + antagList.Count == 0)
{
Tabs.CurrentTab = (int) TabNumbers.Roles;
return;
}
jobs = jobList.ToArray();
antags = antagList.ToArray();
}
if (TypeOption.SelectedId == (int) Types.None)
{
TypeOption.ModulateSelfOverride = Color.Red;
Tabs.CurrentTab = (int) TabNumbers.BasicInfo;
return;
}
var reason = Rope.Collapse(ReasonTextEdit.TextRope);
if (string.IsNullOrWhiteSpace(reason))
{
//Tabs.CurrentTab = (int) TabNumbers.Text;
Tabs.CurrentTab = (int) TabNumbers.BasicInfo;
ReasonTextEdit.GrabKeyboardFocus();
ReasonTextEdit.ModulateSelfOverride = Color.Red;
ReasonTextEdit.OnKeyBindDown += ResetTextEditor;
return;
}
if (ButtonResetOn is null)
{
ButtonResetOn = _gameTiming.CurTime.Add(TimeSpan.FromSeconds(3));
SubmitButton.ModulateSelfOverride = Color.Red;
SubmitButton.Text = Loc.GetString("ban-panel-confirm");
return;
}
var player = PlayerCheckbox.Pressed ? PlayerUsername : null;
var useLastIp = IpCheckbox.Pressed && LastConnCheckbox.Pressed && IpAddress is null;
var useLastHwid = HwidCheckbox.Pressed && LastConnCheckbox.Pressed && Hwid is null;
var severity = (NoteSeverity) SeverityOption.SelectedId;
var erase = EraseCheckbox.Pressed;
var ban = new Ban(
player,
IpAddress,
useLastIp,
Hwid,
useLastHwid,
(uint)(TimeEntered * Multiplier),
reason,
severity,
jobs,
antags,
erase
);
BanSubmitted?.Invoke(ban);
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
// This checks for null for free, do not invert it as null always produces a false value
if (_gameTiming.CurTime > ButtonResetOn)
{
ButtonResetOn = null;
SubmitButton.ModulateSelfOverride = null;
SubmitButton.Text = Loc.GetString("ban-panel-submit");
}
}
}