public static void Authentication(Client pClient, Packet pPacket)
 {
     string username;
     string password;
     byte[] macBytes = new byte[6];
     if (!pPacket.ReadString(out username) ||
         !pPacket.ReadString(out password) ||
         !pPacket.ReadBytes(macBytes))
     {
         pClient.Disconnect();
         return;
     }
     Account account = null;
     using (DatabaseQuery query = Database.Query("SELECT * FROM account WHERE username=@username", new MySqlParameter("@username", username)))
     {
         if (!query.NextRow())
         {
             SendAuthentication(pClient, EAuthenticationResult.Invalid);
             return;
         }
         account = new Account(query);
     }
     if (password != account.Password)
     {
         SendAuthentication(pClient, EAuthenticationResult.Incorrect);
         return;
     }
     if (Server.IsAccountOnline(account.Identifier) ||
         Server.IsPendingPlayerLogin(account.Identifier))
     {
         SendAuthentication(pClient, EAuthenticationResult.Online);
         return;
     }
     pClient.Account = account;
     Log.WriteLine(ELogLevel.Info, "[{0}] Authenticated {1}", pClient.Host, pClient.Account.Username);
     SendAuthentication(pClient, EAuthenticationResult.Ok);
 }
        public static void Chat(Client pClient, Packet pPacket)
        {
            string message;
            bool bubble;
            if (!pPacket.ReadString(out message) ||
                !pPacket.ReadBool(out bubble))
            {
                pClient.Disconnect();
                return;
            }

            Packet packet = new Packet(EOpcode.SMSG_PLAYER_CHAT);
            packet.WriteInt(pClient.Player.Identifier);
            packet.WriteBool(pClient.Account.Level > 0);
            packet.WriteString(message);
            packet.WriteBool(bubble);
            pClient.Player.Map.SendPacketToAll(packet);
        }
        public static void ScriptTrigger(Client pClient, Packet pPacket)
        {
            string name;
            if (!pPacket.ReadSkip(1) ||
                !pPacket.ReadString(out name))
            {
                pClient.Disconnect();
                return;
            }
            Portal portal = pClient.Player.Map.GetPortal(name);
            if (portal == null || portal.Script == null)
            {
                Log.WriteLine(ELogLevel.Debug, "[{0}] Portal Script Blocked {1}", pClient.Host, name);
                pClient.Player.SendPortalBlocked();
                return;
            }

            Log.WriteLine(ELogLevel.Info, "[{0}] Portal Script Triggered {1}", pClient.Host, portal.Script.GetType().FullName);
            portal.Script.Execute(pClient.Player, portal);
        }
        public static void Teleport(Client pClient, Packet pPacket)
        {
            int mapIdentifier;
            if (!pPacket.ReadSkip(1) ||
                !pPacket.ReadInt(out mapIdentifier))
            {
                pClient.Disconnect();
                return;
            }
            if (mapIdentifier == -1)
            {
                string portalName;
                if (!pPacket.ReadString(out portalName))
                {
                    pClient.Disconnect();
                    return;
                }
                Portal portal = pClient.Player.Map.GetPortal(portalName);
                if (portal == null)
                {
                    Log.WriteLine(ELogLevel.Debug, "[{0}] Portal Blocked {1}", pClient.Host, portalName);
                    pClient.Player.SendPortalBlocked();
                    return;
                }
                Map mapDestination = Server.GetActiveMap(portal.Data.ToMapIdentifier);
                if (mapDestination == null)
                {
                    Log.WriteLine(ELogLevel.Debug, "[{0}] Portal Blocked {1}", pClient.Host, portalName);
                    pClient.Player.SendPortalBlocked();
                    return;
                }
                Portal portalDestination = mapDestination.GetPortal(portal.Data.ToName);
                if (portalDestination == null)
                {
                    Log.WriteLine(ELogLevel.Debug, "[{0}] Portal Blocked {1}", pClient.Host, portalName);
                    pClient.Player.SendPortalBlocked();
                    return;
                }

                Log.WriteLine(ELogLevel.Info, "[{0}] Portal Triggered {1}", pClient.Host, portal.Data.Name);
                pClient.Player.Map.RemovePlayer(pClient.Player);
                pClient.Player.Map = mapDestination;
                pClient.Player.Map.AddPlayer(pClient.Player);
                pClient.Player.Spawn = portalDestination.Index;
                pClient.Player.Position.X = portalDestination.Data.X;
                pClient.Player.Position.Y = portalDestination.Data.Y;
                pClient.Player.Stance = 0;
                pClient.Player.Foothold = 0;
                pClient.Player.FallCount = 0;
                pClient.Player.SendMapChange();
                pClient.Player.EnterMap();
            }
        }
 public static void PlayerNameCheck(Client pClient, Packet pPacket)
 {
     string name;
     if (!pPacket.ReadString(out name))
     {
         pClient.Disconnect();
         return;
     }
     bool unusable = name.Length < 4 ||
                     name.Length > 16 ||
                     (long)Database.Scalar("SELECT COUNT(*) FROM player WHERE name=@name", new MySqlParameter("@name", name)) != 0;
     SendPlayerNameCheck(pClient, name, unusable);
 }
        public static void PlayerCreate(Client pClient, Packet pPacket)
        {
            string name;
            int eyesIdentifier;
            int hairIdentifier;
            int hairColor;
            int skin;
            int shirtIdentifier;
            int pantsIdentifier;
            int shoesIdentifier;
            int weaponIdentifier;
            byte gender;

            if (!pPacket.ReadString(out name) ||
                !pPacket.ReadInt(out eyesIdentifier) ||
                !pPacket.ReadInt(out hairIdentifier) ||
                !pPacket.ReadInt(out hairColor) ||
                !pPacket.ReadInt(out skin) ||
                !pPacket.ReadInt(out shirtIdentifier) ||
                !pPacket.ReadInt(out pantsIdentifier) ||
                !pPacket.ReadInt(out shoesIdentifier) ||
                !pPacket.ReadInt(out weaponIdentifier) ||
                !pPacket.ReadByte(out gender))
            {
                pClient.Disconnect();
                return;
            }
            bool unusable = name.Length < 4 ||
                            name.Length > 16 ||
                            (long)Database.Scalar("SELECT COUNT(*) FROM player WHERE name=@name", new MySqlParameter("@name", name)) != 0;
            if (unusable)
            {
                SendPlayerNameCheck(pClient, name, true);
                return;
            }
            hairIdentifier += hairColor;
            int identifier = Database.InsertAndReturnIdentifier("INSERT INTO player(account_identifier,name,gender,skin,eyes_identifier,hair_identifier) " +
                                                                "VALUES(@account_identifier,@name,@gender,@skin,@eyes_identifier,@hair_identifier)",
                                                                new MySqlParameter("@account_identifier", pClient.Account.Identifier),
                                                                new MySqlParameter("@name", name),
                                                                new MySqlParameter("@gender", gender),
                                                                new MySqlParameter("@skin", skin),
                                                                new MySqlParameter("@eyes_identifier", eyesIdentifier),
                                                                new MySqlParameter("@hair_identifier", hairIdentifier));
            Database.Execute("INSERT INTO player_keymap(player_identifier) VALUES(@player_identifier)", new MySqlParameter("@player_identifier", identifier));
            Database.Execute("INSERT INTO player_item(player_identifier,inventory_type,inventory_slot,item_identifier,weapon_defense) " +
                             "VALUES(@player_identifier,@inventory_type,-5,@item_identifier,3)",
                             new MySqlParameter("@player_identifier", identifier),
                             new MySqlParameter("@inventory_type", EInventoryType.Equipment),
                             new MySqlParameter("@item_identifier", shirtIdentifier));
            Database.Execute("INSERT INTO player_item(player_identifier,inventory_type,inventory_slot,item_identifier,weapon_defense) " +
                             "VALUES(@player_identifier,@inventory_type,-6,@item_identifier,2)",
                             new MySqlParameter("@player_identifier", identifier),
                             new MySqlParameter("@inventory_type", EInventoryType.Equipment),
                             new MySqlParameter("@item_identifier", pantsIdentifier));
            Database.Execute("INSERT INTO player_item(player_identifier,inventory_type,inventory_slot,item_identifier,unused_scroll_slots,weapon_defense) " +
                             "VALUES(@player_identifier,@inventory_type,-7,@item_identifier,5,3)",
                             new MySqlParameter("@player_identifier", identifier),
                             new MySqlParameter("@inventory_type", EInventoryType.Equipment),
                             new MySqlParameter("@item_identifier", shoesIdentifier));
            Database.Execute("INSERT INTO player_item(player_identifier,inventory_type,inventory_slot,item_identifier,weapon_attack) " +
                             "VALUES(@player_identifier,@inventory_type,-11,@item_identifier,17)",
                             new MySqlParameter("@player_identifier", identifier),
                             new MySqlParameter("@inventory_type", EInventoryType.Equipment),
                             new MySqlParameter("@item_identifier", weaponIdentifier));
            Database.Execute("INSERT INTO player_item(player_identifier,inventory_type,inventory_slot,item_identifier) VALUES(@player_identifier,@inventory_type,1,4161001)",
                             new MySqlParameter("@player_identifier", identifier),
                             new MySqlParameter("@inventory_type", EInventoryType.Etc));
            using (DatabaseQuery query = Database.Query("SELECT * FROM player WHERE identifier=@identifier", new MySqlParameter("@identifier", identifier)))
            {
                query.NextRow();
                SendPlayerCreate(pClient, query);
            }
        }