1
0

SharedClockSystem.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Linq;
  2. using Content.Shared.Examine;
  3. using Content.Shared.GameTicking;
  4. namespace Content.Shared.Clock;
  5. public abstract class SharedClockSystem : EntitySystem
  6. {
  7. [Dependency] private readonly SharedGameTicker _ticker = default!;
  8. /// <inheritdoc/>
  9. public override void Initialize()
  10. {
  11. SubscribeLocalEvent<ClockComponent, ExaminedEvent>(OnExamined);
  12. }
  13. private void OnExamined(Entity<ClockComponent> ent, ref ExaminedEvent args)
  14. {
  15. if (!args.IsInDetailsRange)
  16. return;
  17. args.PushMarkup(Loc.GetString("clock-examine", ("time", GetClockTimeText(ent))));
  18. }
  19. public string GetClockTimeText(Entity<ClockComponent> ent)
  20. {
  21. var time = GetClockTime(ent);
  22. switch (ent.Comp.ClockType)
  23. {
  24. case ClockType.TwelveHour:
  25. return time.ToString(@"h\:mm");
  26. case ClockType.TwentyFourHour:
  27. return time.ToString(@"hh\:mm");
  28. default:
  29. throw new ArgumentOutOfRangeException();
  30. }
  31. }
  32. private TimeSpan GetGlobalTime()
  33. {
  34. return (EntityQuery<GlobalTimeManagerComponent>().FirstOrDefault()?.TimeOffset ?? TimeSpan.Zero) + _ticker.RoundDuration();
  35. }
  36. public TimeSpan GetClockTime(Entity<ClockComponent> ent)
  37. {
  38. var comp = ent.Comp;
  39. if (comp.StuckTime != null)
  40. return comp.StuckTime.Value;
  41. var time = GetGlobalTime();
  42. switch (comp.ClockType)
  43. {
  44. case ClockType.TwelveHour:
  45. var adjustedHours = time.Hours % 12;
  46. if (adjustedHours == 0)
  47. adjustedHours = 12;
  48. return new TimeSpan(adjustedHours, time.Minutes, time.Seconds);
  49. case ClockType.TwentyFourHour:
  50. return time;
  51. default:
  52. throw new ArgumentOutOfRangeException();
  53. }
  54. }
  55. }