public static List <position_command_for_turnament> CalculatePositionLine(Guid turnamentId, List <game_for_turnament> game, List <position_command_for_turnament> commands)
        {
            var response = new List <position_command_for_turnament>();

            var newPosition = new List <position_command_for_turnament>();

            commands.ForEach(x =>
            {
                var position = new position_command_for_turnament
                {
                    id_account   = x.id_account,
                    command_name = x.command_name,
                    points       = 0,
                };

                var goals = 0;

                game.ForEach(g =>
                {
                    commands.ForEach(c =>
                    {
                        var commandOne = DbHelper.GetGoalsCommandForTurnament(turnamentId, g.id_command_one, g.id_game_for_turnament);
                        var commandTwo = DbHelper.GetGoalsCommandForTurnament(turnamentId, g.id_command_two, g.id_game_for_turnament);

                        if (g.id_command_one == x.id_account && g.id_command_two == c.id_account)
                        {
                            goals += commandOne.Count - commandTwo.Count;
                        }
                        if (g.id_command_two == x.id_account && g.id_command_one == c.id_account)
                        {
                            goals += commandTwo.Count - commandOne.Count;
                        }
                    });
                });

                position.points = goals;
                newPosition.Add(position);
            });

            newPosition.Sort((a, b) => a.points <= b.points ? 1 : -1);

            var groupNewPosition = newPosition.GroupBy(x => x.points).ToList();

            var i = 0;

            groupNewPosition.ForEach(x =>
            {
                x.ToList().ForEach(g =>
                {
                    g.place = i;
                    response.Add(g);
                });

                i++;
            });

            return(response);
        }
Exemple #2
0
        public static void Execute(ElementRequest request)
        {
            var turnamentId           = Guid.Parse(request.Txt);
            var groups                = DbHelper.GetTurnamentGroups(turnamentId);
            var commands              = new List <position_command_for_turnament>();
            var circlePositionCommand = new List <List <position_command_for_turnament> >();

            groups.ForEach(x =>
            {
                var position = DbHelper.GetPositionTopForGroup(turnamentId, x.id_group_for_turnament);
                position.ForEach(p =>
                {
                    commands.Add(p);
                });
            });

            var countCircle = 0;

            for (var p = 1; p < groups.Count * 2; p = p * 2)
            {
                circlePositionCommand.Add(new List <position_command_for_turnament>());
                countCircle++;
            }

            circlePositionCommand.Add(new List <position_command_for_turnament>());
            countCircle++;

            for (var i = 0; i < countCircle; i++)
            {
                var circleId = DbHelper.AddCircleForTurmament(turnamentId, i + 1);

                if (i == countCircle - 1)
                {
                    var command = new position_command_for_turnament
                    {
                        id_account   = Guid.Empty,
                        command_name = "?"
                    };

                    circlePositionCommand[i].Add(command);
                }

                if (i == 0)
                {
                    if (commands.Count % 4 == 0)
                    {
                        commands.ForEach(x =>
                        {
                            circlePositionCommand[i].Add(x);
                        });
                    }
                    else
                    {
                        for (var j = 0; j < Math.Pow(2, countCircle - 1); j++)
                        {
                            circlePositionCommand[i].Add(commands[j]);
                        }
                    }
                }

                if (i == 1)
                {
                    if (commands.Count % 4 == 0)
                    {
                        for (var j = 0; j < circlePositionCommand[i - 1].Count / 2; j++)
                        {
                            var command = new position_command_for_turnament
                            {
                                id_account   = Guid.Empty,
                                command_name = "?"
                            };
                            circlePositionCommand[i].Add(command);
                        }
                    }
                    else
                    {
                        for (var j = 0; j < commands.Count - circlePositionCommand[i - 1].Count; j++)
                        {
                            circlePositionCommand[i].Add(commands[commands.Count - j - 1]);
                        }

                        for (var j = 0; j < circlePositionCommand[i - 1].Count / 2; j++)
                        {
                            var command = new position_command_for_turnament
                            {
                                id_account   = Guid.Empty,
                                command_name = "?"
                            };
                            circlePositionCommand[i].Add(command);
                        }
                    }
                }

                if (i > 1 && i != countCircle - 1)
                {
                    for (var j = 0; j < circlePositionCommand[i - 1].Count / 2; j++)
                    {
                        var command = new position_command_for_turnament
                        {
                            id_account   = Guid.Empty,
                            command_name = "?"
                        };

                        circlePositionCommand[i].Add(command);
                    }
                }

                for (var j = 0; j < circlePositionCommand[i].Count; j++)
                {
                    DbHelper.CreatePositionCommand(circlePositionCommand[i][j], circleId);
                }
            }

            var circle = DbHelper.GetCircleForTurnament(turnamentId);

            circle.Sort((a, b) => a.numbr_circle <= b.numbr_circle ? -1 : 1);

            circle.ForEach(x =>
            {
                var positoinCommands = DbHelper.GetPositionCommand(x.id_circle_for_turnament);
                if (x != circle.Last())
                {
                    for (var j = 0; j < positoinCommands.Count; j = j + 2)
                    {
                        DbHelper.CreateGameForTurnament(x.id_circle_for_turnament, positoinCommands[j].id_account, positoinCommands[j + 1].id_account, positoinCommands[j].command_name, positoinCommands[j + 1].command_name, x.numbr_circle, false);
                    }
                }
            });
        }
        public static List <List <position_command_for_turnament> > CalculatePositionGroup(Guid turnamentId, List <game_for_turnament> game, List <position_command_for_turnament> commands)
        {
            var response = new List <List <position_command_for_turnament> >();

            var newPosition = new List <position_command_for_turnament>();

            commands.ForEach(x =>
            {
                var position = new position_command_for_turnament
                {
                    id_account = x.id_account,
                    points     = 0,
                };

                var points = 0;

                game.ForEach(g =>
                {
                    commands.ForEach(c =>
                    {
                        if (g.id_command_one == x.id_account && g.id_command_two == c.id_account)
                        {
                            points += g.command_one_points;
                        }
                        if (g.id_command_two == x.id_account && g.id_command_one == c.id_account)
                        {
                            points += g.command_two_points;
                        }
                    });
                });

                position.points = points;
                newPosition.Add(position);
            });

            newPosition.Sort((a, b) => a.points <= b.points ? 1 : -1);
            var groupNewPosition = newPosition.GroupBy(x => x.points).ToList();

            if (groupNewPosition.Count() == 1)
            {
                response.Add(CalculatePositionLine(turnamentId, game, groupNewPosition.First().ToList()));
            }
            else
            {
                groupNewPosition.ForEach(x =>
                {
                    if (x.Count() == 1)
                    {
                        response.Add(x.ToList());
                    }
                    else
                    {
                        var listPosition = CalculatePositionGroup(turnamentId, game, x.ToList());
                        listPosition.ForEach(w =>
                        {
                            response.Add(w);
                        });
                    }
                });
            }

            return(response);
        }
        public static void CreateTour(Guid turnamentId)
        {
            var positions = DbHelper.GetPositionCommand(turnamentId);

            positions.Sort((a, b) => a.position <= b.position ? -1 : 1);

            if (positions.Count % 2 != 0)
            {
                positions.Add(new position_command_for_turnament
                {
                    fake_code = true
                });
            }

            commandSize = positions.Count;

            table = new position_command_for_turnament[commandSize - 1, commandSize / 2][];

            for (var row = 0; row < commandSize - 1; row++)
            {
                for (var col = 0; col < commandSize / 2; col++)
                {
                    if (row == 0)
                    {
                        table[row, col] = new position_command_for_turnament[2] {
                            positions[col], positions[commandSize - 1 - col]
                        }
                    }
                    ;

                    if (row != 0 && row != commandSize - 2)
                    {
                        if ((row + 1) % 2 == 0)
                        {
                            if (col == 0)
                            {
                                table[row, col] = new position_command_for_turnament[2] {
                                    table[row - 1, col + 1][1], table[row - 1, col][0]
                                }
                            }
                            ;

                            if (col == commandSize / 2 - 1)
                            {
                                if (commandSize / 2 > 2)
                                {
                                    table[row, col] = new position_command_for_turnament[2] {
                                        table[row - 1, col - 1][0], table[row - 1, col][0]
                                    }
                                }
                            }
                            ;
                            else
                            {
                                table[row, col] = new position_command_for_turnament[2] {
                                    table[row - 1, col - 1][1], table[row - 1, col][0]
                                }
                            };
                            if (col != 0 && col != commandSize / 2 - 1)
                            {
                                if (col > 1)
                                {
                                    table[row, col] = new position_command_for_turnament[2] {
                                        table[row - 1, col - 1][0], table[row - 1, col + 1][1]
                                    }
                                }
                                ;
                                else
                                {
                                    table[row, col] = new position_command_for_turnament[2] {
                                        table[row - 1, col - 1][1], table[row - 1, col + 1][1]
                                    }
                                };
                            }
                        }
                        else
                        {
                            if (col == 0)
                            {
                                table[row, col] = new position_command_for_turnament[2] {
                                    table[row - 1, col][1], table[row - 1, col + 1][1]
                                }
                            }
                            ;
                            if (col == commandSize / 2 - 1)
                            {
                                if (commandSize / 2 > 2)
                                {
                                    table[row, col] = new position_command_for_turnament[2] {
                                        table[row - 1, col - 1][0], table[row - 1, col][0]
                                    }
                                }
                            }
                            ;
                            if (col != 0 && col != commandSize / 2 - 1)
                            {
                                table[row, col] = new position_command_for_turnament[2] {
                                    table[row - 1, col - 1][0], table[row - 1, col + 1][1]
                                }
                            }
                            ;
                        }
                    }

                    if (row == commandSize - 2)
                    {
                        if (col == 0)
                        {
                            table[row, col] = new position_command_for_turnament[2] {
                                positions[0], positions[1]
                            }
                        }
                        ;
                        else
                        {
                            table[row, col] = new position_command_for_turnament[2] {
                                positions[col + 1], positions[commandSize - col]
                            }
                        };
                    }
                }
            }

            for (var row = 0; row < commandSize - 1; row++)
            {
                for (var col = 0; col < commandSize / 2; col++)
                {
                    DbHelper.CreateGameForTurnament(turnamentId, table[row, col][0].id_account, table[row, col][1].id_account, table[row, col][0].command_name, table[row, col][1].command_name, row + 1, table[row, col][0].fake_code || table[row, col][1].fake_code);
                }
            }
        }
    }
}