public TableHockeyEndGameQueueHandler(Dictionary <int, int> i_dictRankedPlayerId, int i_nNumberOfGamesPerRound, int i_nNumberOfRounds) { //{Player ID, Player Rank} //Sort by rank descending. var m_sortedPlayersByRank = (from entry in i_dictRankedPlayerId orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value); m_nNumberOfPlayers = i_dictRankedPlayerId.Count; m_nNumberOfGamesPerRound = i_nNumberOfGamesPerRound; m_nNumberOfRounds = i_nNumberOfRounds; bool m_bEliminationRound = (PageUtility.highestExponentLessThanOrEqualToSum(2, m_nNumberOfPlayers) != m_nNumberOfPlayers); int m_nNumberOfEvenRounds = m_bEliminationRound ? (m_nNumberOfRounds - 1) : m_nNumberOfRounds; //Build end game tree, starting with lowest ranked players.. //First, find out if there are players that do not fit into an end game tree. int m_nNumberOfPlayersToEliminate = m_nNumberOfPlayers - Convert.ToInt32(Math.Pow(2.0, m_nNumberOfEvenRounds)); int m_nNumberOfPlayersToNextRound = m_nNumberOfPlayers - 2 * m_nNumberOfPlayersToEliminate; //Export any odd top players from initial dictionary, into a List (per round) of player dictionaries. //First list entry pertains to first round, etc. m_dictGamePlayersPerRound = new List <Dictionary <int, int> >(); Dictionary <int, int> m_nextRoundPlayerGames = new Dictionary <int, int>(); Dictionary <int, int> m_eliminatePlayerGames = new Dictionary <int, int>(); if ((m_nNumberOfPlayersToNextRound > 0) && (m_nNumberOfPlayersToEliminate > 0)) { m_nextRoundPlayers = (from entry in m_sortedPlayersByRank orderby entry.Value ascending where entry.Value <= m_nNumberOfPlayersToNextRound select entry).ToDictionary(pair => pair.Key, pair => pair.Value); foreach (KeyValuePair <int, int> kvp in m_nextRoundPlayers) { m_nextRoundPlayerGames.Add(kvp.Key, kvp.Value); m_nextRoundPlayerGames.Add(-kvp.Key, -1); } } Dictionary <int, int> m_eliminatePlayers; if (m_nNumberOfPlayersToEliminate > 0) { m_eliminatePlayers = (from entry in m_sortedPlayersByRank orderby entry.Value ascending where entry.Value > m_nNumberOfPlayersToNextRound select entry).ToDictionary(pair => pair.Key, pair => pair.Value); } else { m_eliminatePlayers = (from entry in m_sortedPlayersByRank orderby entry.Value ascending where entry.Value <= m_nNumberOfPlayersToNextRound select entry).ToDictionary(pair => pair.Key, pair => pair.Value); m_nNumberOfPlayersToNextRound = 0; } int m_nHalfNumberOfEvenPlayers = Convert.ToInt32(m_eliminatePlayers.Count / 2); for (int i = 0; i < m_nHalfNumberOfEvenPlayers; i++) { Dictionary <int, int> m_currentHomePlayer = (from entry in m_eliminatePlayers where entry.Value == (m_nNumberOfPlayersToNextRound + i + 1) select entry).ToDictionary(pair => pair.Key, pair => pair.Value); Dictionary <int, int> m_currentAwayPlayer = (from entry in m_eliminatePlayers where entry.Value == (m_nNumberOfPlayers - i) select entry).ToDictionary(pair => pair.Key, pair => pair.Value); m_eliminatePlayerGames.Add(m_currentHomePlayer.First().Key, m_currentHomePlayer.First().Value); m_eliminatePlayerGames.Add(m_currentAwayPlayer.First().Key, m_currentAwayPlayer.First().Value); } m_dictGamePlayersPerRound.Add(m_eliminatePlayerGames); m_dictGamePlayersPerRound.Add(m_nextRoundPlayerGames); m_nCurrentRoundNumber = 1; }