public async Task <IActionResult> RaceStart(int id, int raceId) { var race = await Context.Races .Include(r => r.Stints) .Include(r => r.Season.Drivers) .Include(r => r.DriverResults) .ThenInclude(r => r.StintResults) .Include(r => r.DriverResults) .SingleOrDefaultAsync(r => r.RaceId == raceId); if (race.DriverResults.Count == 0) { // Checks if the user activating the race is the admin SimUser user = await UserManager.GetUserAsync(User); if (User.Identity.IsAuthenticated && await UserManager.IsInRoleAsync(user, Constants.RoleAdmin)) { race = _raceBuilder .Use(race) .AddAllDrivers() .GetResult(); var driverTraits = await Context.DriverTraits .Include(drt => drt.Trait) .ToListAsync(); var teamTraits = await Context.TeamTraits .Include(ttr => ttr.Trait) .ToListAsync(); var trackTraits = await Context.TrackTraits .Include(tet => tet.Trait) .Where(trt => trt.TrackId == race.TrackId) .ToListAsync(); var seasonTeams = await Context.SeasonTeams .Where(st => st.SeasonId == race.SeasonId) .Include(st => st.Rubber) .ToListAsync(); // Get all the possible strategies for this race var strategies = await Context.Strategies .Where(s => s.RaceLen == race.Stints.Count) .Include(s => s.Tyres) .ThenInclude(t => t.Tyre) .ToListAsync(); // If the length is zero then creates a list consisting of the single, default strategy if (strategies.Count == 0) { strategies = await Context.Strategies .Where(s => s.StrategyId == 1) .Include(s => s.Tyres) .ThenInclude(t => t.Tyre) .ToListAsync(); } foreach (var driverRes in race.DriverResults) { // Gets the traits from the driver in the loop and sets them var thisDriverTraits = driverTraits.Where(drt => drt.DriverId == driverRes.SeasonDriver.DriverId); RaceService.SetDriverTraitMods(driverRes, thisDriverTraits, race.Weather); // Gets the seasonteam of the driver in the loop var thisDriverTeam = seasonTeams.First(st => st.SeasonDrivers.Contains(driverRes.SeasonDriver)); // Gets the traits from the team of the driver in the loop and sets them var thisTeamTraits = teamTraits.Where(ttr => ttr.TeamId == thisDriverTeam.TeamId); RaceService.SetTeamTraitMods(driverRes, thisTeamTraits, race.Weather); // Sets the traits from the track to the driver in the loop RaceService.SetTrackTraitMods(driverRes, trackTraits, race.Weather); // Set a random strategy int stratIndex = rng.Next(0, strategies.Count); RaceService.SetRandomStrategy(driverRes, strategies[stratIndex]); // Applies the effect of the related tyre manufacturer var rubber = thisDriverTeam.Rubber; RaceService.SetRubberEffect(driverRes, rubber); } Context.DriverResults.AddRange(race.DriverResults); await Context.SaveChangesAsync(); } else { return(Forbid()); } } return(RedirectToAction("RaceWeekend", new { raceId })); }