diff --git a/TrackerLibrary/TournamentLogic.cs b/TrackerLibrary/TournamentLogic.cs new file mode 100644 index 0000000..1b81419 --- /dev/null +++ b/TrackerLibrary/TournamentLogic.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TrackerLibrary.Models; + +namespace TrackerLibrary +{ + public static class TournamentLogic + { + // Order our list randomly of teams + // Check if it is big enough - if not , add byes 2*2*2*2 - 2^4 + // Create our first round of matchups + // Create every round after that - 8 matchups - 4 matchups - 2 matchups - 1 matchup + + public static void CreateRounds(TournamentModel model) + { + List randomizedTeams = RandomizeTeamOrder(model.EnteredTeams); + int rounds = FindNumberOfRounds(randomizedTeams.Count); + int byes = NumberOfByes(rounds, randomizedTeams.Count); + model.Rounds.Add(CreateFirstRound(byes, randomizedTeams)); + CreateOtherRounds(model, rounds); + } + + private static void CreateOtherRounds(TournamentModel model, int rounds) + { + int round = 2; + List previousRound = model.Rounds[0]; + List currRound = new List(); + MatchupModel currMatchup = new MatchupModel(); + while (round <= rounds) + { + foreach (MatchupModel match in previousRound) + { + currMatchup.Entries.Add(new MatchupEntryModel { ParentMatchup = match }); + + if (currMatchup.Entries.Count > 1) + { + currMatchup.MatchupRound = round; + currRound.Add(currMatchup); + currMatchup = new MatchupModel(); + } + } + + model.Rounds.Add(currRound); + previousRound = currRound; + currRound = new List(); + round += 1; + } + } + + private static List CreateFirstRound(int byes, List teams) + { + List output = new List(); + MatchupModel curr = new MatchupModel(); + + foreach (TeamModel team in teams) + { + curr.Entries.Add(new MatchupEntryModel { TeamCompeting = team }); + if(byes>0 || curr.Entries.Count > 1) + { + curr.MatchupRound = 1; + output.Add(curr); + curr = new MatchupModel(); + + if (byes > 0) + { + byes -= 1; + } + } + } + return output; + } + + private static int NumberOfByes(int rounds, int numberOfTeams) + { + //Math.Pow(2, rounds); + int output = 0; + int totalTeams = 0; + for (int i = 1; i < rounds; i++) + { + totalTeams *= 2; + } + + output = totalTeams - numberOfTeams; + return output; + + } + + private static int FindNumberOfRounds(int teamCount) + { + int output = 0; + int val = 2; + while (val < teamCount) + { + output += 1; + val *= 2; + } + + return output; + } + private static List RandomizeTeamOrder(List teams) + { + // cards.OrderBy(a => Guid.NewGuid()).ToList(); + return teams.OrderBy(t => Guid.NewGuid()).ToList(); + } + } +} diff --git a/TrackerLibrary/TrackerLibrary.csproj b/TrackerLibrary/TrackerLibrary.csproj index 89b8610..10b101c 100644 --- a/TrackerLibrary/TrackerLibrary.csproj +++ b/TrackerLibrary/TrackerLibrary.csproj @@ -58,6 +58,7 @@ + diff --git a/TrackerUI/CreateTournamentForm.cs b/TrackerUI/CreateTournamentForm.cs index 0f7768d..541aa5a 100644 --- a/TrackerUI/CreateTournamentForm.cs +++ b/TrackerUI/CreateTournamentForm.cs @@ -126,6 +126,8 @@ namespace TrackerUI // TODO Wireup our matchups + TournamentLogic.CreateRounds(tm); + // Finally create the tournament entry // Create all of the prizes entries // Create all of team entries