RoleSystem.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Content.Server.Chat.Managers;
  2. using Content.Shared.Chat;
  3. using Content.Shared.Mind;
  4. using Content.Shared.Roles;
  5. using Robust.Shared.Prototypes;
  6. namespace Content.Server.Roles;
  7. public sealed class RoleSystem : SharedRoleSystem
  8. {
  9. [Dependency] private readonly IChatManager _chat = default!;
  10. [Dependency] private readonly IPrototypeManager _proto = default!;
  11. public string? MindGetBriefing(EntityUid? mindId)
  12. {
  13. if (mindId == null)
  14. {
  15. Log.Error($"MingGetBriefing failed for mind {mindId}");
  16. return null;
  17. }
  18. TryComp<MindComponent>(mindId.Value, out var mindComp);
  19. if (mindComp is null)
  20. {
  21. Log.Error($"MingGetBriefing failed for mind {mindId}");
  22. return null;
  23. }
  24. var ev = new GetBriefingEvent();
  25. // This is on the event because while this Entity<T> is also present on every Mind Role Entity's MindRoleComp
  26. // getting to there from a GetBriefing event subscription can be somewhat boilerplate
  27. // and this needs to be looked up for the event anyway so why calculate it again later
  28. ev.Mind = (mindId.Value, mindComp);
  29. // Briefing is no longer raised on the mind entity itself
  30. // because all the components that briefings subscribe to should be on Mind Role Entities
  31. foreach(var role in mindComp.MindRoles)
  32. {
  33. RaiseLocalEvent(role, ref ev);
  34. }
  35. return ev.Briefing;
  36. }
  37. public void RoleUpdateMessage(MindComponent mind)
  38. {
  39. if (mind.Session is null)
  40. return;
  41. if (!_proto.TryIndex(mind.RoleType, out var proto))
  42. return;
  43. var roleText = Loc.GetString(proto.Name);
  44. var color = proto.Color;
  45. var session = mind.Session;
  46. //TODO add audio? Would need to be optional so it does not play on role changes that already come with their own audio
  47. // _audio.PlayGlobal(Sound, session);
  48. var message = Loc.GetString("role-type-update-message", ("color", color), ("role", roleText));
  49. var wrappedMessage = Loc.GetString("chat-manager-server-wrap-message", ("message", message));
  50. _chat.ChatMessageToOne(ChatChannel.Server,
  51. message,
  52. wrappedMessage,
  53. default,
  54. false,
  55. session.Channel);
  56. }
  57. }
  58. /// <summary>
  59. /// Event raised on the mind to get its briefing.
  60. /// Handlers can either replace or append to the briefing, whichever is more appropriate.
  61. /// </summary>
  62. [ByRefEvent]
  63. public sealed class GetBriefingEvent
  64. {
  65. /// <summary>
  66. /// The text that will be shown on the Character Screen
  67. /// </summary>
  68. public string? Briefing;
  69. /// <summary>
  70. /// The Mind to whose Mind Role Entities the briefing is sent to
  71. /// </summary>
  72. public Entity<MindComponent> Mind;
  73. public GetBriefingEvent(string? briefing = null)
  74. {
  75. Briefing = briefing;
  76. }
  77. /// <summary>
  78. /// If there is no briefing, sets it to the string.
  79. /// If there is a briefing, adds a new line to separate it from the appended string.
  80. /// </summary>
  81. public void Append(string text)
  82. {
  83. if (Briefing == null)
  84. {
  85. Briefing = text;
  86. }
  87. else
  88. {
  89. Briefing += "\n" + text;
  90. }
  91. }
  92. }