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 PurchaseBackground(string backgroundSkinName) { bool result = false; BackgroundSkin backgroundSkin = _context.BackgroundSkins.FirstOrDefault(s => s.SkinName == backgroundSkinName); AsteroidUser curUser = GetCurrentUser(); if (backgroundSkin != null && curUser != null && curUser.Coins >= backgroundSkin.SkinCost) { // Adjust users coins and add ship to database AdjustCoins(-backgroundSkin.SkinCost); OwnedBackground purchasedBackground = new OwnedBackground { AsteroidUser = curUser, BackgroundSkinId = backgroundSkin.BackgroundSkinId }; _context.OwnedBackgrounds.Add(purchasedBackground); _context.Users.Update(curUser); _context.SaveChanges(); // Succeeded purchasing return(new JsonResult(new { success = result, background = backgroundSkin })); } else { return(BadRequest(new JsonResult(new { success = false, message = "Not enough coins." }))); } }
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new AsteroidUser { UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password); if (result.Succeeded) { // Add starting ship and background when registering OwnedShip startShip = new OwnedShip { AsteroidUserId = user.Id, ShipSkinId = DbInitializer.DEFAULT_SHIP.ShipSkinId }; OwnedBackground startBackground = new OwnedBackground { AsteroidUserId = user.Id, BackgroundSkinId = DbInitializer.DEFAULT_BACKGROUND.BackgroundSkinId }; _context.OwnedShips.Add(startShip); _context.OwnedBackgrounds.Add(startBackground); _context.SaveChanges(); _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); var callbackUrl = Url.Page( "/Account/ConfirmEmail", pageHandler: null, values: new { area = "Identity", userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."); if (_userManager.Options.SignIn.RequireConfirmedAccount) { return(RedirectToPage("RegisterConfirmation", new { email = Input.Email })); } else { await _signInManager.SignInAsync(user, isPersistent : false); return(LocalRedirect(returnUrl)); } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } // If we got this far, something failed, redisplay form return(Page()); }
// GET: /Store/ public IActionResult Index() { AsteroidUser user = GetCurrentUser(); // Check if null if (user == null) { return(Redirect("Identity/Account/Login")); } // Get all of the user's owned ship objects List <OwnedShip> ownedShips = _context.OwnedShips .Include(os => os.ShipSkin) .Where(os => os.AsteroidUserId == user.Id) .ToList(); // Next loop through and add ships List <StoreShip> storeShips = new List <StoreShip>(); foreach (var ship in _context.ShipSkins) { // Set ship to purchased if user has it bool isPurchased = ownedShips .Exists(os => os.ShipSkin == ship); StoreShip ss = new StoreShip { ShipSkin = ship, IsPurchased = isPurchased }; storeShips.Add(ss); } // Get all of the user's owned background skin objects List <OwnedBackground> ownedBkgnds = _context.OwnedBackgrounds .Include(ob => ob.BackgroundSkin) .Where(ob => ob.AsteroidUserId == user.Id) .ToList(); // Loop through and add bkgrnds List <StoreBackground> storeBackgrounds = new List <StoreBackground>(); foreach (var bkgrnd in _context.BackgroundSkins) { // Set background to purchased if user has it bool isPurchased = ownedBkgnds .Exists(ob => ob.BackgroundSkin == bkgrnd); StoreBackground sb = new StoreBackground { BackgroundSkin = bkgrnd, IsPurchased = isPurchased }; storeBackgrounds.Add(sb); } // Build model and return to view return(View(new StoreModel { User = user, StoreShips = storeShips, StoreBackgrounds = storeBackgrounds })); }
private async Task LoadAsync(AsteroidUser user) { var userName = await _userManager.GetUserNameAsync(user); var phoneNumber = await _userManager.GetPhoneNumberAsync(user); Username = userName; Input = new InputModel { PhoneNumber = phoneNumber }; }
/// <summary> /// Helper method to get current user with ship and background data fetched /// </summary> /// <returns></returns> public string GetCurrentBackground() { // Fetch user with included ship/background meta data AsteroidUser curUser = _userManager.Users .FirstOrDefault(u => u.Email == HttpContext.User.Identity.Name); if (curUser != null) { return("../images/b" + (curUser.CurrentBackgrounId + 1) + ".jpg"); } else { return("../images/b1.jpg"); } }
public IActionResult AddUserScore(int score) { bool result = false; AsteroidUser user = GetCurrentUser(); // TODO: check if user actually finished a game? if (user != null) { Leaderboard leadboardEntry = new Leaderboard { AsteroidUserId = user.Id, Score = score }; _context.Leaderboard.Add(leadboardEntry); _context.SaveChanges(); result = true; } return(new JsonResult(new { success = result })); }
/// <summary> /// Helper method to call in the JavaScript /// </summary> /// <returns>Path to the current ship</returns> public IActionResult GetUserShip() { AsteroidUser user = GetCurrentUser(); if (user != null) { if (user.CurrentShipId != 0) { return(new JsonResult(new { success = true, imageSrc = "../images/s" + (user.CurrentShipId + 1) + ".png" })); } else { return(new JsonResult(new { success = true, imageSrc = "../images/s1.png" })); } } else { return(new JsonResult(new { success = true, imageSrc = "../images/s1.png" })); } }
public IActionResult AdjustCoins(int deltaCoins) { bool result = false; AsteroidUser user = GetCurrentUser(); if (user != null) { user.Coins += deltaCoins; if (user.Coins < 0) { return(new JsonResult(new { success = result })); } _context.Users.Update(user); _context.SaveChanges(); result = true; } return(new JsonResult(new { success = result })); }
public IActionResult SetCurrentBackground(string backgroundSkinName) { bool result = false; BackgroundSkin backgroundSkin = _context.BackgroundSkins.FirstOrDefault(s => s.SkinName == backgroundSkinName); AsteroidUser curUser = GetCurrentUser(); // Check if skin and user are valid. Lastly, verify that user actually owns this skin if (backgroundSkin != null && curUser != null && curUser.OwnedBackgrounds.ToList().Exists(os => os.BackgroundSkinId == backgroundSkin.BackgroundSkinId)) { // Update and save user's current background curUser.CurrentBackgrounId = backgroundSkin.BackgroundSkinId; _context.Users.Update(curUser); _context.SaveChanges(); // Succeeded purchasing result = true; } return(new JsonResult(new { success = result, background = backgroundSkin })); }