Exemplo n.º 1
0
        public void CreateShop(string packet)
        {
            Logger.Debug(packet, Session.SessionId);
            string[]        packetsplit = packet.Split(' ');
            InventoryType[] type        = new InventoryType[20];
            long[]          gold        = new long[20];
            short[]         slot        = new short[20];
            byte[]          qty         = new byte[20];
            string          shopname    = string.Empty;

            if (packetsplit.Length > 2)
            {
                short typePacket;
                short.TryParse(packetsplit[2], out typePacket);
                if (Session.Character.HasShopOpened && typePacket != 1 || !Session.HasCurrentMapInstance)
                {
                    return;
                }
                foreach (Portal por in Session.CurrentMapInstance.Portals)
                {
                    if (Session.Character.PositionX < por.SourceX + 6 && Session.Character.PositionX > por.SourceX - 6 && Session.Character.PositionY < por.SourceY + 6 && Session.Character.PositionY > por.SourceY - 6)
                    {
                        Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NEAR_PORTAL"), 0));
                        return;
                    }
                }
                if (!Session.CurrentMapInstance.ShopAllowed)
                {
                    Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NOT_ALLOWED"), 0));
                    return;
                }
                if (typePacket == 2)
                {
                    Session.SendPacket("ishop");
                }
                else if (typePacket == 0)
                {
                    if (Session.CurrentMapInstance.UserShops.Count(s => s.Value.OwnerId == Session.Character.CharacterId) != 0)
                    {
                        return;
                    }
                    MapShop myShop = new MapShop();

                    if (packetsplit.Length > 82)
                    {
                        short shopSlot = 0;

                        for (short j = 3, i = 0; j < 82; j += 4, i++)
                        {
                            Enum.TryParse(packetsplit[j], out type[i]);
                            short.TryParse(packetsplit[j + 1], out slot[i]);
                            byte.TryParse(packetsplit[j + 2], out qty[i]);

                            long.TryParse(packetsplit[j + 3], out gold[i]);
                            if (gold[i] < 0)
                            {
                                return;
                            }
                            if (qty[i] > 0)
                            {
                                ItemInstance inv = Session.Character.Inventory.LoadBySlotAndType(slot[i], type[i]);
                                if (inv.Amount < qty[i])
                                {
                                    return;
                                }
                                if (!inv.Item.IsTradable || inv.IsBound)
                                {
                                    Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_ONLY_TRADABLE_ITEMS"), 0));
                                    Session.SendPacket("shop_end 0");
                                    return;
                                }

                                PersonalShopItem personalshopitem = new PersonalShopItem()
                                {
                                    ShopSlot     = shopSlot,
                                    Price        = gold[i],
                                    ItemInstance = inv,
                                    SellAmount   = qty[i]
                                };
                                myShop.Items.Add(personalshopitem);
                                shopSlot++;
                            }
                        }
                    }
                    if (myShop.Items.Count != 0)
                    {
                        if (!myShop.Items.Any(s => !s.ItemInstance.Item.IsSoldable || s.ItemInstance.IsBound))
                        {
                            for (int i = 83; i < packetsplit.Length; i++)
                            {
                                shopname += $"{packetsplit[i]} ";
                            }

                            // trim shopname
                            shopname = shopname.TrimEnd(' ');

                            // create default shopname if it's empty
                            if (string.IsNullOrWhiteSpace(shopname) || string.IsNullOrEmpty(shopname))
                            {
                                shopname = Language.Instance.GetMessageFromKey("SHOP_PRIVATE_SHOP");
                            }

                            // truncate the string to a max-length of 20
                            shopname       = StringHelper.Truncate(shopname, 20);
                            myShop.OwnerId = Session.Character.CharacterId;
                            myShop.Name    = shopname;
                            Session.CurrentMapInstance.UserShops.Add(Session.CurrentMapInstance.LastUserShopId++, myShop);

                            Session.Character.HasShopOpened = true;

                            Session.CurrentMapInstance?.Broadcast(Session, Session.Character.GeneratePlayerFlag(Session.CurrentMapInstance.LastUserShopId), ReceiverType.AllExceptMe);
                            Session.CurrentMapInstance?.Broadcast(Session.Character.GenerateShop(shopname));
                            Session.SendPacket(Session.Character.GenerateInfo(Language.Instance.GetMessageFromKey("SHOP_OPEN")));

                            Session.Character.IsSitting  = true;
                            Session.Character.IsShopping = true;

                            Session.Character.LoadSpeed();
                            Session.SendPacket(Session.Character.GenerateCond());
                            Session.CurrentMapInstance?.Broadcast(Session.Character.GenerateRest());
                        }
                        else
                        {
                            Session.SendPacket("shop_end 0");
                            Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("ITEM_NOT_SOLDABLE"), 10));
                        }
                    }
                    else
                    {
                        Session.SendPacket("shop_end 0");
                        Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("SHOP_EMPTY"), 10));
                    }
                }
                else if (typePacket == 1)
                {
                    Session.Character.CloseShop();
                }
            }
        }
        public void CreateShop(string packet)
        {
            Logger.Debug(packet, Session.SessionId);
            string[]        packetsplit = packet.Split(' ');
            InventoryType[] type        = new InventoryType[20];
            long[]          gold        = new long[20];
            short[]         slot        = new short[20];
            byte[]          qty         = new byte[20];
            short           typePacket;
            string          shopname = String.Empty;

            if (packetsplit.Length > 2)
            {
                short.TryParse(packetsplit[2], out typePacket);
                if (Session.Character.InExchangeOrTrade && typePacket != 1)
                {
                    return;
                }
                foreach (PortalDTO por in Session.CurrentMap.Portals)
                {
                    if (Session.Character.MapX < por.SourceX + 6 && Session.Character.MapX > por.SourceX - 6 && Session.Character.MapY < por.SourceY + 6 && Session.Character.MapY > por.SourceY - 6)
                    {
                        Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NEAR_PORTAL"), 0));
                        return;
                    }
                }
                if (!Session.CurrentMap.ShopAllowed)
                {
                    Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NOT_ALLOWED"), 0));
                    return;
                }
                if (typePacket == 2)
                {
                    Session.SendPacket("ishop");
                }
                else if (typePacket == 0)
                {
                    if (Session.CurrentMap.UserShops.Where(s => s.Value.OwnerId == Session.Character.CharacterId).Count() != 0)
                    {
                        return;
                    }
                    MapShop myShop = new MapShop();

                    if (packetsplit.Length > 82)
                    {
                        for (short j = 3, i = 0; j < 82; j += 4, i++)
                        {
                            Enum.TryParse <InventoryType>(packetsplit[j], out type[i]);
                            short.TryParse(packetsplit[j + 1], out slot[i]);
                            byte.TryParse(packetsplit[j + 2], out qty[i]);

                            long.TryParse(packetsplit[j + 3], out gold[i]);
                            if (gold[i] < 0)
                            {
                                return;
                            }
                            if (qty[i] > 0)
                            {
                                Inventory inv = Session.Character.InventoryList.LoadInventoryBySlotAndType(slot[i], type[i]);
                                if (inv.ItemInstance.Amount < qty[i])
                                {
                                    return;
                                }
                                if (!((ItemInstance)inv.ItemInstance).Item.IsTradable || ((ItemInstance)inv.ItemInstance).IsBound)
                                {
                                    Session.SendPacket(Session.Character.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_ONLY_TRADABLE_ITEMS"), 0));
                                    Session.SendPacket("shop_end 0");
                                    return;
                                }

                                PersonalShopItem personalshopitem = new PersonalShopItem()
                                {
                                    Slot         = slot[i],
                                    Type         = type[i],
                                    Price        = gold[i],
                                    Id           = inv.Id,
                                    CharacterId  = inv.CharacterId,
                                    Amount       = qty[i],
                                    ItemInstance = inv.ItemInstance
                                };
                                myShop.Items.Add(personalshopitem);
                            }
                        }
                    }
                    if (myShop.Items.Count != 0)
                    {
                        if (!myShop.Items.Any(s => !(s.ItemInstance as ItemInstance).Item.IsSoldable || (s.ItemInstance as ItemInstance).IsBound))
                        {
                            for (int i = 83; i < packetsplit.Length; i++)
                            {
                                shopname += $"{packetsplit[i]} ";
                            }

                            // trim shopname
                            shopname.TrimEnd(' ');

                            // create default shopname if it's empty
                            if (String.IsNullOrWhiteSpace(shopname) || String.IsNullOrEmpty(shopname))
                            {
                                shopname = Language.Instance.GetMessageFromKey("SHOP_PRIVATE_SHOP");
                            }

                            // truncate the string to a max-length of 20
                            shopname       = StringHelper.Truncate(shopname, 20);
                            myShop.OwnerId = Session.Character.CharacterId;
                            myShop.Name    = shopname;
                            Session.CurrentMap.UserShops.Add(Session.CurrentMap.UserShops.Count(), myShop);

                            Session.Character.HasShopOpened = true;

                            Session.CurrentMap?.Broadcast(Session, Session.Character.GeneratePlayerFlag(Session.CurrentMap.UserShops.Count()), ReceiverType.AllExceptMe);
                            Session.CurrentMap?.Broadcast(Session.Character.GenerateShop(shopname));
                            Session.SendPacket(Session.Character.GenerateInfo(Language.Instance.GetMessageFromKey("SHOP_OPEN")));

                            Session.Character.IsSitting  = true;
                            Session.Character.IsShopping = true;

                            Session.Character.LoadSpeed();
                            Session.SendPacket(Session.Character.GenerateCond());
                            Session.CurrentMap?.Broadcast(Session.Character.GenerateRest());
                        }
                        else
                        {
                            Session.SendPacket("shop_end 0");
                            Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("ITEM_NOT_SOLDABLE"), 10));
                        }
                    }
                    else
                    {
                        Session.SendPacket("shop_end 0");
                        Session.SendPacket(Session.Character.GenerateSay(Language.Instance.GetMessageFromKey("SHOP_EMPTY"), 10));
                    }
                }
                else if (typePacket == 1)
                {
                    KeyValuePair <long, MapShop> shop = Session.CurrentMap.UserShops.FirstOrDefault(mapshop => mapshop.Value.OwnerId.Equals(Session.Character.CharacterId));
                    Session.CurrentMap.UserShops.Remove(shop.Key);
                    Session.CurrentMap?.Broadcast(Session.Character.GenerateShopEnd());
                    Session.CurrentMap?.Broadcast(Session, Session.Character.GeneratePlayerFlag(0), ReceiverType.AllExceptMe);

                    Session.Character.IsShopping = false;
                    Session.Character.IsSitting  = false;

                    Session.Character.LoadSpeed();
                    Session.SendPacket(Session.Character.GenerateCond());
                    Session.CurrentMap?.Broadcast(Session.Character.GenerateRest());
                }
            }
        }
Exemplo n.º 3
0
        private void ExecuteHandler(ClientSession session, string[] packetSplit)
        {
            InventoryType[] inventoryType = new InventoryType[20];
            long[]          gold          = new long[20];
            short[]         slot          = new short[20];
            byte[]          qty           = new byte[20];
            string          shopname      = string.Empty;

            if ((session.Character.HasShopOpened && Type != 1) || !session.HasCurrentMapInstance ||
                session.Character.IsExchanging ||
                session.Character.ExchangeInfo != null)
            {
                return;
            }

            if (session.CurrentMapInstance.Portals.Any(por =>
                                                       session.Character.PositionX < por.SourceX + 6 && session.Character.PositionX > por.SourceX - 6 &&
                                                       session.Character.PositionY < por.SourceY + 6 &&
                                                       session.Character.PositionY > por.SourceY - 6))
            {
                session.SendPacket(
                    UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NEAR_PORTAL"), 0));
                return;
            }

            if (session.Character.Group != null && session.Character.Group?.GroupType != GroupType.Group)
            {
                session.SendPacket(
                    UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NOT_ALLOWED_IN_RAID"),
                                                    0));
                return;
            }

            if (!session.CurrentMapInstance.ShopAllowed)
            {
                session.SendPacket(
                    UserInterfaceHelper.GenerateMsg(Language.Instance.GetMessageFromKey("SHOP_NOT_ALLOWED"), 0));
                return;
            }

            switch (Type)
            {
            case 2:
                session.SendPacket("ishop");
                break;

            case 0:
                if (session.CurrentMapInstance.UserShops.Any(s =>
                                                             s.Value.OwnerId == session.Character.CharacterId))
                {
                    return;
                }

                MapShop myShop = new MapShop();

                if (packetSplit.Length > 82)
                {
                    short shopSlot = 0;
                    for (short j = 3, i = 0; j < 82; j += 4, i++)
                    {
                        Enum.TryParse(packetSplit[j], out inventoryType[i]);
                        short.TryParse(packetSplit[j + 1], out slot[i]);
                        byte.TryParse(packetSplit[j + 2], out qty[i]);
                        long.TryParse(packetSplit[j + 3], out gold[i]);
                        if (gold[i] < 0)
                        {
                            return;
                        }

                        if (qty[i] > 0)
                        {
                            ItemInstance inv = session.Character.Inventory.LoadBySlotAndType(slot[i], inventoryType[i]);
                            if (inv != null)
                            {
                                if (inv.Amount < qty[i])
                                {
                                    return;
                                }

                                if (!inv.Item.IsTradable || (inv.IsBound &&
                                                             !(inv.Item.Type == InventoryType.Equipment &&
                                                               (inv.Item.ItemType == ItemType.Armor ||
                                                                inv.Item.ItemType == ItemType.Weapon))))
                                {
                                    session.SendPacket(UserInterfaceHelper.GenerateMsg(
                                                           Language.Instance.GetMessageFromKey("SHOP_ONLY_TRADABLE_ITEMS"), 0));
                                    session.SendPacket("shop_end 0");
                                    return;
                                }

                                PersonalShopItem personalshopitem = new PersonalShopItem
                                {
                                    ShopSlot     = shopSlot,
                                    Price        = gold[i],
                                    ItemInstance = inv,
                                    SellAmount   = qty[i]
                                };
                                myShop.Items.Add(personalshopitem);
                            }
                        }
                        shopSlot++;
                    }
                }

                if (myShop.Items.Count != 0)
                {
                    if (!myShop.Items.Any(s => !s.ItemInstance.Item.IsSoldable || (s.ItemInstance.IsBound &&
                                                                                   !(s.ItemInstance.Item.Type == InventoryType.Equipment &&
                                                                                     (s.ItemInstance.Item.ItemType == ItemType.Armor ||
                                                                                      s.ItemInstance.Item.ItemType == ItemType.Weapon)))))
                    {
                        for (int i = 83; i < packetSplit.Length; i++)
                        {
                            shopname += $"{packetSplit[i]} ";
                        }

                        // trim shopname
                        shopname = shopname.TrimEnd(' ');

                        // create default shopname if it's empty
                        if (string.IsNullOrWhiteSpace(shopname) || string.IsNullOrEmpty(shopname))
                        {
                            shopname = Language.Instance.GetMessageFromKey("SHOP_PRIVATE_SHOP");
                        }

                        // truncate the string to a max-length of 20
                        shopname       = shopname.Truncate(20);
                        myShop.OwnerId = session.Character.CharacterId;
                        myShop.Name    = shopname;
                        session.CurrentMapInstance.UserShops.Add(session.CurrentMapInstance.LastUserShopId++,
                                                                 myShop);

                        session.Character.HasShopOpened = true;

                        session.CurrentMapInstance?.Broadcast(session,
                                                              session.Character.GeneratePlayerFlag(session.CurrentMapInstance.LastUserShopId),
                                                              ReceiverType.AllExceptMe);
                        session.CurrentMapInstance?.Broadcast(session.Character.GenerateShop(shopname));
                        session.SendPacket(
                            UserInterfaceHelper.GenerateInfo(Language.Instance.GetMessageFromKey("SHOP_OPEN")));

                        session.Character.IsSitting  = true;
                        session.Character.IsShopping = true;

                        session.Character.LoadSpeed();
                        session.SendPacket(session.Character.GenerateCond());
                        session.CurrentMapInstance?.Broadcast(session.Character.GenerateRest());
                    }
                    else
                    {
                        session.SendPacket("shop_end 0");
                        session.SendPacket(
                            session.Character.GenerateSay(
                                Language.Instance.GetMessageFromKey("ITEM_NOT_SOLDABLE"), 10));
                    }
                }
                else
                {
                    session.SendPacket("shop_end 0");
                    session.SendPacket(
                        session.Character.GenerateSay(Language.Instance.GetMessageFromKey("SHOP_EMPTY"), 10));
                }
                break;

            case 1:
                session.Character.CloseShop();
                break;
            }
        }