Ejemplo n.º 1
0
        public override bool OnBuyItems(Mobile buyer, List <BuyItemResponse> list)
        {
            if (!Trading || !IsActiveSeller || CashType == null || !CashType.IsNotNull)
            {
                return(false);
            }

            if (!buyer.CheckAlive())
            {
                return(false);
            }

            if (!CheckVendorAccess(buyer))
            {
                Say("I can't serve you! Company policy.");
                //Say(501522); // I shall not treat with scum like thee!
                return(false);
            }

            UpdateBuyInfo();

            var info         = GetSellInfo();
            var totalCost    = 0;
            var validBuy     = new List <BuyItemResponse>(list.Count);
            var fromBank     = false;
            var fullPurchase = true;
            var controlSlots = buyer.FollowersMax - buyer.Followers;

            foreach (var buy in list)
            {
                var ser    = buy.Serial;
                var amount = buy.Amount;

                if (ser.IsItem)
                {
                    var item = World.FindItem(ser);

                    if (item == null)
                    {
                        continue;
                    }

                    var gbi = LookupDisplayObject(item);

                    if (gbi != null)
                    {
                        ProcessSinglePurchase(buy, gbi, validBuy, ref controlSlots, ref fullPurchase, ref totalCost);
                    }
                    else if (item != BuyPack && item.IsChildOf(BuyPack))
                    {
                        if (amount > item.Amount)
                        {
                            amount = item.Amount;
                        }

                        if (amount <= 0)
                        {
                            continue;
                        }

                        foreach (var ssi in info.Where(ssi => ssi.IsSellable(item) && ssi.IsResellable(item)))
                        {
                            totalCost += ssi.GetBuyPriceFor(item) * amount;
                            validBuy.Add(buy);
                            break;
                        }
                    }
                }
                else if (ser.IsMobile)
                {
                    var mob = World.FindMobile(ser);

                    if (mob == null)
                    {
                        continue;
                    }

                    var gbi = LookupDisplayObject(mob);

                    if (gbi != null)
                    {
                        ProcessSinglePurchase(buy, gbi, validBuy, ref controlSlots, ref fullPurchase, ref totalCost);
                    }
                }
            }

            if (fullPurchase && validBuy.Count == 0)
            {
                // Thou hast bought nothing!
                SayTo(buyer, 500190);
            }
            else if (validBuy.Count == 0)
            {
                // Your order cannot be fulfilled, please try again.
                SayTo(buyer, 500187);
            }

            if (validBuy.Count == 0)
            {
                return(false);
            }

            var bought = buyer.AccessLevel >= AccessLevel.GameMaster;

            if (!bought && CashType.TypeEquals <ObjectProperty>())
            {
                var cashSource = GetCashObject(buyer) ?? buyer;

                bought = CashProperty.Consume(cashSource, totalCost);

                if (!bought)
                {
                    // Begging thy pardon, but thou cant afford that.
                    SayTo(buyer, 500192);
                    return(false);
                }

                SayTo(buyer, "{0:#,0} {1} has been deducted from your total.", totalCost, CashName.GetString(buyer));
            }

            var isGold = CashType.TypeEquals <Gold>();

            var cont = buyer.Backpack;

            if (!bought && cont != null && isGold)
            {
                VitaNexCore.TryCatch(
                    () =>
                {
                    var lt = ScriptCompiler.FindTypeByName("GoldLedger") ?? Type.GetType("GoldLedger");

                    if (lt == null)
                    {
                        return;
                    }

                    var ledger = cont.FindItemByType(lt);

                    if (ledger == null || ledger.Deleted)
                    {
                        return;
                    }

                    var lp = lt.GetProperty("Gold");

                    if (lp == null)
                    {
                        return;
                    }

                    if (lp.PropertyType.IsEqual <Int64>())
                    {
                        var lg = (long)lp.GetValue(ledger, null);

                        if (lg < totalCost)
                        {
                            return;
                        }

                        lp.SetValue(ledger, lg - totalCost, null);
                        bought = true;
                    }
                    else if (lp.PropertyType.IsEqual <Int32>())
                    {
                        var lg = (int)lp.GetValue(ledger, null);

                        if (lg < totalCost)
                        {
                            return;
                        }

                        lp.SetValue(ledger, lg - totalCost, null);
                        bought = true;
                    }

                    if (bought)
                    {
                        buyer.SendMessage(2125, "{0:#,0} gold has been withdrawn from your ledger.", totalCost);
                    }
                });
            }

            if (!bought)
            {
                ConsumeCurrency(buyer, ref totalCost, ref bought);
            }

            if (!bought && cont != null)
            {
                if (cont.ConsumeTotal(CashType, totalCost))
                {
                    bought = true;
                }
            }

            if (!bought && isGold && Banker.Withdraw(buyer, totalCost))
            {
                bought   = true;
                fromBank = true;
            }

            if (!bought && !isGold)
            {
                cont = buyer.FindBankNoCreate();

                if (cont != null && cont.ConsumeTotal(CashType, totalCost))
                {
                    bought   = true;
                    fromBank = true;
                }
            }

            if (!bought)
            {
                // Begging thy pardon, but thou cant afford that.
                SayTo(buyer, 500192);
                return(false);
            }

            buyer.PlaySound(0x32);

            cont = buyer.Backpack ?? buyer.BankBox;

            foreach (var buy in validBuy)
            {
                var ser    = buy.Serial;
                var amount = buy.Amount;

                if (amount < 1)
                {
                    continue;
                }

                if (ser.IsItem)
                {
                    var item = World.FindItem(ser);

                    if (item == null)
                    {
                        continue;
                    }

                    var gbi = LookupDisplayObject(item);

                    if (gbi != null)
                    {
                        ProcessValidPurchase(amount, gbi, buyer, cont);
                    }
                    else
                    {
                        if (amount > item.Amount)
                        {
                            amount = item.Amount;
                        }

                        if (info.Where(ssi => ssi.IsSellable(item)).Any(ssi => ssi.IsResellable(item)))
                        {
                            Item buyItem;

                            if (amount >= item.Amount)
                            {
                                buyItem = item;
                            }
                            else
                            {
                                buyItem = LiftItemDupe(item, item.Amount - amount) ?? item;
                            }

                            if (cont == null || !cont.TryDropItem(buyer, buyItem, false))
                            {
                                buyItem.MoveToWorld(buyer.Location, buyer.Map);
                            }
                        }
                    }
                }
                else if (ser.IsMobile)
                {
                    var mob = World.FindMobile(ser);

                    if (mob == null)
                    {
                        continue;
                    }

                    var gbi = LookupDisplayObject(mob);

                    if (gbi != null)
                    {
                        ProcessValidPurchase(amount, gbi, buyer, cont);
                    }
                }
            }

            if (fullPurchase)
            {
                if (buyer.AccessLevel >= AccessLevel.GameMaster)
                {
                    SayTo(buyer, true, "I would not presume to charge thee anything.  Here are the goods you requested.");
                }
                else if (fromBank)
                {
                    SayTo(
                        buyer,
                        true,
                        "The total of thy purchase is {0:#,0} {1}, which has been withdrawn from your bank account.  My thanks for the patronage.",
                        totalCost,
                        CashName.GetString(buyer));
                }
                else
                {
                    SayTo(
                        buyer,
                        true,
                        "The total of thy purchase is {0:#,0} {1}.  My thanks for the patronage.",
                        totalCost,
                        CashName.GetString(buyer));
                }
            }
            else
            {
                if (buyer.AccessLevel >= AccessLevel.GameMaster)
                {
                    SayTo(
                        buyer,
                        true,
                        "I would not presume to charge thee anything.  Unfortunately, I could not sell you all the goods you requested.");
                }
                else if (fromBank)
                {
                    SayTo(
                        buyer,
                        true,
                        "The total of thy purchase is {0:#,0} {1}, which has been withdrawn from your bank account.  My thanks for the patronage.  Unfortunately, I could not sell you all the goods you requested.",
                        totalCost,
                        CashName.GetString(buyer));
                }
                else
                {
                    SayTo(
                        buyer,
                        true,
                        "The total of thy purchase is {0:#,0} {1}.  My thanks for the patronage.  Unfortunately, I could not sell you all the goods you requested.",
                        totalCost,
                        CashName.GetString(buyer));
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public virtual void ConsumeCurrency(Mobile buyer, ref double totalCost, ref bool bought, ref bool fromBank)
        {
            if (!bought && CashType.TypeEquals <ObjectProperty>())
            {
                var cashSource = GetCashObject(buyer) ?? buyer;

                bought = CashProperty.Consume(cashSource, totalCost);

                if (bought)
                {
                    SayTo(buyer, "{0:#,0} {1} has been deducted from your total.", totalCost, CashName.GetString(buyer));
                }
                else
                {
                    // Begging thy pardon, but thou cant afford that.
                    SayTo(buyer, 500192);
                }

                return;
            }

            var isGold = CashType.TypeEquals <Gold>();

            var cont = buyer.Backpack;

            if (!bought && cont != null && isGold)
            {
                try
                {
                    var lt = ScriptCompiler.FindTypeByName("GoldLedger") ?? Type.GetType("GoldLedger");

                    if (lt != null)
                    {
                        var ledger = cont.FindItemByType(lt);

                        if (ledger != null && !ledger.Deleted)
                        {
                            var lp = lt.GetProperty("Gold");

                            if (lp != null)
                            {
                                if (lp.PropertyType.IsEqual <Int64>())
                                {
                                    var lg = (long)lp.GetValue(ledger, null);

                                    if (lg >= totalCost)
                                    {
                                        lp.SetValue(ledger, lg - (long)totalCost, null);
                                        bought = true;
                                    }
                                }
                                else if (lp.PropertyType.IsEqual <Int32>())
                                {
                                    var lg = (int)lp.GetValue(ledger, null);

                                    if (lg >= totalCost)
                                    {
                                        lp.SetValue(ledger, lg - (int)totalCost, null);
                                        bought = true;
                                    }
                                }

                                if (bought)
                                {
                                    buyer.SendMessage(2125, "{0:#,0} gold has been withdrawn from your ledger.", totalCost);
                                    return;
                                }
                            }
                        }
                    }
                }
                catch
                { }
            }

            if (!bought && cont != null && ConsumeCash(CashType, cont, totalCost))
            {
                bought = true;
            }

            if (!bought && isGold)
            {
                if (totalCost <= Int32.MaxValue)
                {
                    if (Banker.Withdraw(buyer, (int)totalCost))
                    {
                        bought   = true;
                        fromBank = true;
                    }
                }
                else if (buyer.Account != null && AccountGold.Enabled)
                {
                    if (buyer.Account.WithdrawCurrency(totalCost / AccountGold.CurrencyThreshold))
                    {
                        bought   = true;
                        fromBank = true;
                    }
                }
            }

            if (!bought && !isGold)
            {
                cont = buyer.FindBankNoCreate();

                if (cont != null && ConsumeCash(CashType, cont, totalCost))
                {
                    bought   = true;
                    fromBank = true;
                }
            }
        }