private static Influence MakeInfluence(DatabaseContext context, List <KeyValuePair <int, Religion> > religions, int?id) { var influence = InfluenceCalc.zero; if (id.HasValue) { var effectEntity = context.Effects.Find(id); var influenceEntities = context.Influences.Where(x => x.EffectId == effectEntity.Id).ToList(); influence = influenceEntities.Select(x => InfluenceCalc.create(x.ReligionId.HasValue ? religions.Single(y => y.Key == x.ReligionId).Value : null, x.Value)).Aggregate(InfluenceCalc.zero, InfluenceCalc.add); } return(influence); }
public World() { using (var context = new DatabaseContext()) { var resources = new List <KeyValuePair <int, Resource> >(); foreach (var resourceEntity in context.Resources.ToList()) { resources.Add(new KeyValuePair <int, Resource>(resourceEntity.Id, new Resource(resourceEntity.Name))); } var weathers = new List <KeyValuePair <int, Weather> >(); foreach (var weatherEntity in context.Weathers.OrderBy(x => x.Order).ToList()) { weathers.Add(new KeyValuePair <int, Weather>(weatherEntity.Id, new Weather(weatherEntity.Name))); } var seasons = new List <KeyValuePair <int, Season> >(); foreach (var seasonEntity in context.Seasons.OrderBy(x => x.Order).ToList()) { seasons.Add(new KeyValuePair <int, Season>(seasonEntity.Id, new Season(seasonEntity.Name))); } var climates = new List <KeyValuePair <int, Climate> >(); foreach (var climateEntity in context.Climates.ToList()) { var weatherEffects = new List <Tuple <Season, IEnumerable <Tuple <Weather, Effect> > > >(); foreach (var season in seasons) { var entry = new List <Tuple <Weather, Effect> >(); foreach (var weather in weathers) { var weatherEffectEntity = context.WeatherEffects.SingleOrDefault(x => x.SeasonId == season.Key && x.ClimateId == climateEntity.Id && x.WeatherId == weather.Key); var effect = MakeEffect(context, weatherEffectEntity?.EffectId); entry.Add(Tuple.Create(weather.Value, effect)); } weatherEffects.Add(Tuple.Create(season.Value, entry.AsEnumerable())); } climates.Add(new KeyValuePair <int, Climate>(climateEntity.Id, ClimateCalc.create(weatherEffects))); } var religions = new List <KeyValuePair <int, Religion> >(); foreach (var religionEntity in context.Religions.ToList()) { Effect effect = default; int influence = default; if (religionEntity.EffectId.HasValue) { var effectEntity = context.Effects.Find(religionEntity.EffectId.Value); var bonusEntities = context.Bonuses.Where(x => x.EffectId == effectEntity.Id).ToList(); var influenceEntities = context.Influences.Where(x => x.EffectId == effectEntity.Id).ToList(); if (influenceEntities.Any(x => x.ReligionId.HasValue)) { throw new DomainRuleViolationException("Influence of a religion with a set religion id."); } var bonus = bonusEntities.Select(x => IncomeCalc.create(x.Value, x.Category, x.Type)).Aggregate(IncomeCalc.zero, IncomeCalc.add); effect = new Effect(effectEntity.PublicOrder, effectEntity.RegularFood, effectEntity.FertilityDependentFood, effectEntity.ProvincialSanitation, effectEntity.ResearchRate, effectEntity.Growth, effectEntity.Fertility, effectEntity.ReligiousOsmosis, 0, bonus); influence = influenceEntities.Select(x => x.Value).Aggregate(0, (x, y) => x + y); } religions.Add(new KeyValuePair <int, Religion>(religionEntity.Id, new Religion(religionEntity.Name, effect, influence))); } var provinces = new List <KeyValuePair <int, Province> >(); foreach (var provinceEntity in context.Provinces.ToList()) { var effect = MakeEffect(context, provinceEntity.EffectId); var influence = MakeInfluence(context, religions, provinceEntity.EffectId); var regions = new List <Region>(); foreach (var regionEntity in context.Regions.Where(x => x.ProvinceId == provinceEntity.Id).ToList()) { regions.Add(RegionCalc.create(regionEntity.Name, regionEntity.RegionType, regionEntity.IsCoastal, regionEntity.ResourceId.HasValue ? resources.Single(x => x.Key == regionEntity.ResourceId).Value : null, regionEntity.SlotsCountOffset == -1)); } provinces.Add(new KeyValuePair <int, Province>(provinceEntity.Id, ProvinceCalc.create(provinceEntity.Name, regions, climates.Single(x => x.Key == provinceEntity.ClimateId).Value, effect, influence, null, null, null)));;; } var buildings = new List <KeyValuePair <int, Tuple <BuildingLevel, int?> > >(); foreach (var buildingLevelEntity in context.BuildingLevels) { var effect = MakeEffect(context, buildingLevelEntity.EffectId); var influence = MakeInfluence(context, religions, buildingLevelEntity.EffectId); buildings.Add(new KeyValuePair <int, Tuple <BuildingLevel, int?> >(buildingLevelEntity.Id, Tuple.Create(new BuildingLevel(buildingLevelEntity.Name, effect, influence), buildingLevelEntity.ParentBuildingLevelId))); } var branches = new List <KeyValuePair <int, BuildingBranch> >(); foreach (var branchEntity in context.BuildingBranches) { var strains = new List <IEnumerable <BuildingLevel> >(); void RecursiveBranchTraversing(IEnumerable <BuildingLevel> ancestors, KeyValuePair <int, Tuple <BuildingLevel, int?> > current) { var branchLevels = ancestors.Append(current.Value.Item1); var children = buildings.Where(x => x.Value.Item2 == current.Key); if (children.Any()) { foreach (var child in children) { RecursiveBranchTraversing(branchLevels, child); } } else { strains.Add(branchLevels); } } RecursiveBranchTraversing(new List <BuildingLevel>(), buildings.Single(x => x.Key == branchEntity.RootBuildingLevelId)); if (branchEntity.AllowParallel) { foreach (var branchLevels in strains) { branches.Add(new KeyValuePair <int, BuildingBranch>(branchEntity.Id, BuildingBranchCalc.create(branchEntity.SlotType, branchEntity.RegionType, resources.SingleOrDefault(x => x.Key == branchEntity.ResourceId).Value, religions.SingleOrDefault(x => x.Key == branchEntity.ReligionId).Value, branchLevels))); } } else { var branchLevels = strains.SelectMany(x => x).Distinct(); branches.Add(new KeyValuePair <int, BuildingBranch>(branchEntity.Id, BuildingBranchCalc.create(branchEntity.SlotType, branchEntity.RegionType, resources.SingleOrDefault(x => x.Key == branchEntity.ResourceId).Value, religions.SingleOrDefault(x => x.Key == branchEntity.ReligionId).Value, branchLevels))); } } var factions = new List <KeyValuePair <int, Faction> >(); foreach (var factionEntity in context.Factions.ToList()) { var effect = MakeEffect(context, factionEntity.EffectId); var influence = MakeInfluence(context, religions, factionEntity.EffectId); var techs = new List <TechnologyTier>(); var universalEffect = EffectCalc.zero; var antilegacyEffect = EffectCalc.zero; var universalInfluence = default(Influence); var antilegacyInfluence = default(Influence); var universalLocks = new List <BuildingLevel>(); var universalUnlocks = new List <BuildingLevel>(); var antilegacyLocks = new List <BuildingLevel>(); var antilegacyUnlocks = new List <BuildingLevel>(); foreach (var techEntity in context.TechnologyLevels.Where(x => x.FactionId == factionEntity.Id).OrderBy(x => x.Order).ToList()) { universalEffect = EffectCalc.add(universalEffect, MakeEffect(context, techEntity.UniversalEffectId)); antilegacyEffect = EffectCalc.add(antilegacyEffect, MakeEffect(context, techEntity.AntilegacyEffectId)); universalInfluence = InfluenceCalc.add(universalInfluence, MakeInfluence(context, religions, techEntity.UniversalEffectId)); antilegacyInfluence = InfluenceCalc.add(antilegacyInfluence, MakeInfluence(context, religions, techEntity.AntilegacyEffectId)); var universalLocksIds = context.BuildingLevelLocks.Where(y => y.TechnologyLevelId == techEntity.Id && !y.Antilegacy && y.Lock).Select(x => x.BuildingLevelId).ToList(); var universalUnlocksIds = context.BuildingLevelLocks.Where(y => y.TechnologyLevelId == techEntity.Id && !y.Antilegacy && !y.Lock).Select(x => x.BuildingLevelId).ToList(); var antilegacyLocksIds = context.BuildingLevelLocks.Where(y => y.TechnologyLevelId == techEntity.Id && y.Antilegacy && y.Lock).Select(x => x.BuildingLevelId).ToList(); var antilegacyUnlocksIds = context.BuildingLevelLocks.Where(y => y.TechnologyLevelId == techEntity.Id && y.Antilegacy && !y.Lock).Select(x => x.BuildingLevelId).ToList(); universalLocks.AddRange(buildings.Where(x => universalLocksIds.Contains(x.Key)).Select(x => x.Value.Item1)); universalUnlocks.AddRange(buildings.Where(x => universalUnlocksIds.Contains(x.Key)).Select(x => x.Value.Item1)); antilegacyLocks.AddRange(buildings.Where(x => antilegacyLocksIds.Contains(x.Key)).Select(x => x.Value.Item1)); antilegacyUnlocks.AddRange(buildings.Where(x => antilegacyUnlocksIds.Contains(x.Key)).Select(x => x.Value.Item1)); techs.Add(TechnologyTierCalc.create(universalEffect, antilegacyEffect, universalInfluence, antilegacyInfluence, universalLocks, universalUnlocks, antilegacyLocks, antilegacyUnlocks)); } var factionBranches = new List <BuildingBranch>(); foreach (var useEntity in context.BuildingBranchUses.Where(x => x.FactionId == factionEntity.Id)) { var usedBranches = branches.Where(x => useEntity.BuildingBranchId == x.Key); factionBranches.AddRange(usedBranches.Select(x => x.Value)); } factions.Add(new KeyValuePair <int, Faction>(factionEntity.Id, FactionCalc.create(factionEntity.Name, techs, factionBranches, effect, influence))); } this.Religions = religions.Select(x => x.Value); this.Provinces = provinces.Select(x => x.Value); this.Factions = factions.Select(x => x.Value); this.Weathers = weathers.Select(x => x.Value); this.Seasons = seasons.Select(x => x.Value); } }