Beispiel #1
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var city = await GetCityByCityIndex(request.Request.CityIndex, _identityContext.UserId);

                var producibleUnits = await _unitOfWork.Units.GetProducibleUnitTypes(city.Barrack.Stage);

                (bool Producible, BackEnd.Models.Models.Unit Type)unitCheck
                    = CheckIfUnitIsProducible(producibleUnits, request.Request.NameOfUnitType);

                if (unitCheck.Producible == false)
                {
                    throw new NotFoundException();
                }

                var productionCost = new BackEnd.Models.Models.Resources
                {
                    Population = request.Request.Amount * unitCheck.Type.UnitCost.Population,
                    Silver     = request.Request.Amount * unitCheck.Type.UnitCost.Silver,
                    Stone      = request.Request.Amount * unitCheck.Type.UnitCost.Stone,
                    Wood       = request.Request.Amount * unitCheck.Type.UnitCost.Wood
                };

                if (CheckIfCityHasEnoughResources(city, productionCost) == false)
                {
                    throw new BadRequestException("The city does not have enough resources");
                }

                PayTheCost(city, productionCost);



                var unitsOfThisTypeInCity = await _unitOfWork.Units.GetUnitsInCity(unitCheck.Type.Id, city.Barrack.Id);



                //Create a new entry in the db
                if (unitsOfThisTypeInCity == null)
                {
                    await _unitOfWork.Units.InsertNewEntryToUnitsInCity(new BackEnd.Models.Models.UnitsInCity
                    {
                        Barrack   = city.Barrack,
                        BarrackId = city.Barrack.Id,
                        Unit      = unitCheck.Type,
                        UnitId    = unitCheck.Type.Id,
                        Amount    = request.Request.Amount
                    });
                }
                //Increase the amount in the city
                else
                {
                    unitsOfThisTypeInCity.Amount += request.Request.Amount;
                }

                await _unitOfWork.CommitChangesAsync();

                return(new Unit());
            }
Beispiel #2
0
            public async Task <DateTime> Handle(Command request, CancellationToken cancellationToken)
            {
                var city = await GetCityByCityIndex(request.Request.CityIndex, request.IdentityContext.UserId);

                var producibleUnits = await _unitOfWork.Units.GetProducibleUnitTypes(city.Barrack.Stage);

                BackEnd.Models.Models.Unit unit
                    = CheckIfUnitIsProducible(producibleUnits, request.Request.NameOfUnitType);

                if (unit == null)
                {
                    throw new NotFoundException();
                }

                var productionCost = new BackEnd.Models.Models.Resources
                {
                    Population = request.Request.Amount * unit.UnitCost.Population,
                    Silver     = request.Request.Amount * unit.UnitCost.Silver,
                    Stone      = request.Request.Amount * unit.UnitCost.Stone,
                    Wood       = request.Request.Amount * unit.UnitCost.Wood
                };

                if (CheckIfCityHasEnoughResources(city, productionCost) == false)
                {
                    throw new BadRequestException("The city does not have enough resources");
                }

                PayTheCost(city, productionCost);

                var finishTime = await _unitOfWork.HangFire.GetUnitFinishTime(request.IdentityContext.UserId);

                var recruitTime = new TimeSpan(0, 0, (int)Math.Ceiling(unit.RecruitTime.TotalSeconds) * request.Request.Amount);

                var newFinishTime = finishTime.Add(recruitTime);

                await _unitOfWork.HangFire.AddNewUnitProductionJob(request.IdentityContext.UserId, finishTime, newFinishTime,
                                                                   request.Request.NameOfUnitType, request.Request.Amount, request.Request.CityIndex);

                await _unitOfWork.CommitChangesAsync();

                return(newFinishTime);
            }