private void EnterAutomaticResults() { foreach (RoundModel round in _roundList) { foreach (GameModel game in round.Games) { GameParticipantModel homeTeam = game.Competitors[0]; GameParticipantModel awayTeam = game.Competitors[1]; if (homeTeam.TeamName.Contains("Dummy")) { homeTeam.Score = 0; awayTeam.Score = CurrentTournament.OfficialScore; game.Unplayed = false; UpdateGameAndGameParticipants(game); } else if (awayTeam.TeamName.Contains("Dummy")) { awayTeam.Score = 0; homeTeam.Score = CurrentTournament.OfficialScore; game.Unplayed = false; UpdateGameAndGameParticipants(game); } if (SelectedGame == game) { CanEnterResult = false; } } } }
/// <summary> /// Creates a new record of game participant model in the DB /// </summary> /// <param name="participant">GameParticipantModel class object you want to store in the DB</param> internal static void CreateGameParticipant(GameParticipantModel participant) { DynamicParameters parameters = new DynamicParameters(); parameters.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); parameters.Add("@TournamentId", participant.TournamentId); parameters.Add("RoundId", participant.RoundId); parameters.Add("@GameId", participant.GameId); parameters.Add("@TeamName", participant.TeamName); using (IDbConnection connection = new SqlConnection(DatabaseAccess.GetConnectionString())) { connection.Execute("dbo.SP_InsertNewGameParticipant", parameters, commandType: CommandType.StoredProcedure); } participant.Id = parameters.Get <int>("@Id"); }
/// <summary> /// Updates a particular game's participants in the DB by labeling them as cup round winner/loser /// </summary> /// <param name="selectedGame">Game you want game participants to be updated</param> public static void UpdateGameParticipantAsCupRoundWinner(GameModel selectedGame) { GameParticipantModel homeTeam = selectedGame.Competitors[0]; GameParticipantModel awayTeam = selectedGame.Competitors[1]; DynamicParameters parameters = new DynamicParameters(); if (homeTeam.Score > awayTeam.Score) { parameters.Add("@Id", homeTeam.Id); } else { parameters.Add("@Id", awayTeam.Id); } using (IDbConnection connection = new SqlConnection(DatabaseAccess.GetConnectionString())) { connection.Execute("dbo.SP_UpdateGameParticipantCupRoundWinner", parameters, commandType: CommandType.StoredProcedure); } }
/// <summary> /// Updates league participants that played in a specific game, on a specific tournament /// </summary> /// <param name="tournament">Tournament where the game of the 2 league participants happened on</param> /// <param name="game">Game 2 league participants played on</param> public static void UpdateLeagueParticipants(TournamentModel tournament, GameModel game) { GameParticipantModel homeTeam = game.Competitors[0]; GameParticipantModel awayTeam = game.Competitors[1]; LeagueParticipantModel winner = new LeagueParticipantModel(); LeagueParticipantModel loser = new LeagueParticipantModel(); if (homeTeam.Score > awayTeam.Score) { winner = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == homeTeam.TeamName); loser = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == awayTeam.TeamName); winner.Victories += 1; winner.Scored += homeTeam.Score; winner.Conceded += awayTeam.Score; winner.Points += tournament.VictoryPoints; loser.Defeats += 1; loser.Scored += awayTeam.Score; loser.Conceded += homeTeam.Score; } else if (homeTeam.Score == awayTeam.Score) { winner = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == awayTeam.TeamName); loser = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == homeTeam.TeamName); winner.Draws += 1; winner.Scored += awayTeam.Score; winner.Conceded += homeTeam.Score; winner.Points += tournament.DrawPoints; loser.Draws += 1; loser.Scored += homeTeam.Score; loser.Conceded += awayTeam.Score; loser.Points += tournament.DrawPoints; } else { winner = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == awayTeam.TeamName); loser = GetDataByTournament <LeagueParticipantModel>(game.TournamentId, "dbo.SP_GetLeagueParticipantsByTournament") .Find(team => team.TeamName == homeTeam.TeamName); winner.Victories += 1; winner.Scored += awayTeam.Score; winner.Conceded += homeTeam.Score; winner.Points += tournament.VictoryPoints; loser.Defeats += 1; loser.Scored += homeTeam.Score; loser.Conceded += awayTeam.Score; } UpdateThisLeagueParticipant(winner); UpdateThisLeagueParticipant(loser); }
public static void CreateRounds(TournamentModel tournament) { List <TeamModel> participants = tournament.ParticipatingTeams; participants.Shuffle(); if (tournament.IsLeague) { // SOLUTION FOUND ON https://stackoverflow.com/questions/1293058/round-robin-tournament-algorithm-in-c-sharp int numberOfRounds = GetNumberOfRounds(tournament); int halfOfTeams = tournament.ParticipatingTeams.Count / 2; List <TeamModel> shortenedTeamList = new List <TeamModel>(); shortenedTeamList.AddRange(participants); shortenedTeamList.RemoveAt(0); int shortenedCount = shortenedTeamList.Count; // Create rounds for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++) { // Create round RoundModel round = new RoundModel(tournament.Id, roundNumber); SqlDataHandler.CreateRound(tournament, round); tournament.Rounds.Add(round); int teamIndexer = roundNumber % shortenedCount; // Create 1st game in the round GameModel game = new GameModel(tournament.Id, round.Id, true); SqlDataHandler.CreateGame(game); round.Games.Add(game); // Create 1st game participants GameParticipantModel homeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = shortenedTeamList[teamIndexer].TeamName }; GameParticipantModel awayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = game.Id, TeamName = participants[0].TeamName }; game.Competitors.Add(homeTeam); game.Competitors.Add(awayTeam); foreach (GameParticipantModel participant in game.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } // Create other games of the round for (int index = 1; index < halfOfTeams; index++) { int homeIndex = (roundNumber + index) % shortenedCount; int awayIndex = (roundNumber + shortenedCount - index) % shortenedCount; // Create the next game GameModel nextGame = new GameModel(tournament.Id, round.Id, true); SqlDataHandler.CreateGame(nextGame); round.Games.Add(nextGame); // Create next game participants GameParticipantModel nextHomeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[homeIndex].TeamName }; GameParticipantModel nextAwayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = round.Id, GameId = nextGame.Id, TeamName = shortenedTeamList[awayIndex].TeamName }; nextGame.Competitors.Add(nextHomeTeam); nextGame.Competitors.Add(nextAwayTeam); foreach (GameParticipantModel participant in nextGame.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } } } if (tournament.HomeAndAway) { int numberToAdd = 1; // Create the duplicate rounds and duplicate of every game in the round with teams on opposite places foreach (RoundModel createdRound in tournament.Rounds) { // Create the 1st next duplicate round RoundModel nextRound = new RoundModel(tournament.Id, tournament.Rounds.Count + numberToAdd); SqlDataHandler.CreateRound(tournament, nextRound); numberToAdd += 1; foreach (GameModel createdGame in createdRound.Games) { // Create duplicate game GameModel nextGame = new GameModel(tournament.Id, nextRound.Id, true); SqlDataHandler.CreateGame(nextGame); // Create game participants GameParticipantModel homeTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[1].TeamName }; GameParticipantModel awayTeam = new GameParticipantModel { TournamentId = tournament.Id, RoundId = nextRound.Id, GameId = nextGame.Id, TeamName = createdGame.Competitors[0].TeamName }; nextGame.Competitors.Add(homeTeam); nextGame.Competitors.Add(awayTeam); foreach (GameParticipantModel participant in nextGame.Competitors) { SqlDataHandler.CreateGameParticipant(participant); } } } } } else { int numberOfRounds = GetNumberOfRounds(tournament); for (int roundNumber = 1; roundNumber <= numberOfRounds; roundNumber++) { RoundModel round = new RoundModel(tournament.Id, roundNumber); SqlDataHandler.CreateRound(tournament, round); if (round.RoundNumber == 1) { CreateCupRoundGames(participants, round); } // Every next round's games will be created subsequently after all previous round's results are stored // Its only needed to get previous round winners and pass them as parameter to the CreateCupRoundGames() method } } }