|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using GameNetworkLayer.Shared;
-
- namespace CLre_server.API.MainServer
- {
- public class Moderator
- {
- private static Moderator _instance = null;
-
- public static Moderator Instance
- {
- get
- {
- if (_instance == null) Init();
- return _instance;
- }
- }
-
- internal static void Init()
- {
- if (_instance == null) _instance = new Moderator();
- }
-
- private ModerationEngine _moderationEngine;
-
- private Moderator()
- {
- _moderationEngine = new ModerationEngine();
- Server.Instance.PlayerConnect += (sender, args) =>
- {
- #if DEBUG
- Utility.Logging.MetaLog($"Player {args.PlayerId} is connecting, starting ban checker");
- #endif
- CheckConnectingPlayerAsap(args.PlayerId).Run();
- };
- }
-
- public bool DisconnectPlayerById(string publicId)
- {
- int? playerId = _moderationEngine.FindConnectedPlayerById(publicId);
- if (playerId.HasValue)
- {
- UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail);
- return true;
- }
-
- return false;
- }
-
- public bool DisconnectPlayerByName(string name)
- {
- int? playerId = _moderationEngine.FindConnectedPlayerByName(name);
- if (playerId.HasValue)
- {
- UserVerification.Instance.DisconnectPlayer(playerId.Value, NetworkDispatcherCode.GameDataVerificationFail);
- return true;
- }
-
- return false;
- }
-
- public bool BanPlayerById(string publicId)
- {
- List<string> bans = new List<string>(CLre.Config.bans);
- if (!bans.Contains(publicId))
- {
- bans.Add(publicId);
- CLre.Config.bans = bans.ToArray();
- }
- return DisconnectPlayerById(publicId);
- }
-
- public bool BanPlayerByName(string name)
- {
- List<string> bans = new List<string>(CLre.Config.bans);
- if (!bans.Contains(name))
- {
- bans.Add(name);
- CLre.Config.bans = bans.ToArray();
- }
- return DisconnectPlayerByName(name);
- }
-
- public bool IsModerator(string name)
- {
- foreach (string modName in CLre.Config.moderators)
- {
- if (string.Compare(name, modName, StringComparison.InvariantCultureIgnoreCase) == 0)
- {
- return true;
- }
- }
-
- Guid? publicId = _moderationEngine.FindConnectedPlayerGuidByName(name);
- if (publicId.HasValue)
- {
- foreach (string modGuid in CLre.Config.moderators)
- {
- if (modGuid == publicId.ToString())
- {
- return true;
- }
- }
- }
-
- return false;
- }
-
- public IEnumerator CheckConnectingPlayerAsap(int playerId)
- {
- while (Server.Instance.Players.Length <= playerId)
- {
- yield return null;
- yield return null;
- yield return null;
- yield return null;
- }
- var connector = Server.Instance.Players[playerId];
- if (CLre.Config.bans.Contains(connector.accountId.displayName)
- || CLre.Config.bans.Contains(connector.accountId.publicId.ToString()))
- {
- #if DEBUG
- Utility.Logging.MetaLog($"Banned player {connector.accountId.displayName} ({connector.accountId.publicId}) tried to connect, kicking");
- #endif
- UserVerification.Instance.DisconnectPlayer(playerId, NetworkDispatcherCode.GameDataVerificationFail);
- }
- #if DEBUG
- else
- {
- Utility.Logging.MetaLog($"Player {connector.accountId.displayName} ({connector.accountId.publicId}) is not banned, skipping auto-kick");
- }
- #endif
- }
- }
- }
|