public static bool AccountExists(string name)
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         return(DBContext.Accounts.SingleOrDefault(x => x.Name.ToLower().Equals(name.ToLower())) != null);
     }
 }
Exemple #2
0
 private static bool IsItemFav(int AccountId, int SN)
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         return(DBContext.CashshopFavorites.Count(x => x.ItemId == SN && x.AccountId == AccountId) > 0);
     }
 }
        public bool CheckPic(string enteredPic)
        {
            Account DbAccount;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbAccount = DBContext.Accounts.SingleOrDefault(x => x.Id == Id);
            }
            if (DbAccount == null)
            {
                return(false);
            }
            if (DbAccount.Pic == null || DbAccount.PicKey == null)
            {
                return(false);
            }
            string hashedPic = DbAccount.Pic;

            byte[] picKey = Functions.HexToBytes(DbAccount.PicKey);

            string saltedSha512Pic = Functions.GetHMACSha512(enteredPic, picKey);

            if (saltedSha512Pic.Equals(hashedPic, StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }
            return(false);
        }
 public void SaveToDatabase()
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         DB.Models.Guild UpdateGuild = DBContext.Guilds.Single(x => x.Id == GuildId);
         UpdateGuild.Id          = GuildId;
         UpdateGuild.Leader      = LeaderId;
         UpdateGuild.GP          = GP;
         UpdateGuild.Logo        = Logo;
         UpdateGuild.LogoColor   = LogoColor;
         UpdateGuild.LogoBG      = LogoBG;
         UpdateGuild.LogoBGColor = LogoBGColor;
         UpdateGuild.Rank1Title  = RankTitles[0];
         UpdateGuild.Rank2Title  = RankTitles[1];
         UpdateGuild.Rank3Title  = RankTitles[2];
         UpdateGuild.Rank4Title  = RankTitles[3];
         UpdateGuild.Rank5Title  = RankTitles[4];
         UpdateGuild.Capacity    = Capacity;
         UpdateGuild.Notice      = Notice;
         UpdateGuild.Signature   = Signature;
         UpdateGuild.AllianceId  = Alliance;
         DBContext.Entry <DB.Models.Guild>(UpdateGuild).State = System.Data.Entity.EntityState.Modified;
         DBContext.SaveChanges();
     }
 }
        public void Disband()
        {
            List <Character> GuildCharacters;

            using (LeattyContext DBContext = new LeattyContext())
            {
                GuildCharacters = DBContext.Characters.Where(x => x.GuildId == GuildId).ToList();
            }
            Guilds.Remove(this.GuildId);

            foreach (Character character in GuildCharacters)
            {
                MapleClient c = Program.GetClientByCharacterId(character.Id);
                c.Account.Character.Guild = null;
                if (c != null)
                {
                    c.SendPacket(GenerateGuildDisbandPacket());
                    UpdateCharacterGuild(c.Account.Character, "");
                    c.Account.Character.GuildContribution = 0;
                    c.Account.Character.AllianceRank      = 5;
                    c.Account.Character.GuildRank         = 5;
                    MapleCharacter.SaveToDatabase(c.Account.Character);
                }
            }
        }
 public static MapleAccount GetAccountFromDatabase(String name)
 {
     try
     {
         Account DbAccount;
         using (LeattyContext DBContext = new LeattyContext())
         {
             DbAccount = DBContext.Accounts.SingleOrDefault(x => x.Name.ToLower().Equals(name.ToLower()));
         }
         if (DbAccount == null)
         {
             return(null);
         }
         MapleAccount ret = new MapleAccount(DbAccount.Id);
         ret.Name        = DbAccount.Name;
         ret.Password    = DbAccount.Password;
         ret.Key         = DbAccount.Key;
         ret.AccountType = DbAccount.AccountType;
         ret.MaplePoints = DbAccount.MaplePoints;
         ret.NXCredit    = DbAccount.NXCredit;
         ret.NXPrepaid   = DbAccount.NXPrepaid;
         return(ret);
     }
     catch (Exception)
     {
         return(null);
     }
 }
        public bool CheckPassword(string enteredPassword)
        {
            Account DbAccount;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbAccount = DBContext.Accounts.SingleOrDefault(x => x.Id == Id);
            }
            if (DbAccount == null)
            {
                return(false);
            }
            string passwordHash = DbAccount.Password;

            byte[] key = Functions.HexToBytes(DbAccount.Key);

            string saltedSha512Password = Functions.GetHMACSha512(enteredPassword, key);

            if (passwordHash == enteredPassword) //temporary
            {
                return(true);
            }
            else if (saltedSha512Password.Equals(passwordHash, StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        private static Dictionary <int, MapleGuild> LoadGuilds()
        {
            List <DB.Models.Guild> dbGuilds;

            using (LeattyContext dbContext = new LeattyContext())
            {
                dbGuilds = dbContext.Guilds.ToList();
            }
            Dictionary <int, MapleGuild> ret = new Dictionary <int, MapleGuild>();

            foreach (DB.Models.Guild DbGuild in dbGuilds)
            {
                MapleGuild gld = new MapleGuild();
                gld.GuildId       = DbGuild.Id;
                gld.LeaderId      = DbGuild.Leader;
                gld.GP            = DbGuild.GP;
                gld.Logo          = DbGuild.Logo;
                gld.LogoColor     = DbGuild.LogoColor;
                gld.Name          = DbGuild.Name;
                gld.RankTitles[0] = DbGuild.Rank1Title;
                gld.RankTitles[1] = DbGuild.Rank2Title;
                gld.RankTitles[2] = DbGuild.Rank3Title;
                gld.RankTitles[3] = DbGuild.Rank4Title;
                gld.RankTitles[4] = DbGuild.Rank5Title;
                gld.Capacity      = DbGuild.Capacity;
                gld.LogoBG        = DbGuild.LogoBG;
                gld.LogoBGColor   = DbGuild.LogoBGColor;
                gld.Notice        = DbGuild.Notice;
                gld.Signature     = DbGuild.Signature;
                gld.Alliance      = DbGuild.AllianceId;

                ret.Add(gld.GuildId, gld);
            }
            return(ret);
        }
Exemple #9
0
        private static void ShowCategories(MapleClient c)
        {
            PacketWriter HeaderPw = new PacketWriter();
            PacketWriter pw       = new PacketWriter();

            HeaderPw.WriteHeader(SendHeader.CashShopInfo);
            HeaderPw.WriteByte(3);    //Type
            HeaderPw.WriteBool(true); //Unk

            byte count = 0;

            pw.WriteInt(GetCatInt(Base: 2));
            pw.WriteMapleString("Favorite");
            pw.WriteInt(1);
            pw.WriteInt(0); //Type, 1 = New, 2 = Hot
            pw.WriteInt(0);
            count++;

            List <CashshopCat> Categories;

            using (LeattyContext DBContext = new LeattyContext())
            {
                //Categories = DBContext.CashshopCats.Where(x => x.ParentId == 0).ToList();
                Categories = new List <CashshopCat>();
            }
            foreach (CashshopCat Category in Categories)
            {
                int Id = Category.CsId;
                pw.WriteInt(GetCatInt(Id));
                pw.WriteMapleString(Category.Name);
                pw.WriteInt(1);
                pw.WriteInt(Category.Type);
                pw.WriteInt(0);

                List <CashshopCat> SubCategories;
                using (LeattyContext DBContext = new LeattyContext())
                {
                    //SubCategories = DBContext.CashshopCats.Where(x => x.ParentId == Category.Id).ToList();
                    SubCategories = new List <CashshopCat>();
                }
                foreach (CashshopCat SubCategory in SubCategories)
                {
                    int subId = SubCategory.Id;
                    pw.WriteInt(GetCatInt(Id, subId));
                    pw.WriteMapleString(SubCategory.Name);
                    pw.WriteInt(1);
                    pw.WriteInt(SubCategory.Type);
                    pw.WriteInt(0);
                    count++;
                }
                count++;
            }

            //Somehow setByte stopped working </care>
            HeaderPw.WriteByte(count);
            HeaderPw.WriteBytes(pw.ToArray());

            c.SendPacket(HeaderPw);
        }
 public bool HasCharacter(int characterId)
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         Character DbChar = DBContext.Characters.SingleOrDefault(x => x.Id == characterId);
         return(DbChar != null);
     }
 }
Exemple #11
0
        public override void SaveToDatabase(MapleCharacter owner)
        {
            base.SaveToDatabase(owner);
            if (owner == null)
            {
                return;                //delete is handled in base function
            }
            InventoryEquip dbActionEquip;

            using (LeattyContext dbContext = new LeattyContext())
            {
                dbActionEquip = dbContext.InventoryEquips.FirstOrDefault(x => x.InventoryItemId == DbId);
                if (dbActionEquip == null)
                {
                    dbActionEquip = new InventoryEquip {
                        InventoryItemId = DbId
                    };
                    dbContext.InventoryEquips.Add(dbActionEquip);
                    dbContext.SaveChanges();
                }
            }

            dbActionEquip.RemainingUpgradeCount = RemainingUpgradeCount;
            dbActionEquip.UpgradeCount          = UpgradeCount;
            dbActionEquip.Str             = Str;
            dbActionEquip.Dex             = Dex;
            dbActionEquip.Int             = Int;
            dbActionEquip.IncMhp          = IncMhp;
            dbActionEquip.IncMmp          = IncMmp;
            dbActionEquip.Pad             = Pad;
            dbActionEquip.Mad             = Mad;
            dbActionEquip.Pdd             = Pdd;
            dbActionEquip.Mdd             = Mdd;
            dbActionEquip.Acc             = Acc;
            dbActionEquip.Eva             = Eva;
            dbActionEquip.Speed           = Speed;
            dbActionEquip.Jump            = Jump;
            dbActionEquip.Durability      = Durability;
            dbActionEquip.HammerApplied   = HammersApplied;
            dbActionEquip.Enhancements    = Enhancements;
            dbActionEquip.Diligence       = Diligence;
            dbActionEquip.Potential1      = (short)Potential1;
            dbActionEquip.Potential2      = (short)Potential2;
            dbActionEquip.Potential3      = (short)Potential3;
            dbActionEquip.BonusPotential1 = (short)BonusPotential1;
            dbActionEquip.BonusPotential2 = (short)BonusPotential2;
            dbActionEquip.Socket1         = Socket1;
            dbActionEquip.Socket2         = Socket2;
            dbActionEquip.Socket3         = Socket3;
            dbActionEquip.CustomLevel     = CustomLevel;
            dbActionEquip.CustomExp       = CustomExp;
            dbActionEquip.PotentialState  = (byte)PotentialState;
            using (LeattyContext dbContext = new LeattyContext())
            {
                dbContext.Entry(dbActionEquip).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }
 public void SetPic(string pic)
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         byte[]  key       = saltshaker(32);
         string  hashedPic = Functions.GetHMACSha512(pic, key);
         Account DbAccount = DBContext.Accounts.SingleOrDefault(x => x.Id == Id);
         DbAccount.Pic    = hashedPic;
         DbAccount.PicKey = key.ByteArrayToString(nospace: true);
         DBContext.Entry <Account>(DbAccount).State = EntityState.Modified;
         DBContext.SaveChanges();
     }
 }
Exemple #13
0
 /// <param name="owner">if equal to null, deletes the item from the database</param>
 public virtual void SaveToDatabase(MapleCharacter owner)
 {
     using (LeattyContext dbContext = new LeattyContext())
     {
         if (owner == null)
         {
             if (InventoryType == MapleInventoryType.Equip)
             {
                 InventoryEquip equipEntry = dbContext.InventoryEquips.FirstOrDefault(x => x.Id == DbId);
                 if (equipEntry != null)
                 {
                     dbContext.InventoryEquips.Remove(equipEntry);
                 }
             }
             InventoryItem entry = dbContext.InventoryItems.FirstOrDefault(x => x.Id == DbId);
             if (entry != null)
             {
                 dbContext.InventoryItems.Remove(entry);
             }
             DbId = -1;
         }
         else
         {
             InventoryItem dbActionItem = null;
             if (DbId != -1)
             {
                 dbActionItem = dbContext.InventoryItems.FirstOrDefault(x => x.Id == DbId);
             }
             if (dbActionItem == null)
             {
                 dbActionItem = new InventoryItem();
                 dbContext.InventoryItems.Add(dbActionItem);
                 dbContext.SaveChanges();
                 DbId = dbActionItem.Id;
             }
             dbActionItem.CharacterId            = owner.Id;
             dbActionItem.ItemId                 = ItemId;
             dbActionItem.Position               = Position;
             dbActionItem.Quantity               = Quantity;
             dbActionItem.Source                 = Source;
             dbActionItem.Creator                = Creator;
             dbActionItem.Flags                  = (short)Flags;
             dbContext.Entry(dbActionItem).State = System.Data.Entity.EntityState.Modified;
         }
         dbContext.SaveChanges();
     }
 }
Exemple #14
0
        private void SendToAllGuildMembers(PacketWriter pw)
        {
            List <Character> DbChars;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbChars = DBContext.Characters.Where(x => x.GuildId == GuildId).ToList();
            }
            foreach (Character DbChar in DbChars)
            {
                MapleClient c = Program.GetClientByCharacterId(DbChar.Id);
                if (c != null)
                {
                    c.SendPacket(pw);
                }
            }
        }
        public static void Handle(MapleClient c, PacketReader pr)
        {
            string enteredPic  = pr.ReadMapleString();
            int    characterId = pr.ReadInt();

            byte state = 20;

            if (c.Account.CheckPic(enteredPic) && c.Account.HasCharacter(characterId))
            {
                using (LeattyContext DBContext = new LeattyContext())
                {
                    //Do delete stuff
                    List <InventoryItem>  ItemsToDelete  = DBContext.InventoryItems.Where(x => x.CharacterId == characterId).ToList();
                    List <InventoryEquip> EquipsToDelete = new List <InventoryEquip>();
                    foreach (InventoryItem ItemToDelete in ItemsToDelete)
                    {
                        InventoryEquip EquipToDelete = DBContext.InventoryEquips.SingleOrDefault(x => x.InventoryItemId == ItemToDelete.Id);
                        if (EquipToDelete != null)
                        {
                            EquipsToDelete.Add(EquipToDelete);
                        }
                    }
                    DBContext.InventoryItems.RemoveRange(ItemsToDelete);
                    DBContext.InventoryEquips.RemoveRange(EquipsToDelete);
                    DBContext.InventorySlots.RemoveRange(DBContext.InventorySlots.Where(x => x.CharacterId == characterId));
                    DBContext.KeyMaps.RemoveRange(DBContext.KeyMaps.Where(x => x.CharacterId == characterId));
                    DBContext.QuickSlotKeyMaps.RemoveRange(DBContext.QuickSlotKeyMaps.Where(x => x.CharacterId == characterId));
                    DBContext.StolenSkills.RemoveRange(DBContext.StolenSkills.Where(x => x.CharacterId == characterId));

                    DBContext.Characters.Remove(DBContext.Characters.SingleOrDefault(x => x.Id == characterId));


                    DBContext.SaveChanges();
                }

                state = 0;
            }

            //Response
            PacketWriter pw = new PacketWriter();

            pw.WriteHeader(SendHeader.DeleteCharacter);
            pw.WriteInt(characterId);
            pw.WriteByte(state);
            c.SendPacket(pw);
        }
Exemple #16
0
 public static void InitializeDatabase()
 {
     Database.SetInitializer(new MigrateDatabaseToLatestVersion <LeattyContext, Configuration>());
     using (LeattyContext dbContext = new LeattyContext())
     {
         try
         {
             dbContext.Database.Initialize(true);
         }
         catch (Exception e)
         {
             Console.WriteLine(e.ToString());
         }
     }
     ServerInfo.Guild.MapleGuild.InitializeGuildDatabase();
     LoadCashShopItems();
 }
        public bool HasPic()
        {
            Account DbAccount;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbAccount = DBContext.Accounts.SingleOrDefault(x => x.Id == Id);
            }
            try
            {
                if (DbAccount.Pic.Length != 0)
                {
                    return(true);
                }
            }
            catch (Exception) { }
            return(false);
        }
        public List <MapleCharacter> GetCharsFromDatabase()
        {
            List <Character> DbChars;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbChars = DBContext.Characters.Where(x => x.AccountId == Id).ToList();
            }
            List <MapleCharacter> ret = new List <MapleCharacter>();

            foreach (Character DbChar in DbChars)
            {
                MapleCharacter chr = MapleCharacter.LoadFromDatabase(DbChar.Id, true);
                if (chr != null)
                {
                    ret.Add(chr);
                }
            }
            return(ret);
        }
Exemple #19
0
        public static MapleGuild CreateGuild(string name, MapleCharacter leader)
        {
            foreach (MapleGuild g in Guilds.Values)
            {
                if (g.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(null);
                }
            }
            using (LeattyContext DBContext = new LeattyContext())
            {
                DB.Models.Guild InsertGuild = new DB.Models.Guild();
                InsertGuild.Leader = leader.Id;
                InsertGuild.Name   = name;
                DBContext.Guilds.Add(InsertGuild);
                Character DbChar = DBContext.Characters.SingleOrDefault(x => x.Id == leader.Id);
                DbChar.GuildContribution = 500;
                DbChar.AllianceRank      = 5;
                DbChar.GuildRank         = 1;
                DBContext.Entry <Character>(DbChar).State = System.Data.Entity.EntityState.Modified;
                DBContext.SaveChanges();

                MapleGuild gld = new MapleGuild();
                gld.GuildId     = InsertGuild.Id;
                gld.LeaderId    = leader.Id;
                gld.GP          = 0;
                gld.Logo        = 0;
                gld.LogoColor   = 0;
                gld.Name        = name;
                gld.Capacity    = 10;
                gld.LogoBG      = 0;
                gld.LogoBGColor = 0;
                gld.Notice      = null;
                gld.Signature   = 0;
                gld.Alliance    = 0;
                Guilds.Add(gld.GuildId, gld);
                return(gld);
            }
        }
Exemple #20
0
 public void PopulateSkills(List <StolenSkill> skills)
 {
     foreach (StolenSkill skill in skills)
     {
         int index = skill.Index;
         if (index >= 0 && index < 13)
         {
             StolenSkills[index] = skill.SkillId;
             if (skill.Chosen)
             {
                 ChosenSkills[GetChosenIndex(index)] = skill.SkillId;
             }
         }
         else
         {
             using (LeattyContext DBContext = new LeattyContext())
             {
                 DBContext.StolenSkills.Remove(skill); //invalid job index
                 DBContext.SaveChanges();
             }
         }
     }
 }
Exemple #21
0
        private static List <Item> GetItemsForFav(int AccountId, byte Cat = 1, byte SubCat = 1, byte Base = 2)
        {
            List <Item> Ret = new List <Item>();

            using (LeattyContext DBContext = new LeattyContext())
            {
                foreach (CashshopFavorite FavItem in DBContext.CashshopFavorites.Where(x => x.AccountId == AccountId))
                {
                    DbItem DbItem = DataBuffer.CSItems.SingleOrDefault(x => x.Id == FavItem.ItemId);
                    if (DbItem == null)
                    {
                        continue;
                    }
                    Item Item = DbToNormalItem(DbItem);
                    Item.Base     = Base;
                    Item.Cat      = Cat;
                    Item.FakeBase = Base;
                    Item.FakeCat  = Cat;
                    Ret.Add(Item);
                }
            }
            return(Ret);
        }
Exemple #22
0
        private static void SendInventory(MapleClient c)
        {
            PacketWriter pw = new PacketWriter();

            pw.WriteHeader(SendHeader.CashShopInit);
            pw.WriteShort(CashShopOperation + 2);
            List <InventoryItem> CashInventory = new List <InventoryItem>();
            int characterId = c.Account.Character.Id;

            using (LeattyContext DBContext = new LeattyContext())
            {
                //CashInventory = DBContext.InventoryItems.Where(x => x.CharacterId == characterId && x.Inventory == (sbyte)MapleInventoryType.CashShop).ToList();
            }
            pw.WriteShort((short)CashInventory.Count()); //Item count
            foreach (InventoryItem DbItem in CashInventory)
            {
                pw.WriteLong(DbItem.Id);
                pw.WriteLong(c.Account.Id);
                pw.WriteInt(DbItem.ItemId);
                pw.WriteInt(0); //IsFirst? o-o"
                pw.WriteShort(DbItem.Quantity);
                pw.WriteStaticString(c.Account.Character.Name, 13);
                pw.WriteLong(DateTime.UtcNow.ToFileTimeUtc()); //Exp time
                pw.WriteLong(0);                               //IsFirst? o-o"
                pw.WriteZeroBytes(19);
                pw.WriteHexString("40 E0 FD 3B 37 4F");
                pw.WriteBool(true);
                pw.WriteZeroBytes(16);

                pw.WriteInt(0); //Todo: pet stuff
            }
            pw.WriteShort(4);   //Storage slots?
            pw.WriteShort(0xB); //Character slots?
            pw.WriteShort(0);
            pw.WriteShort(0xB); //Character count?
            c.SendPacket(pw);
        }
Exemple #23
0
 public override void SaveToDatabase(int chrId, bool detach = false)
 {
     using (LeattyContext DBContext = new LeattyContext())
     {
         List <StolenSkill> stolenSkillsDatabase = DBContext.StolenSkills.Where(x => x.CharacterId == chrId).ToList();
         for (int i = 0; i < StolenSkills.Length; i++)
         {
             StolenSkill stolenDatabaseSkill = stolenSkillsDatabase.Where(x => x.Index == i).FirstOrDefault();
             if (stolenDatabaseSkill != null) //update record
             {
                 stolenDatabaseSkill.SkillId = StolenSkills[i];
                 stolenDatabaseSkill.Chosen  = IsChosenSkill(StolenSkills[i]);
             }
             else //insert new record
             {
                 if (StolenSkills[i] > 0) //Only insert if skill id > 0, otherwise it isnt nesecary
                 {
                     StolenSkill insertStolenSkill = new StolenSkill();
                     insertStolenSkill.CharacterId = chrId;
                     insertStolenSkill.SkillId     = StolenSkills[i];
                     insertStolenSkill.Index       = (byte)i;
                     insertStolenSkill.Chosen      = IsChosenSkill(StolenSkills[i]);
                     DBContext.StolenSkills.Add(insertStolenSkill);
                     if (detach)
                     {
                         DBContext.SaveChanges();
                     }
                 }
             }
             if (detach)
             {
                 DBContext.Entry <StolenSkill>(stolenDatabaseSkill).State = System.Data.Entity.EntityState.Detached;
             }
         }
     }
 }
Exemple #24
0
        public static ResourceSystem CheckAndSaveResourceSystem(ResourceSystem rs, ResourceSystemType newType, int chrId)
        {
            if (rs != null && rs.Type != newType)
            {
                rs.SaveToDatabase(chrId); //This actually doesn't always do something
            }
            if (rs == null || rs.Type != newType)
            {
                switch (newType)
                {
                case ResourceSystemType.Hunter:
                    return(new QuiverCartridgeSystem());

                case ResourceSystemType.Bandit:
                    return(new BodyCountSystem());

                case ResourceSystemType.Aran:
                    return(new AranSystem());

                case ResourceSystemType.Phantom:
                    PhantomSystem phantomResource = new PhantomSystem();
                    using (LeattyContext DBContext = new LeattyContext())
                    {
                        phantomResource.PopulateSkills(DBContext.StolenSkills.Where(x => x.CharacterId == chrId).ToList());
                    }
                    return(phantomResource);

                case ResourceSystemType.Luminous:
                    return(new LuminousSystem());

                default:
                    return(null);
                }
            }
            return(rs);
        }
Exemple #25
0
        public PacketWriter GenerateGuildDataPacket()
        {
            PacketWriter pw = new PacketWriter();

            pw.WriteHeader(SendHeader.GuildData);
            pw.WriteByte(0x20);
            pw.WriteByte(1);//?
            pw.WriteUInt((uint)GuildId);
            pw.WriteMapleString(Name);
            foreach (string ranks in RankTitles)
            {
                pw.WriteMapleString(ranks);
            }
            List <Character> GuildCharacters;

            using (LeattyContext DBContext = new LeattyContext())
            {
                GuildCharacters = DBContext.Characters.Where(x => x.GuildId == GuildId).ToList();
            }
            pw.WriteByte((byte)GuildCharacters.Count);
            foreach (Character Character in GuildCharacters)
            {
                pw.WriteInt(Character.Id);
            }
            lock (sync)
            {
                GP = 0;
                foreach (Character Character in GuildCharacters)
                {
                    MapleClient c            = Program.GetClientByCharacterId(Character.Id);
                    int         contribution = 0;
                    if (c == null)
                    {
                        pw.WriteStaticString(Character.Name, 13);
                        pw.WriteInt(Character.Job);
                        pw.WriteInt(Character.Level);
                        pw.WriteInt(Character.GuildRank);

                        pw.WriteInt(0);
                        pw.WriteInt(3);//nCommitment ?? "alliance rank"
                        contribution = Character.GuildContribution;
                    }
                    else//the character might have unsaved data so we use this instead.
                    {
                        MapleCharacter ch = c.Account.Character;
                        pw.WriteStaticString(ch.Name, 13);
                        pw.WriteInt(ch.Job);
                        pw.WriteInt(ch.Level);
                        pw.WriteInt(ch.GuildRank);
                        pw.WriteInt(1);
                        pw.WriteInt(3);//nCommitment ?? "alliance rank"
                        contribution = ch.GuildContribution;
                    }
                    GP += contribution;
                    pw.WriteInt(contribution);
                }
            }
            pw.WriteInt(Capacity);
            pw.WriteShort((short)LogoBG);
            pw.WriteByte((byte)LogoBGColor);
            pw.WriteShort((short)Logo);
            pw.WriteByte((byte)LogoColor);
            pw.WriteMapleString(Notice);
            pw.WriteInt(GP);
            pw.WriteInt(GP);//not sure abuot this one it may be something else GP related.
            pw.WriteInt(Alliance);
            pw.WriteByte(GuildLevel(GP));
            List <GuildSkill> DbGuildSkills;

            using (LeattyContext DBContext = new LeattyContext())
            {
                DbGuildSkills = DBContext.GuildSkills.Where(x => x.GuildId == GuildId).ToList();
            }
            pw.WriteShort(0);//unk
            pw.WriteShort((short)DbGuildSkills.Count);

            foreach (GuildSkill DbGuildSkill in DbGuildSkills)
            {
                pw.WriteInt(DbGuildSkill.SkillId);
                pw.WriteShort(DbGuildSkill.Level);
                pw.WriteLong(DbGuildSkill.Timestamp);
                pw.WriteMapleString(DbGuildSkill.Purchaser);
                pw.WriteMapleString("");//activator
            }
            return(pw);
        }
        public static void Handle(MapleClient c, PacketReader pr)
        {
            byte operation = pr.ReadByte();

            switch (operation)
            {
            case 0x1:     //Add buddy
            {
                MapleCharacter chr    = c.Account.Character;
                MapleBuddyList myList = chr.BuddyList;
                if (myList.TotalBuddies >= myList.Capacity)
                {
                    chr.SendPopUpMessage(string.Format("You have reached your limit of {0} buddies", myList.Capacity));
                    c.Account.Character.EnableActions();
                    return;
                }
                string buddyName = pr.ReadMapleString();
                string groupName = pr.ReadMapleString();
                string memo      = pr.ReadMapleString();
                if (buddyName.Length > 13 || groupName.Length > 16 || memo.Length > 256 || buddyName.Equals(chr.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }
                bool accountFriend = pr.ReadBool();

                if (accountFriend)
                {
                    #region accountFriend
                    string nickName = pr.ReadMapleString();
                    if (nickName.Length > 13)
                    {
                        return;
                    }
                    MapleClient buddyClient = Program.GetClientByCharacterName(buddyName);
                    if (buddyClient != null)         //if buddy is online
                    {
                        int buddyAccountId = buddyClient.Account.Id;
                        if (buddyAccountId == chr.AccountId)
                        {
                            c.SendPacket(CannotAddYourselfAsBuddy());
                            return;
                        }
                        MapleBuddy newBuddy = myList.AddAccountBuddy(buddyAccountId, nickName, groupName, memo);
                        if (newBuddy != null)         //newBuddy can be null if the buddy was already on the list
                        {
                            MapleCharacter buddyCharacter = buddyClient.Account.Character;
                            if (buddyCharacter.BuddyList.HasAccountBuddy(chr.AccountId))         //Buddy already has player on his list
                            {
                                if (!buddyCharacter.BuddyList.Invisible)
                                {
                                    newBuddy.Channel = buddyClient.Channel;
                                }
                                if (!myList.Invisible)
                                {
                                    buddyCharacter.BuddyList.BuddyChannelChanged(buddyClient, chr.Id, chr.AccountId, chr.Name, true, c.Channel);         //Tell buddy we are online
                                }
                            }
                            else
                            {
                                MapleBuddy buddysBuddy = buddyClient.Account.Character.BuddyList.AddAccountBuddyRequest(chr.AccountId, chr.Name);
                                if (!myList.Invisible)
                                {
                                    buddyClient.SendPacket(BuddyRequest(buddysBuddy, chr.Id, chr.AccountId, chr.Level, chr.Job, chr.SubJob));
                                }
                            }
                            c.SendPacket(AddBuddy(newBuddy));
                            c.SendPacket(BuddyRequestSuccessfullySent(nickName));
                        }
                        else
                        {
                            c.SendPacket(AlreadyRegisteredAsBuddy());
                        }
                    }
                    else         //Buddy is offline
                    {
                        //check if buddy exists in the database
                        using (LeattyContext dbContext = new LeattyContext())
                        {
                            var dbCharacter = dbContext.Characters.FirstOrDefault(x => x.Name.Equals(buddyName, StringComparison.OrdinalIgnoreCase));
                            if (dbCharacter != null)
                            {
                                int buddyAccountId = dbCharacter.AccountId;
                                if (buddyAccountId == chr.AccountId)
                                {
                                    c.SendPacket(CannotAddYourselfAsBuddy());
                                    return;
                                }
                                MapleBuddy newBuddy = myList.AddAccountBuddy(buddyAccountId, nickName, groupName, memo);
                                if (newBuddy != null)
                                {
                                    var buddyEntry = dbContext.Buddies.FirstOrDefault(x => x.AccountId == buddyAccountId && x.BuddyAccountId == chr.AccountId);
                                    if (buddyEntry == null)         //If target player does not have the requester on his list
                                    {
                                        dbContext.Buddies.Add(new Buddy()
                                            {
                                                IsRequest      = true,
                                                AccountId      = buddyAccountId,
                                                BuddyAccountId = chr.AccountId,
                                                Group          = MapleBuddyList.DEFAULT_GROUP,
                                                Name           = chr.Name,
                                                Memo           = string.Empty,
                                            });
                                        dbContext.SaveChanges();
                                    }
                                    c.SendPacket(AddBuddy(newBuddy));
                                    c.SendPacket(BuddyRequestSuccessfullySent(nickName));
                                }
                                else
                                {
                                    c.SendPacket(AlreadyRegisteredAsBuddy());
                                }
                            }
                            else
                            {
                                c.SendPacket(CharacterNotRegistered());
                            }
                        }
                    }
                    #endregion
                }
                else
                {
                    MapleClient buddyClient = Program.GetClientByCharacterName(buddyName);
                    if (buddyClient != null)         //If buddy is online
                    {
                        MapleCharacter buddyCharacter   = buddyClient.Account.Character;
                        int            buddyCharacterId = buddyCharacter.Id;
                        if (buddyCharacter.AccountId == chr.AccountId)
                        {
                            c.SendPacket(CannotAddYourselfAsBuddy());
                            return;
                        }

                        MapleBuddy newBuddy = myList.AddCharacterBuddy(buddyCharacterId, buddyCharacter.Name, groupName, memo);
                        if (newBuddy != null)         //newBuddy is null if the buddy is already on the list
                        {
                            if (buddyCharacter.BuddyList.HasCharacterBuddy(chr.Id))
                            {
                                if (!buddyCharacter.BuddyList.Invisible)
                                {
                                    newBuddy.Channel = buddyClient.Channel;
                                }
                                if (!myList.Invisible)
                                {
                                    buddyCharacter.BuddyList.BuddyChannelChanged(buddyClient, chr.Id, 0, chr.Name, false, c.Channel);
                                }
                            }
                            else
                            {
                                MapleBuddy buddysBuddy = buddyCharacter.BuddyList.AddCharacterBuddyRequest(chr.Id, chr.Name);
                                buddyClient.SendPacket(BuddyRequest(buddysBuddy, chr.Id, chr.AccountId, chr.Level, chr.Job, chr.SubJob));
                            }
                            c.SendPacket(AddBuddy(newBuddy));
                            c.SendPacket(BuddyRequestSuccessfullySent(buddyCharacter.Name));
                        }
                        else
                        {
                            c.SendPacket(AlreadyRegisteredAsBuddy());
                        }
                    }
                    else         //Buddy is offline
                    {
                        //check if buddy exists in the database
                        using (LeattyContext dbContext = new LeattyContext())
                        {
                            var dbCharacter = dbContext.Characters.FirstOrDefault(x => x.Name.Equals(buddyName, StringComparison.OrdinalIgnoreCase));
                            if (dbCharacter != null)
                            {
                                if (dbCharacter.AccountId == chr.AccountId)
                                {
                                    c.SendPacket(CannotAddYourselfAsBuddy());
                                    return;
                                }
                                int        buddyCharacterId = dbCharacter.Id;
                                MapleBuddy newBuddy         = myList.AddCharacterBuddy(buddyCharacterId, dbCharacter.Name, groupName, memo);
                                if (newBuddy != null)
                                {
                                    // Remove current buddy entries if existing
                                    var buddyEntry = dbContext.Buddies.FirstOrDefault(x => x.CharacterId == buddyCharacterId && x.BuddyCharacterId == chr.Id);
                                    if (buddyEntry == null)
                                    {
                                        dbContext.Buddies.Add(new Buddy()
                                            {
                                                IsRequest        = true,
                                                CharacterId      = buddyCharacterId,
                                                BuddyCharacterId = chr.Id,
                                                Group            = MapleBuddyList.DEFAULT_GROUP,
                                                Name             = chr.Name,
                                                Memo             = string.Empty,
                                            });
                                        dbContext.SaveChanges();
                                    }
                                    c.SendPacket(AddBuddy(newBuddy));
                                    c.SendPacket(BuddyRequestSuccessfullySent(dbCharacter.Name));
                                }
                                else
                                {
                                    c.SendPacket(AlreadyRegisteredAsBuddy());
                                }
                            }
                            else
                            {
                                c.SendPacket(CharacterNotRegistered());
                            }
                        }
                    }
                }
                break;
            }

            case 0x2:     //Accept character buddy request
            {
                int            characterId = pr.ReadInt();
                MapleBuddyList buddyList   = c.Account.Character.BuddyList;
                MapleBuddy     buddy       = buddyList.CharacterRequestAccepted(characterId);
                if (buddy != null)
                {
                    //Check if buddy is online
                    MapleClient requesterClient = Program.GetClientByAccountId(characterId);
                    if (requesterClient != null)
                    {
                        MapleBuddyList requesterBuddyList = requesterClient.Account.Character.BuddyList;
                        //Update buddy's status
                        if (!requesterBuddyList.Invisible)
                        {
                            buddy.Name    = requesterClient.Account.Character.Name;
                            buddy.Channel = requesterClient.Channel;
                        }
                        //Update my status to the new buddy
                        if (!buddyList.Invisible)
                        {
                            requesterBuddyList.BuddyChannelChanged(requesterClient, c.Account.Character.Id, c.Account.Id, c.Account.Character.Name, false, c.Channel);
                        }
                    }
                    //Update buddy to the client
                    buddyList.UpdateBuddyList(c);
                }
                break;
            }

            case 0x3:     //Accept Account Buddy request
            {
                int            accountId = pr.ReadInt();
                MapleBuddyList buddyList = c.Account.Character.BuddyList;
                MapleBuddy     buddy     = buddyList.AccountRequestAccepted(accountId);
                if (buddy != null)
                {
                    //Check if buddy is online
                    MapleClient requesterClient = Program.GetClientByAccountId(accountId);
                    if (requesterClient != null)
                    {
                        //Update buddy's status
                        MapleBuddyList requesterBuddyList = requesterClient.Account.Character.BuddyList;
                        if (!requesterBuddyList.Invisible)
                        {
                            buddy.Name    = requesterClient.Account.Character.Name;
                            buddy.Channel = requesterClient.Channel;
                        }
                        //Update my status to the new buddy
                        if (!buddyList.Invisible)
                        {
                            requesterClient?.Account.Character.BuddyList.BuddyChannelChanged(requesterClient, c.Account.Character.Id, c.Account.Id, c.Account.Character.Name, true, c.Channel);
                        }
                    }
                    //Update buddy to the client
                    buddyList.UpdateBuddyList(c);
                }
                break;
            }

            case 0x4:     //Delete characterbuddy
            {
                int            characterId = pr.ReadInt();
                MapleBuddyList buddyList   = c.Account.Character.BuddyList;
                if (buddyList.RemoveCharacterBuddy(characterId, c))
                {
                    MapleClient requesterClient = Program.GetClientByAccountId(characterId);
                    requesterClient?.Account.Character.BuddyList.BuddyChannelChanged(requesterClient, c.Account.Character.Id, c.Account.Id, c.Account.Character.Name, false, c.Channel);
                }
                break;
            }

            case 0x5:     //Delete account buddy
            {
                int            accountId = pr.ReadInt();
                MapleBuddyList buddyList = c.Account.Character.BuddyList;
                if (buddyList.RemoveAccountBuddy(accountId, c))
                {
                    MapleClient requesterClient = Program.GetClientByAccountId(accountId);
                    requesterClient?.Account.Character.BuddyList.BuddyChannelChanged(requesterClient, c.Account.Character.Id, c.Account.Id, c.Account.Character.Name, true, c.Channel);
                }
                break;
            }

            case 0x6:     //Decline character buddy
            {
                int         characterId     = pr.ReadInt();
                MapleClient requesterClient = Program.GetClientByCharacterId(characterId);
                if (requesterClient != null)
                {
                    requesterClient.Account.Character.BuddyList.RemoveCharacterBuddy(c.Account.Character.Id, requesterClient);
                    requesterClient.SendPacket(BuddyRequestDeclined(c.Account.Character.Name));
                }
                break;
            }

            case 0x7:     //Decline account buddy
            {
                int         accountId       = pr.ReadInt();
                MapleClient requesterClient = Program.GetClientByAccountId(accountId);
                if (requesterClient != null)
                {
                    requesterClient.Account.Character.BuddyList.RemoveAccountBuddy(c.Account.Id, requesterClient);
                    requesterClient.SendPacket(BuddyRequestDeclined(c.Account.Character.Name));
                }
                break;
            }

            case 0xC:     //Change alias and memo
            {
                bool   accountBuddy = pr.ReadBool();
                int    characterId  = pr.ReadInt();
                int    accountId    = pr.ReadInt();
                string newNickName  = pr.ReadMapleString();        //not used for character buddy because you cannot change name
                string newMemo      = pr.ReadMapleString();
                if (newNickName.Length > 13 || newMemo.Length > 256)
                {
                    return;
                }
                if (accountBuddy)
                {
                    c.Account.Character.BuddyList.UpdateAccountBuddyMemo(c, accountId, newNickName, newMemo);
                }
                else
                {
                    c.Account.Character.BuddyList.UpdateCharacterBuddyMemo(c, characterId, newMemo);
                }
                break;
            }

            case 0xF:     //offline mode
            {
                MapleCharacter chr = c.Account.Character;
                chr.BuddyList.SetInvisible(true, chr.Id, chr.AccountId, chr.Name, -1, c);
                break;
            }

            case 0x10:     //online mode
            {
                MapleCharacter chr = c.Account.Character;
                chr.BuddyList.SetInvisible(false, chr.Id, chr.AccountId, chr.Name, c.Channel, c);
                break;
            }
            }
        }