예제 #1
0
        /// <summary>
        /// Update the specified field of the synattr table.
        /// </summary>
        /// <param name="aMember">The syndicate member to update.</param>
        /// <param name="aField">The field to update.</param>
        /// <param name="aValue">The new value.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean UpdateField(Syndicate.Member aMember, String aField, object aValue)
        {
            bool success = false;

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = String.Format("UPDATE `synattr` SET `{0}` = @field WHERE `id` = @id", aField);
                    command.Parameters.AddWithValue("@id", aMember.Id);
                    command.Parameters.AddWithValue("@field", aValue);
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    try
                    {
                        int count = command.ExecuteNonQuery();
                        success = count == 1;
                    }
                    catch (MySqlException exc)
                    {
                        sLogger.Error("Failed to execute the following cmd : \"{0}\"\nError {1}: {2}",
                                      GetSqlCommand(command), exc.Number, exc.Message);
                    }
                }
            }

            return(success);
        }
예제 #2
0
        /// <summary>
        /// Try to create a new syndicate member for the specified player.
        /// </summary>
        /// <param name="aPlayer">The player.</param>
        /// <param name="aSyn">The syndicate of the player.</param>
        /// <param name="aRank">The rank of the player.</param>
        /// <param name="aProffer">The proffer of the player.</param>
        /// <param name="aOutSynMember">The newly created syndicate member.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean CreateSynMember(Player aPlayer, Syndicate aSyn, Byte aRank, UInt32 aProffer, out Syndicate.Member aOutSynMember)
        {
            bool created = false;

            aOutSynMember = null;

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = (
                        "INSERT INTO `synattr` (`id`, `syn_id`, `rank`, `proffer`) " +
                        "VALUES (@id, @syn_id, @rank, @proffer)");
                    command.Parameters.AddWithValue("@id", aPlayer.UniqId);
                    command.Parameters.AddWithValue("@syn_id", aSyn.Id);
                    command.Parameters.AddWithValue("@rank", aRank);
                    command.Parameters.AddWithValue("@proffer", aProffer);
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    try
                    {
                        command.ExecuteNonQuery();
                        created = true;

                        Int32 uid = aPlayer.UniqId;
                        aOutSynMember = new Syndicate.Member(uid, aPlayer.Name, (Byte)aPlayer.Level, (Syndicate.Rank)aRank, aProffer);

                        sLogger.Debug("Created syndicate member {0} in database.", aOutSynMember.Id);
                    }
                    catch (MySqlException exc)
                    {
                        if (exc.Number != 1062) // duplicate key
                        {
                            sLogger.Error("Failed to execute the following cmd : \"{0}\"\nError {1}: {2}",
                                          GetSqlCommand(command), exc.Number, exc.Message);
                        }
                    }
                }
            }

            return(created);
        }
예제 #3
0
        /// <summary>
        /// Load all syndicates in memory.
        /// </summary>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean LoadAllSyndicates()
        {
            bool success = false;

            sLogger.Info("Loading all syndicates in memory...");

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = (
                        "SELECT `id`, `name`, `announce`, `leader_id`, `leader_name`, `money`, `fealty_syn`, " +
                        "`enemy0`, `enemy1`, `enemy2`, `enemy3`, `enemy4`, " +
                        "`ally0`, `ally1`, `ally2`, `ally3`, `ally4` FROM `syndicate`");
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Syndicate syn = new Syndicate(
                                reader.GetUInt16("id"),
                                reader.GetString("name"),
                                reader.GetString("announce"),
                                reader.GetInt32("leader_id"),
                                reader.GetString("leader_name"),
                                reader.GetUInt32("money"),
                                reader.GetUInt16("fealty_syn"),
                                new UInt16[5] {
                                reader.GetUInt16("enemy0"), reader.GetUInt16("enemy1"), reader.GetUInt16("enemy2"), reader.GetUInt16("enemy3"), reader.GetUInt16("enemy4")
                            },
                                new UInt16[5] {
                                reader.GetUInt16("ally0"), reader.GetUInt16("ally1"), reader.GetUInt16("ally2"), reader.GetUInt16("ally3"), reader.GetUInt16("ally4")
                            });

                            if (!World.AllSyndicates.ContainsKey(syn.Id))
                            {
                                World.AllSyndicates.Add(syn.Id, syn);
                            }
                        }
                    }
                }

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT u.`id`, u.`name`, u.`level`, s.`syn_id`, s.`rank`, s.`proffer` FROM `synattr` s, `user` u WHERE u.`id` = s.`id`";
                    command.Prepare();

                    sLogger.Debug("Executing SQL: {0}", GetSqlCommand(command));

                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Int32  uid   = reader.GetInt32("id");
                            String name  = reader.GetString("name");
                            Byte   level = reader.GetByte("level");

                            UInt16 synId = reader.GetUInt16("syn_id");

                            Syndicate syn = null;
                            if (!World.AllSyndicates.TryGetValue(synId, out syn))
                            {
                                Database.LeaveSyn(name);
                                continue;
                            }

                            Syndicate.Rank rank    = (Syndicate.Rank)reader.GetByte("rank");
                            UInt32         proffer = reader.GetUInt32("proffer");

                            Syndicate.Member member = new Syndicate.Member(uid, name, level, rank, proffer);
                            if (syn.LeaderUID == uid)
                            {
                                syn.mLeader = member;
                            }
                            else
                            {
                                if (!syn.mMembers.ContainsKey(uid))
                                {
                                    syn.mMembers.Add(uid, member);
                                }
                            }
                        }
                    }
                }
            }

            return(success);
        }