Exemplo n.º 1
0
        private void UpdateDatabaseUsernames()
        {
            var usersToSend = UnnamedBans.Where(x => UsernameValid(x));

            UnnamedBans.RemoveAll(x => UsernameValid(x));
            if (usersToSend.Count() <= 0)
            {
                Puts("UpdateDatabaseUsernames() no users ready to update!");
                return;
            }
            var query = string.Format(MySqlConstants.ADD_BAN_TO_DATABASE, Settings.Instance.DatabaseName, Settings.Instance.TableName,
                                      string.Join(",", usersToSend.Select(x => $"('{x.SteamID}','{x.Username}','{x.Reason.Replace(";", "").Replace("'", "")}')").ToArray())
                                      );

            try
            {
                PerformDatabaseQuery(query, (responseRows) =>
                {
                });
            }
            catch
            {
                Puts("PushBansToServer() SQL Query failed!");
                Puts(query);
                timer.In(10f, UpdateDatabaseUsernames);
            }
            Puts($"Updated {usersToSend.Count()} usernames in the database!");
        }
Exemplo n.º 2
0
        private void PushBansToServer()
        {
            var bans = new List <PlayerBan>(BansToSend);

            bans.AddRange(UnnamedBans.Where(x => UsernameValid(x)));
            UnnamedBans.RemoveAll(x => UsernameValid(x));
            bans.AddRange(ServerUsers.GetAll(ServerUsers.UserGroup.Banned).Where(x => !Settings.Instance.databaseBans.ContainsKey(x.steamid)).Select(x => new PlayerBan(x)));
            if (bans.Count() <= 0)
            {
                Puts("All bans have been synced with the database.");
                return;
            }
            var query = string.Format(MySqlConstants.ADD_BAN_TO_DATABASE, Settings.Instance.DatabaseName, Settings.Instance.TableName,
                                      string.Join(",", bans.Select(x => $"('{x.SteamID}','{x.Username}','{x.Reason.Replace(";", "").Replace("'", "")}')").ToArray())
                                      );

            Puts(query);
            try
            {
                PerformDatabaseQuery(query, (responseRows) =>
                {
                    Settings.Instance.PushedAllLocalBansToDatabase = true;
                    Puts($"Bans pushed to the database");
                });
            }
            catch
            {
                Puts("PushBansToServer() SQL Query failed!");
                Puts(query);
                timer.In(10f, () =>
                {
                    PushBansToServer();
                });
            }
            Puts($"Started to send {bans.Count()} bans to database!");
        }
Exemplo n.º 3
0
        private void PullBansFromServer()
        {
            var query = string.Format(MySqlConstants.SELECT_ALL_BANS, Settings.Instance.DatabaseName, Settings.Instance.TableName);

            PerformDatabaseQuery(query, (responseRows) =>
            {
                if (responseRows == null)
                {
                    return;
                }

                HashSet <ulong> localBans = new HashSet <ulong>(ServerUsers.GetAll(ServerUsers.UserGroup.Banned).Select(x => x.steamid));

                Settings.Instance.databaseBans.Clear();

                if (responseRows.Any())
                {
                    //Add all database bans to local file
                    foreach (var row in responseRows)
                    {
                        var playerID = row["steamid"].ToString();

                        if (string.IsNullOrEmpty(playerID))
                        {
                            continue;
                        }

                        ulong steamID;
                        if (!ulong.TryParse(playerID, out steamID))
                        {
                            return;
                        }

                        var username = row["name"].ToString();
                        var reason   = row["reason"].ToString();

                        Settings.Instance.databaseBans.Add(steamID, new PlayerBan(steamID, reason));

                        if (!UsernameValid(steamID, username))
                        {
                            UnnamedBans.Add(new PlayerBan(steamID, reason));
                        }

                        if (localBans.Remove(steamID)) //No need to reban people
                        {
                            continue;
                        }

                        ServerUsers.Set(steamID, ServerUsers.UserGroup.Banned, playerID, reason);

                        BasePlayer player = BasePlayer.FindByID(steamID);

                        if (player != null)
                        {
                            player.Kick("Bans share across servers :)");
                        }
                    }

                    //Dont want to delete local ban list accidently
                    foreach (var player in localBans) //Unban users who aren't found on the database
                    {
                        ServerUsers.Remove(player);
                    }

                    if (UnnamedBans.Count > 0)
                    {
                        timer.In(10f, UpdateDatabaseUsernames);
                    }

                    Puts($"Updated ban list - {responseRows.Count} bans pulled from database");

                    Settings.Save();
                }
            });
        }