public ActionResult SellGoods(int goodId, int quantity, int price) { ShipGood shipGood = this.ControllerGame.CurrentPlayer.Ship.GetGood(goodId); if (shipGood != null) { try { shipGood.Sell(this.ControllerGame.CurrentPlayer.Ship, quantity, price); return(RedirectToAction("ListGoods")); } catch (ArgumentOutOfRangeException ex) { // Log this exception ExceptionPolicy.HandleException(ex, "Controller Policy"); if (ex.ParamName == "price") { // Go to the SellGoodsPriceChange view // Price has changed on the good // Confirm the player still wants to sell the good ViewData["goodId"] = goodId; ViewData["quantity"] = quantity; ViewData["oldPrice"] = price; ViewData["goodName"] = shipGood.Good.Name; // Try to get the current price SystemGood systemGood = this.ControllerGame.CurrentPlayer.Ship.CosmoSystem.GetGood(goodId); if (systemGood != null) { ViewData["newPrice"] = systemGood.Price; } return(View("SellGoodsPriceChange")); } else { ModelState.AddModelError("quantity", ex.Message, quantity); } } catch (InvalidOperationException ex) { // Log this exception ExceptionPolicy.HandleException(ex, "Controller Policy"); ModelState.AddModelError("goodId", ex.Message, quantity); } } else { ModelState.AddModelError("goodId", "Good is not bought in the system", goodId); } return(View()); }
public void SellNotSold() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); // Setup player Player testPlayer = this.CreateTestPlayer(); Ship testShip = testPlayer.Ship; CosmoSystem startingSystem = testShip.CosmoSystem; GameManager manager = new GameManager(testPlayer.User.UserName); // Store the player starting cash int playerStartingCash = testPlayer.Ship.Credits; // Add some water to this ship for us to sell Good water = (from g in db.Goods where g.Name == "Water" select g).SingleOrDefault(); Assert.That(water, Is.Not.Null, "We should have a Water good"); ShipGood shipGood = new ShipGood(); shipGood.Good = water; shipGood.Quantity = 10; shipGood.Ship = testShip; testShip.ShipGoods.Add(shipGood); // Verify that the good is for sell in the current system SystemGood systemWater = startingSystem.GetGood(water.GoodId); if (systemWater != null) { // Remove the good from the system db.SystemGoods.DeleteOnSubmit(systemWater); db.SubmitChanges(); } try { shipGood.Sell(testShip, 10, systemWater.Price); } catch (InvalidOperationException ex) { // Correct Assert.That(ex, Is.Not.Null, "InvalidOperationException should be valid"); return; } Assert.Fail("Player should not been able to sell in the system when the good was not sold in the system"); }
public void SellNotEnoughGoods() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); // Setup player Player testPlayer = this.CreateTestPlayer(); Ship testShip = testPlayer.Ship; CosmoSystem startingSystem = testShip.CosmoSystem; GameManager manager = new GameManager(testPlayer.User.UserName); // Add some water to this ship for us to sell Good water = (from g in db.Goods where g.Name == "Water" select g).SingleOrDefault(); Assert.That(water, Is.Not.Null, "We should have a Water good"); ShipGood shipGood = new ShipGood(); shipGood.Good = water; shipGood.Quantity = 10; shipGood.Ship = testShip; testShip.ShipGoods.Add(shipGood); db.SubmitChanges(); // Verify that the good is for sell in the current system SystemGood systemWater = startingSystem.GetGood(water.GoodId); if (systemWater == null) { startingSystem.AddGood(water.GoodId, 0); systemWater = startingSystem.GetGood(water.GoodId); } try { shipGood.Sell(testShip, 20, systemWater.Price); } catch (ArgumentOutOfRangeException ex) { Assert.That(ex.ParamName, Is.EqualTo("quantity"), "Quantity to sell should be the invalid argument"); return; } Assert.Fail("Player should not been able to sell more goods than aboard"); }
public void Sell() { CosmoMongerDbDataContext db = CosmoManager.GetDbContext(); // Setup player Player testPlayer = this.CreateTestPlayer(); Ship testShip = testPlayer.Ship; CosmoSystem startingSystem = testShip.CosmoSystem; GameManager manager = new GameManager(testPlayer.User.UserName); // Store the player starting cash int playerStartingCash = testPlayer.Ship.Credits; // Add some water to this ship for us to sell Good water = (from g in db.Goods where g.Name == "Water" select g).SingleOrDefault(); Assert.That(water, Is.Not.Null, "We should have a Water good"); ShipGood shipGood = new ShipGood(); shipGood.Good = water; shipGood.Quantity = 10; shipGood.Ship = testShip; testShip.ShipGoods.Add(shipGood); // Verify that the good is for sell in the current system SystemGood systemWater = startingSystem.GetGood(water.GoodId); if (systemWater == null) { startingSystem.AddGood(water.GoodId, 0); systemWater = startingSystem.GetGood(water.GoodId); } int playerProfit = systemWater.Price * 10; int systemStartingCount = systemWater.Quantity; shipGood.Sell(testShip, 10, systemWater.Price); Assert.That(systemWater.Quantity, Is.EqualTo(systemStartingCount + 10), "System should now have 10 more water goods"); Assert.That(testPlayer.Ship.Credits, Is.EqualTo(playerStartingCash + playerProfit), "Player should have more cash credits now after selling"); }