private IEnumerable <MatchHeaderInfo> CreateSectionMatches(int playersCount, CompetitionSection section, int?qualifyingToNextSection = default(int?))
        {
            var actualPlayersCount = playersCount - qualifyingToNextSection.GetValueOrDefault();
            var rounds             = (int)Math.Log((playersCount), 2);
            var actualRounds       = rounds - (qualifyingToNextSection.GetValueOrDefault() > 0 ? (int)Math.Log(qualifyingToNextSection.GetValueOrDefault(), 2) : 0);
            var matches            = new List <MatchHeaderInfo>(playersCount);

            for (int i = 0; i < actualPlayersCount; i++)
            {
                var match = new MatchHeaderInfo();
                match.Section  = section;
                match.Position = i;
                match.Round    = rounds - Math.Max(0, (int)Math.Log((playersCount - (i + 1)), 2));
                match.IsFinal  = (match.Round == actualRounds);// && ((actualRounds == 1) || (i < playersCount - 1));
                if (section == CompetitionSection.Final && match.Round == rounds - 1)
                {
                    match.IsSemiFinal = true;
                }
                match.Status = MatchStatus.Created;
                matches.Add(match);
            }
            var matchesByRound = matches.GroupBy(m => m.Round).ToArray();

            foreach (var roundMatches in matchesByRound)
            {
                var index = 0;
                foreach (var match in roundMatches)
                {
                    match.RoundRelativePosition = index++;
                }
            }
            return(matches);
        }
Beispiel #2
0
        private void QualifySameSection(MatchHeaderInfo match)
        {
            var competitionId = match.CompetitionId;
            var winner        = match.Winner == MatchWinner.Player1 ? match.Player1 : match.Player2;
            var competitionMatchesRepository = ServiceProvider.Get <ICompetitionMatchesRepository>();
            var qualifyToMatch = competitionMatchesRepository.GetMatchByRelativePosition(competitionId, match.Section, match.Round + 1,
                                                                                         match.RoundRelativePosition / 2);

            if (qualifyToMatch.IsNotNull() && winner != null)
            {
                competitionMatchesRepository.UpdatePlayersPosition(competitionId,
                                                                   new[]
                {
                    new UpdatePlayerPositionInfo
                    {
                        MatchId  = qualifyToMatch.Id,
                        PlayerId = winner.Id,
                        Position =
                            match.RoundRelativePosition % 2 == 0
                                                                                           ? 0
                                                                                           : 1
                    }
                });
            }

            if (match.IsSemiFinal)
            {
                QualifyToThirdPlace(match);
            }
        }
Beispiel #3
0
        private void QualifyInConsolation(MatchHeaderInfo match)
        {
            var competitionId = match.CompetitionId;
            var winner        = match.Winner == MatchWinner.Player1 ? match.Player1 : match.Player2;
            var competitionMatchesRepository = ServiceProvider.Get <ICompetitionMatchesRepository>();
            var qualifyToMatch = default(MatchHeaderInfo);
            var playersCount   = 32;
            var map            = new ConsolationMap();
            var nextPosition   = map.GetNextPosition(playersCount, match.Position);

            qualifyToMatch = competitionMatchesRepository.GetMatchByPosition(competitionId,
                                                                             CompetitionSection.Consolation,
                                                                             nextPosition.Item1);

            if (qualifyToMatch.IsNotNull() && winner != null)
            {
                competitionMatchesRepository.UpdatePlayersPosition(competitionId,
                                                                   new[]
                {
                    new UpdatePlayerPositionInfo
                    {
                        MatchId  = qualifyToMatch.Id,
                        PlayerId = winner.Id,
                        Position = nextPosition.Item2
                    }
                });
            }
        }
Beispiel #4
0
 private static void RenderScores(HtmlTextWriter htmlWriter, MatchHeaderInfo match, int player)
 {
     htmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, "scores");
     htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
     if (match.SetScores.IsNullOrEmpty() && (int)match.Status < (int)MatchStatus.Completed)
     {
         if (player == 0)
         {
             htmlWriter.Write(match.Date);
         }
         else if (match.StartTime.HasValue)
         {
             htmlWriter.Write(match.StartTime.Value.ToString("HH:mm"));
         }
     }
     else
     {
         foreach (var setScore in match.SetScores)
         {
             htmlWriter.RenderBeginTag(HtmlTextWriterTag.Span);
             var points = (player == 0 ? setScore.Player1Points : setScore.Player2Points);
             htmlWriter.Write(points == 0 ? " " : points.ToString());
             htmlWriter.RenderEndTag();
         }
     }
     htmlWriter.RenderEndTag();
 }
        private IEnumerable <MatchHeaderInfo> CreateConselationMatches(int finalPlayersCount)
        {
            var section = CompetitionSection.Consolation;

            var playersCount          = finalPlayersCount;
            var matches               = new List <MatchHeaderInfo>();
            var round                 = 0;
            var position              = 0;
            var roundRelativePosition = 0;
            var match                 = default(MatchHeaderInfo);

            for (var i = (playersCount / 4); i >= 4; i /= 2)
            {
                for (var x = 0; x < 2; x++)
                {
                    for (var j = 0; j < i; j++)
                    {
                        match          = new MatchHeaderInfo();
                        match.Section  = section;
                        match.Position = position++;
                        match.RoundRelativePosition = roundRelativePosition++;
                        match.Round  = round;
                        match.Status = MatchStatus.Created;
                        matches.Add(match);
                    }
                    round++;
                    roundRelativePosition = 0;
                }
            }

            match             = new MatchHeaderInfo();
            match.Section     = section;
            match.Position    = position++;
            match.Round       = round;
            match.IsSemiFinal = true;
            match.Status      = MatchStatus.Created;
            matches.Add(match);
            match             = new MatchHeaderInfo();
            match.Section     = section;
            match.Position    = position++;
            match.Round       = round;
            match.IsSemiFinal = true;
            match.Status      = MatchStatus.Created;
            matches.Add(match);
            round++;

            match          = new MatchHeaderInfo();
            match.Section  = section;
            match.Position = position++;
            match.Round    = round;
            match.IsFinal  = true;
            match.Status   = MatchStatus.Created;
            matches.Add(match);

            return(matches);
        }
Beispiel #6
0
        private void QualifyToFinal(MatchHeaderInfo match)
        {
            var winner = (match.Winner == MatchWinner.Player1 ? match.Player1 : match.Player2);

            if (winner != null)
            {
                var winnerId  = winner.Id;
                var matchCode = "Q" + (match.RoundRelativePosition + 1);

                PositionPlayerInSection(match.CompetitionId, winnerId, CompetitionSection.Final, matchCode);
            }
        }
Beispiel #7
0
 private void Qualify(MatchHeaderInfo match)
 {
     if (match.IsFinal)
     {
         if (match.Section == CompetitionSection.Qualifying)
         {
             QualifyToFinal(match);
         }
     }
     else
     {
         QualifySameSection(match);
     }
 }
        public static MatchHeaderInfo MapFromData(this Match match)
        {
            var result = new MatchHeaderInfo()
            {
                Id                    = match.Id,
                StartTime             = match.StartTime.HasValue ? DateTime.SpecifyKind(match.StartTime.Value, DateTimeKind.Utc).ToLocalTime() : default(DateTime?),
                Status                = (MatchStatus)match.Status,
                Section               = (CompetitionSection)match.SectionId,
                CompetitionId         = match.CompetitionId,
                StartTimeType         = (StartTimeType)match.StartTimeType,
                Round                 = match.Round,
                RoundRelativePosition = match.RoundRelativePosition,
                Position              = match.Position,
                IsFinal               = match.IsFinal,
                IsSemiFinal           = match.IsSemiFinal,
                SlotPosition          = match.SlotPosition,
                Player1Code           = match.Player1Code,
                Player2Code           = match.Player2Code,
            };

            if (match.SlotType.HasValue)
            {
                result.SlotType = (SlotType)match.SlotType.Value;
            }
            if (match.Winner.HasValue)
            {
                result.Winner = (MatchWinner)match.Winner.Value;
            }
            if (match.Result.HasValue)
            {
                result.Result = (MatchResult)match.Result.Value;
            }

            result.Player1   = CreateMatchPlayerFromData(match.Player);
            result.Player2   = CreateMatchPlayerFromData(match.Player5);
            result.Player3   = CreateMatchPlayerFromData(match.Player6);
            result.Player4   = CreateMatchPlayerFromData(match.Player7);
            result.SetScores =
                match.MatchScores.Select(ms => new SetScore()
            {
                Number        = ms.SetNumber,
                Player1Points = ms.Player1Points,
                Player2Points = ms.Player2Points,
                BreakPoints   = ms.BreakPoints
            }).ToArray();

            return(result);
        }
Beispiel #9
0
        private void QualifyToThirdPlace(MatchHeaderInfo match)
        {
            var positioningEngine     = GetPositioningEngine(CompetitionMethod.Knockout);
            var competitionRepository = ServiceProvider.Get <ICompetitionRepository>();

            var competitionDetails = competitionRepository.GetCompetitionDetails(match.CompetitionId);
            var looserId           = (match.Winner == MatchWinner.Player1 ? match.Player2 : match.Player1).Id;
            var thirdPlaceMatch    = competitionDetails.Matches.Where(m => m.Section == CompetitionSection.Final).OrderBy(m => m.Position).Last();
            var updateInfo         = new UpdatePlayerPositionInfo()
            {
                PlayerId = looserId,
                Position = thirdPlaceMatch.Player1.IsNull() ? 0 : 1,
                MatchId  = thirdPlaceMatch.Id
            };
            var competitionMatchesRepository = ServiceProvider.Get <ICompetitionMatchesRepository>();

            competitionMatchesRepository.UpdatePlayersPosition(match.CompetitionId, new[] { updateInfo });
        }
Beispiel #10
0
 private static void RenderPlayer(HtmlTextWriter htmlWriter, MatchHeaderInfo match, MatchPlayer player)
 {
     htmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, "player");
     htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
     if (player != null)
     {
         htmlWriter.Write(player.LocalFirstName);
         if (player.LocalLastName.NotNullOrEmpty())
         {
             htmlWriter.Write(" , ");
             htmlWriter.Write(player.LocalLastName);
         }
     }
     else
     {
         htmlWriter.Write(match.Status == MatchStatus.Completed ? "BYE" : "&nbsp;");
     }
     htmlWriter.RenderEndTag();
 }
Beispiel #11
0
        private void QualifyToConsolation(MatchHeaderInfo match)
        {
            var playersCount = 32;
            var competitionMatchesRepository = ServiceProvider.Get <ICompetitionMatchesRepository>();
            var round          = 0;
            var rounds         = competitionMatchesRepository.GetCompetitionSectionRounds(match.CompetitionId, CompetitionSection.Final);
            var qualifyToMatch = default(MatchHeaderInfo);

            if (match.Round == (6 - rounds))
            {
                qualifyToMatch = competitionMatchesRepository.GetMatchByRelativePosition(match.CompetitionId,
                                                                                         CompetitionSection.Consolation,
                                                                                         round,
                                                                                         match.Position / 2);
            }
            else
            {
                var map      = new ConsolationMap();
                var position = map.Position(playersCount, match.Position);
                qualifyToMatch = competitionMatchesRepository.GetMatchByPosition(match.CompetitionId,
                                                                                 CompetitionSection.Consolation, position);
            }

            var looser = (match.Winner == MatchWinner.Player1 ? match.Player2 : match.Player1);

            if (looser != null && qualifyToMatch != null)
            {
                var roundMatchesCount = competitionMatchesRepository.GetRoundMatchesCount(match.CompetitionId,
                                                                                          CompetitionSection.Consolation,
                                                                                          qualifyToMatch.Round);
                var updatePlayerInfo = new UpdatePlayerPositionInfo()
                {
                    MatchId  = qualifyToMatch.Id,
                    PlayerId = looser.Id,
                    Position = match.Round == (6 - rounds) ?  (qualifyToMatch.Player1 == null? 0 : 1) : (qualifyToMatch.RoundRelativePosition < roundMatchesCount / 2 ? 1 : 0)
                };



                competitionMatchesRepository.UpdatePlayersPosition(match.CompetitionId, new[] { updatePlayerInfo });
            }
        }
Beispiel #12
0
 private void MapMatchToDataMatch(MatchHeaderInfo match, Match dataMatch)
 {
     dataMatch.SlotPosition = match.SlotPosition;
     if (match.SlotType.HasValue)
     {
         dataMatch.SlotType = (int)match.SlotType.Value;
     }
     else
     {
         dataMatch.SlotType = default(int?);
     }
     dataMatch.SectionId             = (int)match.Section;
     dataMatch.Position              = match.Position;
     dataMatch.Round                 = match.Round;
     dataMatch.RoundRelativePosition = match.RoundRelativePosition;
     dataMatch.Status                = (int)match.Status;
     dataMatch.StartTimeType         = (int)match.StartTimeType;
     dataMatch.IsFinal               = match.IsFinal;
     dataMatch.IsSemiFinal           = match.IsSemiFinal;
     dataMatch.Player1Code           = match.Player1Code;
     dataMatch.Player2Code           = match.Player2Code;
 }
Beispiel #13
0
        private void RenderContainer(HtmlTextWriter htmlWriter, int round, int minRound, Dictionary <int, Queue <MatchHeaderInfo> > map, MatchHeaderInfo match, int player)
        {
            var side = player == 0 ? "top" : "bottom";

            htmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, "round" + round + "-" + side + "wrap");
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
            if (match.IsNotNull())
            {
                htmlWriter.AddAttribute(HtmlTextWriterAttribute.Class, "round" + round + "-" + side);
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Div);
                RenderPlayer(htmlWriter, match, side == "top" ? match.Player1 : match.Player2);
                RenderScores(htmlWriter, match, player);
                htmlWriter.RenderEndTag();
            }

            WriteRound(htmlWriter, round - 1, minRound, map);

            htmlWriter.RenderEndTag();
        }