public async Task <(Guid, ICollection <(int, int, int, int, int, int, int)>)> Attack(Guid attackerId, Guid defenderId) { using (var uow = UnitOfWorkProvider.Create()) { var attacker = await _characterService.GetAsync(attackerId); var defender = await _characterService.GetAsync(defenderId); if (attacker == null) { return(Guid.Empty, null); } if (defender == null) { return(Guid.Empty, null); } var attackerArmor = await GetEquippedArmor(attackerId); var attackerWeapon = await GetEquippedWeapon(attackerId); var defenderArmor = await GetEquippedArmor(defenderId); var defenderWeapon = await GetEquippedWeapon(defenderId); var attackSuccess = ResolveAttack(attacker, attackerWeapon, attackerArmor, defender, defenderWeapon, defenderArmor); Guid fightId = _fightService.Create(new FightDto { Id = Guid.NewGuid(), AttackerId = attacker.Id, DefenderId = defender.Id, AttackerArmorId = attackerArmor?.Id, AttackerWeaponId = attackerWeapon?.Id, DefenderArmorId = defenderArmor?.Id, DefenderWeaponId = defenderWeapon?.Id, Timestamp = DateTime.Now, AttackSuccess = attackSuccess.Item1 }); if (attackSuccess.Item1) { attacker.Score += 30; attacker.Money += 30 + (_random.Next(1, 7 * attacker.Luck)); } else { attacker.Score -= 17; } await _characterService.Update(attacker); await uow.Commit(); return(fightId, attackSuccess.Item2); } }
public ActionResult CreateTable(int id) { Tournament tournament = tournamentService.GetById(id); if (tournament.Organizer == User.Identity.Name && Request.IsAjaxRequest()) { SportUnit[] distinctions = sportUnitService.GetByTypeForGroup(tournament.GroupId, SportUnitType.Distinction).ToArray(); var levels = generalUnitService.GetByType(GeneralUnitType.Level); Fight fight; GeneralUnit levelGU; foreach (var category in tournament.Categories) { List <FightRelation> rels = fightRelationService.GetWithFilter(x => x.FightChild.CategoryId == category.Id || x.FightParent.CategoryId == category.Id).ToList(); foreach (var _rel in rels) { fightRelationService.Delete(_rel); } List <Fight> _fights = category.Fights.ToList(); foreach (var _fight in _fights) { fightService.Delete(_fight); } var participants = categoryService.GetParticipants(category.Id, true); if (participants.Count() > 1 && participants.Count() < 9) { int fightsCount = participants.Count() - 1; int itemInLevel = 0; int level = 0; for (int i = 0; i < fightsCount; i++) { int maxItemsInLevel = (int)Math.Pow(2, level); levelGU = levels.Where(x => x.Description.Equals(string.Format("level_{0}", level + 1))).SingleOrDefault(); if (levelGU != null) { fight = new Fight() { LevelId = levelGU.Id, FightInLevel = itemInLevel, CategoryId = category.Id }; if (itemInLevel == maxItemsInLevel - 1) { itemInLevel = 0; level += 1; } else { itemInLevel += 1; } fightService.Create(fight); } } if (category.OneThirdPlace) { levelGU = levels.Where(x => x.Description.Equals("level_0")).SingleOrDefault(); if (levelGU != null) { fight = new Fight() { LevelId = levelGU.Id, FightInLevel = 0, CategoryId = category.Id }; fightService.Create(fight); } } var fights = category.Fights; int index = 0; bool winnerDirection; foreach (var item in fights) { int lev = Convert.ToInt16(item.Level.Description.Split('_')[1]); if (lev == 0) { lev += 1; winnerDirection = false; } else { winnerDirection = true; } levelGU = levels.Where(s => s.Description.Equals(String.Format("level_{0}", lev + 1))).SingleOrDefault(); if (levelGU != null) { var childFights = fights.Where(x => x.LevelId == levelGU.Id && x.FightInLevel >= item.FightInLevel * 2 && x.FightInLevel <= item.FightInLevel * 2 + 1); foreach (Fight childFight in childFights) { FightRelation fightRelation = new FightRelation() { FightChildId = childFight.Id, FightParentId = item.Id, IsWinner = winnerDirection }; fightRelationService.Create(fightRelation); } if (winnerDirection) { for (int i = 0; i < 2 - childFights.Count(); i++) { Fighter fighter = new Fighter() { FightId = item.Id, ParticipantId = participants.ElementAt(index).Id, DistinctionId = distinctions[i].Id }; index += 1; fighterService.Create(fighter); } } } //else if (winnerDirection) //{ // for (int i = 0; i < 2; i++) // { // Fighter fighter = new Fighter() { FightId = item.Id, ParticipantId = participants.ElementAt(index).Id, DistinctionId = distinctions[i].Id }; // index += 1; // unitOfWork.FighterRepository.Insert(fighter); // } //} } } } } return(RedirectToAction("Details", RouteData)); }