Esempio n. 1
0
        public void BuyItems(Character activeCharacter, List <Asda2ItemTradeRef> itemsToBuyRefs)
        {
            Owner.Map.AddMessage(() =>
            {
                List <Asda2ItemTradeRef> source = new List <Asda2ItemTradeRef>();
                foreach (Asda2ItemTradeRef itemsToBuyRef in itemsToBuyRefs)
                {
                    Asda2ItemTradeRef asda2ItemTradeRef = ItemsOnTrade[itemsToBuyRef.TradeSlot];
                    if (asda2ItemTradeRef == null || asda2ItemTradeRef.Amount == -1 || asda2ItemTradeRef.Amount != 0 &&
                        asda2ItemTradeRef.Amount < itemsToBuyRef.Amount)
                    {
                        Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                     PrivateShopBuyResult.RequestedNumberOfItemsIsNoLongerAvaliable, null);
                        return;
                    }

                    source.Add(new Asda2ItemTradeRef
                    {
                        Amount    = itemsToBuyRef.Amount,
                        Item      = asda2ItemTradeRef.Item,
                        Price     = asda2ItemTradeRef.Price,
                        TradeSlot = asda2ItemTradeRef.TradeSlot
                    });
                }

                ulong num1 = source.Aggregate(0UL,
                                              (current, asda2ItemTradeRef) =>
                                              current + (ulong)(asda2ItemTradeRef.Price * asda2ItemTradeRef.Amount));
                if (num1 > int.MaxValue)
                {
                    activeCharacter.YouAreFuckingCheater("Trying to buy items with wrong money amount.", 50);
                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                 PrivateShopBuyResult.NotEnoghtGold, null);
                }
                else if (activeCharacter.Money < num1)
                {
                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                 PrivateShopBuyResult.NotEnoghtGold, null);
                }
                else if (Owner.Money + num1 > int.MaxValue)
                {
                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                 PrivateShopBuyResult.Error, null);
                    SendMessage(Owner.Name + " has to much gold.", Owner, Locale.Start);
                }
                else if (source.Any(asda2ItemTradeRef =>
                {
                    if (asda2ItemTradeRef.Item == null || asda2ItemTradeRef.Item.IsDeleted)
                    {
                        return(true);
                    }
                    if (asda2ItemTradeRef.Item.Amount != 0)
                    {
                        return(asda2ItemTradeRef.Item.Amount < asda2ItemTradeRef.Amount);
                    }
                    return(false);
                }))
                {
                    Owner.YouAreFuckingCheater("Trying to cheat while trading items from private shop", 10);
                    Exit(Owner);
                }
                else if (activeCharacter.Asda2Inventory.FreeRegularSlotsCount <
                         source.Count(i =>
                                      i.Item.InventoryType == Asda2InventoryType.Regular) ||
                         activeCharacter.Asda2Inventory.FreeShopSlotsCount < source.Count(
                             i => i.Item.InventoryType == Asda2InventoryType.Shop))
                {
                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                 PrivateShopBuyResult.NoSlotAreAvailable, null);
                }
                else
                {
                    activeCharacter.SubtractMoney((uint)num1);
                    Owner.AddMoney((uint)num1);
                    List <Asda2Item> buyedItems         = new List <Asda2Item>();
                    List <Asda2ItemTradeRef> itemsBuyed = new List <Asda2ItemTradeRef>();
                    foreach (Asda2ItemTradeRef asda2ItemTradeRef1 in source)
                    {
                        if (asda2ItemTradeRef1.Amount == 0)
                        {
                            asda2ItemTradeRef1.Amount = 1;
                        }
                        if (asda2ItemTradeRef1.Amount >= asda2ItemTradeRef1.Item.Amount)
                        {
                            LogHelperEntry lgDelete = Log
                                                      .Create(Log.Types.ItemOperations, LogSourceType.Character, Owner.EntryId)
                                                      .AddAttribute("source", 0.0, "selled_from_private_shop")
                                                      .AddItemAttributes(asda2ItemTradeRef1.Item, "")
                                                      .AddAttribute("buyer_id", activeCharacter.EntryId, "")
                                                      .AddAttribute("amount", asda2ItemTradeRef1.Amount, "").Write();
                            Asda2Item itemToCopyStats = asda2ItemTradeRef1.Item;
                            Asda2Item asda2Item       = null;
                            int num2 = (int)activeCharacter.Asda2Inventory.TryAdd(itemToCopyStats.ItemId,
                                                                                  itemToCopyStats.Amount, true, ref asda2Item, new Asda2InventoryType?(),
                                                                                  itemToCopyStats);
                            Log.Create(Log.Types.ItemOperations, LogSourceType.Character, activeCharacter.EntryId)
                            .AddAttribute("source", 0.0, "buyed_from_private_shop").AddItemAttributes(asda2Item, "")
                            .AddAttribute("seller_id", Owner.EntryId, "").AddReference(lgDelete)
                            .AddAttribute("amount", asda2ItemTradeRef1.Amount, "").Write();
                            buyedItems.Add(asda2Item);
                            itemToCopyStats.Destroy();
                            ItemsOnTrade[asda2ItemTradeRef1.TradeSlot].Amount = -1;
                            ItemsOnTrade[asda2ItemTradeRef1.TradeSlot].Price  = 0;
                        }
                        else
                        {
                            LogHelperEntry lgDelete = Log
                                                      .Create(Log.Types.ItemOperations, LogSourceType.Character, Owner.EntryId)
                                                      .AddAttribute("source", 0.0, "selled_from_private_shop_split")
                                                      .AddItemAttributes(asda2ItemTradeRef1.Item, "")
                                                      .AddAttribute("buyer_id", activeCharacter.EntryId, "")
                                                      .AddAttribute("amount", asda2ItemTradeRef1.Amount, "").Write();
                            asda2ItemTradeRef1.Item.Amount -= asda2ItemTradeRef1.Amount;
                            Asda2Item asda2Item             = null;
                            int num2 = (int)activeCharacter.Asda2Inventory.TryAdd(
                                (int)asda2ItemTradeRef1.Item.Template.ItemId, asda2ItemTradeRef1.Amount, true,
                                ref asda2Item, new Asda2InventoryType?(), asda2ItemTradeRef1.Item);
                            Log.Create(Log.Types.ItemOperations, LogSourceType.Character, activeCharacter.EntryId)
                            .AddAttribute("source", 0.0, "buyed_from_private_shop_split")
                            .AddItemAttributes(asda2Item, "new_item")
                            .AddItemAttributes(asda2ItemTradeRef1.Item, "old_item")
                            .AddAttribute("amount", asda2ItemTradeRef1.Amount, "")
                            .AddAttribute("seller_id", Owner.EntryId, "").AddReference(lgDelete)
                            .Write();
                            asda2ItemTradeRef1.Item.Save();
                            buyedItems.Add(asda2Item);
                        }

                        Asda2ItemTradeRef asda2ItemTradeRef2 = ItemsOnTrade[asda2ItemTradeRef1.TradeSlot];
                        itemsBuyed.Add(asda2ItemTradeRef2);
                        if (asda2ItemTradeRef2 != null && asda2ItemTradeRef2.Amount > 0)
                        {
                            asda2ItemTradeRef2.Amount -= asda2ItemTradeRef1.Amount;
                            if (asda2ItemTradeRef2.Amount <= 0)
                            {
                                asda2ItemTradeRef2.Amount = -1;
                            }
                        }
                    }

                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopResponse(activeCharacter,
                                                                                 PrivateShopBuyResult.Ok, buyedItems);
                    Asda2PrivateShopHandler.SendItemBuyedFromPrivateShopToOwnerNotifyResponse(this, itemsBuyed,
                                                                                              activeCharacter);
                    Asda2PrivateShopHandler.SendPrivateShopChatNotificationAboutBuyResponse(this, itemsBuyed,
                                                                                            activeCharacter);
                    Owner.SendMoneyUpdate();
                    activeCharacter.SendMoneyUpdate();
                }
            });
        }