示例#1
0
        public async Task <IHttpActionResult> Accepted(string id, GameAcceptedModel model)
        {
            GameModel game = await _gameService.GetByIdAsync(id);

            if (game == null)
            {
                return(NotFound());
            }
            if (game.Status != GameStatus.Requesting)
            {
                return(BadRequest("Game status must be requesting"));
            }
            // get castles
            List <PositionModel> curList = new List <PositionModel>();
            List <PositionModel> result  = await GetNearByPlaces(curList, game.Position.Lat, game.Position.Lng, string.Empty);

            if (result == null)
            {
                return(InternalServerError(new Exception("Can not contact to map service")));
            }
            if (result.Count < 20)
            {
                curList = new List <PositionModel>();
                result  = await GetNearByPlaces(curList, game.Position.Lat, game.Position.Lng, string.Empty, 0, 1000);
            }
            if (result.Count < 20)
            {
                return(InternalServerError(new Exception("Error.NotEnoughVincityPlaceToPlay")));
            }



            var opponentArmySetting = game.SelfPlaying
                ? await GetOpponentArmySettingForSelfPlayingGame(game) : model.ArmySetting;

            await _gameService.SetOpponentArmySettingAsync(id, opponentArmySetting);

            game.OpponentArmySetting = opponentArmySetting;
            var castlesData = game.SelfPlaying ?
                              await GenerateCastleForSelfPlayingGame(result, game) :
                              await GenerateCastleForNormalGame(result, game);

            // get user info & accept game
            string userId = User.Identity.GetUserId();

            if (game.CreatedBy != userId)
            {
                UserDto opponent = await _userService.GetByIdAsync(userId);

                if (opponent == null)
                {
                    return(Unauthorized());
                }
                if (opponent.Heroes == null || opponent.Heroes.Count == 0)
                {
                    return(NotFound());
                }
                game.OpponentId     = opponent.Id;
                game.OpponentHeroId = opponent.Heroes[0].Id;
                await _gameService.AcceptedAsync(id, userId, game.OpponentHeroId);
            }
            else
            {
                if (!game.SelfPlaying)
                {
                    return(BadRequest("This game is not self-playing mode"));
                }
                await _gameService.AcceptedSelfPlayingGameAsync(id);
            }
            // save castle
            await _gameService.GenerateCastlesAsync(id, castlesData);

            return(Ok(game.Id));
        }