Beispiel #1
0
        /// <summary>
        ///     Loads the clans.
        /// </summary>
        public void Load()
        {
            lock (_syncLock)
            {
                _clans.Clear();
                using (var reader = _connection.QueryReader("SELECT * FROM Clans"))
                {
                    while (reader.Read())
                    {
                        var name           = reader.Get <string>("Clan");
                        var owner          = reader.Get <string>("Owner");
                        var prefix         = reader.Get <string>("Prefix");
                        var chatColor      = reader.Get <string>("ChatColor");
                        var motd           = reader.Get <string>("Motd");
                        var isFriendlyFire = reader.Get <int>("IsFriendlyFire") == 1;

                        var clan = new Clan(name, owner)
                        {
                            Prefix         = prefix,
                            ChatColor      = chatColor,
                            Motd           = motd,
                            IsFriendlyFire = isFriendlyFire
                        };
                        using (var reader2 =
                                   _connection.QueryReader("SELECT Permission FROM ClanHasPermission WHERE Clan = @0", name))
                        {
                            while (reader2.Read())
                            {
                                var permission = reader2.Get <string>("Permission");
                                clan.Permissions.Add(permission);
                            }
                        }
                        using (var reader2 =
                                   _connection.QueryReader("SELECT Rank, Tag FROM ClanRanks WHERE Clan = @0", name))
                        {
                            while (reader2.Read())
                            {
                                var rank     = reader2.Get <string>("Rank");
                                var tag      = reader2.Get <string>("Tag");
                                var clanRank = new ClanRank(rank, tag);

                                using (var reader3 = _connection.QueryReader(
                                           "SELECT Permission FROM ClanRankHasPermission WHERE Clan = @0 AND Rank = @1", clan,
                                           rank))
                                {
                                    while (reader3.Read())
                                    {
                                        var permission = reader3.Get <string>("Permission");
                                        clanRank.Permissions.Add(permission);
                                    }
                                }

                                clan.Ranks.Add(clanRank);
                            }
                        }

                        _clans.Add(clan);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Updates a clan.
        /// </summary>
        /// <param name="clan">The clan, which must not be <c>null</c>.</param>
        /// <exception cref="ArgumentNullException"><paramref name="clan" /> is null.</exception>
        public void Update([NotNull] Clan clan)
        {
            if (clan == null)
            {
                throw new ArgumentNullException(nameof(clan));
            }

            _connection.Query(
                "UPDATE Clans SET Prefix = @0, ChatColor = @1, Motd = @2, SpawnPoint = @3, IsFriendlyFire = @4, IsPrivate = @5 WHERE Clan = @6",
                clan.Prefix, clan.ChatColor, clan.Motd, clan.BaseCoordinates?.ToString(), clan.IsFriendlyFire ? 1 : 0,
                clan.IsPrivate ? 1 : 0, clan.Name);
            _connection.Query("DELETE FROM ClanHasBan WHERE Clan = @0", clan.Name);
            _connection.Query("DELETE FROM ClanHasPermission WHERE Clan = @0", clan.Name);
            _connection.Query("DELETE FROM ClanRanks WHERE Clan = @0", clan.Name);
            _connection.Query("DELETE FROM ClanRankHasPermission WHERE Clan = @0", clan.Name);
            using (var dbConnection = _connection.CloneEx())
            {
                dbConnection.Open();
                using (var transaction = dbConnection.BeginTransaction())
                {
                    using (var command = (SqliteCommand)dbConnection.CreateCommand())
                    {
                        command.CommandText = "INSERT INTO ClanHasBan (Clan, Username) VALUES (@0, @1)";
                        command.AddParameter("@0", clan.Name);
                        command.AddParameter("@1", null);

                        foreach (var bannedUsername in clan.BannedUsers)
                        {
                            command.Parameters["@1"].Value = bannedUsername;
                            command.ExecuteNonQuery();
                        }
                    }

                    using (var command = (SqliteCommand)dbConnection.CreateCommand())
                    {
                        command.CommandText = "INSERT INTO ClanHasPermission (Clan, Permission) VALUES (@0, @1)";
                        command.AddParameter("@0", clan.Name);
                        command.AddParameter("@1", null);

                        foreach (var permission in clan.Permissions)
                        {
                            command.Parameters["@1"].Value = permission;
                            command.ExecuteNonQuery();
                        }
                    }

                    foreach (var rank in clan.Ranks)
                    {
                        using (var command = (SqliteCommand)dbConnection.CreateCommand())
                        {
                            command.CommandText = "INSERT INTO ClanRanks (Clan, Rank, Tag) VALUES (@0, @1, @2)";
                            command.AddParameter("@0", clan.Name);
                            command.AddParameter("@1", rank.Name);
                            command.AddParameter("@2", rank.Tag);
                            command.ExecuteNonQuery();

                            foreach (var permission in rank.Permissions)
                            {
                                command.CommandText =
                                    "INSERT INTO ClanRankHasPermission (Clan, Rank, Permission) VALUES (@0, @1, @2)";
                                command.Parameters["@2"].Value = permission;
                                command.ExecuteNonQuery();
                            }
                        }
                    }

                    transaction.Commit();
                }
            }
        }
Beispiel #3
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="PlayerMetadata" /> class with the specified clan and rank.
 /// </summary>
 /// <param name="clan">The clan, which must not be <c>null</c>.</param>
 /// <param name="clanRank">The rank, which must not be <c>null</c>.</param>
 public PlayerMetadata([NotNull] Clan clan, [NotNull] ClanRank clanRank)
 {
     Clan = clan ?? throw new ArgumentNullException(nameof(clan));
     Rank = clanRank ?? throw new ArgumentNullException(nameof(clanRank));
 }
Beispiel #4
0
        /// <summary>
        ///     Loads the clans.
        /// </summary>
        public void Load()
        {
            lock (_syncLock)
            {
                _clans.Clear();
                using (var reader = _connection.QueryReader("SELECT * FROM Clans"))
                {
                    while (reader.Read())
                    {
                        var name                  = reader.Get <string>("Clan");
                        var owner                 = reader.Get <string>("Owner");
                        var prefix                = reader.Get <string>("Prefix");
                        var chatColor             = reader.Get <string>("ChatColor");
                        var motd                  = reader.Get <string>("Motd");
                        var spawnPointCoordinates = reader.Get <string>("SpawnPoint");
                        var splitCoordinates      =
                            Regex.Split(
                                Regex.Replace(spawnPointCoordinates ?? string.Empty, @"[{|}|X|:]", string.Empty), "Y");
                        var isFriendlyFire = reader.Get <int>("IsFriendlyFire") == 1;
                        var isPrivate      = reader.Get <int>("IsPrivate") == 1;

                        var clan = new Clan(name, owner)
                        {
                            Prefix          = prefix,
                            ChatColor       = chatColor,
                            Motd            = motd,
                            BaseCoordinates = string.IsNullOrWhiteSpace(spawnPointCoordinates)
                                ? (Vector2?)null
                                : new Vector2(float.Parse(splitCoordinates[0]), float.Parse(splitCoordinates[1])),
                            IsFriendlyFire = isFriendlyFire,
                            IsPrivate      = isPrivate
                        };
                        using (var reader2 =
                                   _connection.QueryReader("SELECT Username FROM ClanHasBan WHERE Clan = @0", name))
                        {
                            while (reader2.Read())
                            {
                                var username = reader2.Get <string>("Username");
                                clan.BannedUsers.Add(username);
                            }
                        }

                        using (var reader2 =
                                   _connection.QueryReader("SELECT Permission FROM ClanHasPermission WHERE Clan = @0", name))
                        {
                            while (reader2.Read())
                            {
                                var permission = reader2.Get <string>("Permission");
                                clan.Permissions.Add(permission);
                            }
                        }

                        using (var reader2 =
                                   _connection.QueryReader("SELECT Rank, Tag FROM ClanRanks WHERE Clan = @0", name))
                        {
                            while (reader2.Read())
                            {
                                var rank     = reader2.Get <string>("Rank");
                                var tag      = reader2.Get <string>("Tag");
                                var clanRank = new ClanRank(rank, tag);

                                using (var reader3 = _connection.QueryReader(
                                           "SELECT Permission FROM ClanRankHasPermission WHERE Clan = @0 AND Rank = @1", name,
                                           rank))
                                {
                                    while (reader3.Read())
                                    {
                                        var permission = reader3.Get <string>("Permission");
                                        clanRank.Permissions.Add(permission);
                                    }
                                }

                                clan.Ranks.Add(clanRank);
                            }
                        }

                        _clans.Add(clan);
                    }
                }
            }
        }