Ejemplo n.º 1
0
        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);
        }