1
0

SharedRoleCodewordSystem.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Content.Shared.Mind;
  2. using Robust.Shared.GameStates;
  3. using Robust.Shared.Player;
  4. namespace Content.Shared.Roles.RoleCodeword;
  5. public abstract class SharedRoleCodewordSystem : EntitySystem
  6. {
  7. [Dependency] private readonly SharedMindSystem _mindSystem = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<RoleCodewordComponent, ComponentGetStateAttemptEvent>(OnCodewordCompGetStateAttempt);
  12. }
  13. /// <summary>
  14. /// Determines if a codeword component should be sent to the client.
  15. /// </summary>
  16. private void OnCodewordCompGetStateAttempt(EntityUid uid, RoleCodewordComponent comp, ref ComponentGetStateAttemptEvent args)
  17. {
  18. args.Cancelled = !CanGetState(args.Player, comp);
  19. }
  20. /// <summary>
  21. /// The criteria that determine whether a codeword component should be sent to a client.
  22. /// Sends the component if its owner is the player mind.
  23. /// </summary>
  24. /// <param name="player"> The Player the component will be sent to.</param>
  25. /// <param name="comp"> The component being checked against</param>
  26. /// <returns></returns>
  27. private bool CanGetState(ICommonSession? player, RoleCodewordComponent comp)
  28. {
  29. if (!_mindSystem.TryGetMind(player, out EntityUid mindId, out var _))
  30. return false;
  31. if (!TryComp(mindId, out RoleCodewordComponent? playerComp) && comp != playerComp)
  32. return false;
  33. return true;
  34. }
  35. public void SetRoleCodewords(RoleCodewordComponent comp, string key, List<string> codewords, Color color)
  36. {
  37. var data = new CodewordsData(color, codewords);
  38. comp.RoleCodewords[key] = data;
  39. }
  40. }