コード例 #1
0
        /// <summary>
        /// Update the specified field of the syndicate table.
        /// </summary>
        /// <param name="aSyndicate">The syndicate 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 aSyn, String aField, object aValue)
        {
            bool success = false;

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = String.Format("UPDATE `syndicate` SET `{0}` = @field WHERE `id` = @id", aField);
                    command.Parameters.AddWithValue("@id", aSyn.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>
        /// Delete the specified syndicate.
        /// </summary>
        /// <param name="aSyn">The syndicate to delete.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean Delete(Syndicate aSyn)
        {
            bool success = false;

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "DELETE FROM `syndicate` WHERE `id` = @id";
                    command.Parameters.AddWithValue("@id", aSyn.Id);
                    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);
        }
コード例 #3
0
        public bool LeaveSyn(Player aPlayer, bool aKickout, bool aSynchro)
        {
            Syndicate syn = aPlayer.Syndicate;

            // TODO destroy syn if player is leader

            aPlayer.Syndicate = null;
            Database.LeaveSyn(aPlayer.Name);

            lock (mMembers) { mMembers.Remove(aPlayer.UniqId); }

            if (aSynchro)
            {
                SynchroInfo(aPlayer, true);
            }

            if (aKickout)
            {
                Msg msg = new MsgTalk(
                    "SYSTEM", "ALLUSERS",
                    String.Format(StrRes.STR_KICKOUT, aPlayer.Name),
                    Channel.Syndicate, Color.White);
                World.BroadcastSynMsg(this, msg);
            }

            return(true);
        }
コード例 #4
0
        /// <summary>
        /// Try to create a new sub-syn for the specified syndicate with the specified
        /// player as the branch manager.
        /// </summary>
        /// <param name="aFealtySyn">The fealty syndicate.</param>
        /// <param name="aLeader">The branch manager.</param>
        /// <param name="aName">The name of the syndicate.</param>
        /// <param name="aMoney">The money of the syndicate.</param>
        /// <param name="aOutSyn">The newly created syndicate.</param>
        /// <returns>True on success, false otherwise.</returns>
        private static Boolean Create(Syndicate aFealtySyn, Player aLeader, String aName, UInt32 aMoney, out Syndicate aOutSyn)
        {
            if (Database.CreateSyndicate(aFealtySyn, aLeader, aName, aMoney, out aOutSyn))
            {
                aLeader.Syndicate = aOutSyn;
                return(true);
            }

            return(false);
        }
コード例 #5
0
        public Syndicate GetMasterSyn()
        {
            if (mFealtySyn == 0)
            {
                return(this);
            }

            Syndicate syn = null;

            if (!World.AllSyndicates.TryGetValue(mFealtySyn, out syn))
            {
                return(this);
            }

            return(syn);
        }
コード例 #6
0
        public static void BroadcastSynMsg(Syndicate Syndicate, Msg aMsg)
        {
            Player Member = null;

            if (World.AllPlayers.TryGetValue(Syndicate.Leader.Id, out Member))
            {
                Member.Send(aMsg);
            }

            foreach (Int32 MemberUID in Syndicate.Members.Keys)
            {
                if (World.AllPlayers.TryGetValue(MemberUID, out Member))
                {
                    Member.Send(aMsg);
                }
            }
        }
コード例 #7
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);
        }
コード例 #8
0
        public Boolean CreateSubSyn(Player aLeader, String aName, UInt32 aMoney, out Syndicate aOutSyn)
        {
            aOutSyn = null;

            if (mFealtySyn != 0)
            {
                return(false);
            }

            if (Create(this, aLeader, aName, aMoney, out aOutSyn))
            {
                return(true);
            }

            // synchro relation
            //var msg = new MsgSyndicate(
            //    CMsgSyndicate	msg;
            //    IF_OK(msg.Create(SET_SYN, idNewSyn, idFealty))
            //        pFealty->BroadcastSynMsg(&msg);
            //    return idNewSyn;

            return(false);
        }
コード例 #9
0
        /// <summary>
        /// Try to create a new sub-syn for the specified syndicate with the specified
        /// player as the branch manager.
        /// </summary>
        /// <param name="aFealtySyn">The fealty syndicate.</param>
        /// <param name="aLeader">The branch manager.</param>
        /// <param name="aName">The name of the syndicate.</param>
        /// <param name="aMoney">The money of the syndicate.</param>
        /// <param name="aOutSyn">The newly created syndicate.</param>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean CreateSyndicate(Syndicate aFealtySyn, Player aLeader, String aName, UInt32 aMoney, out Syndicate aOutSyn)
        {
            bool created = false;

            aOutSyn = null;

            using (var connection = sDefaultPool.GetConnection())
            {
                // TODO use transactions...

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = (
                        "INSERT INTO `syndicate` (`name`, `announce`, `leader_id`, `leader_name`, `money`, `fealty_syn`) " +
                        "VALUES (@name, @announce, @leader_id, @leader_name, @money, @fealty_syn)");
                    command.Parameters.AddWithValue("@name", aName);
                    command.Parameters.AddWithValue("@announce", "");
                    command.Parameters.AddWithValue("@leader_id", aLeader.UniqId);
                    command.Parameters.AddWithValue("@leader_name", aLeader.Name);
                    command.Parameters.AddWithValue("@money", aMoney);
                    command.Parameters.AddWithValue("@fealty_syn", aFealtySyn.Id);
                    command.Prepare();

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

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

                        UInt16 uid = (UInt16)command.LastInsertedId;
                        aOutSyn = new Syndicate(uid, aName, "", aLeader.UniqId, aLeader.Name, aMoney, aFealtySyn.Id,
                                                new UInt16[Syndicate.MAX_ENEMIES_COUNT] {
                            0, 0, 0, 0, 0
                        },
                                                new UInt16[Syndicate.MAX_ALLIES_COUNT] {
                            0, 0, 0, 0, 0
                        });

                        sLogger.Debug("Created syndicate {0} in database.", aOutSyn.Id);

                        bool success = aFealtySyn.LeaveSyn(aLeader, false, false);
                        if (success)
                        {
                            created = Database.CreateSynMember(aLeader, aOutSyn, (Byte)Syndicate.Rank.BranchMgr, aMoney, out aOutSyn.mLeader);
                        }

                        if (created)
                        {
                            lock (World.AllSyndicates)
                                World.AllSyndicates.Add(aOutSyn.Id, aOutSyn);
                        }
                    }
                    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);
        }
コード例 #10
0
        public bool Destroy()
        {
            lock (World.AllSyndicates)
            {
                if (World.AllSyndicates.ContainsKey(Id))
                {
                    Msg msg = null;

                    msg = new MsgTalk(
                        "SYSTEM", "ALLUSERS",
                        String.Format(StrRes.STR_SYN_DISBAND, Name),
                        Channel.GM, Color.White);
                    World.BroadcastMsg(msg);

                    msg = new MsgSyndicate((UInt16)Id, MsgSyndicate.Action.DestroySyn);
                    World.BroadcastMsg(msg);

                    Syndicate master = GetMasterSyn();
                    Player    player = null;

                    lock (mMembers)
                    {
                        List <Syndicate.Member> members = new List <Syndicate.Member>(mMembers.Values);
                        members.Add(mLeader);

                        foreach (Syndicate.Member member in members)
                        {
                            if (master == this)
                            {
                                if (World.AllPlayers.TryGetValue(member.Id, out player))
                                {
                                    LeaveSyn(player, false, true);
                                }
                                else
                                {
                                    Database.LeaveSyn(member.Name);
                                }
                            }
                            else
                            {
                                master.AddMember(player);
                            }
                        }
                    }

                    foreach (UInt16 allyId in Allies)
                    {
                        Syndicate ally = null;
                        if (World.AllSyndicates.TryGetValue(allyId, out ally))
                        {
                            if (ally.Allies.Contains(Id))
                            {
                                var allies = ally.Allies;
                                allies[allies.IndexOf(Id)] = 0;
                                ally.Allies = allies;

                                msg = new MsgSyndicate(Id, MsgSyndicate.Action.ClearAlly);
                                World.BroadcastSynMsg(ally, msg);
                            }
                        }
                    }

                    World.AllSyndicates.Remove(Id);
                    return(Database.Delete(this));
                }
            }

            return(false);
        }
コード例 #11
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);
        }