Exemple #1
0
        /// <summary>
        /// Try to retreive the player information for the specified client.
        /// </summary>
        /// <param name="aClient">The client</param>
        /// <returns>True on success, false otherwise.</returns>
        public static Boolean GetPlayerInfo(ref Client aClient)
        {
            bool success = false;

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = "SELECT * FROM `user` WHERE `account_id` = @account_id";
                    command.Parameters.AddWithValue("@account_id", aClient.AccountID);
                    command.Prepare();

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

                    using (var reader = command.ExecuteReader())
                    {
                        int count = 0;
                        while (reader.Read())
                        {
                            ++count;

                            Int32  uid    = reader.GetInt32("id");
                            Player player = new Player(uid, aClient);

                            player.Name = reader.GetString("name");
                            player.Mate = reader.GetString("mate");
                            player.Look = reader.GetUInt32("lookface");
                            player.Hair = reader.GetUInt16("hair");

                            player.Money   = reader.GetUInt32("money");
                            player.WHMoney = reader.GetUInt32("money_saved");

                            player.Profession     = reader.GetByte("profession");
                            player.Level          = reader.GetByte("level");
                            player.Exp            = reader.GetUInt32("exp");
                            player.Metempsychosis = reader.GetByte("metempsychosis");

                            player.FirstProfession  = reader.GetByte("first_profession");
                            player.FirstLevel       = reader.GetByte("first_level");
                            player.SecondProfession = reader.GetByte("second_profession");
                            player.SecondLevel      = reader.GetByte("second_level");

                            player.Strength  = reader.GetUInt16("force");
                            player.Agility   = reader.GetUInt16("dexterity");
                            player.Vitality  = reader.GetUInt16("health");
                            player.Spirit    = reader.GetUInt16("soul");
                            player.AddPoints = reader.GetUInt16("add_points");

                            player.CurHP = reader.GetUInt16("life");
                            player.CurMP = reader.GetUInt16("mana");

                            player.PkPoints = reader.GetInt16("pk");
                            player.VPs      = reader.GetInt32("virtue");

                            UInt32 mapId = reader.GetUInt32("record_map");
                            if (MapManager.TryGetMap(mapId, out player.Map))
                            {
                                player.X = reader.GetUInt16("record_x");
                                player.Y = reader.GetUInt16("record_y");
                            }
                            else
                            {
                                // map not found...
                                if (MapManager.TryGetMap(1002, out player.Map))
                                {
                                    player.X = 400;
                                    player.Y = 400;
                                }
                            }

                            player.PrevMap = player.Map;
                            player.PrevX   = player.X;
                            player.PrevY   = player.Y;

                            player.mLockPin = reader.GetUInt32("depot_pin");

                            // TODO KO in superman table
                            // Player.KO = AMSXml.GetValue("Informations", "KO", 0);

                            player.TimeAdd = reader.GetInt32("time_add");

                            player.DblExpEndTime = Environment.TickCount + (reader.GetInt32("dbl_exp_time") * 1000);
                            player.LuckyTime     = reader.GetInt32("lucky_time");

                            player.JailC = reader.GetByte("jail");

                            #region Friends / Enemies
                            String[] parts = null;

                            String friends = reader.GetString("friends");
                            parts = friends.Split(',');
                            foreach (String friend in parts)
                            {
                                String[] info = friend.Split(':');
                                if (info.Length < 2)
                                {
                                    continue;
                                }

                                Int32 friendId = 0;
                                if (!Int32.TryParse(info[0], out friendId))
                                {
                                    continue;
                                }

                                if (!player.Friends.ContainsKey(friendId))
                                {
                                    player.Friends.Add(friendId, info[1]);
                                }
                            }

                            String enemies = reader.GetString("enemies");
                            parts = enemies.Split(',');
                            foreach (String enemy in parts)
                            {
                                String[] info = enemy.Split(':');
                                if (info.Length < 2)
                                {
                                    continue;
                                }

                                Int32 enemyId = 0;
                                if (!Int32.TryParse(info[0], out enemyId))
                                {
                                    continue;
                                }

                                if (!player.Enemies.ContainsKey(enemyId))
                                {
                                    player.Enemies.Add(enemyId, info[1]);
                                }
                            }
                            #endregion

                            //MyMath
                            player.CalcMaxHP();
                            player.CalcMaxMP();

                            Database.GetPlayerSyndicate(player);
                            Database.GetPlayerWeaponSkills(player);
                            Database.GetPlayerMagics(player);

                            aClient.Player = player;
                        }

                        success = count == 1;
                    }
                }
            }

            return(success);
        }
Exemple #2
0
        public static Boolean Save(Player aPlayer, bool aIsOnline)
        {
            bool success = false;

            // TODO superman table
            //AMSXml.SetValue("Informations", "KO", 0);

            String friends = "";

            foreach (var friend in aPlayer.Friends)
            {
                friends += friend.Key.ToString() + ":" + friend.Value + ",";
            }
            if (friends.Length > 0)
            {
                friends = friends.Substring(0, friends.Length - 1);
            }

            String enemies = "";

            foreach (var enemy in aPlayer.Enemies)
            {
                enemies += enemy.Key.ToString() + ":" + enemy.Value + ",";
            }
            if (enemies.Length > 0)
            {
                enemies = enemies.Substring(0, enemies.Length - 1);
            }

            using (var connection = sDefaultPool.GetConnection())
            {
                using (var command = connection.CreateCommand())
                {
                    command.CommandText = (
                        "UPDATE `user` SET " +
                        "`money` = @money, `money_saved` = @money_saved,  " +
                        "`profession` = @profession, `level` = @level, `exp` = @exp, " +
                        "`first_profession` = @first_profession, `first_level` = @first_level, `second_profession` = @second_profession, `second_level` = @second_level, " +
                        "`force` = @force, `dexterity` = @dexterity, `health` = @health, `soul` = @soul, `add_points` = @add_points, " +
                        "`life` = @life, `mana` = @mana, `pk` = @pk, `virtue` = @virtue, " +
                        "`time_add` = @time_add, " +
                        "`dbl_exp_time` = @dbl_exp_time, `lucky_time` = @lucky_time, " +
                        "`record_map` = @record_map, `record_x` = @record_x, `record_y` = @record_y, `friends` = @friends, `enemies` = @enemies, `online` = @online WHERE `id` = @id");

                    command.Parameters.AddWithValue("@id", aPlayer.UniqId);

                    command.Parameters.AddWithValue("@money", aPlayer.Money);
                    command.Parameters.AddWithValue("@money_saved", aPlayer.WHMoney);

                    command.Parameters.AddWithValue("@profession", aPlayer.Profession);
                    command.Parameters.AddWithValue("@level", aPlayer.Level);
                    command.Parameters.AddWithValue("@exp", aPlayer.Exp);

                    command.Parameters.AddWithValue("@first_profession", aPlayer.FirstProfession);
                    command.Parameters.AddWithValue("@first_level", aPlayer.FirstLevel);
                    command.Parameters.AddWithValue("@second_profession", aPlayer.SecondProfession);
                    command.Parameters.AddWithValue("@second_level", aPlayer.SecondLevel);

                    command.Parameters.AddWithValue("@force", aPlayer.Strength);
                    command.Parameters.AddWithValue("@dexterity", aPlayer.Agility);
                    command.Parameters.AddWithValue("@health", aPlayer.Vitality);
                    command.Parameters.AddWithValue("@soul", aPlayer.Spirit);
                    command.Parameters.AddWithValue("@add_points", aPlayer.AddPoints);

                    command.Parameters.AddWithValue("@life", aPlayer.CurHP > 0 ? aPlayer.CurHP : 1);
                    command.Parameters.AddWithValue("@mana", aPlayer.CurMP);

                    command.Parameters.AddWithValue("@pk", aPlayer.PkPoints);
                    command.Parameters.AddWithValue("@virtue", aPlayer.VPs);

                    if (aPlayer.CurHP > 0)
                    {
                        if (!aPlayer.Map.IsRecord_Disable())
                        {
                            command.Parameters.AddWithValue("@record_map", aPlayer.Map.Id);
                            command.Parameters.AddWithValue("@record_x", aPlayer.X);
                            command.Parameters.AddWithValue("@record_y", aPlayer.Y);
                        }
                        else
                        {
                            GameMap rebornMap = null;
                            if (MapManager.TryGetMap(aPlayer.Map.RebornMap, out rebornMap))
                            {
                                command.Parameters.AddWithValue("@record_map", rebornMap.Id);
                                command.Parameters.AddWithValue("@record_x", rebornMap.PortalX);
                                command.Parameters.AddWithValue("@record_y", rebornMap.PortalY);
                            }
                            else
                            {
                                command.Parameters.AddWithValue("@record_map", 1002);
                                command.Parameters.AddWithValue("@record_x", 400);
                                command.Parameters.AddWithValue("@record_y", 400);
                            }
                        }
                    }
                    else
                    {
                        GameMap rebornMap;
                        if (MapManager.TryGetMap(aPlayer.Map.RebornMap, out rebornMap))
                        {
                            command.Parameters.AddWithValue("@record_map", rebornMap.Id);
                            command.Parameters.AddWithValue("@record_x", rebornMap.PortalX);
                            command.Parameters.AddWithValue("@record_y", rebornMap.PortalY);
                        }
                    }

                    command.Parameters.AddWithValue("@time_add", aPlayer.TimeAdd);

                    if (aPlayer.DblExpEndTime == 0)
                    {
                        command.Parameters.AddWithValue("@dbl_exp_time", 0);
                    }
                    else
                    {
                        command.Parameters.AddWithValue("@dbl_exp_time", (Int32)((aPlayer.DblExpEndTime - Environment.TickCount) / 1000));
                    }
                    command.Parameters.AddWithValue("@lucky_time", aPlayer.LuckyTime);

                    command.Parameters.AddWithValue("@friends", friends);
                    command.Parameters.AddWithValue("@enemies", enemies);
                    command.Parameters.AddWithValue("@online", aIsOnline);
                    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);
        }
Exemple #3
0
        /// <summary>
        /// Start the execution of the server.
        /// </summary>
        public static void Run()
        {
            if (!File.Exists(CONFIG_FILE))
            {
                sLogger.Fatal("Couldn't find the configuration file at '{0}'.", CONFIG_FILE);
                Environment.Exit(0);
            }

            UInt16 port         = 0;
            Int32  backlog      = 0;
            Byte   threadAmount = 1;

            String mysql_host, mysql_db, mysql_user, mysql_pwd;
            int    mysql_nb_connections;

            String acc_host, acc_db, acc_user, acc_pwd;
            int    acc_nb_connections;

            String mongo_host, mongo_db, mongo_user, mongo_pwd;

            using (Ini doc = new Ini(CONFIG_FILE))
            {
                port         = doc.ReadUInt16("Socket", "Port", 5816);
                backlog      = doc.ReadInt32("Socket", "BackLog", 100);
                threadAmount = doc.ReadUInt8("Socket", "ThreadAmount", 1);

                Name             = doc.ReadString("Program", "Name", "NULL");
                Program.Encoding = Encoding.GetEncoding(doc.ReadString("Program", "Encoding", "iso-8859-1"));
                Program.Debug    = doc.ReadBoolean("Program", "Debug", false);

                mysql_host           = doc.ReadString("MySQL", "Host", "localhost");
                mysql_db             = doc.ReadString("MySQL", "Database", "zfserver");
                mysql_user           = doc.ReadString("MySQL", "Username", "zfserver");
                mysql_pwd            = doc.ReadString("MySQL", "Password", "");
                mysql_nb_connections = doc.ReadInt32("MySQL", "NbConnections", 1);

                acc_host           = doc.ReadString("AccMySQL", "Host", "localhost");
                acc_db             = doc.ReadString("AccMySQL", "Database", "zfserver");
                acc_user           = doc.ReadString("AccMySQL", "Username", "zfserver");
                acc_pwd            = doc.ReadString("AccMySQL", "Password", "");
                acc_nb_connections = doc.ReadInt32("AccMySQL", "NbConnections", 1);

                mongo_host = doc.ReadString("MongoDB", "Host", "localhost");
                mongo_db   = doc.ReadString("MongoDB", "Database", "zfserver");
                mongo_user = doc.ReadString("MongoDB", "Username", "zfserver");
                mongo_pwd  = doc.ReadString("MongoDB", "Password", "");
            }

            StrRes.LoadStrRes();

            if (!Database.SetupAccMySQL(acc_nb_connections, acc_host, acc_db, acc_user, acc_pwd))
            {
                sLogger.Fatal("Failed to setup MySQL...");
                Environment.Exit(0);
            }

            if (!Database.SetupMySQL(mysql_nb_connections, mysql_host, mysql_db, mysql_user, mysql_pwd))
            {
                sLogger.Fatal("Failed to setup MySQL...");
                Environment.Exit(0);
            }

            if (!Database.SetupMongo(mongo_host, mongo_db, mongo_user, mongo_pwd))
            {
                sLogger.Fatal("Failed to setup MongoDB...");
                Environment.Exit(0);
            }


            // load Lua VM
            Task.RegisterFunctions(); // register Lua functions
            TaskHandler.LoadAllTasks();

            if (!MapManager.LoadData())
            {
                sLogger.Fatal("Failed to load DMaps in memory...");
                Environment.Exit(0);
            }

            Database.GetItemsInfo();
            Database.GetItemsBonus();
            Database.GetShopsInfo();
            Database.GetMagicsInfo();
            Database.GetLevelsInfo();
            Database.GetPointAllotInfo();
            Database.LoadWeaponSkillReqExp();
            Database.GetPortalsInfo();
            Database.GetAllMaps();
            Database.GetMonstersInfo();
            Database.GetSpawnsInfo();
            Database.GetAllNPCs();
            Database.GetAllItems();
            Database.LoadAllSyndicates();

            ServiceEventsListener.Create();

            NetworkIO = new NetworkIO(threadAmount);

            sSocket               = new TcpServer();
            sSocket.OnConnect    += new NetworkClientConnection(ConnectionHandler);
            sSocket.OnReceive    += new NetworkClientReceive(ReceiveHandler);
            sSocket.OnDisconnect += new NetworkClientConnection(DisconnectionHandler);
            sSocket.Listen(port, backlog);
            sSocket.Accept();

            LaunchTime = DateTime.Now;
            sLogger.Info("Waiting for new connection...");
        }