Exemplo n.º 1
0
        public async Task AttackCity(int userId, StartAttackDto startAttackDto)
        {
            var player = await _dbContext.Players
                         .Include(p => p.City)
                         .ThenInclude(c => c.CityBuildings)
                         .ThenInclude(cb => cb.Building)
                         .Include(p => p.Army)
                         .ThenInclude(a => a.Squads)
                         .ThenInclude(s => s.SquadUnits)
                         .ThenInclude(su => su.Unit)
                         .Where(p => p.UserId == userId)
                         .SingleOrDefaultAsync();

            var defendingSquad = player.Army.Squads
                                 .Where(s => s.CityId == player.City.Id)
                                 .SingleOrDefault();

            foreach (var squadUnit in defendingSquad.SquadUnits)
            {
                var unit = startAttackDto.AttackingUnits.SquadUnits.Where(s => s.Name == squadUnit.Unit.Type).SingleOrDefault();
                if (unit == null)
                {
                    throw new GeneralGameException($"Adj értéket a {squadUnit.Unit.Name} egységnek!");
                }
                if (squadUnit.NumberOfUnits < unit.NumberOfUnits)
                {
                    if (squadUnit.Unit.Type == UnitTypes.Warlord)
                    {
                        throw new GeneralGameException("Nincs hadvezéred, hogy vezesse a támadást!");
                    }
                    else
                    {
                        throw new GeneralGameException("Nem támadhatsz több egységgel, mint amennyid van!");
                    }
                }
                if (squadUnit.Unit.Type == UnitTypes.Warlord)
                {
                    if (unit.NumberOfUnits != 1)
                    {
                        throw new GeneralGameException("Pontosan egy hadvezért küldj egy támadó sereggel!");
                    }
                }
            }

            var squad = new Squad {
                ArmyId = player.Army.Id
            };
            var newSquad = await _squadService.InsertSquad(squad, player.Army, startAttackDto.CityId);

            foreach (var squadUnit in startAttackDto.AttackingUnits.SquadUnits)
            {
                await _squadService.UpdateUnitTypeNumber(newSquad, squadUnit.Name, squadUnit.NumberOfUnits);

                await _squadService.DecreaseUnitTypeNumber(defendingSquad, squadUnit.Name, squadUnit.NumberOfUnits);
            }
        }