示例#1
0
        public TransactionV2 BuyItem(ItemRef it, int amount) //Shop qui achète !
        {
            if (CanShop)
            {
                int?priceItem = PriceBuyForItem(it);

                TransactionV2 ret = null;

                if (priceItem.HasValue)
                {
                    Controller.Stock.GetStock().Add(it, amount);

                    //Verif qu'on a de la money
                    var currentMoney = Controller.Stock.GetMoneyComponement().GetMoney();

                    if (currentMoney < priceItem * amount)
                    {
                        amount = Convert.ToInt32(amount - ((priceItem * amount - currentMoney) / priceItem));
                    }

                    Controller.Stock.GetMoneyComponement().RemoveMoney(priceItem.Value * amount);

                    ret = new TransactionV2()
                    {
                        TypeTransaction = TypeTransaction.Buy,
                        ItemRef         = it,
                        Amount          = amount,
                        Money           = priceItem.Value * amount,
                        PriceItem       = priceItem.Value
                    };
                }
                return(ret);
            }
            return(null);
        }
示例#2
0
        public TransactionV2 SellItem(ItemRef it, int amount, float money)
        {
            if (CanShop)
            {
                int nbItem = 0;

                var prixPourItem      = PriceSellForItem(it);
                var amountItemInStock = Controller.Stock.GetStock().GetAmount(it);
                if (prixPourItem != null && amountItemInStock > 0)
                {
                    int amountSell;
                    if (amountItemInStock >= amount)
                    {
                        amountSell = amount;
                    }
                    else
                    {
                        amountSell = amountItemInStock;
                    }


                    float valTotalMaxitem = prixPourItem.Value * amountSell;

                    if (valTotalMaxitem <= money)
                    {
                        nbItem = amount;
                    }
                    else
                    {
                        nbItem = Convert.ToInt32(amount - ((valTotalMaxitem - money) / prixPourItem));
                    }

                    Controller.Stock.GetStock().Remove(it, nbItem);


                    var transct = new TransactionV2()
                    {
                        TypeTransaction = TypeTransaction.Sell,
                        ItemRef         = it,
                        Money           = nbItem * prixPourItem.Value,
                        Amount          = nbItem,
                        PriceItem       = prixPourItem.Value
                    };
                    return(transct);
                }
            }
            return(null);
        }