public IList <IList <Match> > GetMatchesPerRound(IList <Match> allMatches) { var matchesPerRound = new List <IList <Match> >(); var matches = new List <Match>(); var round = -1; foreach (var match in allMatches.OrderBy(x => x.Name)) { var matchRound = EliminationRoundNames.GetRound(match.Name); if (matchRound != -1 && matchRound != round) { if (matches.Any()) { matchesPerRound.Add(matches.OrderBy(x => EliminationRoundNames.GetMatchNumber(x.Name, round)).ToList()); matches = new List <Match>(); } round = matchRound; } matches.Add(match); } if (matches.Any()) { matchesPerRound.Add(matches.OrderBy(x => EliminationRoundNames.GetMatchNumber(x.Name, round)).ToList()); } return(matchesPerRound); }
public IList <Match> UpdateMatchesAfterFinishedMatch(Match match, IList <Match> matches) { var updatedMatches = new List <Match>(); Person winner = null; Person loser = null; if (match.Result == MatchResult.WinBlue || match.Result == MatchResult.DisqualificationRed || match.Result == MatchResult.ForfeitRed) { winner = match.FighterBlue; if (match.Result == MatchResult.WinBlue) { loser = match.FighterRed; } } else if (match.Result == MatchResult.WinRed || match.Result == MatchResult.DisqualificationBlue || match.Result == MatchResult.ForfeitBlue) { winner = match.FighterRed; if (match.Result == MatchResult.WinRed) { loser = match.FighterBlue; } } var round = EliminationRoundNames.GetRound(match.Name); if (round > 0 && winner != null) { var matchNumber = EliminationRoundNames.GetMatchNumber(match.Name, round); var nextRound = round - 1; var nextMatchNumber = ((matchNumber - 1) >> 1) + 1; var nextMatchName = EliminationRoundNames.GetMatchName(nextRound, nextMatchNumber).Trim(); var nextMatch = matches.SingleOrDefault(x => x.Name.Trim() == nextMatchName); if (nextMatch != null) { if (matchNumber % 2 == 1) { nextMatch.FighterBlue = winner; } else { nextMatch.FighterRed = winner; } updatedMatches.Add(nextMatch); } if (round == 1 && loser != null) { nextMatchName = EliminationRoundNames.GetMatchName(0, 2).Trim(); nextMatch = matches.SingleOrDefault(x => x.Name.Trim() == nextMatchName); if (nextMatch != null) { if (matchNumber % 2 == 1) { nextMatch.FighterBlue = loser; } else { nextMatch.FighterRed = loser; } updatedMatches.Add(nextMatch); } } } return(updatedMatches); }