public async Task <Report> Insert(Report entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } _dbContext.Reports.Add(entity); await _dbContext.SaveChangesAsync(); _reportSender.CreateReport(new Messaging.ReportCommand() { Location = entity.Location }); return(entity); }
public async Task <MediatR.Unit> Handle(Command request, CancellationToken cancellationToken) { //Initialize the variables for the attack process var initValues = await InitAttackProcess(request.Request); AttackingTroops attackingTroops = initValues.attackingTroops; DefendingTroops defendingTroops = initValues.defendingTroops; IEnumerable <UnitsInCity> unitsOfAttacker = initValues.unitsOfAttacker; IEnumerable <UnitsInCity> defendingUnits = initValues.defendingUnits; int wallStage = initValues.wallStage; var infantryPhaseResult = new InfantryAttackPhaseBehaviour().Action(attackingTroops, defendingTroops, wallStage); //Add the survivors of the previous phase to the next one defendingTroops.AddSurvivorsOfPreviousPhase(infantryPhaseResult.defendingTroops, defendingTroops.CavalryPhaseDefendingUnits); attackingTroops.AddSurvivorsOfPreviousPhase(infantryPhaseResult.attackerTroops, attackingTroops.CavalryPhaseTroops); var cavalryPhaseResult = new CavalryAttackPhaseBehaviour().Action(attackingTroops, defendingTroops, wallStage); //Add the survivors of the previous phase to the next one defendingTroops.AddSurvivorsOfPreviousPhase(cavalryPhaseResult.defendingTroops, defendingTroops.ArcheryPhaseDefendingUnits); attackingTroops.AddSurvivorsOfPreviousPhase(cavalryPhaseResult.attackerTroops, attackingTroops.ArcheryPhaseTroops); var archeryPhaseResult = new ArcheryAttackPhaseBehaviour().Action(attackingTroops, defendingTroops, wallStage); //Update the attacking side foreach (var item in archeryPhaseResult.attackerTroops) { foreach (var unitsInCity in unitsOfAttacker) { if (unitsInCity.Unit.Name.Equals(item.Key.Name)) { var fallenSoldierAmount = (request.Request.AttackingForces.First(x => x.Key.Equals(item.Key.Name)).Value - item.Value); unitsInCity.Amount -= fallenSoldierAmount; initValues.attackerCity.Resources.Population += fallenSoldierAmount * item.Key.UnitCost.Population; } } } int initialWoodAmount = initValues.attackerCity.Resources.Wood; int initialStoneAmount = initValues.attackerCity.Resources.Stone; int initialSilverAmount = initValues.attackerCity.Resources.Silver; //Steal resources int totalCarryingCapacity = CalculateCarryingCapacity(archeryPhaseResult.attackerTroops); if (totalCarryingCapacity > 0) { ResourceStealingProcess(initValues.attackerCity, initValues.defenderCity, totalCarryingCapacity); CheckWarehouseCapacity(initValues.attackerCity); } int stolenWoodAmount = initValues.attackerCity.Resources.Wood - initialWoodAmount; int stolenStoneAmount = initValues.attackerCity.Resources.Stone - initialStoneAmount; int stolenSilverAmount = initValues.attackerCity.Resources.Silver - initialSilverAmount; await _reportSender.CreateReport(initValues.attackerName, initValues.attackerCity.CityName, initValues.defenderName, initValues.defenderCity.CityName, archeryPhaseResult.attackerTroops, archeryPhaseResult.defendingTroops, request.Request.AttackingForces, initValues.defendingUnits, stolenWoodAmount, stolenStoneAmount, stolenSilverAmount); //Update the defending side foreach (var item in archeryPhaseResult.defendingTroops) { var fallenSoldierAmount = defendingUnits.First(d => d.Unit.Name.Equals(item.Key.Name)).Amount - item.Value; initValues.defenderCity.Resources.Population += fallenSoldierAmount * item.Key.UnitCost.Population; defendingUnits.First(d => d.Unit.Name.Equals(item.Key.Name)).Amount = item.Value; } await _unitOfWork.CommitChangesAsync(); return(new MediatR.Unit()); }