Esempio n. 1
0
        public async Task <IActionResult> AddTracks(int id, [Bind("TrackId")] int trackId)
        {
            var track = await _trackService.GetTrackById(trackId);

            var season = await _seasonService.GetSeasonById(id, true, true);

            if (track == null || season == null)
            {
                return(NotFound());
            }

            // Finds the last time track was used and uses same stintsetup as then
            var lastracemodel = await _raceService.GetLastRace(season.ChampionshipId, trackId);

            Race race;

            if (lastracemodel != null)
            {
                var stintlist = lastracemodel.Stints;
                race = _raceBuilder
                       .InitializeRace(track, season)
                       .AddModifiedStints(stintlist)
                       .GetResultAndRefresh();
            }
            else
            {
                race = _raceBuilder
                       .InitializeRace(track, season)
                       .AddDefaultStints()
                       .GetResultAndRefresh();
            }

            season.Races.Add(race);
            _seasonService.Update(season);
            await Context.SaveChangesAsync();

            return(RedirectToAction(nameof(AddTracks), new { id }));
        }
Esempio n. 2
0
        public async Task <int> CreateCopyOfLastSeason(int championshipID)
        {
            var newSeason  = new Season();
            var lastSeason = await Data.AsNoTracking()
                             .Where(e => e.ChampionshipId == championshipID)
                             .Include(e => e.Championship)
                             .OrderByDescending(e => e.SeasonNumber)
                             .FirstOrDefaultAsync();

            if (lastSeason != null)
            {
                var lastRaces = await Context.Races
                                .Where(e => e.SeasonId == lastSeason.SeasonId)
                                .Include(e => e.Stints)
                                .ToListAsync();

                foreach (var pos in lastSeason.PointsPerPosition)
                {
                    newSeason.PointsPerPosition.Add(pos);
                }
                newSeason.PolePoints = lastSeason.PolePoints;
                newSeason.QualificationRemainingDriversQ2 = lastSeason.QualificationRemainingDriversQ2;
                newSeason.QualificationRemainingDriversQ3 = lastSeason.QualificationRemainingDriversQ3;
                newSeason.QualificationRNG = lastSeason.QualificationRNG;
                newSeason.QualyBonus       = lastSeason.QualyBonus;
                newSeason.SeasonNumber     = (lastSeason.SeasonNumber + 1);
                newSeason.PitMax           = lastSeason.PitMax;
                newSeason.PitMin           = lastSeason.PitMin;

                foreach (var race in lastRaces.OrderBy(e => e.Round))
                {
                    var track  = Context.Tracks.SingleOrDefaultAsync(e => e.Id == race.TrackId);
                    var stints = new List <Stint>();
                    foreach (var lastStint in race.Stints.OrderBy(e => e.Number))
                    {
                        var stint = new Stint
                        {
                            Number               = lastStint.Number,
                            ApplyDriverLevel     = lastStint.ApplyDriverLevel,
                            ApplyChassisLevel    = lastStint.ApplyChassisLevel,
                            ApplyEngineLevel     = lastStint.ApplyEngineLevel,
                            ApplyQualifyingBonus = lastStint.ApplyQualifyingBonus,
                            ApplyReliability     = lastStint.ApplyReliability,
                            RNGMaximum           = lastStint.RNGMaximum,
                            RNGMinimum           = lastStint.RNGMinimum
                        };
                        stints.Add(stint);
                    }
                    var newRace = _raceBuilder
                                  .InitializeRace(await track, newSeason)
                                  .AddModifiedStints(stints)
                                  .GetResultAndRefresh();

                    newSeason.Races.Add(newRace);
                }
            }
            newSeason.ChampionshipId = championshipID;
            await Add(newSeason);
            await SaveChangesAsync();

            return(newSeason.SeasonId);
        }