Exemplo n.º 1
0
        public async Task SendAllTypesToAttack(BattleDTO battleDto)
        {
            foreach (UnitDTO unit in battleDto.Army)
            {
                UnitDTOValidator unitValidator    = new UnitDTOValidator();
                ValidationResult validatorResults = unitValidator.Validate(unit);

                if (!validatorResults.IsValid)
                {
                    foreach (var failure in validatorResults.Errors)
                    {
                        throw new HttpResponseException {
                                  Status = 400, Value = failure.ErrorMessage
                        };
                    }
                }
            }
            if (!battleDto.Army.Any(u => u.UnitTypeID == UnitData.General.ID))
            {
                throw new HttpResponseException {
                          Status = 400, Value = "A küldött csapat nem tartalmaz Hadvezért"
                }
            }
            ;

            var attackingCountry = await _context.Countries.FirstOrDefaultAsync(c => c.ID == battleDto.IdAtt);

            var defendingCountry = await _context.Countries.FirstOrDefaultAsync(c => c.ID == battleDto.IdDef);

            if (attackingCountry == null)
            {
                throw new HttpResponseException {
                          Status = 400, Value = "Támadó ország nem található"
                };
            }
            if (defendingCountry == null)
            {
                throw new HttpResponseException {
                          Status = 400, Value = "Védekező ország nem található"
                };
            }

            foreach (UnitDTO unitDto in battleDto.Army)
            {
                await SendUnitsOfTypeToAttack(attackingCountry, defendingCountry, unitDto.UnitCount, unitDto.UnitTypeID);
            }

            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task PurchaseCountryUnitsAsync(int countryId, List <UnitDTO> army)
        {
            foreach (UnitDTO unit in army)
            {
                UnitDTOValidator unitValidator    = new UnitDTOValidator();
                ValidationResult validatorResults = unitValidator.Validate(unit);

                if (!validatorResults.IsValid)
                {
                    foreach (var failure in validatorResults.Errors)
                    {
                        throw new HttpResponseException {
                                  Status = 400, Value = failure.ErrorMessage
                        };
                    }
                }
            }

            Country country = await _appDbContext.Countries
                              .Include(c => c.Resources).ThenInclude(r => r.ResourceData)
                              .Include(c => c.Units)
                              .SingleOrDefaultAsync(c => c.ID == countryId);

            var unitDatas = await _appDbContext.UnitData.ToListAsync();

            var totalCost  = new List <Resource>();
            var unitsToBuy = new List <Unit>();

            foreach (var unit in army)
            {
                var unitdata     = unitDatas.SingleOrDefault(u => u.ID == unit.UnitTypeID);
                var existingCost = totalCost.SingleOrDefault(c => c.ResourceDataID == unitdata.PriceUnitID);
                if (existingCost == null)
                {
                    totalCost.Add(new Resource {
                        Amount = unitdata.Price * unit.UnitCount, ResourceDataID = (int)unitdata.PriceUnitID
                    });
                }
                else
                {
                    existingCost.Amount += unitdata.Price * unit.UnitCount;
                }
                unitsToBuy.Add(new Unit()
                {
                    UnitDataID = unit.UnitTypeID, Count = unit.UnitCount, CountryID = countryId,
                });
            }
            foreach (var resource in country.Resources)
            {
                var cost = totalCost.SingleOrDefault(r => r.ResourceDataID == resource.ResourceDataID);
                if (cost == null)
                {
                    continue;
                }
                if (resource.Amount < cost.Amount)
                {
                    throw new HttpResponseException {
                              Status = 400, Value = "Nincs elég pénzed"
                    };
                }
            }

            int numberOfBoughtUnits = 0;

            foreach (var unit in unitsToBuy)
            {
                numberOfBoughtUnits += unit.Count;
            }
            var numberOfExistingUnits = country.Units.Sum(u => u.Count);

            if (numberOfBoughtUnits + numberOfExistingUnits > country.ArmyCapacity)
            {
                throw new HttpResponseException {
                          Status = 400, Value = "Nincs elég helyed a katonáknak"
                };
            }

            foreach (var unit in unitsToBuy)
            {
                var existingUnits = country.Units.SingleOrDefault(u => u.UnitDataID == unit.UnitDataID);
                if (existingUnits == null)
                {
                    country.Units.Add(unit);
                }
                else
                {
                    existingUnits.Count += unit.Count;
                }
            }

            foreach (var cost in totalCost)
            {
                country.Resources.SingleOrDefault(r => r.ResourceDataID == cost.ResourceDataID).Amount -= cost.Amount;
            }
            await _appDbContext.SaveChangesAsync();

            return;
        }