public async Task <IActionResult> BuyWeaponPost(int Id) { var weapon = await weaponDataService.GetWeaponByIdFromDatabaseAsync(Id); if (weapon == null) { return(NotFound()); } //TODO await on all methods var user = await userManager.GetUserAsync(HttpContext.User); var userCoins = user.Coins; var weaponPrice = weapon.Price; if (userCoins >= weaponPrice) { user.Weapons.Add(weapon); user.Coins -= weaponPrice; weapon.UserId = user.Id; dbContextService.SaveChanges(); TempData["Message"] = $"Successfully bought {weapon.Name}"; return(RedirectToAction("Weapons")); } var missingMoney = weaponPrice - user.Coins; TempData["Message"] = $"You need {missingMoney} more coins"; return(RedirectToAction("Weapons")); }
public async Task <IActionResult> Repair(int itemId, string itemType) { if (itemType == "ArmorRepairViewModel") { var model = await armorDataService.GetArmorWithOwnerByIdFromDatabaseAsync(itemId); if (model == null) { return(NotFound()); } var repairPrice = (100 - model.Durability) * 5; var user = model.User; if (user.Coins >= repairPrice) { user.Coins -= repairPrice; model.Durability = 100; dbContextService.SaveChanges(); TempData["RepairSuccess"] = $"Succesfully repaired {model.Name}"; return(RedirectToAction("Index")); } var missingMoney = repairPrice - user.Coins; TempData["RepairError"] = $"You need {missingMoney} more coins"; } else if (itemType == "WeaponRepairViewModel") { var model = await weaponDataService.GetWeaponWithOwnerByIdFromDatabaseAsync(itemId); if (model == null) { return(NotFound()); } var repairPrice = (100 - model.Durability) * 5; var user = model.User; if (user.Coins >= repairPrice) { user.Coins -= repairPrice; model.Durability = 100; model.CriticalAttackMode = true; dbContextService.SaveChanges(); TempData["RepairSuccess"] = $"Succesfully repaired {model.Name}"; return(RedirectToAction("Index")); } var missingMoney = repairPrice - user.Coins; TempData["RepairError"] = $"You need {missingMoney} more coins"; } else if (itemType == "RobotRepairViewModel") { var model = await robotDataService.GetRobotWithOwnerByIdFromDatabaseAsync(itemId); if (model == null) { return(NotFound()); } var repairPrice = (model.BaseHealth - model.CurrentHealth) / 3; var user = model.Owner; if (user.Coins >= repairPrice) { user.Coins -= repairPrice; model.CurrentHealth = model.BaseHealth; dbContextService.SaveChanges(); TempData["RepairSuccess"] = $"Succesfully repaired {model.Name}"; return(RedirectToAction("Index")); } var missingMoney = repairPrice - user.Coins; TempData["RepairError"] = $"You need {missingMoney} more coins"; } return(RedirectToAction("Index")); }
public async Task <IActionResult> Fight(int RobotId, int CreepId) { var robot = await robotDataService.GetRobotWithAllItemsByIdAsync(RobotId); if (robot == null) { return(NotFound()); } var creep = await creepDataService.GetCreepByIdAsync(CreepId); if (robot == null) { return(NotFound()); } int roundsCount = 0; int robotDamageDone = 0; int creepDamageDone = 0; bool ignoreArmor = false; bool robotIsAlive = true; bool creepIsAlive = true; var preCreepDamage = creep.Damage; var creepDamage = (int)(creep.Damage * decimal.Divide((100 - robot.DamageReduction), 100)); var creepType = creep.Type; var robotType = robot.Type; var robotDamage = robot.TotalDamage; var creepHealth = creep.Health; var reducedCreepDamage = (int)(creep.Damage * decimal.Divide(robot.DamageReduction, 100)); if ((creepType == "Water" && robotType == "Fire") || (creepType == "Nature" && robotType == "Water") || (creepType == "Fire" && robotType == "Nature")) { creepDamage = preCreepDamage; ignoreArmor = true; } else if ((robotType == "Water" && creepType == "Fire") || (robotType == "Nature" && creepType == "Water") || (robotType == "Fire" && creepType == "Nature")) { robotDamage *= 2; } while (true) { //TODO ALL CASES ABOUT TYPES FIRE VS WATER ETC roundsCount++; robot.CurrentHealth -= creepDamage; creepDamageDone += creepDamage; if (robot.CurrentHealth <= 0) { robotIsAlive = false; robot.Losses += 1; break; } creepHealth -= robotDamage; robotDamageDone += robotDamage; if (creepHealth <= 0) { creepIsAlive = false; robot.Wins += 1; break; } } int robotReducedDamage = reducedCreepDamage * roundsCount; if (ignoreArmor) { robotReducedDamage = 0; } var robotModel = Mapper.Map <RobotFightViewModel>(robot); var creepModel = Mapper.Map <CreepFightViewModel>(creep); var fightResultModel = new FightResultViewModel { RobotModel = robotModel, CreepModel = creepModel, CreepDamageDone = creepDamageDone, RobotDamageDone = robotDamageDone, RobotReducedDamage = robotReducedDamage, RoundsCount = roundsCount, RobotIsAlive = robotIsAlive, CreepIsAlive = creepIsAlive }; var user = userManager.GetUserAsync(this.ControllerContext.HttpContext.User).Result; if (!creepIsAlive) { user.Coins += creep.Bounty; } robot.Weapons.ToList().ForEach(w => w.ReduceDurability()); robot.Weapons.ToList().ForEach(w => w.RemoveCriticalStrike()); robot.Armors.ToList().ForEach(a => a.ReduceDurability()); // context.Robots.Update(robot); // context.Users.Update(user); dbContextService.SaveChanges(); return(View("Result", fightResultModel)); }