Example #1
0
        public Task CreateAsync(CountryCreateEditViewModel countryView)
        {
            Country country = new Country(countryView);

            _context.Countries.Add(country);

            CountryBuilding farm = new CountryBuilding
            {
                CountryId  = country.Id,
                Country    = country,
                BuildingId = 1,
                Count      = 0
            };
            CountryBuilding barrack = new CountryBuilding
            {
                CountryId  = country.Id,
                Country    = country,
                BuildingId = 2,
                Count      = 0
            };

            _context.CountryBuildings.Add(farm);
            _context.CountryBuildings.Add(barrack);

            Game game = _context.Games.FirstOrDefault();

            game.Countries.Add(country);
            _context.Games.Update(game);

            return(_context.SaveChangesAsync());
        }
Example #2
0
        /// <summary>
        /// Checks all in-progress buildings and researches of the country,
        /// and adds any completed ones to it. Does not delete in progress values that are completed.
        /// This method does not perform any safety check regarding the amount of buildings or researches!
        /// </summary>
        /// <param name="country">The country to build in. The buildings, researches,
        /// in progress buildings, researches, and their buildings and researches, must be included.</param>
        /// <returns>If the building could be started.</returns>
        protected void CheckAddCompleted(Model.Entities.Country country, UnderSeaDatabaseContext context)
        {
            var researches = country.InProgressResearches
                             .Where(r => r.TimeLeft == 0)
                             .GroupBy(r => r.Research)
                             .ToList();

            if (researches.Count > 0)
            {
                foreach (var research in researches)
                {
                    var existing = country.Researches.FirstOrDefault(r => r.Research.Equals(research.Key));

                    // Add a new research, or update an existing one
                    if (existing == null)
                    {
                        var res = new CountryResearch
                        {
                            ParentCountry = country,
                            Research      = research.Key,
                            Count         = research.Count()
                        };
                        country.Researches.Add(res);
                        context.CountryResearches.Add(res);
                    }
                    else
                    {
                        existing.Count += research.Count();
                    }
                }
            }

            // Get and complete buildings
            var buildings = country.InProgressBuildings
                            .Where(r => r.TimeLeft == 0)
                            .GroupBy(b => b.Building)
                            .ToList();

            if (buildings.Count > 0)
            {
                foreach (var building in buildings)
                {
                    var existing = country.Buildings.FirstOrDefault(b => b.Building.Equals(building.Key));

                    // Add a new building, or update an existing one
                    if (existing == null)
                    {
                        var bld = new CountryBuilding()
                        {
                            ParentCountry = country,
                            Building      = building.Key,
                            Count         = building.Count()
                        };
                        country.Buildings.Add(bld);
                        context.CountryBuildings.Add(bld);
                    }
                    else
                    {
                        existing.Count += building.Count();
                    }
                }
            }
        }