public IActionResult PurchaseShip(string shipSkinName) { bool result = false; ShipSkin shipSkin = _context.ShipSkins.FirstOrDefault(s => s.SkinName == shipSkinName); AsteroidUser curUser = GetCurrentUser(); if (shipSkin != null && curUser != null && curUser.Coins >= shipSkin.SkinCost) { // Adjust users coins and add ship to database AdjustCoins(-shipSkin.SkinCost); OwnedShip purchasedShip = new OwnedShip { AsteroidUserId = curUser.Id, ShipSkinId = shipSkin.ShipSkinId }; _context.OwnedShips.Add(purchasedShip); _context.Users.Update(curUser); _context.SaveChanges(); // Succeeded purchasing return(new JsonResult(new { success = result, ship = shipSkin })); } else { return(BadRequest(new JsonResult(new { success = false, message = "Not enough coins." }))); } }
public IActionResult SetCurrentShip(string shipSkinName) { bool result = false; ShipSkin shipSkin = _context.ShipSkins.FirstOrDefault(s => s.SkinName == shipSkinName); AsteroidUser curUser = GetCurrentUser(); // Check if skin and user are valid. Lastly, verify that user actually owns this skin if (shipSkin != null && curUser != null && curUser.OwnedShips.ToList().Exists(os => os.ShipSkinId == shipSkin.ShipSkinId)) { // Update and save user's current ship curUser.CurrentShipId = shipSkin.ShipSkinId; curUser.CurrentShip = shipSkin; _context.Users.Update(curUser); _context.SaveChanges(); // Succeeded purchasing result = true; } return(new JsonResult(new { success = result, ship = shipSkin })); }
protected override void Awake() { base.Awake(); skin = item as ShipSkin; }