/// <summary> /// Creates a new tournament. /// </summary> /// <param name="model">Model of the tournament.</param> public void CreateTournament(TournamentModel model) { // Connect to the database. using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(Db))) { // Save the tournament data. this.SaveTournament(connection, model); // Save the prize data for this tournament. this.SaveTournamentPrizes(connection, model); // Save the entries data for this tournament. this.SaveTournamentEntries(connection, model); // Save the rounds data for this tournament. this.SaveTournamentRounds(connection, model); // Used to update the tournament (mainly for bye weeks). TournamentLogic.UpdateTournamentResults(model); } }
/// <summary> /// Creates a tournament. /// </summary> /// <param name="model">Model of the tournament.</param> public void CreateTournament(TournamentModel model) { List <TournamentModel> tournament = GlobalConfig.TournamentFile.FullFilePath().LoadFile().ConvertToTournamentModel(); int currentId = 1; if (tournament.Count > 0) { currentId = currentId = tournament.OrderByDescending(x => x.Id).First().Id + 1; } model.Id = currentId; model.SaveRoundsToFile(); tournament.Add(model); tournament.SaveToTournamentFile(); TournamentLogic.UpdateTournamentResults(model); }
public static void UpdateTournamentResults(TournamentModel tournament, MatchupModel matchupToUpdate) { int startingRound = GetCurrentRound(tournament); SetMatchupWinner(matchupToUpdate); GlobalConfig.Connections.UpdateMatchup(matchupToUpdate); AdvanceWinner(matchupToUpdate, tournament); int endingRound = GetCurrentRound(tournament); if (endingRound > tournament.Rounds.Count) { // last round is finished CompleteTournament(tournament); } else if (endingRound > startingRound) { tournament.AlertUsersToNewRound(); } }
private static int GetCurrentRound(this TournamentModel tournament) { int output = 1; foreach (List <MatchupModel> round in tournament.Rounds) { if (round.All(x => x.Winner != null)) { output++; } else { return(output); } } // the tournament is complete CompleteTournament(tournament); return(output - 1); }
public static void UpdateTournamentResults(TournamentModel model) { List <MatchupModel> toScore = new List <MatchupModel>(); foreach (List <MatchupModel> round in model.Rounds) { foreach (MatchupModel rm in round) { if (rm.Winner == null && (rm.Entries.Any(x => x.Score != 0) || rm.Entries.Count == 1)) { toScore.Add(rm); } } } MarkWinnersInMatchup(toScore); AdvanceWinners(toScore, model); toScore.ForEach(x => GlobalConfig.Connection.UpdateMatchup(x)); }
private static int CheckCurrentRound(this TournamentModel model) { int currentRound = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) { currentRound += 1; } else { return(currentRound); } } // Tournament is over CompleteTournament(model); return(currentRound - 1); }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) { output += 1; } else { return(output); } } // 如果程式跑到這裡 就代表他在上面沒有找到任何的winner = null, 也就是說我們的tournament 完成所有比賽了 所以這邊就要結束這個tournament // Tournament is complete CompleteTournament(model); return(output - 1); }
public static void AlertUsersToNewRound(this TournamentModel model) { int currentRoundNumber = model.CheckCurrentRound(); // Find whole round which the first matchup's round is equals currentRoundNumber List <MatchupModel> currentRound = model.Rounds.Where(x => x.First().MatchupRound == currentRoundNumber).First(); foreach (MatchupModel matchup in currentRound) { foreach (MatchupEntryModel me in matchup.Entries) { foreach (PersonModel p in me.TeamCompeting.TeamMembers) { AlertPersonToNewRound( p, me.TeamCompeting.TeamName, matchup.Entries.Where(x => x.TeamCompeting != me.TeamCompeting).FirstOrDefault()); } } } }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) { output += 1; } else { return(output); } } // Tournament is complete CompleteTournament(model); return(output - 1); }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) { output += 1; // if all mathups in a round have been scored, mark that round as done. } else { return(output); } } // Tournament is complete CompleteTournament(model); return(output - 1); }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { //look at every single matchup inside the round and check if there all of them have a winner if (round.All(x => x.Winner != null)) //If ALL are true { output += 1; } else { return(output); } } //Tournament is complete CompleteTournament(model); return(output - 1); }
/// <summary> /// Create all the rounds, except the first one /// </summary> /// <param name="model">Tournament model</param> /// <param name="rounds">Number of rounds of the tournament</param> private static void CreateOtherRounds(TournamentModel model, int rounds) { // the current round we are on int round = 2; // grab the first list of Matchup model // initially this is the first round List <MatchupModel> previousRound = model.Rounds[0]; List <MatchupModel> currentRound = new List <MatchupModel>(); MatchupModel currentMatchup = new MatchupModel(); while (round <= rounds) { // loop through the previous round // the first person from the competing teams gets the bye-team // Eg from Matchup System logic - Sue VS Bye foreach (MatchupModel match in previousRound) { // I don't know the score or the team currentMatchup.Entries.Add(new MatchupEntryModel { ParentMatchup = match }); if (currentMatchup.Entries.Count > 1) { currentMatchup.MatchupRound = round; currentRound.Add(currentMatchup); currentMatchup = new MatchupModel(); } } model.Rounds.Add(currentRound); previousRound = currentRound; currentRound = new List <MatchupModel>(); round += 1; } }
private static void CreateOtherRounds(TournamentModel model, int rounds) { int round = 2; List <MatchupModel> previousRound = model.Rounds[0]; List <MatchupModel> currRound = new List <MatchupModel>(); MatchupModel currMatchup = new MatchupModel(); // Starting from round 2. If there are more then keep looping until all // rounds have matched up. while (round <= rounds) { // For each match in the previous round of matchups foreach (MatchupModel match in previousRound) { // Use that matchup as the parent matchup of this matchup entry. We don't // know who the winner from that matchup is, but we know who the parent matchup is. // So we put in an anonymous team in where only the parent matchup is known. currMatchup.Entries.Add(new MatchupEntryModel { ParentMatchup = match }); if (currMatchup.Entries.Count > 1) { currMatchup.MatchupRound = round; currRound.Add(currMatchup); currMatchup = new MatchupModel(); } } // Add the new list of matchups to the tournament model.Rounds.Add(currRound); // Make the completed list the new previous list previousRound = currRound; // Reset the current list. currRound = new List <MatchupModel>(); // Change the round number identity. round++; } }
// 4. Create every round after the 1st round. We're dividing by 2 now. 8/2 = 4, 4/2 = 2 private static void CreateOtherRounds(TournamentModel model, int totalTournamentRounds) { // Represents the current round that we're on int currentRoundCounter = 2; // We have to work on the previous round to get the values for the current round List <MatchupModel> previousRound = model.Rounds[0]; // This is a List because a Round is a List<List<MatchupModel>> List <MatchupModel> currentRound = new List <MatchupModel>(); // MatchupModel currentMatchup = new MatchupModel(); // While 2 is less than or equal to our total number of rounds while (currentRoundCounter <= totalTournamentRounds) { // foreach (MatchupModel match in previousRound) { // Assigning parent matchup from the previous round currentMatchup.Entries.Add(new MatchupEntryModel { ParentMatchup = match }); // if (currentMatchup.Entries.Count > 1) { currentMatchup.MatchupRound = currentRoundCounter; currentRound.Add(currentMatchup); // clearing out the model now that it has been added currentMatchup = new MatchupModel(); } } model.Rounds.Add(currentRound); previousRound = currentRound; currentRound = new List <MatchupModel>(); currentRoundCounter += 1; } }
private void SaveTournamentRounds(IDbConnection connection, TournamentModel model) { foreach (List <MatchupModel> round in model.Rounds) { foreach (MatchupModel matchup in round) { var p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@MatchupRound", matchup.MatchupRound); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.spMatchups_Insert", p, commandType: CommandType.StoredProcedure); matchup.Id = p.Get <int>("@id"); foreach (MatchupEntryModel entry in matchup.Entries) { p = new DynamicParameters(); p.Add("@MatchupId", matchup.Id); if (entry.ParentMatchup == null) { p.Add("@ParentMatchupId", null); } else { p.Add("@ParentMatchupId", entry.ParentMatchup.Id); } if (entry.TeamCompeting == null) { p.Add("@TeamCompetingId", null); } else { p.Add("@TeamCompetingId", entry.TeamCompeting.Id); } p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.spMatchupEntries_Insert", p, commandType: CommandType.StoredProcedure); } } } }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) // In a certain round every matchup has finished (so, has a winner) { ++output; } else { // Beak out the loop because there are no completed rounds after an uncompleted one. return(output); } } // If all the Winners has already been assigned then Tournament is finished. CompleteTournament(model); return(output - 1); }
public void CreateTournament(TournamentModel model) { using (TransactionScope scope = new TransactionScope()) { using (IDbConnection connection = new SqlConnection(GlobalConfig.ConnectionString(db))) { try { SaveTournament(connection, model); SaveTournamentPrizes(connection, model); SaveTournamentEntries(connection, model); SaveTournamentRounds(connection, model); scope.Complete(); } catch (Exception e) { connection.BeginTransaction().Rollback(); throw e; } } } }
/// <summary> /// Notify all team members of all teams playing in the current round. /// </summary> /// <param name="tournament">Tournament information.</param> public static void AlertUsersToNewRound(this TournamentModel tournament) { int currentRoundNumber = tournament.CheckCurrentRound(); List <MatchupModel> currentRound = tournament.Rounds.Where(x => x.First().MatchupRound == currentRoundNumber).First(); foreach (MatchupModel matchup in currentRound) { foreach (MatchupEntryModel matchupEntry in matchup.Entries) { foreach (PersonModel person in matchupEntry.TeamCompeting.TeamMembers) { AlertPersonToNewRound( person, matchupEntry.TeamCompeting.TeamName, matchup.Entries .Where(x => x.TeamCompeting != matchupEntry.TeamCompeting) .FirstOrDefault() ); } } } }
private static void AdvanceWinners(List <MatchupModel> models, TournamentModel tournament) { foreach (MatchupModel m in models) { foreach (List <MatchupModel> round in tournament.Rounds) { foreach (MatchupModel rm in round) { foreach (MatchupEntryModel me in rm.Entries) { if (me.ParentMatchup != null) { if (me.ParentMatchup.Id == m.Id) { me.TeamCompeting = m.Winner; GlobalConfig.Connection.UpdateMatchup(rm); } } } } } } }
/// <summary> /// Promote the winners to next rounds /// </summary> /// <param name="models"></param> /// <param name="tournament"></param> private static void AdvanceWinners(List <MatchUpModel> models, TournamentModel tournament) { foreach (MatchUpModel model in models) { foreach (List <MatchUpModel> round in tournament.Rounds) { foreach (MatchUpModel rm in round) { foreach (MatchUpEntryModel ent in rm.Entries) { if (ent.ParentMatch != null) { if (ent.ParentMatch.id == model.id) { ent.TeamCompeting = model.Winner; GlobalConfig.Connection.UpdateMatchup(rm); } } } } } } }
private static void AdvanceBracketWinner(TournamentModel tournament, List <MatchupModel> models) { foreach (var m in models) { foreach (var round in tournament.Rounds) { foreach (var roundMatch in round) { foreach (var matchEntry in roundMatch.Entries) { if (matchEntry.ParentMatchup != null) { if (matchEntry.ParentMatchup.Id == m.Id) { matchEntry.TeamCompeting = m.Winner; GlobalConfig.Connection.UpdateMatchup(roundMatch); } } } } } } }
private static void AdvanceWinners(List <MatchupModel> models, TournamentModel tournament) { foreach (MatchupModel m in models) // The matchups with valid score { foreach (List <MatchupModel> round in tournament.Rounds) { foreach (MatchupModel rm in round) { foreach (MatchupEntryModel me in rm.Entries) { if (me.ParentMatchup != null) { if (me.ParentMatchup.Id == m.Id) //If the current matchup is the parent of a certain matchupentry { me.TeamCompeting = m.Winner; // Then the winner of this matchup is the TeamCompeting in that certain matchupentry! GlobalConfig.Connection.UpdateMatchup(rm); //Saves the matchup and the matchupentries inside that } } } } } } }
private static void AdvanceWinners(List <MatchupModel> models, TournamentModel tournament) { models.ForEach(m => { tournament.Rounds.ForEach(round => { round.ForEach(rm => { rm.Entries.ForEach(me => { if (me.ParentMatchup != null) { if (me.ParentMatchup.Id == m.Id) { me.TeamCompeting = m.Winner; GlobalConfig.Connection.UpdateMatchup(rm); } } }); }); }); }); }
/// <summary> /// Pass winners to the next round. Set up their new matchups. /// </summary> /// <param name="matchups">New Winners.</param> /// <param name="tournament">Tournament playing.</param> private static void AdvanceWinners(List <MatchupModel> matchups, TournamentModel tournament) { foreach (MatchupModel matchup in matchups) { foreach (List <MatchupModel> round in tournament.Rounds) { foreach (MatchupModel roundMatchup in round) { foreach (MatchupEntryModel matchupEntry in roundMatchup.Entries) { if (matchupEntry.ParentMatchup != null) { if (matchupEntry.ParentMatchup.Id == matchup.Id) { matchupEntry.TeamCompeting = matchup.Winner; GlobalConfig.Connection.UpdateMatchup(roundMatchup); } } } } } } }
/// <summary> /// Get round that's currently playing on tournament. /// </summary> /// <param name="tournament">Tournament</param> /// <returns></returns> private static int CheckCurrentRound(this TournamentModel tournament) { // Tournament starts with round 1 int currRound = 1; foreach (List <MatchupModel> round in tournament.Rounds) { if (round.All(x => x.Winner != null)) { currRound += 1; } else { return(currRound); } } // If it gets to here, then // Tournament is completed CompleteTournament(tournament); return(currRound - 1); }
public static void AlertUsersToNewRound(this TournamentModel model) { int currentRoundNumber = model.CheckCurrentRoud(); List <MatchupModel> currentRound = model.Rounds.Where(x => x.First().MatchupRound == currentRoundNumber).First(); foreach (MatchupModel matchup in currentRound) { foreach (MatchupEntryModel me in matchup.Entries) { if (me.TeamCompeting != null) { foreach (PersonModel p in me.TeamCompeting.TeamMembers) { AlertPersonToNewRound(p, me.TeamCompeting.TeamName, matchup.Entries.Where(x => x.TeamCompeting != me.TeamCompeting).FirstOrDefault()); } } else { continue; } } } }
private static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> matchup in model.Rounds) { if (matchup.All(x => x.Winner != null)) { output += 1; } else { // Ending the method if a round has a game that isn't finished return(output); } } // Tournament is complete CompleteTournament(model); // Wrapping Up: 30:26 return(output - 1); }
public static int CheckCurrentRound(this TournamentModel model) { int output = 1; foreach (List <MatchupModel> round in model.Rounds) { if (round.All(x => x.Winner != null)) { output += 1; } else { return(output); } } //The tournament is complete. double finalScoreTeam1 = model.Rounds.Last().Last().Entries.First().Score; double finalScoreTeam2 = model.Rounds.Last().Last().Entries.Last().Score; if (finalScoreTeam1 > finalScoreTeam2) { model.Winner = model.Rounds.Last().Last().Entries.First().TeamCompeting; model.WinnerId = model.Winner.Id; } else { model.Winner = model.Rounds.Last().Last().Entries.Last().TeamCompeting; model.WinnerId = model.Winner.Id; } model.Active = 0; CompleteTournament(model); //<= Modification here //GlobalConfig.Connection.CompleteTournament(model); return(output - 1); }
public static void UpdateTournamentRound(this TournamentModel tournament, RoundModel round) { bool roundCompleted = round.Matchups.All(m => m.Winner != null); if (!roundCompleted) { throw new Exception("Winners have not been assigned to matchups in this round"); } round.Active = false; GlobalConfig.Connection.UpdateRound(round); bool isFinalRound = tournament.Rounds.Count == round.Number; if (!isFinalRound) { RoundModel nextRound = tournament.Rounds.First(r => r.Number == round.Number + 1); nextRound.Active = true; nextRound.Matchups.ForEach(m => m.Entries.ForEach(me => { me.TeamCompeting = me.ParentMatchup.Winner; GlobalConfig.Connection.UpdateMatchupEntry(me); })); GlobalConfig.Connection.UpdateRound(nextRound); tournament.NotifyEnteredTeams(nextRound); } else { tournament.CompleteTournament(); tournament.NotifyTournamentComplete(); } }
public static void UpdateTournamentResults(TournamentModel model) { int startingRound = model.CheckCurrentRound(); List <MatchUpModel> toScore = new List <MatchUpModel>(); foreach (List <MatchUpModel> round in model.Rounds) { foreach (MatchUpModel rm in round) { //rm.Entries.Count==1 chacks for byes entry in matchup //rm.Entries.Any(x=>x.Score!=0 => we dont do ties in tourenamet there for one team at least have a score // of score!=0 in a match up ! if (rm.Winner == null && (rm.Entries.Any(x => x.Score != 0) || rm.Entries.Count == 1)) { toScore.Add(rm); } } } MarkWinnewrsInMatchup(toScore); AdvancedWinners(toScore, model); //shorter code //foreach (var x in toScore) //{ // GlobalConfig.Connaction.UpadateMatchup(x)); //} toScore.ForEach(x => GlobalConfig.Connaction.UpadateMatchup(x)); int endingRound = model.CheckCurrentRound(); if (endingRound > startingRound) { model.AlertUsersToNewRound(); } }