コード例 #1
0
ファイル: TeamServices.cs プロジェクト: masuar/BRIATORES
        /// <summary>
        /// Creates a new empty team.
        /// </summary>
        /// <param name="managerName">Name of the manager.</param>
        /// <param name="countryName">Name of the country.</param>
        /// <param name="teamName">Name of the team.</param>
        /// <param name="teamShortName">Short name of the team.</param>
        public void CreateEmptyTeam(string managerName, string countryName, string teamName, string teamShortName)
        {
            if (string.IsNullOrEmpty(managerName))
            {
                throw new ArgumentNullException("managerName");
            }

            if (string.IsNullOrEmpty(countryName))
            {
                throw new ArgumentNullException("countryName");
            }

            if (string.IsNullOrEmpty(teamName))
            {
                throw new ArgumentNullException("teamName");
            }

            if (string.IsNullOrEmpty(teamShortName))
            {
                throw new ArgumentNullException("teamShortName");
            }

            var playerRepo = this.container.Resolve<IGenericRepository<Player>>();
            var countryRepo = this.container.Resolve<ICountryRepository>();
            var teamRepo = this.container.Resolve<ITeamRepository>();
            var uow = this.container.Resolve<IUnitOfWork>();

            // Check the country
            Country theCountry = countryRepo.GetByName(countryName);
            if (theCountry == null)
            {
                throw new InvalidOperationException("Country not exists.");
            }

            // TODO: Unique constraints in team name, team short name, and player name...
            Player thePlayer = new Player() { Name = managerName, Country = theCountry };
            playerRepo.Save(thePlayer);

            Team theTeam = new Team() { Manager = thePlayer, Name = teamName, ShortName = teamShortName, IsPlayable = true };
            theTeam.Vehicles.Add(new Vehicle() { Team = theTeam, VehicleType = EVehicleType.FirstVehicle });
            theTeam.Vehicles.Add(new Vehicle() { Team = theTeam, VehicleType = EVehicleType.SecondVehicle });
            teamRepo.Save(theTeam);

            uow.Commit();
        }
コード例 #2
0
ファイル: DataInitializator.cs プロジェクト: masuar/BRIATORES
        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);
                }
            }
        }