Esempio n. 1
0
        public void AddProductsFromAnotherProductAmountCollection()
        {
            // Build the one to add for later use.
            var diff = 10;
            var src  = new ProductAmountCollection();

            src.AddProducts(ProductMock1.Object, diff);
            src.AddProducts(ProductMock2.Object, diff);

            // Check that the sut is empty.
            Assert.That(sut.Products.Count, Is.EqualTo(0));
            Assert.That(sut.ProductDict.Count, Is.EqualTo(0));

            // Add the src to the sut
            sut.AddProducts(src);

            // CHeck that it was added properly
            Assert.That(sut.Products.Any(x => x.Id == ProductMock1.Object.Id), Is.True);
            Assert.That(sut.ProductDict.ContainsKey(ProductMock1.Object.Id), Is.True);
            Assert.That(sut.GetProductValue(ProductMock1.Object), Is.EqualTo(diff));
            Assert.That(sut.GetProductValue(ProductMock2.Object), Is.EqualTo(diff));

            // add it again
            sut.AddProducts(src);

            // check it again to see it was added twice.
            Assert.That(sut.GetProductValue(ProductMock1.Object), Is.EqualTo(diff * 2));
            Assert.That(sut.GetProductValue(ProductMock2.Object), Is.EqualTo(diff * 2));
        }
Esempio n. 2
0
 /// <summary>
 /// Plot Constructor
 /// </summary>
 /// <param name="Territory">The Territory of the plot of land.</param>
 /// <param name="rand">Randomizer</param>
 public Plot(ITerritory Territory, IRandomizer rand) : base(rand)
 {
     //
     this.Territory = Territory;
     Maintenance    = new ProductAmountCollection();
     FailsInto      = new ProductAmountCollection();
 }
Esempio n. 3
0
        /// <summary>
        /// Runs maintenance on a number of products.
        /// </summary>
        /// <param name="products">The number of products we are maintaining.</param>
        /// <param name="MaintenanceProducts">The products being used to maintain the products.</param>
        /// <param name="Satisfaction">How much was satisfied.</param>
        /// <returns>The items consumed.</returns>
        public IProductAmountCollection RunMaintenance(double products, IReadOnlyProductAmountCollection MaintenanceProducts, out double Satisfaction)
        {
            // null check
            if (MaintenanceProducts is null)
            {
                throw new ArgumentNullException(nameof(MaintenanceProducts));
            }
            // if Maintenance is empty, return empty collection, and set satisfaction to 1.
            if (Maintenance.Count() == 0)
            {
                Satisfaction = 1;
                return(new ProductAmountCollection());
            }

            // Get from all available maintenance goods, the exact goods we're looking for.
            var availableGoods = MaintenanceProducts.GetProducts(Maintenance.Products.ToList());

            // Get how many maintenance products are actually needed.
            var requiredGoods = Maintenance.Multiply(products);

            // Start the cumulative satisfaction.
            var cumulativeSat = 0.0;

            // Create our return collection
            var result = new ProductAmountCollection();

            // go through the available goods.
            foreach (var pair in availableGoods)
            {
                // for the product, available amount, and required amount
                var product   = pair.Item1;
                var available = pair.Item2;
                var required  = requiredGoods.GetProductValue(product);

                // get how well the good was satisfied
                var sat = Math.Min(1, available / required);

                // to the cumulative satisfaction, multiplying by the requried amount to weight it correctly.
                cumulativeSat += sat * required;

                // and add how much was consumed, choosing either available or required amount.
                if (sat == 1)
                {
                    result.AddProducts(product, required);
                }
                else
                {
                    result.AddProducts(product, available);
                }
            }

            // average out the cumulative satisfaction by all of the required goods.
            // if all are fully satisfied, then Satisfaction should be 1.
            // Products which have higher requirements should have a larger impact on the average.
            Satisfaction = cumulativeSat / requiredGoods.Sum(x => x.Item2);

            // return our result.
            return(result);
        }
Esempio n. 4
0
        public void ReturnUnsuccessfulPurchaseWhenBuyerCannotAffordAnything()
        {
            // set up currency.
            MarketMock.Setup(x => x.GetPrice(CurrencyMock1.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 1);
            MarketMock.Setup(x => x.GetPrice(CurrencyMock2.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 100);

            // setup immediate cash.
            var cash = new ProductAmountCollection();

            cash.AddProducts(CurrencyMock1.Object, 0);
            cash.AddProducts(CurrencyMock2.Object, 0);

            // Setup Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, 100);
            sut.Storage.AddProducts(CurrencyMock1.Object, 10000);
            sut.Storage.AddProducts(CurrencyMock2.Object, 10000);
            sut.ForSale.AddProducts(LifeNeed.Object, 100);
            sut.ForSale.AddProducts(CurrencyMock1.Object, 10000);
            sut.ForSale.AddProducts(CurrencyMock2.Object, 10000);

            // setup Product
            MarketMock.Setup(x => x.GetPrice(LifeNeed.Object))
            .Returns(123);

            // setup change return
            var change = new ProductAmountCollection();

            change.AddProducts(CurrencyMock1.Object, 0);
            change.AddProducts(CurrencyMock2.Object, 0);

            // Setup seller's change
            var sellerChange = new ProductAmountCollection();

            sellerChange.AddProducts(CurrencyMock1.Object, 0);
            sellerChange.AddProducts(CurrencyMock2.Object, 0);

            // Setup changes, general to specific
            MarketMock
            .Setup(x => x.ChangeForPrice(It.IsAny <IProductAmountCollection>(), 77))
            .Returns(sellerChange);
            MarketMock.Setup(x => x.ChangeForPrice(cash, 123)).Returns(change);

            // test it out
            var result = sut.BuyGood(cash, LifeNeed.Object, 1, MarketMock.Object);

            // Ensure that the seller has appropriately changed his goods.
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 100);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 10000);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 10000);

            // ensure result is correct transaction reciept
            Assert.That(result.Contains(LifeNeed.Object), Is.False);
            Assert.That(result.Contains(CurrencyMock1.Object), Is.False);
            Assert.That(result.Contains(CurrencyMock2.Object), Is.False);
        }
        public IProductAmountCollection LossPhase()
        {
            var result = new ProductAmountCollection();

            foreach (var pop in Pops)
            {
                result.AddProducts(pop.LossPhase());
            }

            return(result);
        }
        public IProductAmountCollection SellPhase()
        {
            var result = new ProductAmountCollection();

            // For all pops, put everything they have stored on the market.
            foreach (var pop in Pops)
            {
                result.AddProducts(pop.UpForSale());
            }

            return(result);
        }
Esempio n. 7
0
        public void Setup()
        {
            ProductMock1 = new Mock <IProduct>();
            ProductMock2 = new Mock <IProduct>();

            TestId1 = Guid.NewGuid();
            TestId2 = Guid.NewGuid();

            ProductMock1.SetupGet(x => x.Id).Returns(TestId1);
            ProductMock2.SetupGet(x => x.Id).Returns(TestId2);

            sut = new ProductAmountCollection();
        }
Esempio n. 8
0
        public void ReturnSuccessfulPurchaseFromBuyGood()
        {
            // set up currency.
            MarketMock.Setup(x => x.GetPrice(CurrencyMock1.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 1);
            MarketMock.Setup(x => x.GetPrice(CurrencyMock2.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 100);

            // setup immediate cash.
            var cash = new ProductAmountCollection();

            cash.AddProducts(CurrencyMock1.Object, 10000);
            cash.AddProducts(CurrencyMock2.Object, 10000);

            // Setup Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, 100);
            sut.Storage.AddProducts(CurrencyMock1.Object, 10000);
            sut.Storage.AddProducts(CurrencyMock2.Object, 10000);
            sut.ForSale.AddProducts(LifeNeed.Object, 100);
            sut.ForSale.AddProducts(CurrencyMock1.Object, 10000);
            sut.ForSale.AddProducts(CurrencyMock2.Object, 10000);

            // setup Product
            MarketMock.Setup(x => x.GetPrice(LifeNeed.Object))
            .Returns(123);

            // setup change return
            var change = new ProductAmountCollection();

            change.AddProducts(CurrencyMock2.Object, 1);
            change.AddProducts(CurrencyMock1.Object, 23);
            MarketMock.Setup(x => x.ChangeForPrice(cash, 123))
            .Returns(change);

            // test it out
            var result = sut.BuyGood(cash, LifeNeed.Object, 1, MarketMock.Object);

            // Ensure that the seller has appropriately changed his goods.
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 99);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 10001);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 10023);

            // ensure result is correct transaction reciept
            AssertProductAmountIsEqual(result, LifeNeed, 1);
            AssertProductAmountIsEqual(result, CurrencyMock2, -1);
            AssertProductAmountIsEqual(result, CurrencyMock1, -23);
        }
        public IProductAmountCollection TotalProduction()
        {
            // the result to eventually return
            var result = new ProductAmountCollection();

            // for every pop.
            foreach (var pop in Pops)
            {
                // get the hypothetical max production for the pop
                var maxOutput = pop.PrimaryJob.Outputs.Multiply(pop.Count);

                // and add it to the result
                result.AddProducts(maxOutput);
            }

            return(result);
        }
Esempio n. 10
0
        public void MultiplyTwoCollectionsTogetherCorrectlyWhenOriginExcludesAProduct()
        {
            // Add products to sut.
            sut.AddProducts(ProductMock1.Object, TestValue1);

            // add products to other.
            var other = new ProductAmountCollection();

            other.AddProducts(ProductMock1.Object, TestValue1);
            other.AddProducts(ProductMock2.Object, TestValue2);

            // multiply
            var result = sut.MultiplyBy(other);

            // verify correct calculation
            Assert.That(result.Products.Count, Is.EqualTo(2));
            Assert.That(result.ProductDict.Count, Is.EqualTo(2));
            Assert.That(result.GetProductValue(ProductMock1.Object), Is.EqualTo(TestValue1 * TestValue1));
            Assert.That(result.GetProductValue(ProductMock2.Object), Is.EqualTo(0));
        }
Esempio n. 11
0
        public IProductAmountCollection TotalDemand()
        {
            // get result to eventually return
            var result = new ProductAmountCollection();

            // add Normal Pop Demands
            foreach (var pop in Pops)
            {
                result.AddProducts(pop.TotalNeeds);
            }

            // add merchants
            result.AddProducts(Merchants.TotalNeeds);

            // Add Money Changers
            result.AddProducts(MoneyChangers.TotalNeeds);

            // return results.
            return(result);
        }
Esempio n. 12
0
        public void SucessfullyBarterForFractionalGood()
        {
            MarketMock.Setup(x => x.GetPrice(JobInput.Object))
            .Returns(20);

            LifeNeed.Setup(x => x.Fractional)
            .Returns(true);

            JobInputs.SetProductAmount(JobInput.Object, 6);

            var changeRet = new ProductAmountCollection();

            changeRet.SetProductAmount(JobInput.Object, 5);

            // Add Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, 2);
            sut.ForSale.AddProducts(LifeNeed.Object, 2);

            // Setup Market Change for price.
            MarketMock.Setup(x => x.ChangeForPrice(JobInputs, 100))
            .Returns(changeRet);

            // Barter 1 LifeNeed for 1 JobInput.
            var result = sut.BarterGood(JobInputs, LifeNeed.Object, 2, MarketMock.Object);

            // check transaction
            AssertProductAmountIsEqual(result, LifeNeed, 1.2);
            AssertProductAmountIsEqual(result, JobInput, -6);

            // Ensure no change in storage or for sale
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 2);
            AssertProductAmountIsEqual(sut.ForSale, LifeNeed, 2);

            // Ensure that receipt doesn't go above storage or ForSale
            Assert.That(sut.Storage.GetProductValue(LifeNeed.Object),
                        Is.GreaterThanOrEqualTo(result.GetProductValue(LifeNeed.Object)));
            Assert.That(sut.ForSale.GetProductValue(LifeNeed.Object),
                        Is.GreaterThanOrEqualTo(result.GetProductValue(LifeNeed.Object)));
        }
Esempio n. 13
0
        /// <summary>
        /// Default Constructor.
        /// </summary>
        public Product(IRandomizer randomizer)
        {
            if (randomizer is null)
            {
                throw new ArgumentNullException();
            }

            Id           = Guid.NewGuid();
            Name         = "DefaultName";
            VariantName  = "VariantName";
            UnitName     = "Unit";
            Quality      = 0;
            DefaultPrice = 1;
            Mass         = 1;
            Bulk         = 1;
            ProductType  = ProductTypes.Good;
            Maintainable = false;
            Fractional   = false;
            MTTF         = 0;
            FailsInto    = new ProductAmountCollection();
            Maintenance  = new ProductAmountCollection();

            this.randomizer = randomizer;
        }
Esempio n. 14
0
        public void ReturnSuccessWhenSellerIsShortOnChange()
        {
            // test varablies
            var goodPrice   = 123;
            var BuyAmount   = 1;
            var sellAmount  = 100;
            var expectedBuy = 1;

            // Change good to produce change.
            MarketMock.Setup(x => x.GetPrice(LifeNeed.Object)).Returns(goodPrice);

            // set up currency.
            MarketMock.Setup(x => x.GetPrice(CurrencyMock1.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 1);
            MarketMock.Setup(x => x.GetPrice(CurrencyMock2.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 100);

            // setup immediate cash.
            var cash = new ProductAmountCollection();

            cash.AddProducts(CurrencyMock1.Object, 0);
            cash.AddProducts(CurrencyMock2.Object, 10000);

            // Setup Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, sellAmount);
            sut.Storage.AddProducts(CurrencyMock1.Object, 50);
            sut.Storage.AddProducts(CurrencyMock2.Object, 0);
            sut.ForSale.AddProducts(LifeNeed.Object, sellAmount);
            sut.ForSale.AddProducts(CurrencyMock1.Object, 50);
            sut.ForSale.AddProducts(CurrencyMock2.Object, 0);

            // setup Product
            MarketMock.Setup(x => x.GetPrice(LifeNeed.Object))
            .Returns(goodPrice);

            // setup change return
            var change = new ProductAmountCollection();

            change.AddProducts(CurrencyMock1.Object, 0);
            change.AddProducts(CurrencyMock2.Object, 2);

            // Setup seller's change
            var sellerChange = new ProductAmountCollection();

            sellerChange.AddProducts(CurrencyMock1.Object, 50);
            sellerChange.AddProducts(CurrencyMock2.Object, 0);

            // Setup changes, general to specific
            MarketMock
            .Setup(x => x.ChangeForPrice(It.IsAny <IProductAmountCollection>(), 77))
            .Returns(sellerChange);
            MarketMock.Setup(x => x.ChangeForPrice(cash, goodPrice)).Returns(change);

            // test it out
            var result = sut.BuyGood(cash, LifeNeed.Object, BuyAmount, MarketMock.Object);

            // Ensure that the seller has appropriately changed his goods.
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, sellAmount - expectedBuy);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 2);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 0);

            // ensure result is correct transaction reciept
            AssertProductAmountIsEqual(result, LifeNeed, expectedBuy);
            AssertProductAmountIsEqual(result, CurrencyMock2, -2);
            AssertProductAmountIsEqual(result, CurrencyMock1, 50);
        }
Esempio n. 15
0
        public void ReturnPartialSuccessWhenGoodIsFractionalAndSomeCanBeBought()
        {
            // test varablies
            var goodPrice = 100;
            var BuyAmount = 1;

            // make good fractional
            LifeNeed.Setup(x => x.Fractional).Returns(true);

            // set up currency.
            MarketMock.Setup(x => x.GetPrice(CurrencyMock1.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 1);
            MarketMock.Setup(x => x.GetPrice(CurrencyMock2.Object, It.IsAny <double>()))
            .Returns((IProduct prod, double x) => x * 100);

            // setup immediate cash.
            var cash = new ProductAmountCollection();

            cash.AddProducts(CurrencyMock1.Object, 50);
            cash.AddProducts(CurrencyMock2.Object, 0);

            // Setup Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, 100);
            sut.Storage.AddProducts(CurrencyMock1.Object, 10000);
            sut.Storage.AddProducts(CurrencyMock2.Object, 10000);
            sut.ForSale.AddProducts(LifeNeed.Object, 100);
            sut.ForSale.AddProducts(CurrencyMock1.Object, 10000);
            sut.ForSale.AddProducts(CurrencyMock2.Object, 10000);

            // setup Product
            MarketMock.Setup(x => x.GetPrice(LifeNeed.Object))
            .Returns(goodPrice);

            // setup change return
            var change = new ProductAmountCollection();

            change.AddProducts(CurrencyMock1.Object, 50);
            change.AddProducts(CurrencyMock2.Object, 0);

            // Setup seller's change
            var sellerChange = new ProductAmountCollection();

            sellerChange.AddProducts(CurrencyMock1.Object, 0);
            sellerChange.AddProducts(CurrencyMock2.Object, 0);

            // Setup changes, general to specific
            MarketMock
            .Setup(x => x.ChangeForPrice(It.IsAny <IProductAmountCollection>(), 77))
            .Returns(sellerChange);
            MarketMock.Setup(x => x.ChangeForPrice(cash, goodPrice)).Returns(change);

            // test it out
            var result = sut.BuyGood(cash, LifeNeed.Object, BuyAmount, MarketMock.Object);

            // Ensure that the seller has appropriately changed his goods.
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 99.5);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 10000);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 10050);

            // ensure result is correct transaction reciept
            AssertProductAmountIsEqual(result, LifeNeed, 0.5);
            AssertProductAmountIsEqual(result, CurrencyMock2, 0);
            AssertProductAmountIsEqual(result, CurrencyMock1, -50);
        }
Esempio n. 16
0
 public Currency(IRandomizer rand) : base(rand)
 {
     Trust       = 0.5;
     Symbol      = 'x';
     Maintenance = new ProductAmountCollection();
 }
Esempio n. 17
0
        public void CompleteATransactionsCorrectly()
        {
            // create transaction
            var transaction = new ProductAmountCollection();

            transaction.AddProducts(LifeNeed.Object, 1);
            transaction.AddProducts(DailyNeed.Object, 2);
            transaction.AddProducts(LuxNeed.Object, 3);
            transaction.AddProducts(CurrencyMock1.Object, 4);
            transaction.AddProducts(CurrencyMock2.Object, 5);

            // complete transaction
            sut.CompleteTransaction(transaction);

            // check it's all there in Storage
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 1);
            AssertProductAmountIsEqual(sut.Storage, DailyNeed, 2);
            AssertProductAmountIsEqual(sut.Storage, LuxNeed, 3);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 4);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 5);

            // Check it's in For Sale
            AssertProductAmountIsEqual(sut.ForSale, LifeNeed, 1);
            AssertProductAmountIsEqual(sut.ForSale, DailyNeed, 2);
            AssertProductAmountIsEqual(sut.ForSale, LuxNeed, 3);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock1, 4);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock2, 5);

            // Add it again to double check
            sut.CompleteTransaction(transaction);

            // check it's all there in Storage
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 2);
            AssertProductAmountIsEqual(sut.Storage, DailyNeed, 4);
            AssertProductAmountIsEqual(sut.Storage, LuxNeed, 6);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 8);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 10);

            // Check it's in For Sale
            AssertProductAmountIsEqual(sut.ForSale, LifeNeed, 2);
            AssertProductAmountIsEqual(sut.ForSale, DailyNeed, 4);
            AssertProductAmountIsEqual(sut.ForSale, LuxNeed, 6);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock1, 8);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock2, 10);

            // check again, but subtract this time.
            transaction.SetProductAmount(LifeNeed.Object, -2);

            sut.CompleteTransaction(transaction);

            // check it's all there in Storage
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 0);
            AssertProductAmountIsEqual(sut.Storage, DailyNeed, 6);
            AssertProductAmountIsEqual(sut.Storage, LuxNeed, 9);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock1, 12);
            AssertProductAmountIsEqual(sut.Storage, CurrencyMock2, 15);

            // Check it's in For Sale
            AssertProductAmountIsEqual(sut.ForSale, DailyNeed, 6);
            AssertProductAmountIsEqual(sut.ForSale, LuxNeed, 9);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock1, 12);
            AssertProductAmountIsEqual(sut.ForSale, CurrencyMock2, 15);

            // ensure that Subtracted product is deleted
            Assert.That(sut.ForSale.Contains(LifeNeed.Object), Is.False);
        }