/// <summary> /// Fires a driver. /// </summary> /// <param name="theCar">The car wich driver will be fired.</param> public void FireDriver(Vehicle theCar) { if (theCar == null) { throw new ArgumentNullException("theCar"); } if (theCar.Driver == null) { throw new InvalidOperationException("Car has not a driver."); } var carRepo = this.container.Resolve<IVehicleRepository>(); var uow = this.container.Resolve<IUnitOfWork>(); ////theCar.Driver.Owner = null; theCar.Driver = null; carRepo.Update(theCar); uow.Commit(); }
/// <summary> /// Mounts a part in a car. /// </summary> /// <param name="thePart">The part to mount.</param> /// <param name="theCar">The car where the part will be mounted.</param> public void Mount(Part thePart, Vehicle theCar) { if (thePart == null) { throw new ArgumentNullException("thePart"); } if (theCar == null) { throw new ArgumentNullException("theCar"); } if (thePart.ProductionState != EDevelopmentState.Finished) { throw new InvalidOperationException("Part is not finished or is deprecated."); } if (thePart.MountedIn != null) { throw new InvalidOperationException("Part is mounted in other vehicle."); } var carRepo = this.container.Resolve<IVehicleRepository>(); var uow = this.container.Resolve<IUnitOfWork>(); // TODO: Filter this in the repository (wearing part by design type) if (theCar.WearingParts.Where(x => x.Design.PartDesignType == thePart.Design.PartDesignType).ToList().Count > 0) { throw new InvalidOperationException("Part type is already mounted."); } thePart.MountedIn = theCar; theCar.WearingParts.Add(thePart); carRepo.Update(theCar); uow.Commit(); }
private void InitializeTeam(FastTeamData teamData, List<FastPartDesignData> designData) { // Repositories var countriesRepo = this.container.Resolve<ICountryRepository>(); var teamsRepo = this.container.Resolve<ITeamRepository>(); var driversRepo = this.container.Resolve<IDriverRepository>(); var designTypesRepo = this.container.Resolve<IPartDesignTypeRepository>(); var partDesignsRepo = this.container.Resolve<IPartDesignRepository>(); var partsRepo = this.container.Resolve<IPartRepository>(); var playersRepo = this.container.Resolve<IGenericRepository<Player>>(); // Country Country country = countriesRepo.GetByName(teamData.PlayerCountryName); if (country == null) { country = new Country() { Name = teamData.PlayerCountryName }; countriesRepo.Save(country); } // Design Types var designTypes = designTypesRepo.GetAllInDictionary(); // Team Team theTeam = teamsRepo.GetByShortName(teamData.ShortTeamName); if (theTeam == null) { Player thePlayer = new Player() { Name = teamData.ManagerName, Country = country }; playersRepo.Save(thePlayer); theTeam = new Team() { Name = teamData.TeamName, WebName = teamData.WebTeamName, Manager = thePlayer, ShortName = teamData.ShortTeamName }; teamsRepo.Save(theTeam); } // Drivers Driver driver1 = driversRepo.GetByShortName(teamData.Driver1ShortName); if (driver1 == null) { driver1 = new Driver() { Name = teamData.Driver1Name, ShortName = teamData.Driver1ShortName, Owner = theTeam }; driversRepo.Save(driver1); } Driver driver2 = driversRepo.GetByShortName(teamData.Driver2ShortName); if (driver2 == null) { driver2 = new Driver() { Name = teamData.Driver2Name, ShortName = teamData.Driver2ShortName, Owner = theTeam }; driversRepo.Save(driver2); } Driver testDriver = null; if (!String.IsNullOrEmpty(teamData.TestDriverName)) { testDriver = driversRepo.GetByShortName(teamData.TestDriverShortName); if (testDriver == null) { testDriver = new Driver() { Name = teamData.TestDriverName, ShortName = teamData.TestDriverShortName, Owner = theTeam }; driversRepo.Save(testDriver); } } // Part designs IList<PartDesign> designs = new List<PartDesign>(); foreach (var item in designData) { Team t = null; if (item.DesignOwnerShortName == theTeam.Name) { t = theTeam; } else if (!string.IsNullOrEmpty(item.DesignOwnerShortName)) { t = teamsRepo.GetByShortName(item.DesignOwnerShortName); } designs.Add(this.CreateAndAssignPartDesign(item.DesignName, item.DesignShortName, partDesignsRepo, designTypes[item.DesignTypeName], t)); } // Parts foreach (var design in designs) { for (int i = 0; i < 4; i++) { string code = string.Concat(design.Code, ".", i.ToString("000", CultureInfo.CurrentCulture)); if (design.Owner == null || design.Owner.ShortName != theTeam.ShortName) { code = string.Concat(theTeam.ShortName, ".", code); } Part thePart = partsRepo.GetByCode(code); if (thePart == null) { thePart = new Part(design) { Code = code, Owner = theTeam, ProductionState = EDevelopmentState.Finished }; partsRepo.Save(thePart); } if (!theTeam.Parts.Contains(thePart)) { theTeam.Parts.Add(thePart); } } } // CARS if (theTeam.Vehicles.Count < 2) { Vehicle car1 = new Vehicle() { Team = theTeam, Driver = driver1, Number = teamData.Driver1No, VehicleType = EVehicleType.FirstVehicle }; foreach (var kvp in designTypes) { Part part = theTeam.Parts.First(x => x.Design.PartDesignType == kvp.Value && x.MountedIn == null); if (part != null) { car1.WearingParts.Add(part); part.MountedIn = car1; } } theTeam.Vehicles.Add(car1); Vehicle car2 = new Vehicle() { Team = theTeam, Driver = driver2, Number = teamData.Driver2No, VehicleType = EVehicleType.SecondVehicle }; foreach (var kvp in designTypes) { Part part = theTeam.Parts.First(x => x.Design.PartDesignType == kvp.Value && x.MountedIn == null); if (part != null) { car2.WearingParts.Add(part); part.MountedIn = car2; } } theTeam.Vehicles.Add(car2); if (!string.IsNullOrEmpty(teamData.TestDriverName)) { Vehicle testCar = new Vehicle() { Team = theTeam, Driver = testDriver, VehicleType = EVehicleType.Muleto }; foreach (var kvp in designTypes) { Part part = theTeam.Parts.First(x => x.Design.PartDesignType == kvp.Value && x.MountedIn == null); if (part != null) { testCar.WearingParts.Add(part); part.MountedIn = testCar; } } theTeam.Vehicles.Add(testCar); } } }
/// <summary> /// Calculates the car time. /// </summary> /// <param name="car">The car to calculate time.</param> /// <param name="grandPrixResult">The grand prix result.</param> /// <returns>A partially complete race result.</returns> public RaceResult CalculateCarTime(Vehicle car, IList<RaceResult> grandPrixResult) { if (car == null) { throw new ArgumentNullException("car"); } if (grandPrixResult == null) { throw new ArgumentNullException("grandPrixResult"); } if (grandPrixResult.Count == 0) { throw new ArgumentException("List is empty.", "grandPrixResult"); } if (car.Team == null) { throw new ArgumentException("Car has no team.", "car"); } if (!car.Team.IsPlayable) { throw new InvalidOperationException("Car is not playable."); } RaceResult result = new RaceResult() { Vehicle = car, GrandPrix = grandPrixResult[0].GrandPrix, LapsNo = grandPrixResult[0].LapsNo }; if (!this.CheckVehicleToRace(car)) { result.Time = TimeSpan.FromMilliseconds(-1); // Not ready to start the race result.Description = "Not ready to start the race"; } else { var vehRepo = this.container.Resolve<IVehicleRepository>(); RaceResult lastWithTime = this.GetTheLastWithTime(grandPrixResult); Dictionary<string, double> percents = this.BuildPercentComponentsMap(); IncidentProbabilityHelper probHelper = new IncidentProbabilityHelper(grandPrixResult); probHelper.RegisterIncidentPerDriver(car.Driver); if (probHelper.DecideIfThereIsAnIncident("Driver")) { result.Time = TimeSpan.FromMilliseconds(-1); result.Description = "Accident"; } else { RaceResult driverResult = grandPrixResult.Where(x => x.Vehicle.Driver.Equals(car.Driver) && x.Time > TimeSpan.FromMilliseconds(0)).FirstOrDefault(); if (driverResult == null) { // Driver with less laps... get the time of the last with a time driverResult = lastWithTime; } double driverMillisecs = driverResult.Time.TotalMilliseconds * percents["Driver"]; result.Time = result.Time.Add(TimeSpan.FromMilliseconds(driverMillisecs)); foreach (var part in car.WearingParts) { IList<Vehicle> vehs = vehRepo.GetVehiclesWearingADesign(this.GetBaseDesign(part.Design)); probHelper.RegisterIncidentsPerDesign(part.Design, vehs); if (probHelper.DecideIfThereIsAnIncident(part.Design.PartDesignType.Name)) { result.Time = TimeSpan.FromMilliseconds(-1); result.Description = part.Design.PartDesignType.Name; break; } else { double millisecs = 0; int count = 0; foreach (var v in vehs) { RaceResult r = grandPrixResult.Where(x => x.Vehicle.Id == v.Id && x.Time > TimeSpan.FromMilliseconds(0)).FirstOrDefault(); if (r != null) { millisecs += r.Time.TotalMilliseconds * percents[part.Design.PartDesignType.Name]; count++; } } if (count > 0) { millisecs = millisecs / count; } else { // No reference time to make the calculation // Get the last driver with a time, and make the calculation on it millisecs = lastWithTime.Time.TotalMilliseconds * percents[part.Design.PartDesignType.Name]; } result.Time = result.Time.Add(TimeSpan.FromMilliseconds(millisecs)); } } } } if (result.Time < TimeSpan.FromMilliseconds(0)) { result.LapsNo = 0; } return result; }
/// <summary> /// Checks if the vehicle is ready to race (see if it has all the parts needed). /// </summary> /// <param name="vehicle">The vehicle to check.</param> /// <returns>True if it is ready; False if it is not.</returns> public bool CheckVehicleToRace(Vehicle vehicle) { if (vehicle == null) { throw new ArgumentNullException("vehicle"); } bool isReady = true; var designTypeRepo = this.container.Resolve<IPartDesignTypeRepository>(); IList<PartDesignType> designTypes = designTypeRepo.GetAll(); foreach (var item in designTypes) { Part thePart = vehicle.WearingParts.Where(x => x.Design.PartDesignType.Name == item.Name).FirstOrDefault(); if (thePart == null) { isReady = false; break; } } return isReady; }
/// <summary> /// Hires a driver. /// </summary> /// <param name="theDriver">The driver to hire.</param> /// <param name="theCar">The car to assign him.</param> public void HireDriver(Driver theDriver, Vehicle theCar) { if (theDriver == null) { throw new ArgumentNullException("theDriver"); } if (theCar == null) { throw new ArgumentNullException("theCar"); } if (theCar.Driver != null) { throw new InvalidOperationException("Car already has a driver."); } var carRepo = this.container.Resolve<IVehicleRepository>(); var uow = this.container.Resolve<IUnitOfWork>(); theCar.Driver = theDriver; carRepo.Update(theCar); uow.Commit(); }