public ActionResult SetWinner(int Id, int refereeId) { if (Request.IsAjaxRequest()) { Fighter winner = fighterService.GetById(Id); winner.IsWinner = true; fighterService.Update(winner); SportUnit[] distinctions = sportUnitService.GetByTypeForGroup(winner.Fight.Category.Tournament.GroupId, SportUnitType.Distinction).ToArray(); var nextWinnerFight = winner.Fight.FightRelationsChildFights.Where(x => x.IsWinner).SingleOrDefault(); if (nextWinnerFight != null) { int fighterCount = nextWinnerFight.FightParent.Fighters.Count(); Fighter fighter = new Fighter() { FightId = (int)nextWinnerFight.FightParentId, ParticipantId = winner.ParticipantId, DistinctionId = distinctions[fighterCount].Id }; fighterService.Create(fighter); } var nexLoserFight = winner.Fight.FightRelationsChildFights.Where(x => !x.IsWinner).SingleOrDefault(); if (nexLoserFight != null) { var loser = winner.Fight.Fighters.Where(x => x.Id != Id).Single(); int fighterCount = nexLoserFight.FightParent.Fighters.Count(); Fighter fighter = new Fighter() { FightId = (int)nexLoserFight.FightParentId, ParticipantId = loser.ParticipantId, DistinctionId = distinctions[fighterCount].Id }; fighterService.Create(fighter); } return(RedirectToAction("Details", "Referees", new { id = refereeId })); } return(null); }
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)); }