1
0

JobWhitelistManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Linq;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Content.Server.Database;
  5. using Content.Shared.CCVar;
  6. using Content.Shared.Players.JobWhitelist;
  7. using Content.Shared.Roles;
  8. using Robust.Server.Player;
  9. using Robust.Shared.Configuration;
  10. using Robust.Shared.Network;
  11. using Robust.Shared.Player;
  12. using Robust.Shared.Prototypes;
  13. using Serilog;
  14. namespace Content.Server.Players.JobWhitelist;
  15. public sealed class JobWhitelistManager : IPostInjectInit
  16. {
  17. [Dependency] private readonly IConfigurationManager _config = default!;
  18. [Dependency] private readonly IServerDbManager _db = default!;
  19. [Dependency] private readonly INetManager _net = default!;
  20. [Dependency] private readonly IPlayerManager _player = default!;
  21. [Dependency] private readonly IPrototypeManager _prototypes = default!;
  22. [Dependency] private readonly UserDbDataManager _userDb = default!;
  23. private readonly Dictionary<NetUserId, HashSet<string>> _whitelists = new();
  24. public void Initialize()
  25. {
  26. _net.RegisterNetMessage<MsgJobWhitelist>();
  27. }
  28. private async Task LoadData(ICommonSession session, CancellationToken cancel)
  29. {
  30. var whitelists = await _db.GetJobWhitelists(session.UserId, cancel);
  31. cancel.ThrowIfCancellationRequested();
  32. _whitelists[session.UserId] = whitelists.ToHashSet();
  33. }
  34. private void FinishLoad(ICommonSession session)
  35. {
  36. SendJobWhitelist(session);
  37. }
  38. private void ClientDisconnected(ICommonSession session)
  39. {
  40. _whitelists.Remove(session.UserId);
  41. }
  42. public async void AddWhitelist(NetUserId player, ProtoId<JobPrototype> job)
  43. {
  44. if (_whitelists.TryGetValue(player, out var whitelists))
  45. whitelists.Add(job);
  46. await _db.AddJobWhitelist(player, job);
  47. if (_player.TryGetSessionById(player, out var session))
  48. SendJobWhitelist(session);
  49. }
  50. public bool IsAllowed(ICommonSession session, ProtoId<JobPrototype> job)
  51. {
  52. if (!_config.GetCVar(CCVars.GameRoleWhitelist))
  53. return true;
  54. if (!_prototypes.TryIndex(job, out var jobPrototype) ||
  55. !jobPrototype.Whitelisted)
  56. {
  57. return true;
  58. }
  59. return IsWhitelisted(session.UserId, job);
  60. }
  61. public bool IsWhitelisted(NetUserId player, ProtoId<JobPrototype> job)
  62. {
  63. if (!_whitelists.TryGetValue(player, out var whitelists))
  64. {
  65. Log.Error("Unable to check if player {Player} is whitelisted for {Job}. Stack trace:\\n{StackTrace}",
  66. player,
  67. job,
  68. Environment.StackTrace);
  69. return false;
  70. }
  71. return whitelists.Contains(job);
  72. }
  73. public async void RemoveWhitelist(NetUserId player, ProtoId<JobPrototype> job)
  74. {
  75. _whitelists.GetValueOrDefault(player)?.Remove(job);
  76. await _db.RemoveJobWhitelist(player, job);
  77. if (_player.TryGetSessionById(new NetUserId(player), out var session))
  78. SendJobWhitelist(session);
  79. }
  80. public void SendJobWhitelist(ICommonSession player)
  81. {
  82. var msg = new MsgJobWhitelist
  83. {
  84. Whitelist = _whitelists.GetValueOrDefault(player.UserId) ?? new HashSet<string>()
  85. };
  86. _net.ServerSendMessage(msg, player.Channel);
  87. }
  88. void IPostInjectInit.PostInject()
  89. {
  90. _userDb.AddOnLoadPlayer(LoadData);
  91. _userDb.AddOnFinishLoad(FinishLoad);
  92. _userDb.AddOnPlayerDisconnect(ClientDisconnected);
  93. }
  94. }