Example #1
0
        public Contest Get(Contest contest)
        {
            if (contest.Id == 0)
            {
                throw new IdentifierUndefinedException();
            }

            return(_contestRepository.Get(contest.Id));
        }
        public void SaveResult(int contestId, bool isDrawn, int?winnerId)
        {
            //validate if contestId is prsent in our system
            var contest = _contestRepo.Get(contestId);

            if (contest == null)
            {
                throw new System.ArgumentException("There are no Contest found with the ContestId");
            }

            //todo: validate if Contest is already decided and saved

            var competitors = _competitorRepository.GetByContest(contestId);

            if (!competitors.Any() || competitors.Count() < 2)
            {
                throw new System.ArgumentException("There are no participant found with the ContestId");
            }

            //validate if winnerId belongs to the Right Contest
            if (winnerId.HasValue)
            {
                if (!competitors.Any(c => c.CompetitorId == winnerId.Value))
                {
                    throw new System.ArgumentException("WinnerId is not right");
                }
            }

            var matchResult = new MatchResult(contestId);

            foreach (var competitor in competitors)
            {
                var participant = new Participant(_leaderboardService)
                {
                    ContestId    = contestId,
                    CompetitorId = competitor.CompetitorId
                };
                matchResult.AddParticipant(participant);
            }

            matchResult.IsDrawn  = isDrawn;
            matchResult.WinnerId = winnerId;
            matchResult.Notify();
        }
        public async Task <List <FightResponseModel> > ScheduleFights(int contestId)
        {
            var contestFights = await _fightsRepository.GetFights(contestId);

            var contestCategories = await _contestCategoryMappingsRepository.GetByContest(contestId);

            var indexedContestFights = contestFights
                                       .GroupBy(fight => fight.ContestCategoryId)
                                       .OrderByDescending(group => group.Count())
                                       .ToDictionary(group => group.Key,
                                                     group => _fightsIndexer
                                                     .CreateIndex(group.ToList())
                                                     .OrderByDescending(fight => fight.Fight.Id)
                                                     .ToLookup(f => f.DrawDeepLevel, f => f));

            int startNumber = 1;
            int maxDeep     = indexedContestFights.Max(fights => fights.Value.Max(index => index.Key));

            for (int deep = maxDeep; deep > 0; deep--)
            {
                foreach (var categoryFights in indexedContestFights)
                {
                    if (!categoryFights.Value.Contains(deep))
                    {
                        continue;
                    }

                    var fights = categoryFights.Value[deep];
                    foreach (var fight in fights)
                    {
                        fight.Index = startNumber;
                        startNumber++;
                    }
                }
            }

            var contest = await _contestRepository.Get(contestId);

            var ringsEnties = await _contestRingsRepository.GetByContest(contestId);

            var rings         = _mapper.Map <List <RingAvailabilityModel> >(ringsEnties);
            var indexedFights = indexedContestFights.SelectMany(index => index.Value.SelectMany(f => f).ToList()).OrderBy(f => f.Index).ToList();

            foreach (var fight in indexedFights)
            {
                var category = contestCategories.First(c => c.ContestCategoryId == fight.Fight.ContestCategoryId);

                var round     = category.ContestCategory.FightStructure.Round;
                int fightTime = (round.Duration + round.BreakDuration) * round.RoundsCount + contest.WaiKhruTime;

                var ring = rings.Where(r => r.From < r.To).MinBy(r => r.From).FirstOrDefault();

                fight.Fight.StartDate = ring.From;
                fight.Fight.Ring      = ring.Name;
                ring.From             = ring.From.AddSeconds(fightTime);
            }


            foreach (var cf in indexedContestFights)
            {
                Console.WriteLine(cf.GetHashCode());
                foreach (var deepFights in cf.Value)
                {
                    Console.WriteLine("Deep: " + deepFights.Key);

                    foreach (IndexedFight fight in deepFights.ToList())
                    {
                        Console.WriteLine($"Index: {fight.Index}   Fight:{fight.Fight.Id}   Red:{fight.Fight.RedAthlete?.FirstName} {fight.Fight.RedAthlete?.Surname}  VS   Blue:{fight.Fight.BlueAthlete?.FirstName} {fight.Fight.BlueAthlete?.Surname} at: {fight.Fight.StartDate} on {fight.Fight.Ring}");
                    }

                    Console.WriteLine();
                }

                Console.WriteLine();
            }

            var scheduledFights  = indexedContestFights.SelectMany(index => index.Value.SelectMany(fights => fights.Select(f => f.Fight))).ToList();
            int fightStartNumber = 1;

            scheduledFights.ForEach(fight =>
            {
                fight.StartNumber = fightStartNumber++;
            });

            var savedFights = await Save(scheduledFights);

            return(savedFights);
        }
        public async Task <ContestResponseModel> GetContest(int id)
        {
            var contest = await _repository.Get(id);

            return(_mapper.Map <ContestResponseModel>(contest));
        }