internal PlayerItem(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mInventoryType = (EInventoryType)(byte)pQuery["inventory_type"];
     mInventorySlot = (short)pQuery["inventory_slot"];
     mItemIdentifier = (int)pQuery["item_identifier"];
     mUnusedScrollSlots = (byte)pQuery["unused_scroll_slots"];
     mUsedScrollSlots = (byte)pQuery["used_scroll_slots"];
     mStrength = (ushort)pQuery["strength"];
     mDexterity = (ushort)pQuery["dexterity"];
     mIntellect = (ushort)pQuery["intellect"];
     mLuck = (ushort)pQuery["luck"];
     mHealth = (ushort)pQuery["health"];
     mMana = (ushort)pQuery["mana"];
     mWeaponAttack = (ushort)pQuery["weapon_attack"];
     mMagicAttack = (ushort)pQuery["magic_attack"];
     mWeaponDefense = (ushort)pQuery["weapon_defense"];
     mMagicDefense = (ushort)pQuery["magic_defense"];
     mAccuracy = (ushort)pQuery["accuracy"];
     mAvoidance = (ushort)pQuery["avoidance"];
     mHands = (ushort)pQuery["hands"];
     mSpeed = (ushort)pQuery["speed"];
     mJump = (ushort)pQuery["jump"];
     mQuantity = (ushort)pQuery["quantity"];
     mOwner = (string)pQuery["owner"];
     mFlags = (ushort)pQuery["flags"];
 }
 internal PlayerBuddy(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mBuddyIdentifier = (int)pQuery["buddy_identifier"];
     mName = (string)pQuery["name"];
     mStatus = (byte)pQuery["status"];
 }
 internal PlayerTeleport(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mMapIdentifier = (int)pQuery["map_identifier"];
     mSlot = (byte)pQuery["slot"];
     mVIP = (bool)pQuery["vip"];
 }
 internal Account(DatabaseQuery pQuery)
 {
     mIdentifier = (int)pQuery["identifier"];
     mUsername = (string)pQuery["username"];
     mPassword = (string)pQuery["password"];
     mLevel = (byte)pQuery["level"];
 }
 internal PlayerCards(DatabaseQuery pQuery)
 {
     while (pQuery.NextRow())
     {
         PlayerCard card = new PlayerCard(pQuery);
         mCards.Add(card.CardIdentifier, card);
     }
 }
 internal PlayerKeymap(DatabaseQuery pQuery)
 {
     for (byte index = 0; index < MAX_KEYS; ++index)
     {
         mTypes[index] = (byte)pQuery["type_" + index];
         mActions[index] = (uint)pQuery["action_" + index];
     }
 }
 internal PlayerSkill(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mSkillIdentifier = (int)pQuery["skill_identifier"];
     mLevel = (byte)pQuery["level"];
     mMaxLevel = (byte)pQuery["max_level"];
     mCooldown = (ushort)pQuery["cooldown"];
 }
 internal PlayerQuests(DatabaseQuery pQuery)
 {
     while (pQuery.NextRow())
     {
         PlayerQuest quest = new PlayerQuest(pQuery);
         mQuests.Add(quest.QuestIdentifier, quest);
     }
 }
 internal PlayerSkills(DatabaseQuery pQuery)
 {
     while (pQuery.NextRow())
     {
         PlayerSkill skill = new PlayerSkill(pQuery);
         mSkills.Add(skill.SkillIdentifier, skill);
     }
 }
 internal PlayerTeleports(DatabaseQuery pQuery)
 {
     mTeleports = new PlayerTeleport[MAX_TELEPORTS + MAX_VIP_TELEPORTS];
     while (pQuery.NextRow())
     {
         PlayerTeleport teleport = new PlayerTeleport(pQuery);
         mTeleports[teleport.Slot] = teleport;
     }
 }
 internal PlayerQuest(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mQuestIdentifier = (ushort)pQuery["quest_identifier"];
     mMobIdentifier = (int)pQuery["mob_identifier"];
     mMobKills = (ushort)pQuery["mob_kills"];
     mState = (string)pQuery["state"];
     mCompleted = (long)pQuery["completed"];
 }
 internal PlayerMacro(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mSlot = (byte)pQuery["slot"];
     mName = (string)pQuery["name"];
     mShout = (bool)pQuery["shout"];
     mFirstSkillIdentifier = (int)pQuery["first_skill_identifier"];
     mSecondSkillIdentifier = (int)pQuery["second_skill_identifier"];
     mThirdSkillIdentifier = (int)pQuery["third_skill_identifier"];
 }
 internal PlayerMacros(DatabaseQuery pQuery)
 {
     mMacros = new PlayerMacro[0];
     while (pQuery.NextRow())
     {
         PlayerMacro macro = new PlayerMacro(pQuery);
         if (mMacros.Length <= macro.Slot) Array.Resize(ref mMacros, macro.Slot + 1);
         mMacros[macro.Slot] = macro;
     }
 }
 internal PlayerItems(int pMesos, byte[] pSlots, DatabaseQuery pQuery)
 {
     mMesos = pMesos;
     mEquipped = new PlayerItem[(byte)EEquipmentSlot.Count];
     mCashEquipped = new PlayerItem[(byte)EEquipmentSlot.Count];
     mItems = new PlayerItem[pSlots.Length][];
     for (int index = 0; index < pSlots.Length; ++index) mItems[index] = new PlayerItem[pSlots[index]];
     while (pQuery.NextRow())
     {
         PlayerItem item = new PlayerItem(pQuery);
         if (item.InventoryType == EInventoryType.Equipment && item.InventorySlot < 0)
         {
             if (item.InventorySlot < -100) mCashEquipped[(-item.InventorySlot) - 100] = item;
             else mEquipped[-item.InventorySlot] = item;
         }
         else mItems[(byte)item.InventoryType][item.InventorySlot] = item;
     }
 }
        public Player(Client pClient, DatabaseQuery pQuery)
        {
            mClient = pClient;
            mRandom = new Random();

            mIdentifier = (int)pQuery["identifier"];
            mName = (string)pQuery["name"];
            mGender = (byte)pQuery["gender"];
            mSkin = (byte)pQuery["skin"];
            mEyesIdentifier = (int)pQuery["eyes_identifier"];
            mHairIdentifier = (int)pQuery["hair_identifier"];
            mLevel = (byte)pQuery["level"];
            mJob = (ushort)pQuery["job"];
            mStrength = (ushort)pQuery["strength"];
            mDexterity = (ushort)pQuery["dexterity"];
            mIntellect = (ushort)pQuery["intellect"];
            mLuck = (ushort)pQuery["luck"];
            mHealth = (ushort)pQuery["health"];
            mMaxHealth = (ushort)pQuery["max_health"];
            mMana = (ushort)pQuery["mana"];
            mMaxMana = (ushort)pQuery["max_mana"];
            mAbilityPoints = (ushort)pQuery["ability_points"];
            mSkillPoints = (ushort)pQuery["skill_points"];
            mExperience = (int)pQuery["experience"];
            mFame = (ushort)pQuery["fame"];

            int mapIdentifier = (int)pQuery["map_identifier"];
            MapData mapData = Server.GetMapData(mapIdentifier);
            byte spawn = (byte)pQuery["map_spawn"];
            if (mapData.ForcedReturnMapIdentifier != MapData.INVALID_MAP_IDENTIFIER)
            {
                mapData = Server.GetMapData(mapData.ForcedReturnMapIdentifier);
                spawn = 0;
                if (mHealth == 0) mHealth = 50;
            }
            else if (mHealth == 0)
            {
                mapData = Server.GetMapData(mapData.ReturnMapIdentifier);
                spawn = 0;
                mHealth = 50;
            }
            mMap = Server.GetActiveMap(mapData.Identifier);
            mSpawn = spawn;
            mPosition = new Coordinates(mapData.Portals[spawn].X, mapData.Portals[spawn].Y);

            using (DatabaseQuery query = Database.Query("SELECT * FROM player_item WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                byte[] slots = new byte[(byte)EInventoryType.Count];
                slots[(byte)EInventoryType.Equipment] = (byte)pQuery["equipment_slots"];
                slots[(byte)EInventoryType.Use] = (byte)pQuery["use_slots"];
                slots[(byte)EInventoryType.Setup] = (byte)pQuery["setup_slots"];
                slots[(byte)EInventoryType.Etc] = (byte)pQuery["etc_slots"];
                slots[(byte)EInventoryType.Cash] = (byte)pQuery["cash_slots"];
                mItems = new PlayerItems((int)pQuery["mesos"], slots, query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_skill WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mSkills = new PlayerSkills(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_quest WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mQuests = new PlayerQuests(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_teleport WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mTeleports = new PlayerTeleports(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_card WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mCards = new PlayerCards(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_macro WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mMacros = new PlayerMacros(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_keymap WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                query.NextRow();
                mKeymap = new PlayerKeymap(query);
            }
            using (DatabaseQuery query = Database.Query("SELECT * FROM player_buddy WHERE [email protected]_identifier", new MySqlParameter("@player_identifier", mIdentifier)))
            {
                mBuddies = new PlayerBuddies((byte)pQuery["buddy_slots"], query);
            }
        }
 internal PlayerBuddies(byte pMaxBuddies, DatabaseQuery pQuery)
 {
     mMaxBuddies = pMaxBuddies;
     mBuddies = new List<PlayerBuddy>(mMaxBuddies);
     while (pQuery.NextRow()) mBuddies.Add(new PlayerBuddy(pQuery));
 }
 internal PlayerCard(DatabaseQuery pQuery)
 {
     mPlayerIdentifier = (int)pQuery["player_identifier"];
     mCardIdentifier = (int)pQuery["card_identifier"];
     mLevel = (byte)pQuery["level"];
 }
        private static void WritePlayer(Packet pPacket, DatabaseQuery pQuery)
        {
            pPacket.WriteInt((int)pQuery["identifier"]);
            pPacket.WritePaddedString((string)pQuery["name"], 13);

            pPacket.WriteByte((byte)pQuery["gender"]);
            pPacket.WriteByte((byte)pQuery["skin"]);
            pPacket.WriteInt((int)pQuery["eyes_identifier"]);
            pPacket.WriteInt((int)pQuery["hair_identifier"]);
            pPacket.WriteSkip(24);
            pPacket.WriteByte((byte)pQuery["level"]);
            pPacket.WriteUShort((ushort)pQuery["job"]);
            pPacket.WriteUShort((ushort)pQuery["strength"]);
            pPacket.WriteUShort((ushort)pQuery["dexterity"]);
            pPacket.WriteUShort((ushort)pQuery["intellect"]);
            pPacket.WriteUShort((ushort)pQuery["luck"]);
            pPacket.WriteUShort((ushort)pQuery["health"]);
            pPacket.WriteUShort((ushort)pQuery["max_health"]);
            pPacket.WriteUShort((ushort)pQuery["mana"]);
            pPacket.WriteUShort((ushort)pQuery["max_mana"]);
            pPacket.WriteUShort((ushort)pQuery["ability_points"]);
            pPacket.WriteUShort((ushort)pQuery["skill_points"]);
            pPacket.WriteInt((int)pQuery["experience"]);
            pPacket.WriteUShort((ushort)pQuery["fame"]);
            pPacket.WriteSkip(4);
            pPacket.WriteInt((int)pQuery["map_identifier"]);
            pPacket.WriteByte((byte)pQuery["map_spawn"]);
            pPacket.WriteSkip(4);

            pPacket.WriteByte((byte)pQuery["gender"]);
            pPacket.WriteByte((byte)pQuery["skin"]);
            pPacket.WriteInt((int)pQuery["eyes_identifier"]);
            pPacket.WriteBool(true);
            pPacket.WriteInt((int)pQuery["hair_identifier"]);

            SortedDictionary<byte, Doublet<int, int>> equipment = new SortedDictionary<byte, Doublet<int, int>>();
            using (DatabaseQuery queryEquipment = Database.Query("SELECT inventory_slot,item_identifier FROM player_item WHERE [email protected]_identifier AND inventory_type=0 AND inventory_slot<0", new MySqlParameter("@player_identifier", (int)pQuery["identifier"])))
            {
                while (queryEquipment.NextRow())
                {
                    short slot = (short)(-((short)queryEquipment["inventory_slot"]));
                    if (slot > 100) slot -= 100;
                    Doublet<int, int> pair = equipment.GetOrDefault((byte)slot, null);
                    if (pair == null)
                    {
                        pair = new Doublet<int, int>((int)queryEquipment["item_identifier"], 0);
                        equipment.Add((byte)slot, pair);
                    }
                    else if ((short)queryEquipment["inventory_slot"] < -100)
                    {
                        pair.Second = pair.First;
                        pair.First = (int)queryEquipment["item_identifier"];
                    }
                    else pair.Second = (int)queryEquipment["item_identifier"];
                }
            }
            foreach (KeyValuePair<byte, Doublet<int, int>> pair in equipment)
            {
                pPacket.WriteByte(pair.Key);
                if (pair.Key == 11 && pair.Value.Second > 0) pPacket.WriteInt(pair.Value.Second);
                else pPacket.WriteInt(pair.Value.First);
            }
            pPacket.WriteByte(0xFF);
            foreach (KeyValuePair<byte, Doublet<int, int>> pair in equipment)
            {
                if (pair.Key != 11 && pair.Value.Second > 0)
                {
                    pPacket.WriteByte(pair.Key);
                    pPacket.WriteInt(pair.Value.Second);
                }
            }
            pPacket.WriteByte(0xFF);
            Doublet<int, int> cashWeapon = equipment.GetOrDefault((byte)11, null);
            pPacket.WriteInt(cashWeapon == null ? 0 : cashWeapon.First);
            pPacket.WriteSkip(12);

            pPacket.WriteBool(false);
        }
 private static void SendPlayerCreate(Client pClient, DatabaseQuery pQuery)
 {
     Packet packet = new Packet(EOpcode.SMSG_PLAYER_CREATE);
     packet.WriteByte(0x00);
     WritePlayer(packet, pQuery);
     pClient.SendPacket(packet);
 }