CriminalRecordsConsoleWindow.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using Content.Client.UserInterface.Controls;
  2. using Content.Shared.Access.Systems;
  3. using Content.Shared.Administration;
  4. using Content.Shared.CriminalRecords;
  5. using Content.Shared.Dataset;
  6. using Content.Shared.Random.Helpers;
  7. using Content.Shared.Security;
  8. using Content.Shared.StationRecords;
  9. using Robust.Client.AutoGenerated;
  10. using Robust.Client.Player;
  11. using Robust.Client.UserInterface.Controls;
  12. using Robust.Client.UserInterface.XAML;
  13. using Robust.Shared.Prototypes;
  14. using Robust.Shared.Random;
  15. using Robust.Shared.Utility;
  16. using System.Linq;
  17. using System.Numerics;
  18. using Content.Shared.StatusIcon;
  19. using Robust.Client.GameObjects;
  20. namespace Content.Client.CriminalRecords;
  21. // TODO: dedupe shitcode from general records theres a lot
  22. [GenerateTypedNameReferences]
  23. public sealed partial class CriminalRecordsConsoleWindow : FancyWindow
  24. {
  25. private readonly IPlayerManager _player;
  26. private readonly IPrototypeManager _proto;
  27. private readonly IRobustRandom _random;
  28. private readonly AccessReaderSystem _accessReader;
  29. [Dependency] private readonly IEntityManager _entManager = default!;
  30. private readonly SpriteSystem _spriteSystem;
  31. public readonly EntityUid Console;
  32. [ValidatePrototypeId<LocalizedDatasetPrototype>]
  33. private const string ReasonPlaceholders = "CriminalRecordsWantedReasonPlaceholders";
  34. public Action<uint?>? OnKeySelected;
  35. public Action<StationRecordFilterType, string>? OnFiltersChanged;
  36. public Action<SecurityStatus>? OnStatusSelected;
  37. public Action<uint>? OnCheckStatus;
  38. public Action<CriminalRecord, bool, bool>? OnHistoryUpdated;
  39. public Action? OnHistoryClosed;
  40. public Action<SecurityStatus, string>? OnDialogConfirmed;
  41. public Action<SecurityStatus>? OnStatusFilterPressed;
  42. private uint _maxLength;
  43. private bool _access;
  44. private uint? _selectedKey;
  45. private CriminalRecord? _selectedRecord;
  46. private DialogWindow? _reasonDialog;
  47. private StationRecordFilterType _currentFilterType;
  48. private SecurityStatus _currentCrewListFilter;
  49. public CriminalRecordsConsoleWindow(EntityUid console, uint maxLength, IPlayerManager playerManager, IPrototypeManager prototypeManager, IRobustRandom robustRandom, AccessReaderSystem accessReader)
  50. {
  51. RobustXamlLoader.Load(this);
  52. Console = console;
  53. _player = playerManager;
  54. _proto = prototypeManager;
  55. _random = robustRandom;
  56. _accessReader = accessReader;
  57. IoCManager.InjectDependencies(this);
  58. _spriteSystem = _entManager.System<SpriteSystem>();
  59. _maxLength = maxLength;
  60. _currentFilterType = StationRecordFilterType.Name;
  61. _currentCrewListFilter = SecurityStatus.None;
  62. OpenCentered();
  63. foreach (var item in Enum.GetValues<StationRecordFilterType>())
  64. {
  65. FilterType.AddItem(GetTypeFilterLocals(item), (int)item);
  66. }
  67. foreach (var status in Enum.GetValues<SecurityStatus>())
  68. {
  69. AddStatusSelect(status);
  70. }
  71. //Populate status to filter crew list
  72. foreach (var item in Enum.GetValues<SecurityStatus>())
  73. {
  74. CrewListFilter.AddItem(GetCrewListFilterLocals(item), (int)item);
  75. }
  76. OnClose += () => _reasonDialog?.Close();
  77. RecordListing.OnItemSelected += args =>
  78. {
  79. if (RecordListing[args.ItemIndex].Metadata is not uint cast)
  80. return;
  81. OnKeySelected?.Invoke(cast);
  82. };
  83. RecordListing.OnItemDeselected += _ =>
  84. {
  85. OnKeySelected?.Invoke(null);
  86. };
  87. FilterType.OnItemSelected += eventArgs =>
  88. {
  89. var type = (StationRecordFilterType)eventArgs.Id;
  90. if (_currentFilterType != type)
  91. {
  92. _currentFilterType = type;
  93. FilterListingOfRecords(FilterText.Text);
  94. }
  95. };
  96. //Select Status to filter crew
  97. CrewListFilter.OnItemSelected += eventArgs =>
  98. {
  99. var type = (SecurityStatus)eventArgs.Id;
  100. if (_currentCrewListFilter != type)
  101. {
  102. _currentCrewListFilter = type;
  103. StatusFilterPressed(type);
  104. }
  105. };
  106. FilterText.OnTextEntered += args =>
  107. {
  108. FilterListingOfRecords(args.Text);
  109. };
  110. StatusOptionButton.OnItemSelected += args =>
  111. {
  112. SetStatus((SecurityStatus)args.Id);
  113. };
  114. HistoryButton.OnPressed += _ =>
  115. {
  116. if (_selectedRecord is { } record)
  117. OnHistoryUpdated?.Invoke(record, _access, true);
  118. };
  119. }
  120. public void StatusFilterPressed(SecurityStatus statusSelected)
  121. {
  122. OnStatusFilterPressed?.Invoke(statusSelected);
  123. }
  124. public void UpdateState(CriminalRecordsConsoleState state)
  125. {
  126. if (state.Filter != null)
  127. {
  128. if (state.Filter.Type != _currentFilterType)
  129. {
  130. _currentFilterType = state.Filter.Type;
  131. }
  132. if (state.Filter.Value != FilterText.Text)
  133. {
  134. FilterText.Text = state.Filter.Value;
  135. }
  136. }
  137. if (state.FilterStatus != _currentCrewListFilter)
  138. {
  139. _currentCrewListFilter = state.FilterStatus;
  140. }
  141. _selectedKey = state.SelectedKey;
  142. FilterType.SelectId((int)_currentFilterType);
  143. CrewListFilter.SelectId((int)_currentCrewListFilter);
  144. NoRecords.Visible = state.RecordListing == null || state.RecordListing.Count == 0;
  145. PopulateRecordListing(state.RecordListing);
  146. // set up the selected person's record
  147. var selected = _selectedKey != null;
  148. PersonContainer.Visible = selected;
  149. RecordUnselected.Visible = !selected;
  150. _access = _player.LocalSession?.AttachedEntity is {} player
  151. && _accessReader.IsAllowed(player, Console);
  152. // hide access-required editing parts when no access
  153. var editing = _access && selected;
  154. StatusOptionButton.Disabled = !editing;
  155. if (state is { CriminalRecord: not null, StationRecord: not null })
  156. {
  157. PopulateRecordContainer(state.StationRecord, state.CriminalRecord);
  158. OnHistoryUpdated?.Invoke(state.CriminalRecord, _access, false);
  159. _selectedRecord = state.CriminalRecord;
  160. }
  161. else
  162. {
  163. _selectedRecord = null;
  164. OnHistoryClosed?.Invoke();
  165. }
  166. }
  167. private void PopulateRecordListing(Dictionary<uint, string>? listing)
  168. {
  169. if (listing == null)
  170. {
  171. RecordListing.Clear();
  172. return;
  173. }
  174. var entries = listing.ToList();
  175. entries.Sort((a, b) => string.Compare(a.Value, b.Value, StringComparison.Ordinal));
  176. // `entries` now contains the definitive list of items which should be in
  177. // our list of records and is in the order we want to present those items.
  178. // Walk through the existing items in RecordListing and in the updated listing
  179. // in parallel to synchronize the items in RecordListing with `entries`.
  180. int i = RecordListing.Count - 1;
  181. int j = entries.Count - 1;
  182. while (i >= 0 && j >= 0)
  183. {
  184. var strcmp = string.Compare(RecordListing[i].Text, entries[j].Value, StringComparison.Ordinal);
  185. if (strcmp == 0)
  186. {
  187. // This item exists in both RecordListing and `entries`. Nothing to do.
  188. i--;
  189. j--;
  190. }
  191. else if (strcmp > 0)
  192. {
  193. // Item exists in RecordListing, but not in `entries`. Remove it.
  194. RecordListing.RemoveAt(i);
  195. i--;
  196. }
  197. else if (strcmp < 0)
  198. {
  199. // A new entry which doesn't exist in RecordListing. Create it.
  200. RecordListing.Insert(i + 1, new ItemList.Item(RecordListing){Text = entries[j].Value, Metadata = entries[j].Key});
  201. j--;
  202. }
  203. }
  204. // Any remaining items in RecordListing don't exist in `entries`, so remove them
  205. while (i >= 0)
  206. {
  207. RecordListing.RemoveAt(i);
  208. i--;
  209. }
  210. // And finally, any remaining items in `entries`, don't exist in RecordListing. Create them.
  211. while (j >= 0)
  212. {
  213. RecordListing.Insert(0, new ItemList.Item(RecordListing){ Text = entries[j].Value, Metadata = entries[j].Key });
  214. j--;
  215. }
  216. }
  217. private void PopulateRecordContainer(GeneralStationRecord stationRecord, CriminalRecord criminalRecord)
  218. {
  219. var specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Misc/job_icons.rsi"), "Unknown");
  220. var na = Loc.GetString("generic-not-available-shorthand");
  221. PersonName.Text = stationRecord.Name;
  222. PersonJob.Text = stationRecord.JobTitle ?? na;
  223. // Job icon
  224. if (_proto.TryIndex<JobIconPrototype>(stationRecord.JobIcon, out var proto))
  225. {
  226. PersonJobIcon.Texture = _spriteSystem.Frame0(proto.Icon);
  227. }
  228. PersonPrints.Text = stationRecord.Fingerprint ?? Loc.GetString("generic-not-available-shorthand");
  229. PersonDna.Text = stationRecord.DNA ?? Loc.GetString("generic-not-available-shorthand");
  230. if (criminalRecord.Status != SecurityStatus.None)
  231. {
  232. specifier = new SpriteSpecifier.Rsi(new ResPath("Interface/Misc/security_icons.rsi"), GetStatusIcon(criminalRecord.Status));
  233. }
  234. PersonStatusTX.SetFromSpriteSpecifier(specifier);
  235. PersonStatusTX.DisplayRect.TextureScale = new Vector2(3f, 3f);
  236. StatusOptionButton.SelectId((int)criminalRecord.Status);
  237. if (criminalRecord.Reason is { } reason)
  238. {
  239. var message = FormattedMessage.FromMarkupOrThrow(Loc.GetString("criminal-records-console-wanted-reason"));
  240. if (criminalRecord.Status == SecurityStatus.Suspected)
  241. {
  242. message = FormattedMessage.FromMarkupOrThrow(Loc.GetString("criminal-records-console-suspected-reason"));
  243. }
  244. message.AddText($": {reason}");
  245. WantedReason.SetMessage(message);
  246. WantedReason.Visible = true;
  247. }
  248. else
  249. {
  250. WantedReason.Visible = false;
  251. }
  252. }
  253. private void AddStatusSelect(SecurityStatus status)
  254. {
  255. var name = Loc.GetString($"criminal-records-status-{status.ToString().ToLower()}");
  256. StatusOptionButton.AddItem(name, (int)status);
  257. }
  258. private void FilterListingOfRecords(string text = "")
  259. {
  260. OnFiltersChanged?.Invoke(_currentFilterType, text);
  261. }
  262. private void SetStatus(SecurityStatus status)
  263. {
  264. if (status == SecurityStatus.Wanted || status == SecurityStatus.Suspected)
  265. {
  266. GetReason(status);
  267. return;
  268. }
  269. OnStatusSelected?.Invoke(status);
  270. }
  271. private void GetReason(SecurityStatus status)
  272. {
  273. if (_reasonDialog != null)
  274. {
  275. _reasonDialog.MoveToFront();
  276. return;
  277. }
  278. var field = "reason";
  279. var title = Loc.GetString("criminal-records-status-" + status.ToString().ToLower());
  280. var placeholders = _proto.Index<LocalizedDatasetPrototype>(ReasonPlaceholders);
  281. var placeholder = Loc.GetString("criminal-records-console-reason-placeholder", ("placeholder", _random.Pick(placeholders))); // just funny it doesn't actually get used
  282. var prompt = Loc.GetString("criminal-records-console-reason");
  283. var entry = new QuickDialogEntry(field, QuickDialogEntryType.LongText, prompt, placeholder);
  284. var entries = new List<QuickDialogEntry>() { entry };
  285. _reasonDialog = new DialogWindow(title, entries);
  286. _reasonDialog.OnConfirmed += responses =>
  287. {
  288. var reason = responses[field];
  289. if (reason.Length < 1 || reason.Length > _maxLength)
  290. return;
  291. OnDialogConfirmed?.Invoke(status, reason);
  292. };
  293. _reasonDialog.OnClose += () => { _reasonDialog = null; };
  294. }
  295. private string GetStatusIcon(SecurityStatus status)
  296. {
  297. return status switch
  298. {
  299. SecurityStatus.Paroled => "hud_paroled",
  300. SecurityStatus.Wanted => "hud_wanted",
  301. SecurityStatus.Detained => "hud_incarcerated",
  302. SecurityStatus.Discharged => "hud_discharged",
  303. SecurityStatus.Suspected => "hud_suspected",
  304. _ => "SecurityIconNone"
  305. };
  306. }
  307. private string GetTypeFilterLocals(StationRecordFilterType type)
  308. {
  309. return Loc.GetString($"criminal-records-{type.ToString().ToLower()}-filter");
  310. }
  311. private string GetCrewListFilterLocals(SecurityStatus type)
  312. {
  313. string result;
  314. // If "NONE" override to "show all"
  315. if (type == SecurityStatus.None)
  316. {
  317. result = Loc.GetString("criminal-records-console-show-all");
  318. }
  319. else
  320. {
  321. result = Loc.GetString($"criminal-records-status-{type.ToString().ToLower()}");
  322. }
  323. return result;
  324. }
  325. }