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 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()); }
/// <summary> /// Public database initialization method /// </summary> /// <param name="context"></param> public static async Task Initialize(AsteroidDodgeContext context, UserManager <AsteroidUser> userManager) { // Check to make sure db is properly created context.Database.EnsureCreated(); // Loop through and add all skins foreach (ShipSkin skin in SKINS) { if (!context.ShipSkins.Contains(skin)) { context.ShipSkins.Add(skin); } } context.SaveChanges(); string pass = "******"; // Loop through and create all new users foreach (AsteroidUser user in USERS) { var findRes = await userManager.FindByEmailAsync(user.Email); if (findRes == null) { await userManager.CreateAsync(user, pass); OwnedShip owned = new OwnedShip { AsteroidUserId = user.Id, ShipSkinId = DEFAULT_SHIP.ShipSkinId }; context.OwnedShips.Add(owned); } } foreach (var backgroundSkin in BCKGRDSKIN) { if (!context.BackgroundSkins.Contains(backgroundSkin)) { context.BackgroundSkins.Add(backgroundSkin); } } context.SaveChanges(); }