Пример #1
0
        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());
        }
Пример #2
0
        /// <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();
        }
Пример #3
0
        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 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 }));
        }