Exemplo n.º 1
0
        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);
        }