Esempio n. 1
0
        public ActionOption <ExchangeResult <T> > Sell(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            // using the buying rate because this is a sell from the "other". I.E. a purchase from the market
            var exchangeRate = marketBuyRates[type];

            return(ExchangeInto(type, amount, marketInventory, selfInventory, exchangeRate));
        }
Esempio n. 2
0
        public ActionOption <ExchangeResult <T> > Purchase(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            // using the selling rate because this is a purchase from the "other". I.E. a sell from the perspective of the market
            var exchangeRate = marketSellRates[type];

            return(ExchangeInto(type, amount, selfInventory, marketInventory, exchangeRate));
        }
        public ActionOption <ExchangeResult <T> > Sell(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            //throw new NotImplementedException();
            // using the buying rate because this is a sell from the "other". I.E. a purchase from the market
            var exchangeRateFunction      = marketBuyRates[type];
            var amountLeftAfterMarketsBuy = exchangeRateFunction.GetPointFromNetExtraValueFromPoint(marketInventory.GetCurrentFunds(), marketInventory.Get(type));// marketInventory.GetCurrentFunds() / exchangeRate;
            var maxSell = amountLeftAfterMarketsBuy - marketInventory.Get(type);

            var amountToSell = Math.Min(amount, maxSell);

            return(selfInventory
                   .TransferResourceInto(type, marketInventory, amountToSell)
                   .Then(totalDeposited => new ExchangeResult <T>
            {
                amount = totalDeposited,
                cost = exchangeRateFunction.GetIncrementalValue(marketInventory.Get(type), totalDeposited),
                type = type
            }, exchangeResult =>
            {
                marketInventory.TransferResourceInto(moneyType, selfInventory, exchangeResult.cost).Execute();
            }));
        }
        public ActionOption <ExchangeResult <T> > Purchase(T type, float amount, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
        {
            //throw new NotImplementedException();
            // using the buying rate because this is a purchase from the "other". I.E. a sell from the perspective of the market
            var exchangeRateFunction       = marketSellRates[type];
            var amountLeftAfterMarketsSell = exchangeRateFunction.GetPointFromNetExtraValueFromPoint(-selfInventory.GetCurrentFunds(), marketInventory.Get(type));
            var maxPurchase = marketInventory.Get(type) - amountLeftAfterMarketsSell;

            var amountToPurchase = Math.Min(amount, maxPurchase);

            return(marketInventory
                   .TransferResourceInto(type, selfInventory, amountToPurchase)
                   .Then(withdrawn => new ExchangeResult <T>
            {
                amount = withdrawn,
                cost = -exchangeRateFunction.GetIncrementalValue(marketInventory.Get(type), -withdrawn),
                type = type
            }, exchangeResult =>
            {
                selfInventory.TransferResourceInto(moneyType, marketInventory, exchangeResult.cost).Execute();
            }));
        }
 public bool CanSell(T type, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
 {
     return(selfInventory.Get(type) > 0 &&
            marketInventory.Get(moneyType) > 0 &&
            marketInventory.CanFitMoreOf(type));
 }
Esempio n. 6
0
 public bool CanSell(T type, TradingInventoryAdapter <T> selfInventory, TradingInventoryAdapter <T> marketInventory)
 {
     return(CanExchangeInto(type, marketInventory, selfInventory));
 }
Esempio n. 7
0
 private bool CanExchangeInto(T type, TradingInventoryAdapter <T> targetInventory, TradingInventoryAdapter <T> sourceInventory)
 {
     return(sourceInventory.Get(type) > 0 &&
            targetInventory.Get(moneyType) > 0 &&
            targetInventory.CanFitMoreOf(type));
 }
Esempio n. 8
0
        private ActionOption <ExchangeResult <T> > ExchangeInto(T type, float amount, TradingInventoryAdapter <T> targetInventory, TradingInventoryAdapter <T> sourceInventory, float exchangeRate)
        {
            var maxPurchase      = targetInventory.GetCurrentFunds() / exchangeRate;
            var amountToPurchase = Math.Min(amount, maxPurchase);

            return(sourceInventory
                   .TransferResourceInto(type, targetInventory, amountToPurchase)
                   .Then(withdrawn => new ExchangeResult <T>
            {
                amount = withdrawn,
                cost = withdrawn * exchangeRate,
                type = type
            }, exchangeResult =>
            {
                targetInventory.TransferResourceInto(moneyType, sourceInventory, exchangeResult.cost).Execute();
            }));
        }
Esempio n. 9
0
 public float GetTotalUtility(T type, TradingInventoryAdapter <T> inventory)
 {
     return(utilityFunctions[type].GetNetValue(inventory.Get(type)));
 }
Esempio n. 10
0
 public float GetIncrementalUtility(T type, TradingInventoryAdapter <T> selfInventory, float increment)
 {
     return(utilityFunctions[type].GetIncrementalValue(selfInventory.Get(type), increment));
 }
Esempio n. 11
0
        public Task <GathererState> HandleState(GathererBehavior data)
        {
            var sellingStateDate = data.stateData[stateHandle] as SellingStateData;

            data.AttemptToEnsureTarget <MarketBehavior>(gameObject => true,
                                                        (market, distance) =>
            {
                return(-distance);
            });
            var touchedTarget = data.objectSeeker.seekTargetToTouch();

            if (touchedTarget != null)
            {
                var initialInventory = ResourceConfiguration.spaceFillingItems.ToDictionary(type => type, type => data.inventory.Get(type));

                var market = touchedTarget.GetComponentInChildren <MarketBehavior>();

                var exchangeAdapter = market.GetExchangeAdapter();
                var selfAdapter     = new TradingInventoryAdapter <ResourceType>(data.inventory, ResourceType.Gold);
                var marketAdapter   = new TradingInventoryAdapter <ResourceType>(data.inventory, ResourceType.Gold);

                var optimizer = new PurchaseOptimizer <ResourceType, TradingInventoryAdapter <ResourceType>, TradingInventoryAdapter <ResourceType> >(
                    selfAdapter,
                    marketAdapter,
                    ResourceConfiguration.spaceFillingItems,
                    exchangeAdapter,
                    data.utilityFunction
                    );

                var ledger          = optimizer.Optimize();
                var sourceUtilities = (new UtilityAnalyzer <ResourceType>()).GetTotalUtilityByInitialResource(
                    ResourceConfiguration.spaceFillingItems,
                    selfAdapter,
                    ledger,
                    data.utilityFunction);

                //Debug.Log("Ledger");
                //foreach (var transaction in ledger)
                //{
                //    Debug.Log($"Sold: {transaction.Item1?.amount} {this.str(transaction.Item1?.type)}");
                //    foreach (var bought in transaction.Item2.exchages)
                //    {
                //        Debug.Log($"Bought: {bought.amount} {this.str(bought.type)}");
                //    }
                //}


                var timeSummary = data.timeTracker.getResourceTimeSummary();

                //Debug.Log(data.inventory.ToString(x => Enum.GetName(typeof(ResourceType), x)));
                //Debug.Log(TradeModeling.MyUtilities.SerializeEnumDictionary(sourceUtilities));
                //Debug.Log(TradeModeling.MyUtilities.SerializeEnumDictionary(timeSummary));

                data.gatheringWeights = data.optimizer.nextWeights(timeSummary, sourceUtilities);
                sellingStateDate.weightsChart.values = data.gatheringWeights;

                //Debug.Log(TradeModeling.MyUtilities.SerializeEnumDictionary(data.gatheringWeights));
                data.timeTracker.clearTime();
                return(Task.FromResult(GathererState.GoingHomeToEat));
            }
            return(Task.FromResult(GathererState.Selling));
        }
Esempio n. 12
0
 public Dictionary <Resource, float> GetTotalUtilityByInitialResource(
     IList <Resource> tradeableResources,
     TradingInventoryAdapter <Resource> endingInventory,
     IList <(ExchangeResult <Resource>?, PurchaseOperationResult <Resource>)> transactionLedger,