GhostRoleEntryButtons.xaml.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Content.Shared.Ghost.Roles;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.UserInterface.Controls;
  4. using Robust.Client.UserInterface.XAML;
  5. using Robust.Shared.Timing;
  6. namespace Content.Client.UserInterface.Systems.Ghost.Controls.Roles;
  7. [GenerateTypedNameReferences]
  8. public sealed partial class GhostRoleEntryButtons : BoxContainer
  9. {
  10. [Dependency] private readonly IGameTiming _timing = default!;
  11. private readonly GhostRoleKind _ghostRoleKind;
  12. private readonly uint _playerCount;
  13. private readonly TimeSpan _raffleEndTime = TimeSpan.MinValue;
  14. public GhostRoleEntryButtons(GhostRoleInfo ghostRoleInfo)
  15. {
  16. RobustXamlLoader.Load(this);
  17. IoCManager.InjectDependencies(this);
  18. _ghostRoleKind = ghostRoleInfo.Kind;
  19. if (IsActiveRaffle(_ghostRoleKind))
  20. {
  21. _playerCount = ghostRoleInfo.RafflePlayerCount;
  22. _raffleEndTime = ghostRoleInfo.RaffleEndTime;
  23. }
  24. UpdateRequestButton();
  25. }
  26. private void UpdateRequestButton()
  27. {
  28. var messageId = _ghostRoleKind switch
  29. {
  30. GhostRoleKind.FirstComeFirstServe => "ghost-roles-window-request-role-button",
  31. GhostRoleKind.RaffleReady => "ghost-roles-window-join-raffle-button",
  32. GhostRoleKind.RaffleInProgress => "ghost-roles-window-raffle-in-progress-button",
  33. GhostRoleKind.RaffleJoined => "ghost-roles-window-leave-raffle-button",
  34. _ => throw new ArgumentOutOfRangeException(nameof(_ghostRoleKind),
  35. $"Unknown {nameof(GhostRoleKind)} '{_ghostRoleKind}'")
  36. };
  37. if (IsActiveRaffle(_ghostRoleKind))
  38. {
  39. var timeLeft = _timing.CurTime <= _raffleEndTime
  40. ? _raffleEndTime - _timing.CurTime
  41. : TimeSpan.Zero;
  42. var timeString = $"{timeLeft.Minutes:0}:{timeLeft.Seconds:00}";
  43. RequestButton.Text = Loc.GetString(messageId, ("time", timeString), ("players", _playerCount));
  44. }
  45. else
  46. {
  47. RequestButton.Text = Loc.GetString(messageId);
  48. }
  49. }
  50. private static bool IsActiveRaffle(GhostRoleKind kind)
  51. {
  52. return kind is GhostRoleKind.RaffleInProgress or GhostRoleKind.RaffleJoined;
  53. }
  54. protected override void FrameUpdate(FrameEventArgs args)
  55. {
  56. base.FrameUpdate(args);
  57. if (IsActiveRaffle(_ghostRoleKind))
  58. {
  59. UpdateRequestButton();
  60. }
  61. }
  62. }