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); } }
public async Task InitPlayer(User user, string cityName) { var game = await _dbContext.Games.Include(g => g.Players).OrderByDescending(g => g.Id).FirstOrDefaultAsync(); if (game == null) { //If there aren't any game being played currently , then create a new game. game = new Game { FirstConnectedPlayersTS = DateTime.Now, Turn = 0, Players = new List <Player>() }; } var incident = await _dbContext.Incidents.Where(i => i.Type == IncidentTypes.NothingHappened).SingleOrDefaultAsync(); Player player = new Player { UserId = user.Id, Points = 0, GameId = game.Id, IncidentId = incident.Id }; game.Players.Add(player); var newPlayer = await InsertPlayer(player); City city = new City { Name = cityName, PlayerId = newPlayer.Id }; await _cityService.InsertCity(city, newPlayer); Army army = new Army { PlayerId = newPlayer.Id, ScoutXP = 0, AttackXP = 0, DefenseXP = 0 }; var newArmy = await _armyService.InsertArmy(army, newPlayer); Stock stock = new Stock { PlayerId = newPlayer.Id, CoralAmount = 0, PearlAmount = 0 }; await _stockService.InsertStock(stock, newPlayer); await _stockService.GiveInitialMoneyAsync(stock); Laboratory laboratory = new Laboratory { PlayerId = newPlayer.Id }; await _laboratoryService.InsertLaboratory(laboratory, newPlayer); Squad squad = new Squad { ArmyId = newArmy.Id }; await _squadService.InsertSquad(squad, newArmy, city.Id); }