public CompetitionPosition[] Evaluate(QualifyingPositionParameters parameters)
        {
            var playersCount = parameters.PlayersCount;
            var positions    = new List <CompetitionPosition>(playersCount);
            var players      = parameters.Players.Take(playersCount).ToList();

            players.ForEach(
                player =>
            {
                if (player.Rank <= 0)
                {
                    player.Rank = playersCount + 1;
                }
            });
            players.Sort((p1, p2) =>
            {
                if (p1.Rank > p2.Rank)
                {
                    return(1);
                }
                else if (p2.Rank > p1.Rank)
                {
                    return(-1);
                }
                else
                {
                    if (p1.AverageScore > p2.AverageScore)
                    {
                        return(1);
                    }
                    else if (p2.AverageScore > p1.AverageScore)
                    {
                        return(-1);
                    }
                    else
                    {
                        if (p1.AccumulatedScore > p2.AccumulatedScore)
                        {
                            return(1);
                        }
                        else if (p2.AccumulatedScore > p1.AccumulatedScore)
                        {
                            return(-1);
                        }
                        else
                        {
                            return(0);
                        }
                    }
                }
            });
            var qEngine = new QualifyingDrawPositionEngine();

            positions.AddRange(Enumerable.Range(0, PlayersCountCalculator.CalculatePlayersCount(players.Count)).Select(i => new CompetitionPosition()));
            var ranks = qEngine.Rank(players.Count);
            var map   = new Dictionary <string, Player>();

            for (var i = 0; i < players.Count; i++)
            {
                map["Q" + (i + 1)] = players[i];
            }

            foreach (var item in ranks)
            {
                Player player;
                if (map.TryGetValue(item.Code, out player))
                {
                    var position = new CompetitionPosition
                    {
                        PlayerId = player.Id
                    };

                    positions[item.Index] = position;
                }
            }

            //var position = 0;
            //var skip = parameters.PlayersCount / parameters.QualifyingCount;

            //var availablePositions = Enumerable.Range(0, playersCount).ToList();
            //var directRankedPositions = new List<int>();
            //var rankedPositions = new List<int>();
            //while (players.Count > 0 && position < playersCount)
            //{
            //    var player = players[0];
            //    players.RemoveAt(0);
            //    positions[position].PlayerId = player.Id;
            //    availablePositions.RemoveAt(availablePositions.IndexOf(position));
            //    directRankedPositions.Add(position + 1);
            //    position += skip;
            //}

            //var randomPositionGenerator =
            //        new RandomPositionGenerator(Enumerable.Range(0, parameters.QualifyingCount ).Select(i=> ((i+1)*skip) -1 ).ToArray());

            //while (players.Count > 0 && randomPositionGenerator.CanTake())
            //{
            //    var player = players[0];
            //    players.RemoveAt(0);
            //    position = randomPositionGenerator.Take();
            //    availablePositions.RemoveAt(availablePositions.IndexOf(position));
            //    rankedPositions.Add(position+1);
            //    positions[position].PlayerId = player.Id;
            //}

            //if (availablePositions.Count > 0)
            //{
            //    randomPositionGenerator =
            //        new RandomPositionGenerator(availablePositions.Except(directRankedPositions.Concat(rankedPositions)).ToArray());

            //    while (players.Count > 0 && randomPositionGenerator.CanTake())
            //    {
            //        var player = players[0];
            //        players.RemoveAt(0);
            //        position = randomPositionGenerator.Take();
            //        availablePositions.RemoveAt(availablePositions.IndexOf(position));
            //        positions[position].PlayerId = player.Id;
            //    }
            //}

            //if (availablePositions.Count > 0)
            //{
            //    randomPositionGenerator =
            //        new RandomPositionGenerator(
            //            availablePositions.Except(directRankedPositions).ToArray());

            //    while (players.Count > 0 && randomPositionGenerator.CanTake())
            //    {
            //        var player = players[0];
            //        players.RemoveAt(0);
            //        position = randomPositionGenerator.Take();
            //        availablePositions.RemoveAt(availablePositions.IndexOf(position));
            //        positions[position].PlayerId = player.Id;
            //    }
            //}

            //if (availablePositions.Count > 0)
            //{
            //    availablePositions = availablePositions.Skip(availablePositions.Count - players.Count).ToList();

            //    randomPositionGenerator =
            //        new RandomPositionGenerator(availablePositions.ToArray());

            //    while (players.Count > 0 && randomPositionGenerator.CanTake())
            //    {
            //        var player = players[0];
            //        players.RemoveAt(0);
            //        position = randomPositionGenerator.Take();
            //        availablePositions.RemoveAt(availablePositions.IndexOf(position));
            //        positions[position].PlayerId = player.Id;
            //    }
            //}


            return(positions.ToArray());
        }
Ejemplo n.º 2
0
        public PositioningItem[] Rank(int actualPlayersCount, int qPlayers)
        {
            var playersCount  = PlayersCountCalculator.CalculatePlayersCount(actualPlayersCount);
            var rankedPlayers = playersCount / 4;


            var items = new List <PositioningItem>(playersCount);

            for (int i = 0; i < playersCount; i++)
            {
                items.Add(new PositioningItem()
                {
                    Index = i
                });
            }

            var codes = new Queue <string>();

            for (var i = 0; i < actualPlayersCount - qPlayers; i++)
            {
                codes.Enqueue("MD" + (i + 1));
            }

            for (int i = 0; i < qPlayers; i++)
            {
                codes.Enqueue("Q" + (i + 1));
            }

            if (actualPlayersCount < playersCount)
            {
                for (var i = 0; i < (playersCount - actualPlayersCount); i++)
                {
                    codes.Enqueue("BYE");
                }
            }

            var vsRanked = new Stack <int>();

            items[0].Code = codes.Dequeue();
            items.Last().Code = codes.Dequeue();
            vsRanked.Push(1);
            vsRanked.Push(items.Count - 2);

            var randomizer = default(RandomPositionGenerator);

            if (rankedPlayers > 2)
            {
                var p1 = items.Count / 4;
                var p2 = (items.Count - 1) - items.Count / 4;
                randomizer = new RandomPositionGenerator(new[] { p1, p2 });
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                vsRanked.Push(p1 + 1);
                vsRanked.Push(p2 - 1);
            }

            if (rankedPlayers > 4)
            {
                var p = new[]
                {
                    items.Count / 2 - 1,
                    items.Count / 2,
                    items.Count / 4 - 1,
                    (items.Count) - items.Count / 4,
                };
                randomizer = new RandomPositionGenerator(p);
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();
                items[randomizer.Take()].Code = codes.Dequeue();

                vsRanked.Push(p[0] - 1);
                vsRanked.Push(p[1] + 1);
                vsRanked.Push(p[2] - 1);
                vsRanked.Push(p[3] + 1);
            }


            var emptySlots = items.Where(item => string.IsNullOrEmpty(item.Code));
            var positions  = emptySlots.Select(i => i.Index).Except(vsRanked).ToList();

            randomizer = new RandomPositionGenerator(positions.ToArray());

            while (randomizer.CanTake())
            {
                items[randomizer.Take()].Code = codes.Dequeue();
            }
            if (rankedPlayers > 4)
            {
                positions = new List <int>();
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                randomizer = new RandomPositionGenerator(positions.ToArray());
                while (randomizer.CanTake())
                {
                    items[randomizer.Take()].Code = codes.Dequeue();
                }
            }

            if (rankedPlayers > 2)
            {
                positions = new List <int>();
                positions.Add(vsRanked.Pop());
                positions.Add(vsRanked.Pop());
                randomizer = new RandomPositionGenerator(positions.ToArray());
                while (randomizer.CanTake())
                {
                    items[randomizer.Take()].Code = codes.Dequeue();
                }
            }

            items[vsRanked.Pop()].Code = codes.Dequeue();
            items[vsRanked.Pop()].Code = codes.Dequeue();

            return(items.ToArray());
        }
Ejemplo n.º 3
0
        public CompetitionPosition[] Evaluate(FinalPositioningParameters parameters)
        {
            var playersCount = parameters.Players.Length;
            var positions    = new List <CompetitionPosition>(playersCount);

            if (playersCount > 0)
            {
                var players = parameters.Players.Take(playersCount).ToList();
                players.ForEach(
                    player =>
                {
                    if (player.Rank <= 0)
                    {
                        player.Rank = playersCount + 1;
                    }
                });
                players.Sort((p1, p2) =>
                {
                    if (p1.Rank > p2.Rank)
                    {
                        return(1);
                    }
                    else if (p2.Rank > p1.Rank)
                    {
                        return(-1);
                    }
                    else
                    {
                        if (p1.AverageScore > p2.AverageScore)
                        {
                            return(1);
                        }
                        else if (p2.AverageScore > p1.AverageScore)
                        {
                            return(-1);
                        }
                        else
                        {
                            if (p1.AccumulatedScore > p2.AccumulatedScore)
                            {
                                return(1);
                            }
                            else if (p2.AccumulatedScore > p1.AccumulatedScore)
                            {
                                return(-1);
                            }
                            else
                            {
                                return(0);
                            }
                        }
                    }
                });

                var mdEngine = new MainDrawPositionEngine();
                positions.AddRange(Enumerable.Range(0, PlayersCountCalculator.CalculatePlayersCount(players.Count)).Select(i => new CompetitionPosition()));
                var ranks = mdEngine.Rank(players.Count, parameters.QualifyingToFinal);
                var map   = new Dictionary <string, Player>();
                for (var i = 0; i < players.Count - parameters.QualifyingToFinal; i++)
                {
                    map["MD" + (i + 1)] = players[i];
                }

                for (int i = players.Count - parameters.QualifyingToFinal; i < players.Count; i++)
                {
                    map["Q" + (i + 1)] = players[i];
                }

                foreach (var item in ranks)
                {
                    Player player;
                    if (map.TryGetValue(item.Code, out player))
                    {
                        var position = new CompetitionPosition
                        {
                            PlayerId = player.Id
                        };

                        positions[item.Index] = position;
                    }
                }

                //var availablePositions = Enumerable.Range(0, playersCount).ToList();
                //var rankGroups = new List<List<int>>()
                //                     {
                //                         new List<int>()
                //                     };
                //var rankedPositions = new List<int>();
                //var position = 0;

                //RankPlayer(positions, position, players, availablePositions, rankGroups[0]);

                //if (players.Count > 0)
                //{
                //    position = playersCount - 1;
                //    RankPlayer(positions, position, players, availablePositions, rankGroups[0]);
                //}

                //if (parameters.QualifyingToFinal > 2)
                //{
                //    rankGroups.Add(new List<int>());
                //    RankPlayers(positions, new[] { parameters.QualifyingToFinal, (playersCount - parameters.QualifyingToFinal-1) }, players, availablePositions, rankGroups[1]);
                //}

                //if (parameters.QualifyingToFinal > 4)
                //{
                //    rankGroups.Add(new List<int>());
                //    var rndpositions = new[]
                //                           {
                //                               parameters.QualifyingToFinal - 1,
                //                               parameters.QualifyingToFinal*2 - 1,
                //                               (playersCount - parameters.QualifyingToFinal*2),
                //                               playersCount - parameters.QualifyingToFinal
                //                           };
                //    RankPlayers(positions, rndpositions, players, availablePositions, rankGroups[2]);
                //}

                //if (parameters.QualifyingToFinal > 8)
                //{
                //    throw new NotSupportedException("Only up to 32->8");
                //    rankGroups.Add(new List<int>());
                //    var rndpositions = new[]
                //                           {
                //                               parameters.QualifyingToFinal - 1,
                //                               parameters.QualifyingToFinal*2 - 1,
                //                               (playersCount - parameters.QualifyingToFinal*2),
                //                               playersCount - parameters.QualifyingToFinal
                //                           };
                //    RankPlayers(positions, rndpositions, players, availablePositions, rankGroups[3]);
                //}

                //var set1 = availablePositions.Except(rankGroups.SelectMany(i=>i.ToList())).ToList();
                //RankPlayers(positions, set1.ToArray(), players, availablePositions, null);
                //for(var groupIndex = rankGroups.Count - 1; groupIndex >=0; groupIndex--)
                //{
                //    var set = availablePositions.Except(rankGroups[groupIndex]).ToList();
                //    RankPlayers(positions, set1.ToArray(), players, availablePositions, null);
                //}

                //RankPlayers(positions, availablePositions.ToArray(), players, availablePositions, null);
            }

            return(positions.ToArray());
        }