Files
tbd-station-14/Content.Client/Administration/UI/BwoinkWindow.xaml.cs
Jacob Tong 09c6a5b252 Add Context Menu to Bwoink Window (#9374)
* Clean up EntityListDisplay

* Rename EntityListDisplay to ListContainer

* Rename stuff

* Rework ListContainer

* Add tests

* Replace IControlData with record ListData

* Make _data non-nullable

* Fix test record items being duplicates

* Split filter method from populate

* Rename UpdateList to DirtyList

* Replace _count with _data.Count

* Clarify local variable toRemove

* Cleanup

* Add data selection to ListContainer

* Add selection test

* Fix comments and test name

* Fix ListContainer layout hiding items when scaled

* Add test for ListContainer top item

* Toggle fix

* Ensure buttons are re-generated

* Update unread count on select

* a

* Fix toggle test

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
2022-09-14 19:03:13 -05:00

233 lines
7.6 KiB
C#

#nullable enable
using System.Text;
using System.Threading;
using Content.Client.Administration.Managers;
using Content.Client.Administration.Systems;
using Content.Client.Administration.UI.CustomControls;
using Content.Client.Administration.UI.Tabs.AdminTab;
using Content.Client.Stylesheets;
using Content.Shared.Administration;
using Robust.Client.AutoGenerated;
using Robust.Client.Console;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Network;
using Robust.Shared.Utility;
using Timer = Robust.Shared.Timing.Timer;
namespace Content.Client.Administration.UI
{
/// <summary>
/// This window connects to a BwoinkSystem channel. BwoinkSystem manages the rest.
/// </summary>
[GenerateTypedNameReferences]
public sealed partial class BwoinkWindow : DefaultWindow
{
[Dependency] private readonly IClientAdminManager _adminManager = default!;
[Dependency] private readonly IClientConsoleHost _console = default!;
private readonly BwoinkSystem _bwoinkSystem;
private PlayerInfo? _currentPlayer = default;
public BwoinkWindow(BwoinkSystem bs)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
_bwoinkSystem = bs;
_adminManager.AdminStatusUpdated += FixButtons;
FixButtons();
ChannelSelector.OnSelectionChanged += sel =>
{
_currentPlayer = sel;
if (sel is not null)
{
SwitchToChannel(sel.SessionId);
Title = $"{sel.CharacterName} / {sel.Username}";
}
ChannelSelector.PlayerListContainer.DirtyList();
};
ChannelSelector.OverrideText += (info, text) =>
{
var sb = new StringBuilder();
sb.Append(info.Connected ? '●' : '○');
sb.Append(' ');
if (_bwoinkSystem.TryGetChannel(info.SessionId, out var panel) && panel.Unread > 0)
{
if (panel.Unread < 11)
sb.Append(new Rune('➀' + (panel.Unread-1)));
else
sb.Append(new Rune(0x2639)); // ☹
sb.Append(' ');
}
if (info.Antag)
sb.Append(new Rune(0x1F5E1)); // 🗡
sb.AppendFormat("\"{0}\"", text);
return sb.ToString();
};
ChannelSelector.Comparison = (a, b) =>
{
var aChannelExists = _bwoinkSystem.TryGetChannel(a.SessionId, out var ach);
var bChannelExists = _bwoinkSystem.TryGetChannel(b.SessionId, out var bch);
if (!aChannelExists && !bChannelExists)
return 0;
if (!aChannelExists)
return 1;
if (!bChannelExists)
return -1;
return bch!.LastMessage.CompareTo(ach!.LastMessage);
};
Notes.OnPressed += _ =>
{
if (_currentPlayer is not null)
_console.ExecuteCommand($"adminnotes \"{_currentPlayer.SessionId}\"");
};
// ew
Ban.OnPressed += _ =>
{
var bw = new BanWindow();
bw.OnPlayerSelectionChanged(_currentPlayer);
bw.Open();
};
Kick.OnPressed += _ =>
{
if (!TryConfirm(Kick))
{
return;
}
// TODO: Reason field
if (_currentPlayer is not null)
_console.ExecuteCommand($"kick \"{_currentPlayer.Username}\"");
};
Teleport.OnPressed += _ =>
{
if (_currentPlayer is not null)
_console.ExecuteCommand($"tpto \"{_currentPlayer.Username}\"");
};
Respawn.OnPressed += _ =>
{
if (!TryConfirm(Respawn))
{
return;
}
if (_currentPlayer is not null)
_console.ExecuteCommand($"respawn \"{_currentPlayer.Username}\"");
};
}
private Dictionary<Control, (CancellationTokenSource cancellation, string? originalText)> Confirmations { get; } = new();
public void OnBwoink(NetUserId channel)
{
ChannelSelector.PopulateList();
}
public void SelectChannel(NetUserId channel)
{
if (!ChannelSelector.PlayerInfo.TryFirstOrDefault(
i => i.SessionId == channel, out var info))
return;
ChannelSelector.PopulateList();
ChannelSelector.PlayerListContainer.Select(new PlayerListData(info));
}
private void FixButtons()
{
Notes.Visible = _adminManager.HasFlag(AdminFlags.ViewNotes);
Notes.Disabled = !Notes.Visible;
Ban.Visible = _adminManager.HasFlag(AdminFlags.Ban);
Ban.Disabled = !Ban.Visible;
Kick.Visible = _adminManager.CanCommand("kick");
Kick.Disabled = !Kick.Visible;
Teleport.Visible = _adminManager.CanCommand("tpto");
Teleport.Disabled = !Teleport.Visible;
Respawn.Visible = _adminManager.CanCommand("respawn");
Respawn.Disabled = !Respawn.Visible;
}
private string FormatTabTitle(ItemList.Item li, PlayerInfo? pl = default)
{
pl ??= (PlayerInfo) li.Metadata!;
var sb = new StringBuilder();
sb.Append(pl.Connected ? '●' : '○');
sb.Append(' ');
if (_bwoinkSystem.TryGetChannel(pl.SessionId, out var panel) && panel.Unread > 0)
{
if (panel.Unread < 11)
sb.Append(new Rune('➀' + (panel.Unread-1)));
else
sb.Append(new Rune(0x2639)); // ☹
sb.Append(' ');
}
if (pl.Antag)
sb.Append(new Rune(0x1F5E1)); // 🗡
sb.AppendFormat("\"{0}\"", pl.CharacterName);
if (pl.IdentityName != pl.CharacterName && pl.IdentityName != string.Empty)
sb.Append(' ').AppendFormat("[{0}]", pl.IdentityName);
sb.Append(' ').Append(pl.Username);
return sb.ToString();
}
private void SwitchToChannel(NetUserId ch)
{
foreach (var bw in BwoinkArea.Children)
bw.Visible = false;
var panel = _bwoinkSystem.EnsurePanel(ch);
panel.Visible = true;
}
private bool TryConfirm(Button button)
{
if (Confirmations.Remove(button, out var tuple))
{
tuple.cancellation.Cancel();
button.ModulateSelfOverride = null;
button.Text = tuple.originalText;
return true;
}
tuple = (new CancellationTokenSource(), button.Text);
Confirmations[button] = tuple;
Timer.Spawn(TimeSpan.FromSeconds(5), () =>
{
Confirmations.Remove(button);
button.ModulateSelfOverride = null;
button.Text = tuple.originalText;
}, tuple.cancellation.Token);
button.ModulateSelfOverride = StyleNano.ButtonColorCautionDefault;
button.Text = Loc.GetString("admin-player-actions-confirm");
return false;
}
}
}