public async Task <IActionResult> CaughtPokemon(int id, bool isShiny, int areaId) { Pokemon model = await _context.Pokemon.FindAsync(id); CaughtPokemon caught = new CaughtPokemon { UserID = _userManager.GetUserId(User), PokemonName = model.PokemonName, IsShiny = isShiny, CatchDate = DateTime.UtcNow, }; PokePCUser user = await _userContext.Users.FindAsync(_userManager.GetUserId(User)); ViewData["PrevExperience"] = user.Experience; var levelup = false; int areaExp = _context.Areas.Find(areaId).ExpPerCatch; // Calculate the experience that the player's next level requires double nextLvlExp = (user.Level * 1000) * (1.5); // Add the experience from catching the Pokémon user.Experience = user.Experience + areaExp; // Calculate the experience the user needs to level up // Ternary operator to account for if user experience is over the next level experience, which sets required exp to 0 double expToLvl = nextLvlExp - user.Experience < 0 ? 0 : nextLvlExp - user.Experience; // If the user's experience exceeds the amount required for their next level, level them up by 1 if (user.Experience >= nextLvlExp) { user.Level = user.Level + 1; levelup = true; } user.UniquePokemon = user.UniquePokemon + 1; _userContext.SaveChanges(); _context.Add(caught); _context.SaveChanges(); ViewData["IsShiny"] = isShiny; ViewData["NewExperience"] = user.Experience; ViewData["LevelUp"] = levelup; ViewData["Level"] = user.Level; ViewData["ExpGain"] = areaExp; ViewData["ExpLeft"] = expToLvl; return(PartialView("_CaughtPokemon", model)); }
public async Task <IActionResult> Register(Register obj) { if (ModelState.IsValid) { if (!roleManager.RoleExistsAsync("Standard").Result) { PokePCRoles role = new PokePCRoles(); role.Name = "Standard"; IdentityResult roleResult = roleManager.CreateAsync(role).Result; } PokePCUser user = new PokePCUser(); user.UserName = obj.UserName; user.Email = obj.Email; user.CreationDate = DateTime.UtcNow; user.UniquePokemon = 1; user.Money = 1000; user.Level = 1; user.Experience = 0; IdentityResult result = userManager.CreateAsync(user, obj.Password).Result; if (result.Succeeded) { userManager.AddToRoleAsync(user, "Standard").Wait(); CaughtPokemon starter = new CaughtPokemon { UserID = user.Id, PokemonName = obj.Starter, IsShiny = false, CatchDate = DateTime.UtcNow, }; _context.Add(starter); _context.SaveChanges(); return(RedirectToAction("Login", "Account")); } else { ModelState.AddModelError("", "Invalid user details"); } } ViewData["Starters"] = await _context.Pokemon.Where(e => (e.PokedexNum == 1 || e.PokedexNum == 4 || e.PokedexNum == 7)).ToListAsync(); return(View(obj)); }