Example #1
0
        private void RunWar(DeltaMetadata deltaMetadata, War war)
        {
            var borderView   = _empireRepository.GetEmpireBorderView(war.AttackerId, war.DefenderId);
            var attacker     = borderView.Empire;
            var defender     = borderView.BorderingEmpire;
            var combatResult = _militaryCalculator.Combat(attacker, defender);

            _warRepository.ContinueWar(
                deltaMetadata,
                war.Id,
                combatResult.AttackerDamage.MilitaryDamage,
                combatResult.DefenderDamage.MilitaryDamage);
            _economicSimulator.ApplyDamage(
                deltaMetadata,
                borderView,
                combatResult.AttackerDamage.CollateralDamage,
                combatResult.DefenderDamage.CollateralDamage);
            _empireRepository.ApplyMilitaryDamage(
                deltaMetadata,
                war.AttackerId,
                combatResult.DefenderDamage.MilitaryDamage);
            _empireRepository.ApplyMilitaryDamage(
                deltaMetadata,
                war.DefenderId,
                combatResult.AttackerDamage.MilitaryDamage);
            SystemTransfer(deltaMetadata, combatResult.TerritoryGain, borderView);
            TryEndWar(deltaMetadata, war.Id, combatResult.TerritoryGain, war.AttackerId, war.DefenderId);
        }
        public void RunEmpire(DeltaMetadata deltaMetadata, ObjectId empireId)
        {
            var empire = _empireRepository.GetEmpireSystemsView(empireId);

            ApplyEconomicGrowth(deltaMetadata, empire);
            ApplyMilitaryProduction(deltaMetadata, empire);
        }
Example #3
0
 private void RunForTick(Simulation simulation, DeltaMetadata deltaMetadata)
 {
     _militarySimulator.Run(deltaMetadata, simulation.Id);
     foreach (var id in simulation.EmpireIds)
     {
         _economicSimulator.RunEmpire(deltaMetadata, id);
         _diplomacySimulator.RunEmpire(deltaMetadata, id);
     }
 }
Example #4
0
        public void Run(DeltaMetadata deltaMetadata, ObjectId simulationId)
        {
            var wars = _warRepository.GetWars(simulationId);

            foreach (var war in wars)
            {
                RunWar(deltaMetadata, war);
            }
        }
        private void ApplyEconomicGrowth(DeltaMetadata deltaMetadata, EmpireSystemsView empire)
        {
            var growthValues = empire.StarSystems
                               .SelectMany(x => _developmentCalculator.GrowthFromSystem(x, empire))
                               .ToArray();
            var deltas = GetDeltasFromGrowthValues(growthValues, deltaMetadata);

            _starSystemRepository.ApplyDevelopment(deltas);
            ApplyDeltas(empire.StarSystems, deltas);
        }
Example #6
0
 public void EndWar(DeltaMetadata deltaMetadata, ObjectId warId)
 {
     _warCollection.UpdateOne(GetWarById(warId), DeactivateWar());
     _deltaCollection.InsertOne(new Delta
     {
         DeltaType    = DeltaType.WarEnd,
         SimulationId = deltaMetadata.SimulationId,
         Tick         = deltaMetadata.Tick,
         ReferenceId  = warId
     });
 }
Example #7
0
        public void RunForTicks(ObjectId simulationId, int ticks)
        {
            var simulation = _simulationRepository.GetSimulation(simulationId);

            for (var i = 0; i < ticks; i++)
            {
                var deltaMetadata = new DeltaMetadata(simulationId, simulation.Ticks + i + 1);
                RunForTick(simulation, deltaMetadata);
                _simulationRepository.IncrementTick(simulationId);
            }
        }
 public void TransferSystems(DeltaMetadata deltaMetadata, ObjectId senderId, ObjectId receiverId, IEnumerable<ObjectId> systemIds)
 {
     if (!systemIds.Any())
     {
         return;
     }
     
     _empireCollection.UpdateOne(GetEmpireById(senderId), RemoveSystems(systemIds));
     _empireCollection.UpdateOne(GetEmpireById(receiverId), AddSystems(systemIds));
     CreateDeltas(deltaMetadata, senderId, receiverId, systemIds);
 }
Example #9
0
 private void TryEndWar(DeltaMetadata deltaMetadata, ObjectId warId, TerritoryGain territoryGain, ObjectId attackerId, ObjectId defenderId)
 {
     if (territoryGain == TerritoryGain.Attacker && AllSystemsLost(defenderId))
     {
         _warRepository.EndWarsWithParticipant(deltaMetadata, defenderId);
         _empireRepository.EmpireDefeated(deltaMetadata, defenderId);
     }
     else if (territoryGain == TerritoryGain.Defender && AllSystemsLost(attackerId))
     {
         _warRepository.EndWarsWithParticipant(deltaMetadata, attackerId);
         _empireRepository.EmpireDefeated(deltaMetadata, attackerId);
     }
 }
        public void ApplyMilitaryProduction(DeltaMetadata deltaMetadata, ObjectId empireId, double increase)
        {
            _empireCollection.UpdateOne(GetEmpireById(empireId), ChangeMilitary(increase));
            var delta = new Delta<double>
            {
                DeltaType = DeltaType.EmpireMilitary,
                SimulationId = deltaMetadata.SimulationId,
                Tick = deltaMetadata.Tick,
                ReferenceId = empireId,
                Value = increase
            };
            _deltaDoubleCollection.InsertOne(delta);

        }
Example #11
0
        public void EndWarsWithParticipant(DeltaMetadata deltaMetadata, ObjectId empireId)
        {
            var wars = _warCollection.Find(GetActiveWarWithParticipant(empireId)).ToList();

            if (!wars.Any())
            {
                return;
            }

            _warCollection.UpdateMany(GetActiveWarWithParticipant(empireId), DeactivateWar());
            _deltaCollection.InsertMany(wars.Select(war => new Delta
            {
                DeltaType    = DeltaType.WarEnd,
                SimulationId = deltaMetadata.SimulationId,
                Tick         = deltaMetadata.Tick,
                ReferenceId  = war.Id
            }));
        }
Example #12
0
 private void SystemTransfer(DeltaMetadata deltaMetadata, TerritoryGain territory, EmpireBorderView empireBorderView)
 {
     if (territory == TerritoryGain.Attacker)
     {
         _empireRepository.TransferSystems(
             deltaMetadata,
             empireBorderView.BorderingEmpire.Id,
             empireBorderView.Empire.Id,
             empireBorderView.BorderingEmpireSystems.Select(x => x.Id));
     }
     else if (territory == TerritoryGain.Defender)
     {
         _empireRepository.TransferSystems(
             deltaMetadata,
             empireBorderView.Empire.Id,
             empireBorderView.BorderingEmpire.Id,
             empireBorderView.EmpireSystems.Select(x => x.Id));
     }
 }
        public void RunEmpire(DeltaMetadata deltaMetadata, ObjectId empireId)
        {
            var existingWars = _warRepository.GetWarsForEmpire(empireId);

            if (existingWars.Any())
            {
                return;
            }

            var borderingEmpires = _empireRepository.GetEmpireBorderViews(empireId);

            foreach (var borderEmpire in borderingEmpires)
            {
                if (_diplomacyCalculator.DeclareWar(borderEmpire.Empire, borderEmpire.BorderingEmpire))
                {
                    _warRepository.BeginWar(deltaMetadata, empireId, borderEmpire.BorderingEmpire.Id);
                    break;
                }
            }
        }
 private void CreateDeltas(DeltaMetadata deltaMetadata, ObjectId senderId, ObjectId receiverId, IEnumerable<ObjectId> systemIds)
 {
     var addDeltas = systemIds.Select(x => new Delta<ObjectId>
     {
         DeltaType = DeltaType.EmpireSystemGain,
         SimulationId = deltaMetadata.SimulationId,
         Tick = deltaMetadata.Tick,
         ReferenceId = receiverId,
         Value = x
     });
     var removeDeltas = systemIds.Select(x => new Delta<ObjectId>
     {
         DeltaType = DeltaType.EmpireSystemLoss,
         SimulationId = deltaMetadata.SimulationId,
         Tick = deltaMetadata.Tick,
         ReferenceId = senderId,
         Value = x
     });
     _deltaObjectCollection.InsertMany(addDeltas.Concat(removeDeltas));
 }
        public void ApplyDamage(DeltaMetadata deltaMetadata, EmpireBorderView empireBorderView, double empireDamage, double borderingEmpireDamage)
        {
            var empireDeltas = empireBorderView.EmpireSystems.Select(x => new Delta <double>
            {
                DeltaType    = DeltaType.SystemDevelopment,
                SimulationId = deltaMetadata.SimulationId,
                Tick         = deltaMetadata.Tick,
                ReferenceId  = x.Id,
                Value        = empireDamage
            });
            var borderingDeltas = empireBorderView.BorderingEmpireSystems.Select(x => new Delta <double>
            {
                DeltaType    = DeltaType.SystemDevelopment,
                SimulationId = deltaMetadata.SimulationId,
                Tick         = deltaMetadata.Tick,
                ReferenceId  = x.Id,
                Value        = borderingEmpireDamage
            });

            _starSystemRepository.ApplyDamage(empireDeltas.Concat(borderingDeltas));
        }
Example #16
0
        public ObjectId BeginWar(DeltaMetadata deltaMetadata, ObjectId attackerId, ObjectId defenderId)
        {
            var war = new War
            {
                AttackerId = attackerId,
                DefenderId = defenderId,
                Active     = true
            };

            _warCollection.InsertOne(war);
            _simulationCollection.UpdateOne(GetSimulationById(deltaMetadata.SimulationId), AddWarToSimulation(war.Id));
            _deltaCollection.InsertOne(new Delta
            {
                DeltaType    = DeltaType.WarBegin,
                SimulationId = deltaMetadata.SimulationId,
                Tick         = deltaMetadata.Tick,
                ReferenceId  = war.Id
            });

            return(war.Id);
        }
Example #17
0
 public void ContinueWar(DeltaMetadata deltaMetadata, ObjectId warId, double attackerDamage, double defenderDamage)
 {
     _warCollection.UpdateOne(GetWarById(warId), TakeTurn(attackerDamage, defenderDamage));
     _deltaDoubleCollection.InsertMany(new Delta <double>[]
     {
         new Delta <double>
         {
             DeltaType    = DeltaType.WarAttackerDamage,
             SimulationId = deltaMetadata.SimulationId,
             Tick         = deltaMetadata.Tick,
             ReferenceId  = warId,
             Value        = attackerDamage
         },
         new Delta <double>
         {
             DeltaType    = DeltaType.WarDefenderDamage,
             SimulationId = deltaMetadata.SimulationId,
             Tick         = deltaMetadata.Tick,
             ReferenceId  = warId,
             Value        = defenderDamage
         }
     });
 }
        private void ApplyMilitaryProduction(DeltaMetadata deltaMetadata, EmpireSystemsView empire)
        {
            var production = _militaryCalculator.ProductionForEmpire(empire);

            _empireRepository.ApplyMilitaryProduction(deltaMetadata, empire.Empire.Id, production);
        }
 public void EmpireDefeated(DeltaMetadata deltaMetadata, ObjectId empireId)
 {
     var empire = _empireCollection.Find(GetEmpireById(empireId)).Single();
     ApplyMilitaryDamage(deltaMetadata, empireId, empire.Military);
 }
 private IEnumerable <Delta <double> > GetDeltasFromGrowthValues(IEnumerable <GrowthFromSystemResult> values, DeltaMetadata metadata) =>
 values.Select(x => new Delta <double>
 {
     DeltaType    = DeltaType.SystemDevelopment,
     SimulationId = metadata.SimulationId,
     Tick         = metadata.Tick,
     Value        = x.Growth,
     ReferenceId  = x.SystemId
 });
 public void ApplyMilitaryDamage(DeltaMetadata deltaMetadata, ObjectId empireId, double damage)
 {
     var empire = _empireCollection.Find(GetEmpireById(empireId)).Single();
     damage = Math.Min(damage, empire.Military);
     ApplyMilitaryProduction(deltaMetadata, empireId, -damage);
 }