Пример #1
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());
        }
        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();

        }
 private CompetitionPosition[] PositionFinalPlayersInCompetition(CompetitionDetails details, CompetitionPlayer[] players)
 {
     var positioningEngine = new FinalPositioningEngine();
     var positioningParameters = new FinalPositioningParameters
     {
         QualifyingToFinal = details.Type.QualifyingToFinalPlayersCount,
         Players = (from p in players select new Simple.SAMS.CompetitionEngine.Player
                                                 {
                                                     Id = p.Id, 
                                                     Rank = p.CompetitionRank,
                                                     AccumulatedScore = p.AccumulatedScore.GetValueOrDefault(),
                                                     AverageScore = p.AverageScore.GetValueOrDefault()
                                                 }).ToArray<Simple.SAMS.CompetitionEngine.Player>()
     };
     
     return positioningEngine.Evaluate(positioningParameters);
 }