Example #1
0
        //Generate contest rounds.
        private int calcNumberOfContestRounds(int i_nContestId)
        {
            //First, count the number of rounds to generate for end game.
            //This depends on the number of participating players.
            int m_nPlayerCount = -1;

            using (var context = new TableHockeyData.UHSSWEB_DEVEntities())
            {
                //Get players.
                var contestPlayersQuery = from p in context.TableHockeyPlayer
                                          join cp in context.TableHockeyContestPlayer on p.PlayerId equals cp.PlayerId
                                          where cp.ContestId == i_nContestId
                                          select new { p.PlayerId };

                m_nPlayerCount = contestPlayersQuery.ToList().Count();
            }

            //Determine and return number of rounds. If uneven number of players with respect to even end game pairs, add elimination rund.

            int m_nHighestEvenPairs   = PageUtility.highestExponentLessThanOrEqualToSum(2, m_nPlayerCount);
            int m_nNumberOfEvenRounds = Convert.ToInt32(0.5 * m_nHighestEvenPairs) - 1;

            return(((m_nPlayerCount - m_nHighestEvenPairs) == 0) ? m_nNumberOfEvenRounds : 1 + m_nNumberOfEvenRounds);
        }
Example #2
0
        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;
        }