Esempio n. 1
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));
        }
        public void Optimize_EmptyRequirements()
        {
            var ex = Assert.Throws <ArgumentException>(() => _purchaseOptimizer.Optimize(new List <PurchaseRequirement>()));

            Assert.That(ex.Message, Is.EqualTo("Purchase requirements cannot be empty"));
        }