Exemplo n.º 1
0
        /// <summary>
        /// This action takes in the passed upgrade id,  buys the ship upgrade via the SystemEngineUpgrade.Buy method
        /// and then redirects to the Index action.
        /// </summary>
        /// <param name="jumpDriveId">The jump drive id.</param>
        /// <returns>A redirect to the ViewShip action on success, the BuyJumpDriveUpgrade view on error</returns>
        public ActionResult BuyJumpDriveUpgrade(int jumpDriveId)
        {
            Ship currentShip = this.ControllerGame.CurrentPlayer.Ship;

            SystemJumpDriveUpgrade upgrade = currentShip.CosmoSystem.GetJumpDriveUpgrade(jumpDriveId);

            if (upgrade != null)
            {
                try
                {
                    // Buy the upgrade
                    upgrade.Buy(currentShip);

                    // Success, redirect to display the newly upgrade on their ship
                    return(RedirectToAction("ViewShip"));
                }
                catch (InvalidOperationException ex)
                {
                    // Log this exception
                    ExceptionPolicy.HandleException(ex, "Controller Policy");

                    ModelState.AddModelError("_FORM", ex.Message);
                }
            }
            else
            {
                ModelState.AddModelError("jumpDriveId", "Invalid JumpDrive Upgrade", jumpDriveId);
            }

            return(View());
        }
        public void BuyNotEnoughCargoSpace()
        {
            // Arrange
            SystemJumpDriveUpgrade upgrade  = this.CreateSystemJumpDriveUpgrade();
            Mock <User>            userMock = new Mock <User>();
            Mock <Ship>            shipMock = new Mock <Ship>();

            shipMock.Expect(s => s.BaseShip.Level)
            .Returns(1).Verifiable();

            JumpDrive currentUpgrade = new JumpDrive();

            // Trade value will be 80
            currentUpgrade.BasePrice = 100;
            currentUpgrade.CargoCost = 1;

            shipMock.Expect(s => s.JumpDrive)
            .Returns(currentUpgrade).Verifiable();

            // Cargo space free is 1
            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(1).AtMostOnce().Verifiable();

            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).AtMostOnce().Verifiable();

            // Act, should throw an exception
            upgrade.Buy(shipMock.Object);
        }
        public void BuyPlayerUpgradeAddedToSystem2()
        {
            // Arrange
            SystemJumpDriveUpgrade upgrade = this.CreateSystemJumpDriveUpgrade();

            BaseShip playerBaseShip = new BaseShip();

            playerBaseShip.Level = 1;

            JumpDrive currentUpgrade = new JumpDrive();

            // Trade value will be 160
            currentUpgrade.BasePrice = 200;
            currentUpgrade.CargoCost = 1;

            SystemJumpDriveUpgrade playerSystemUpgrade = new SystemJumpDriveUpgrade();

            playerSystemUpgrade.JumpDrive   = currentUpgrade;
            playerSystemUpgrade.CosmoSystem = upgrade.CosmoSystem;
            playerSystemUpgrade.Quantity    = 2;

            Mock <Ship> shipMock = new Mock <Ship>();

            // Setup player base upgrade model
            shipMock.Expect(s => s.BaseShip)
            .Returns(playerBaseShip).Verifiable();

            // Setup player base upgrade
            shipMock.Expect(s => s.JumpDrive)
            .Returns(currentUpgrade).Verifiable();

            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(25).Verifiable();

            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).Verifiable();

            // Act
            upgrade.Buy(shipMock.Object);

            // Assert
            shipMock.Verify();
            // Cost of the upgrade should be (750 - 160) credits
            shipMock.VerifySet(m => m.Credits, 5000 - (750 - 160));
            Assert.That(upgrade.Quantity, Is.EqualTo(0), "Should be no ships left in the system of this model");
            Assert.That(upgrade.CosmoSystem.SystemJumpDriveUpgrades.Where(m => m.JumpDrive == playerSystemUpgrade.JumpDrive && m.Quantity == 3).Any(), Is.True, "The players base ship should have been added to the system for sale");
        }
        public void BuyPlayerUpgradeAddedToSystem()
        {
            // Arrange
            SystemJumpDriveUpgrade upgrade = this.CreateSystemJumpDriveUpgrade();

            Mock <Ship> shipMock = new Mock <Ship>();

            shipMock.Expect(s => s.BaseShip.Level)
            .Returns(1).Verifiable();

            JumpDrive currentUpgrade = new JumpDrive();

            // Trade value will be 80
            currentUpgrade.BasePrice = 100;
            currentUpgrade.CargoCost = 1;

            shipMock.Expect(s => s.JumpDrive)
            .Returns(currentUpgrade).Verifiable();

            // Cargo space free is 10
            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(10).AtMostOnce().Verifiable();

            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).Verifiable();


            // Act
            upgrade.Buy(shipMock.Object);

            // Assert
            shipMock.Verify();
            // Cost of the upgrade should be (750 - 80) credits
            shipMock.VerifySet(m => m.Credits, 5000 - (750 - 80));
            Assert.That(upgrade.Quantity, Is.EqualTo(0), "Should be no upgrades left in the system of this model");
            Assert.That(upgrade.CosmoSystem.SystemJumpDriveUpgrades.Where(m => m.JumpDrive == currentUpgrade && m.Quantity == 1).Any(), Is.True, "The players upgrade should have been added to the system for sale");
        }
        public void BuyWithLevel2Ship()
        {
            // Arrange
            SystemJumpDriveUpgrade upgrade = this.CreateSystemJumpDriveUpgrade();

            Mock <Ship> shipMock = new Mock <Ship>();

            shipMock.Expect(s => s.BaseShip.Level)
            .Returns(2).Verifiable();

            JumpDrive currentUpgrade = new JumpDrive();

            currentUpgrade.JumpDriveId = 2;
            // Trade value will be 80
            currentUpgrade.BasePrice = 100;
            currentUpgrade.CargoCost = 1;

            shipMock.Expect(s => s.JumpDrive)
            .Returns(currentUpgrade).Verifiable();

            // Cargo space free is 10
            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(10).Verifiable();

            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).Verifiable();

            // Act
            upgrade.Buy(shipMock.Object);

            // Assert
            shipMock.Verify();
            // Cost of the upgrade should be (750*2 - 80*2) credits
            shipMock.VerifySet(s => s.Credits, 5000 - (750 * 2 - (80 * 2)));
            Assert.That(upgrade.Quantity, Is.EqualTo(0), "Should be no upgrades left in the system of this model");
        }