//Returns a table to be assigned to a matchup. Also keeps a record of allocated tables for round. public static int AllocateTable(List <int> tables, List <int> allocated, TournamentDbContext _context) { var isAvailable = true; foreach (int tableNo in tables) { isAvailable = true; for (int i = 0; i < allocated.Count; i++) { if (allocated[i] == tableNo) { isAvailable = false; } } if (isAvailable == true) { allocated.Add(tableNo); return(tableNo); } } //if unble to allocate a table that has not been played on then a random table will be assigned if (allocated.Count > 0) { // Debug.WriteLine("THIS IS THE ALLOCATED.Count----))* \n" + allocated.Count); for (int i = 1; i <= GetNoOfTables(_context); i++) { isAvailable = true; foreach (int table in allocated) { if (i == table) { isAvailable = false; } } if (isAvailable == true) { allocated.Add(i); return(i); } } } else { Random r = new Random(); int randomTable = r.Next(1, 12); int NumOfTables = GetNoOfTables(_context); if (NumOfTables >= 1) { randomTable = r.Next(1, NumOfTables); } allocated.Add(randomTable); return(randomTable); } //At the moment this is just to return a number when there are no more possible combinations for players and tables return(999); }
public static int GetNoOfTables(TournamentDbContext _context) { if (_context.RoundsModel.Count() > 0) { return(_context.RoundsModel.Last().NoTableTops); } return(0); }
public async Task <int> GetLastRound(TournamentDbContext _context) { int currentRound = 1; Round lastRound = await _context.Rounds.LastOrDefaultAsync(); if (lastRound != null) { currentRound = lastRound.RoundNo; } return(currentRound); }
static void Main(string[] args) { var optionsBuilder = new DbContextOptionsBuilder <TournamentDbContext>(); optionsBuilder.UseSqlServer("Server=./MSSQLSERVER2017; Database=Tournament_Local; Trusted_Connection=True"); using (var ctx = new TournamentDbContext(optionsBuilder.Options)) { ctx.SaveChanges(); } }
public static int GetLastRoundNo(TournamentDbContext _context) { int lastRoundNo = 0; RoundMatchup lastRound = _context.RoundMatchups.LastOrDefault(); if (lastRound != null) { lastRoundNo = lastRound.RoundNo; } return(lastRoundNo); }
public static void SetPlayerScores(int id, TournamentDbContext _context) { Player player = _context.Players.SingleOrDefault(p => p.Id == id); int[] playerScores = PlayerActions.GetPlayerScores(id, _context); int playerBattleScore = playerScores[0]; int playerSportsmanshipScore = playerScores[1]; player.BattleScore = playerBattleScore; player.SportsmanshipScore = playerSportsmanshipScore; _context.SaveChanges(); }
public static List <Player> GetAllOpponents(Player player, TournamentDbContext _context) { List <Player> opponents = new List <Player>(); var preSortedRoundMatchups = _context.RoundMatchups.Include(r => r.PlayerOne).Include(r => r.PlayerTwo).Where(r => !(r is PairRoundMatchup) && r.PlayerTwo != null).ToList(); var preSortedPairRoundMatchups = _context.RoundMatchups.OfType <PairRoundMatchup>().Include(r => r.PlayerOne).Include(r => r.PlayerThree).Where(r => r.PlayerTwo != null).ToList(); List <RoundMatchup> roundMatchups = preSortedRoundMatchups.Union(preSortedPairRoundMatchups).OrderBy(r => r.RoundNo).ToList(); //Loop through all round matchups and add the players opponents for each round to the list foreach (var roundMatchup in roundMatchups) { //Must add both opponents if this round is a pair round if (roundMatchup is PairRoundMatchup) { var pairRoundMatchup = roundMatchup as PairRoundMatchup; if (pairRoundMatchup.PlayerOne == player) { opponents.Add(pairRoundMatchup.PlayerThree); opponents.Add(pairRoundMatchup.PlayerFour); } else if (pairRoundMatchup.PlayerTwo == player) { opponents.Add(pairRoundMatchup.PlayerThree); opponents.Add(pairRoundMatchup.PlayerFour); } else if (pairRoundMatchup.PlayerThree == player) { opponents.Add(pairRoundMatchup.PlayerOne); opponents.Add(pairRoundMatchup.PlayerTwo); } else if (pairRoundMatchup.PlayerFour == player) { opponents.Add(pairRoundMatchup.PlayerOne); opponents.Add(pairRoundMatchup.PlayerTwo); } } //Adding opponents for a standard round else { if (roundMatchup.PlayerOne == player) { opponents.Add(roundMatchup.PlayerTwo); } else if (roundMatchup.PlayerTwo == player) { opponents.Add(roundMatchup.PlayerOne); } } } return(opponents); }
public static List <Player> GetAllOpponents(Player player, TournamentDbContext _context) { List <Player> opponents = new List <Player>(); List <RoundMatchup> roundMatchups = _context.RoundMatchups.ToList(); //Loop through all round matchups and add the players opponents for each round to the list foreach (var roundMatchup in roundMatchups) { //Must add both opponents if this round is a pair round if (roundMatchup is PairRoundMatchup) { var pairRoundMatchup = roundMatchup as PairRoundMatchup; if (pairRoundMatchup.PlayerOne == player) { opponents.Add(pairRoundMatchup.PlayerThree); opponents.Add(pairRoundMatchup.PlayerFour); } else if (pairRoundMatchup.PlayerTwo == player) { opponents.Add(pairRoundMatchup.PlayerThree); opponents.Add(pairRoundMatchup.PlayerFour); } else if (pairRoundMatchup.PlayerThree == player) { opponents.Add(pairRoundMatchup.PlayerOne); opponents.Add(pairRoundMatchup.PlayerTwo); } else if (pairRoundMatchup.PlayerFour == player) { opponents.Add(pairRoundMatchup.PlayerOne); opponents.Add(pairRoundMatchup.PlayerTwo); } } //Adding opponents for a standard round else { if (roundMatchup.PlayerOne == player) { opponents.Add(roundMatchup.PlayerTwo); } else if (roundMatchup.PlayerTwo == player) { opponents.Add(roundMatchup.PlayerOne); } } } return(opponents); }
//Get the BattleScores of each player based on the RoundMatchup and PairRoundMatchup Entries. Return a Dictionary object with each player and their BattleScore public static Dictionary <Player, int> GetAllPlayerBattleScores(TournamentDbContext _context) { Dictionary <Player, int> playerBattleScores = new Dictionary <Player, int>(); List <Player> players = _context.Players.ToList(); List <RoundMatchup> roundMatchups = _context.RoundMatchups.ToList(); foreach (Player player in players) { playerBattleScores.Add(player, 0); } foreach (RoundMatchup roundMatchup in roundMatchups) { playerBattleScores[roundMatchup.PlayerOne] += roundMatchup.PlayerOneBattleScore; playerBattleScores[roundMatchup.PlayerTwo] += roundMatchup.PlayerTwoBattleScore; } return(playerBattleScores); }
//returns a list of available tables that the player has not played on public static List <int> GetTables(Player currentPlayer, TournamentDbContext _context) { // List<int> tables = _context.RoundMatchups.Where(a => a.PlayerOne == currentPlayer || a.PlayerOne.CurrentOpponent == currentPlayer).Select(a => a.Table).ToList(); List <int> tables = new List <int>(); List <RoundMatchup> roundMatchups = _context.RoundMatchups.ToList(); int currentRound = 1; if (_context.RoundMatchups.LastOrDefault() != null) { currentRound = _context.RoundMatchups.Last().RoundNo + 1; } //adds all tables to list for (int j = 1; j <= GetNoOfTables(_context); j++) { tables.Add(j); } if (currentRound < 2) { return(tables); } else { //List<int> temp = _context.RoundMatchups.Where(a => a.PlayerOne == currentPlayer || a.PlayerTwo == currentPlayer).Select(a => a.Table).ToList(); // tables = (List<int>)tables.Except(temp); // returns all the firms except those in _context.RoundMatchups.Where(Ect..) //* foreach (var roundMatchup in roundMatchups) { if (roundMatchup.PlayerOne == currentPlayer || roundMatchup.PlayerTwo == currentPlayer || currentPlayer.CurrentOpponent == roundMatchup.PlayerOne || currentPlayer.CurrentOpponent == roundMatchup.PlayerTwo) { tables.Remove(roundMatchup.Table); } } //*/ } //Where(r => r.roundNo == currentRound); return(tables); }
//Get the Battle and Sportsmanship Scores of each player based on the RoundMatchup and PairRoundMatchup Entries. Return a Pair of Dictionary objects with each player and their BattleScore + SportsmanshipScore public static List <Dictionary <Player, int> > GetAllPlayerScores(TournamentDbContext _context) { Dictionary <Player, int> playerBattleScores = new Dictionary <Player, int>(); Dictionary <Player, int> playerSportsmanshipScores = new Dictionary <Player, int>(); List <Player> players = _context.Players.ToList(); List <RoundMatchup> roundMatchups = _context.RoundMatchups.Where(r => r.PlayerTwo != null).ToList(); foreach (Player player in players) { playerBattleScores.Add(player, 0); playerSportsmanshipScores.Add(player, 0); } foreach (RoundMatchup roundMatchup in roundMatchups) { playerBattleScores[roundMatchup.PlayerOne] += roundMatchup.PlayerOneBattleScore; playerBattleScores[roundMatchup.PlayerTwo] += roundMatchup.PlayerTwoBattleScore; playerSportsmanshipScores[roundMatchup.PlayerOne] += roundMatchup.PlayerOneSportsmanshipScore; playerSportsmanshipScores[roundMatchup.PlayerTwo] += roundMatchup.PlayerTwoSportsmanshipScore; if (roundMatchup is PairRoundMatchup) { var pairRoundMatchup = roundMatchup as PairRoundMatchup; playerBattleScores[pairRoundMatchup.PlayerThree] += pairRoundMatchup.PlayerThreeBattleScore; playerBattleScores[pairRoundMatchup.PlayerFour] += pairRoundMatchup.PlayerFourBattleScore; playerSportsmanshipScores[pairRoundMatchup.PlayerThree] += pairRoundMatchup.PlayerThreeSportsmanshipScore; playerSportsmanshipScores[pairRoundMatchup.PlayerFour] += pairRoundMatchup.PlayerFourSportsmanshipScore; } } List <Dictionary <Player, int> > playerScores = new List <Dictionary <Player, int> >() { playerBattleScores, playerSportsmanshipScores }; return(playerScores); }
public PlayersController(TournamentDbContext context) { _context = context; }
public static int[] GetPlayerScores(int id, TournamentDbContext _context) { int playerBattleScore = 0; int playerSportsmanshipScore = 0; var roundMatchups = _context.RoundMatchups .Include(r => r.PlayerOne).Include(r => r.PlayerTwo) .Where(r => !(r is PairRoundMatchup) && r.PlayerTwo != null && r.PlayerOne.Id == id || r.PlayerTwo.Id == id) .ToList(); var pairRoundMatchups = _context.PairRoundMatchups .Include(r => r.PlayerOne).Include(r => r.PlayerTwo).Include(r => r.PlayerThree).Include(r => r.PlayerFour) .Where(r => r.PlayerTwo != null && r.PlayerOne.Id == id || r.PlayerTwo.Id == id || r.PlayerThree.Id == id || r.PlayerFour.Id == id) .ToList(); roundMatchups = roundMatchups.Union(pairRoundMatchups).ToList(); foreach (RoundMatchup roundMatchup in roundMatchups) { if (roundMatchup.PlayerOne.Id == id) { playerBattleScore += roundMatchup.PlayerOneBattleScore; } if (roundMatchup.PlayerTwo.Id == id) { playerBattleScore += roundMatchup.PlayerTwoBattleScore; } if (roundMatchup.PlayerOne.Id == id) { playerSportsmanshipScore += roundMatchup.PlayerOneSportsmanshipScore; } if (roundMatchup.PlayerTwo.Id == id) { playerSportsmanshipScore += roundMatchup.PlayerTwoSportsmanshipScore; } if (roundMatchup is PairRoundMatchup) { var pairRoundMatchup = roundMatchup as PairRoundMatchup; if (pairRoundMatchup.PlayerThree.Id == id) { playerBattleScore += pairRoundMatchup.PlayerThreeBattleScore; } if (pairRoundMatchup.PlayerFour.Id == id) { playerBattleScore += pairRoundMatchup.PlayerFourBattleScore; } if (pairRoundMatchup.PlayerThree.Id == id) { playerSportsmanshipScore += pairRoundMatchup.PlayerThreeSportsmanshipScore; } if (pairRoundMatchup.PlayerFour.Id == id) { playerSportsmanshipScore += pairRoundMatchup.PlayerFourSportsmanshipScore; } } } int[] playerScores = new int[2] { playerBattleScore, playerSportsmanshipScore }; return(playerScores); }
public UserService(TournamentDbContext dbContext) { _dbContext = dbContext; _userSet = _dbContext.Set <User>(); }
public UserTournamentRepository(TournamentDbContext context) { _context = context; }
public static bool RoundMatchupsExists(int id, TournamentDbContext _context) { return(_context.RoundMatchups.Any(e => e.Id == id)); }
public DeleteTournamentCommand(int tournamentid, TournamentDbContext context) { _tournamentid = tournamentid; _tournamentDataRepository = new TournamentDataRespository(context); }
public TeamsController(TournamentDbContext dbContext) { _dbContext = dbContext; }
public EventDataRepository(TournamentDbContext coneTournamentDbContext) { _coneTournamentDbContext = coneTournamentDbContext; }
//Add SetPlayerScore //Validation Function //Returns a Dictionary that contains public static Dictionary <Player, List <Player> > GetPreviouslyPlayedOpponentClashes(TournamentDbContext _context) { List <Player> players = _context.Players.ToList(); Dictionary <Player, List <Player> > duplicateOpponents = new Dictionary <Player, List <Player> >(); foreach (Player player in players) { List <Player> opponents = GetAllOpponents(player, _context); var duplicates = opponents .GroupBy(i => i) .Where(g => g.Count() > 1) .Select(g => g.Key) .ToList(); duplicateOpponents[player] = duplicates; } return(duplicateOpponents); }
public CreateTournamentCommand(Tournament tournament, TournamentDbContext context) { _tournamentModel = tournament; _tournamentDataRepository = new TournamentDataRespository(context); }
/** * * Round Generation * **/ public static void GenerateNextRound(TournamentDbContext _context) { List <Player> players = _context.Players.OrderByDescending(p => p.BattleScore).ToList(); //List<int> AllocatedTables = new List<int>(GetnoOfTables()); int secondaryIndex = 0; int i = 0; while (i < players.Count) { //Skip this player if they are already allocated an opponent if (players[i].CurrentOpponent == null) { if (secondaryIndex == 0) { secondaryIndex = i + 1; } int s = 0; for (s = secondaryIndex; s < players.Count; s++) { if (players[s].CurrentOpponent == null) { //Check if higher player has ever played lower player var opponents = PlayerActions.GetAllOpponents(players[i], _context); var hasPlayed = false; foreach (Player opponent in opponents) { if (players[s] == opponent) { hasPlayed = true; } } //If they have not played allocate them as opponents if (hasPlayed == false) { players[i].CurrentOpponent = players[s]; players[s].CurrentOpponent = players[i]; secondaryIndex = 0; break; } /** * Following block is to deallocate the next lowest ranked allocated pair **/ if (players.Where(p => p.CurrentOpponent == null).LastOrDefault() == players[s] && (players[i].CurrentOpponent == null)) //if (s == (players.Count - 1) && (players[i].CurrentOpponent == null)) { if (i - 1 >= 0) { //Set the lowestAllocatedPair to the highest ranked player Player lowestAllocatedPair = players[0]; //Iterate from the second highest ranked player all the way to the player ranked one higher than the player currently being matched for (int playerIndex = 1; playerIndex < i; playerIndex++) { //Assign the current player to the player being examined in the current iteration of the loop Player currentPlayer = players[playerIndex]; //Check that the current player has an opponent (if not, skip to the next iteration of the loop) if (currentPlayer.CurrentOpponent != null) { //Proceed if the current player's opponent has a higher rank than the current player if (players.IndexOf(currentPlayer.CurrentOpponent) < players.IndexOf(currentPlayer)) { if (players.IndexOf(currentPlayer.CurrentOpponent) > players.IndexOf(lowestAllocatedPair)) { //Set lowestAllocatedPair to the current player's opponent //(The highest ranked member of the new lowest ranked allocated pair) lowestAllocatedPair = currentPlayer.CurrentOpponent; } } //Proceed if the current player has a higher rank than their opponent else if (players.IndexOf(currentPlayer) < players.IndexOf(currentPlayer.CurrentOpponent)) { //Proceed if the current player has a lower rank than the previous value of lowestAllocatedPair if (players.IndexOf(currentPlayer) > players.IndexOf(lowestAllocatedPair)) { //Set lowestAllocatedPair to the current player //(The highest ranked member of the new lowest ranked allocated pair) lowestAllocatedPair = currentPlayer; } } } } //Set the new player to be allocated to the highest member of the lowestAllocatedPair i = players.IndexOf(lowestAllocatedPair) - 1; //Set the starting player that will be tested for allocation suitability to one rank lower than //the opponent of the highest member of the allocated pair secondaryIndex = players.IndexOf(lowestAllocatedPair.CurrentOpponent) + 1; //Deallocate the lowest allocated pair as each other's opponent lowestAllocatedPair.CurrentOpponent.CurrentOpponent = null; lowestAllocatedPair.CurrentOpponent = null; } } } } } i++; } int newRound = GetLastRoundNo(_context) + 1; foreach (Player player in players) { if (players.IndexOf(player) < players.IndexOf(player.CurrentOpponent)) { RoundMatchup roundMatchup = new RoundMatchup { RoundNo = newRound, PlayerOne = player, PlayerTwo = player.CurrentOpponent }; //allocates table for matchup // roundMatchup.Table = AllocateTable(GetTables(player), AllocatedTables); _context.Add(roundMatchup); } } foreach (Player player in players) { player.CurrentOpponent = null; _context.Update(player); } _context.SaveChanges(); }
public CreateEventCommand(TournamentEvent tournamentEventModel, TournamentDbContext context) { _tournamentEventModel = tournamentEventModel; _eventsDataRepository = new EventDataRepository(context); }
public QuizRepository(TournamentDbContext context) : base(context) { _context = context; }
public AdminController(TournamentDbContext context) { _context = context; }
/** * * Validation * **/ //Get the errors for all roundMatchups, including any players on a team with themself or versing themself, any players //who have played the same opponent more than once, any players who have not played in every round and any players who have //played twice in one round public static List <List <string> > GetRoundMatchupErrors(TournamentDbContext _context) { var roundMatchups = _context.RoundMatchups.Include(r => r.PlayerOne).Include(r => r.PlayerTwo).OrderByDescending(r => r.PlayerOne.BattleScore).ToList(); List <string> duplicatePlayers = new List <string>(); List <string> duplicateOpponents = new List <string>(); List <string> unallocatedPlayers = new List <string>(); List <string> overallocatedPlayers = new List <string>(); Dictionary <string, int> playerRoundCount = new Dictionary <string, int>(); List <Player> players = _context.Players.ToList(); foreach (Player player in players) { playerRoundCount.Add(player.Name, 0); } List <String> tempDuplicatePlayers = new List <string>(); foreach (RoundMatchup roundMatchup in roundMatchups) { if (roundMatchup is PairRoundMatchup) { var pairRoundMatchup = roundMatchup as PairRoundMatchup; // Check if PlayerOne is on a team with themself if (pairRoundMatchup.PlayerOne == pairRoundMatchup.PlayerTwo) { duplicatePlayers.Add(pairRoundMatchup.PlayerOne.Name + " is on a team with themself in round " + pairRoundMatchup.RoundNo); } // Check if PlayerOne is playing themself if (pairRoundMatchup.PlayerOne == pairRoundMatchup.PlayerThree || pairRoundMatchup.PlayerOne == pairRoundMatchup.PlayerFour) { duplicatePlayers.Add(pairRoundMatchup.PlayerOne.Name + " is playing themself in round " + pairRoundMatchup.RoundNo); } //Check if PlayerTwo is playing themself if (pairRoundMatchup.PlayerTwo == pairRoundMatchup.PlayerThree || pairRoundMatchup.PlayerTwo == pairRoundMatchup.PlayerFour) { duplicatePlayers.Add(pairRoundMatchup.PlayerTwo.Name + " is playing themself in round " + pairRoundMatchup.RoundNo); } //Check if PlayerThree is on a team with themself if (pairRoundMatchup.PlayerThree == pairRoundMatchup.PlayerFour) { duplicatePlayers.Add(pairRoundMatchup.PlayerThree.Name + " is on a team with themself in round " + pairRoundMatchup.RoundNo); } } //Check if there are any players versing themselves else if (!(roundMatchup is PairRoundMatchup)) { if (roundMatchup.PlayerOne == roundMatchup.PlayerTwo) { duplicatePlayers.Add(roundMatchup.PlayerOne.Name + " is playing themself in round " + roundMatchup.RoundNo); } } //Check if there are players who either do not play at all or play more than once foreach (Player player in _context.Players) { if (player.Id == roundMatchup.PlayerOne.Id) { playerRoundCount[player.Name] += 1; } if (player.Id == roundMatchup.PlayerTwo.Id) { playerRoundCount[player.Name] += 1; } if (roundMatchup is PairRoundMatchup) { PairRoundMatchup pairRoundMatchup = roundMatchup as PairRoundMatchup; if (player.Id == pairRoundMatchup.PlayerThree.Id) { playerRoundCount[player.Name] += 1; } if (player.Id == pairRoundMatchup.PlayerFour.Id) { playerRoundCount[player.Name] += 1; } } } } int roundCount = GetLastRoundNo(_context); foreach (KeyValuePair <string, int> entry in playerRoundCount) { if (entry.Value > roundCount) { overallocatedPlayers.Add(entry.Key + " has been allocated to " + entry.Value + " matchups with only " + roundCount + " rounds played"); } if (entry.Value < roundCount) { unallocatedPlayers.Add(entry.Key + " has only been allocated to " + entry.Value + " matchup/s when " + roundCount + " have been played"); } } Dictionary <Player, List <string> > duplicateOpponentsDictionary = new Dictionary <Player, List <string> >(); foreach (Player player in players) { List <Player> opponents = PlayerActions.GetAllOpponents(player, _context); List <string> duplicateNames = opponents .GroupBy(i => i) .Where(g => g.Count() > 1) .Select(g => g.Key.Name) .ToList(); duplicateOpponentsDictionary[player] = duplicateNames; } foreach (var duplicateOpponentSet in duplicateOpponentsDictionary) { string duplicateOpponentFormattedNameList = string.Join(", ", duplicateOpponentSet.Value); if (duplicateOpponentSet.Value.Count != 0) { duplicateOpponents.Add(duplicateOpponentSet.Key.Name + " has played " + duplicateOpponentFormattedNameList + " at least twice"); } } var errors = new List <List <string> >() { duplicatePlayers, duplicateOpponents, overallocatedPlayers, unallocatedPlayers }; return(errors); }
public RoundMatchupsController(TournamentDbContext context) { _context = context; }
public ResultsController(TournamentDbContext context) { _context = context; }
public EventsController(TournamentDbContext context, ITournamentDomainClient tournamentDomainClient) { _context = context; _tournamentDomainClient = tournamentDomainClient; }
public SqlTournamentDataBaseService(TournamentDbContext context) { _context = context; }