public async Task <ActionResult> Claim(UserAnimals model) { if (ModelState.IsValid) { var _userManager = _serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); ApplicationUser user = await _userManager.GetUserAsync(User); var animal = _db.UserAnimals.Where(ua => ua.UserId == user.Id && ua.AnimalId == model.AnimalId).Include(ua => ua.Animal).FirstOrDefault(); if (user != null && animal != null) { DateTime now = DateTime.UtcNow; double secondsDif = (now - animal.LastClaimTime).TotalSeconds; int intProduce = (int)((secondsDif / animal.Animal.ProductionTimeSeconds) * animal.AnimalCount * decimal.ToDouble(animal.Animal.ProductionSpeed)); if (intProduce > 0) { animal.ClaimedProduceCount += intProduce; /*Add time that it needs to produce intProduce production, so decimals stays*/ decimal secondsToAdd = 0; secondsToAdd = ((animal.Animal.ProductionTimeSeconds / animal.AnimalCount) / animal.Animal.ProductionSpeed) * intProduce; animal.LastClaimTime = animal.LastClaimTime.AddSeconds(Decimal.ToDouble(secondsToAdd)); _db.UserAnimals.Update(animal); _db.SaveChanges(); } } } return(RedirectToAction("Index")); }
public async Task <IActionResult> SellProduce(UserAnimals model) { if (ModelState.IsValid) { var _userManager = _serviceProvider.GetRequiredService <UserManager <ApplicationUser> >(); ApplicationUser user = await _userManager.GetUserAsync(User); UserAnimals DBmodel = _db.UserAnimals.Where(ua => ua.UserId == user.Id && ua.Id == model.Id).Include(ua => ua.Animal).FirstOrDefault(); if (DBmodel != null) { decimal UnitPrice = getAnimalProducePrice(); decimal PriceToAddToBalance = UnitPrice * DBmodel.ClaimedProduceCount; user.TRTLBalance += PriceToAddToBalance; _db.Users.Update(user); DBmodel.ClaimedProduceCount = 0; _db.UserAnimals.Update(DBmodel); _db.SaveChanges(); } } return(RedirectToAction("Sell")); }