public ActionResult BuyShip(int shipId)
        {
            Ship       currentShip = this.ControllerGame.CurrentPlayer.Ship;
            SystemShip shipToBuy   = currentShip.CosmoSystem.GetBuyableShip(shipId);

            if (shipToBuy != null)
            {
                try
                {
                    shipToBuy.Buy(currentShip);

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

                    ModelState.AddModelError("_FORM", ex.Message);
                }
            }
            else
            {
                ModelState.AddModelError("shipId", "Ship is no longer for sell in system", shipId);
            }

            // If we got down here, then an error was encountered
            // Get back to the list of ships
            return(this.ListShips());
        }
Exemple #2
0
        public void Buy()
        {
            // Arrange
            SystemShip ship = this.CreateSystemShip();

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

            // Trade value is 5500
            shipMock.Expect(s => s.TradeInValue)
            .Returns(5500).Verifiable();
            // Cargo space is 50, with 25 free
            shipMock.Expect(s => s.CargoSpaceTotal)
            .Returns(50).Verifiable();
            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(25).Verifiable();
            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).Verifiable();

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

            // Assert
            shipMock.Verify();
            // Cost of the ship should be 2000 credits
            shipMock.VerifySet(s => s.Credits, 5000 - 2000);
            Assert.That(ship.Quantity, Is.EqualTo(0), "Should be no ships left in the system of this model");
        }
Exemple #3
0
        public void BuyPlayerShipAddedToSystem()
        {
            // Arrange
            SystemShip ship           = this.CreateSystemShip();
            BaseShip   playerBaseShip = new BaseShip();

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

            // Setup player base ship model
            shipMock.Expect(s => s.BaseShip)
            .Returns(playerBaseShip).Verifiable();
            // Trade value is 5500
            shipMock.Expect(s => s.TradeInValue)
            .Returns(5500).Verifiable();
            // Cargo space is 50, with 25 free
            shipMock.Expect(s => s.CargoSpaceTotal)
            .Returns(50).Verifiable();
            shipMock.Expect(s => s.CargoSpaceFree)
            .Returns(25).Verifiable();
            // Cash on hand is 5000
            shipMock.Expect(s => s.Credits)
            .Returns(5000).Verifiable();

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

            // Assert
            shipMock.Verify();
            // Cost of the ship should be 2000 credits
            shipMock.VerifySet(m => m.Credits, 5000 - 2000);
            Assert.That(ship.Quantity, Is.EqualTo(0), "Should be no ships left in the system of this model");
            Assert.That(ship.CosmoSystem.SystemShips.Where(m => m.BaseShip == playerBaseShip && m.Quantity == 1), Is.Not.Empty, "The players base ship should have been added to the system for sale");
        }
Exemple #4
0
        public void BuyNotEnoughCredits()
        {
            // Arrange
            SystemShip  ship     = this.CreateSystemShip();
            Mock <User> userMock = new Mock <User>();
            Mock <Ship> shipMock = new Mock <Ship>();

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

            // Act, should throw an exception
            ship.Buy(shipMock.Object);
        }