private void SeedKits()
        {
            var seasonTask = _seasonRepository.SingleOrDefaultAsync(s => s.Name == "2018/2019");

            seasonTask.Wait();
            var season = seasonTask.Result;

            var kitsData = File.ReadAllText("Persistence/Data/Kits.json");
            var kits     = JsonConvert.DeserializeObject <JArray>(kitsData);

            foreach (var kitToken in kits)
            {
                var clubName = kitToken["clubName"].ToString();

                var clubTask = _clubRepository.SingleOrDefaultAsync(c => c.Name == clubName);
                clubTask.Wait();

                var club = clubTask.Result;
                if (club == null)
                {
                    continue;
                }

                var squadTask = _squadRepository
                                .SingleOrDefaultAsync(s => s.SeasonId == season.Id && s.ClubId == club.Id);
                squadTask.Wait();

                var squad = squadTask.Result;
                if (squad == null)
                {
                    continue;
                }

                var kit = JsonConvert.DeserializeObject <Kit>(kitToken.ToString(), _jsonSerializerSettings);
                kit.Squad = squad;

                _kitRepository.Add(kit);
            }

            _unitOfWork.CompleteAsync().Wait();
        }
Exemple #2
0
        public async Task GenerateAsync(int seasonId)
        {
            var season = await _seasonRepository.GetAsync(seasonId);

            var clubs = await _clubRepository.GetBriefListAsync(seasonId);

            var clubList        = clubs.ToList();
            var clubCount       = clubList.Count;
            var roundCount      = clubCount - 1;
            var matchesPerRound = clubCount / 2;

            var matchTime = new DateTime(
                season.StartDate.Year,
                season.StartDate.Month,
                season.StartDate.Day,
                17, 00, 00);

            var matches = new List <Match>();

            for (var round = 0; round < roundCount; round++)
            {
                for (var match = 0; match < matchesPerRound; match++)
                {
                    var home = (round + match) % (clubCount - 1);
                    var away = (clubCount - 1 - match + round) % (clubCount - 1);

                    if (match == 0)
                    {
                        away = clubCount - 1;
                    }

                    var homeClub = await _clubRepository.GetAsync(clubList[home].Id);

                    var awayClub = await _clubRepository.GetAsync(clubList[away].Id);

                    var homeSquad = await _squadRepository
                                    .SingleOrDefaultAsync(s => s.SeasonId == seasonId &&
                                                          s.ClubId == homeClub.Id);

                    var awaySquad = await _squadRepository
                                    .SingleOrDefaultAsync(s => s.SeasonId == seasonId &&
                                                          s.ClubId == awayClub.Id);

                    var homeClubKit = await _kitRepository
                                      .SingleOrDefaultAsync(k => k.SquadId == homeSquad.Id &&
                                                            k.KitType == KitType.HomeKit);

                    var awayClubKit = await _kitRepository
                                      .SingleOrDefaultAsync(k => k.SquadId == awaySquad.Id &&
                                                            k.KitType == KitType.AwayKit);

                    matches.Add(new Match
                    {
                        SeasonId      = seasonId,
                        Round         = round + 1,
                        StadiumId     = homeClub.StadiumId,
                        HomeClubId    = clubList[home].Id,
                        AwayClubId    = clubList[away].Id,
                        HomeClubKitId = homeClubKit.Id,
                        AwayClubKitId = awayClubKit.Id,
                        MatchTime     = matchTime,
                        IsPlayed      = false
                    });

                    matches.Add(new Match
                    {
                        SeasonId      = seasonId,
                        Round         = round + 1 + roundCount,
                        StadiumId     = awayClub.StadiumId,
                        HomeClubId    = clubList[away].Id,
                        AwayClubId    = clubList[home].Id,
                        HomeClubKitId = awayClubKit.Id,
                        AwayClubKitId = homeClubKit.Id,
                        MatchTime     = matchTime.AddDays(roundCount * 7),
                        IsPlayed      = false
                    });
                }

                if (round < roundCount - 1)
                {
                    matchTime = matchTime.AddDays(7);
                }
            }

            _matchRepository.AddRange(matches);

            season.EndDate = matchTime.AddDays(roundCount * 7);

            await _unitOfWork.CompleteAsync();
        }