IEnumerator KickPlayer(ConnectedPlayer connPlayer, string reason,
                           bool ban = false, int banLengthInMinutes = 0)
    {
        string message  = "";
        string clientID = connPlayer.ClientId;

        if (ban)
        {
            message = $"You have been banned for {banLengthInMinutes}" +
                      $" minutes. Reason: {reason}";

            var index = banList.banEntries.FindIndex(x => x.userId == connPlayer.UserId);
            if (index != -1)
            {
                banList.banEntries.RemoveAt(index);
            }

            banList.banEntries.Add(new BanEntry
            {
                userId        = connPlayer.UserId,
                userName      = connPlayer.Username,
                minutes       = banLengthInMinutes,
                reason        = reason,
                dateTimeOfBan = DateTime.Now.ToString("O"),
                ipAddress     = connPlayer.Connection.address
            });

            File.WriteAllText(banPath, JsonUtility.ToJson(banList));
        }
        else
        {
            message = $"You have been kicked. Reason: {reason}";
        }

        SendClientLogMessage.SendLogToClient(connPlayer.GameObject, message, Category.Admin, true);
        yield return(WaitFor.Seconds(0.1f));

        if (connPlayer.Connection == null)
        {
            Logger.Log($"Not kicking, already disconnected: {connPlayer.Name}");
            yield break;
        }

        Logger.Log($"Kicking client {clientID} : {message}");
        InfoWindowMessage.Send(connPlayer.GameObject, message, "Disconnected");

        yield return(WaitFor.Seconds(1f));

        connPlayer.Connection.Disconnect();
        connPlayer.Connection.Dispose();

        while (!loggedOff.Contains(connPlayer))
        {
            yield return(WaitFor.EndOfFrame);
        }

        loggedOff.Remove(connPlayer);
    }
Exemplo n.º 2
0
    IEnumerator KickPlayer(ConnectedPlayer connPlayer, string reason,
                           bool ban = false, int banLengthInMinutes = 0)
    {
        string message  = "";
        string clientID = connPlayer.ClientId;

        if (ban)
        {
            message = $"You have been banned for {banLengthInMinutes}" +
                      $" minutes. Reason: {reason}";
        }
        else
        {
            message = $"You have been kicked. Reason: {reason}";
        }

        SendClientLogMessage.SendLogToClient(connPlayer.GameObject, message, Category.Connections, true);
        yield return(WaitFor.Seconds(0.1f));

        if (!connPlayer.Connection.isConnected)
        {
            Logger.Log($"Not kicking, already disconnected: {connPlayer.Name}", Category.Connections);
            yield break;
        }

        Logger.Log($"Kicking client {clientID} : {message}", Category.Connections);
        InfoWindowMessage.Send(connPlayer.GameObject, message, "Disconnected");
        //Chat.AddGameWideSystemMsgToChat($"Player '{player.Name}' got kicked: {raisins}");

        connPlayer.Connection.Disconnect();
        connPlayer.Connection.Dispose();

        while (!loggedOff.Contains(connPlayer))
        {
            yield return(WaitFor.EndOfFrame);
        }

        loggedOff.Remove(connPlayer);
    }
    IEnumerator KickPlayer(ConnectedPlayer connPlayer, string reason,
                           bool ban = false, int banLengthInMinutes = 0)
    {
        Logger.Log("Processing KickPlayer/ban for " + "\n"
                   + "UserId " + connPlayer?.UserId + "\n"
                   + "Username " + connPlayer?.Username + "\n"
                   + "address " + connPlayer?.Connection?.address + "\n"
                   + "clientId " + connPlayer?.ClientId + "\n"
                   );

        string message = "";

        if (ban)
        {
            message = $"You have been banned for {banLengthInMinutes}" +
                      $" minutes. Reason: {reason}";

            var index = banList.banEntries.FindIndex(x => x.userId == connPlayer.UserId);
            if (index != -1)
            {
                Logger.Log("removing pre-existing ban entry for userId of" + connPlayer.UserId);
                banList.banEntries.RemoveAt(index);
            }

            banList.banEntries.Add(new BanEntry
            {
                userId        = connPlayer?.UserId,
                userName      = connPlayer?.Username,
                minutes       = banLengthInMinutes,
                reason        = reason,
                dateTimeOfBan = DateTime.Now.ToString("O"),
                ipAddress     = connPlayer?.Connection?.address,
                clientId      = connPlayer?.ClientId
            });

            SaveBanList();
            if (banList.banEntries.Count != 0)
            {
                Logger.Log(banList.banEntries[banList.banEntries.Count - 1].ToString());
            }
        }
        else
        {
            message = $"You have been kicked. Reason: {reason}";
        }

        SendClientLogMessage.SendLogToClient(connPlayer.GameObject, message, Category.Admin, true);
        yield return(WaitFor.Seconds(0.1f));

        if (connPlayer.Connection == null)
        {
            Logger.Log($"Not kicking, already disconnected: {connPlayer.Name}");
            yield break;
        }

        Logger.Log($"Kicking client {connPlayer.Username} : {message}");
        InfoWindowMessage.Send(connPlayer.GameObject, message, "Disconnected");

        yield return(WaitFor.Seconds(1f));

        connPlayer.Connection.Disconnect();
        connPlayer.Connection.Dispose();

        while (!loggedOff.Contains(connPlayer))
        {
            yield return(WaitFor.EndOfFrame);
        }

        loggedOff.Remove(connPlayer);
    }