Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.3KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using GameNetworkLayer.Shared;
  7. namespace CLre_server.API.MainServer
  8. {
  9. public class Moderator
  10. {
  11. private static Moderator _instance = null;
  12. public static Moderator Instance
  13. {
  14. get
  15. {
  16. if (_instance == null) Init();
  17. return _instance;
  18. }
  19. }
  20. internal static void Init()
  21. {
  22. if (_instance == null) _instance = new Moderator();
  23. }
  24. private ModerationEngine _moderationEngine;
  25. private Moderator()
  26. {
  27. _moderationEngine = new ModerationEngine();
  28. Server.Instance.PlayerConnect += (sender, args) =>
  29. {
  30. #if DEBUG
  31. Utility.Logging.MetaLog($"Player {args.PlayerId} is connecting, starting ban checker");
  32. #endif
  33. CheckConnectingPlayerAsap(args.PlayerId).Run();
  34. };
  35. }
  36. public bool DisconnectPlayerById(string publicId)
  37. {
  38. int? playerId = _moderationEngine.FindConnectedPlayerById(publicId);
  39. if (playerId.HasValue)
  40. {
  41. UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail);
  42. return true;
  43. }
  44. return false;
  45. }
  46. public bool DisconnectPlayerByName(string name)
  47. {
  48. int? playerId = _moderationEngine.FindConnectedPlayerByName(name);
  49. if (playerId.HasValue)
  50. {
  51. UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail);
  52. return true;
  53. }
  54. return false;
  55. }
  56. public bool BanPlayerById(string publicId)
  57. {
  58. List<string> bans = new List<string>(CLre.Config.bans);
  59. if (!bans.Contains(publicId))
  60. {
  61. bans.Add(publicId);
  62. CLre.Config.bans = bans.ToArray();
  63. }
  64. return DisconnectPlayerById(publicId);
  65. }
  66. public bool BanPlayerByName(string name)
  67. {
  68. List<string> bans = new List<string>(CLre.Config.bans);
  69. if (!bans.Contains(name))
  70. {
  71. bans.Add(name);
  72. CLre.Config.bans = bans.ToArray();
  73. }
  74. return DisconnectPlayerByName(name);
  75. }
  76. public bool IsModerator(string name)
  77. {
  78. foreach (string modName in CLre.Config.moderators)
  79. {
  80. if (string.Compare(name, modName, StringComparison.InvariantCultureIgnoreCase) == 0)
  81. {
  82. return true;
  83. }
  84. }
  85. Guid? publicId = _moderationEngine.FindConnectedPlayerGuidByName(name);
  86. if (publicId.HasValue)
  87. {
  88. foreach (string modGuid in CLre.Config.moderators)
  89. {
  90. if (modGuid == publicId.ToString())
  91. {
  92. return true;
  93. }
  94. }
  95. }
  96. return false;
  97. }
  98. public IEnumerator CheckConnectingPlayerAsap(int playerId)
  99. {
  100. while (Server.Instance.Players.Length <= playerId)
  101. {
  102. yield return null;
  103. yield return null;
  104. yield return null;
  105. yield return null;
  106. }
  107. var connector = Server.Instance.Players[playerId];
  108. if (CLre.Config.bans.Contains(connector.accountId.displayName)
  109. || CLre.Config.bans.Contains(connector.accountId.publicId.ToString()))
  110. {
  111. #if DEBUG
  112. Utility.Logging.MetaLog($"Banned player {connector.accountId.displayName} ({connector.accountId.publicId}) tried to connect, kicking");
  113. #endif
  114. UserVerification.Instance.DisconnectPlayer(playerId, NetworkDispatcherCode.GameDataVerificationFail);
  115. }
  116. #if DEBUG
  117. else
  118. {
  119. Utility.Logging.MetaLog($"Player {connector.accountId.displayName} ({connector.accountId.publicId}) is not banned, skipping auto-kick");
  120. }
  121. #endif
  122. }
  123. }
  124. }