Exemplo n.º 1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            _pokemonService = context.HttpContext.RequestServices.GetService <IPokemonService>();

            if (context.ActionArguments.Values.FirstOrDefault() is PokemonForCreationDto)
            {
                var dto = new PokemonForCreationDto();
                dto = context.ActionArguments.Values.Cast <PokemonForCreationDto>().FirstOrDefault();

                if (dto != null)
                {
                    if (string.IsNullOrEmpty(dto.ImageUrl))
                    {
                        context.ModelState.AddModelError(nameof(dto.ImageUrl), "Image is required.");
                    }

                    if (!_pokemonService.NameIsUnique(dto.Name))
                    {
                        context.ModelState.AddModelError(nameof(dto.Name), "Name is not unique.");
                    }
                }
            }

            if (!context.ModelState.IsValid)
            {
                context.Result = new ValidationFailedResult(context.ModelState);
            }
        }
Exemplo n.º 2
0
        public async Task Catch()
        {
            var cwp = _provider.GetRequiredService <CurrentWanderingPokemon>();

            if (cwp._isCaptured)
            {
                await ReplyAsync("There aren't any pokemon around right now. Watch out for the next one!");

                return;
            }

            var user = Context.Message.Author;

            if (!(await _userController.UserExists(user.Id)))
            {
                await RegisterUser(user);
            }

            PokemonForCreationDto pokemonForCreation = CreatePokemon(cwp._pokemon);

            cwp.SetIsCaptured(true);

            await _userController.AddToUserPokeCollectionByDiscordId(user.Id, pokemonForCreation);

            await ReplyAsync($":medal: Congratulations, {user.Username}! You've successfully caught a `{cwp._pokemon.Name}`.\n\nType `!inventory` to see it in your inventory!");
        }
Exemplo n.º 3
0
        private PokemonForCreationDto CreatePokemon(PokemonDataForReturnDto pokemonDataForReturn)
        {
            //Get a random set of moves for our pokemon
            int[] moveIds = GetRandomMoveIds(pokemonDataForReturn.MoveLinks);
            //Create our pokemon:
            PokemonForCreationDto pokemonForCreation = new PokemonForCreationDto
            {
                PokeId          = pokemonDataForReturn.PokeId,
                Name            = pokemonDataForReturn.Name,
                MaxHP           = pokemonDataForReturn.MaxHP,
                Level           = pokemonDataForReturn.Level,
                Base_Experience = pokemonDataForReturn.Base_Experience,
                Experience      = pokemonDataForReturn.Experience,
                Attack          = pokemonDataForReturn.Attack,
                Defense         = pokemonDataForReturn.Defense,
                SpecialAttack   = pokemonDataForReturn.SpecialAttack,
                SpecialDefense  = pokemonDataForReturn.SpecialDefense,
                Speed           = pokemonDataForReturn.Speed,
                Type            = pokemonDataForReturn.Type,
                MoveId_One      = moveIds[0],
                MoveId_Two      = moveIds[1],
                MoveId_Three    = moveIds[2],
                MoveId_Four     = moveIds[3]
            };

            return(pokemonForCreation);
        }
Exemplo n.º 4
0
        public async Task <PokemonDto> PostPokemonAsync(PokemonForCreationDto pokemonForCreationDto)
        {
            var pokemonEntity = _mapper.Map <Pokemon>(pokemonForCreationDto);

            _repositoryManager.Pokemon.CreatePokemon(pokemonEntity);
            await _repositoryManager.Save();

            return(_mapper.Map <PokemonDto>(pokemonEntity));
        }
Exemplo n.º 5
0
        public IActionResult AddPokemon([FromBody] PokemonForCreationDto pokemonForCreateDto)
        {
            var pokemon = _mapper.Map <Core.Entities.Pokemon>(pokemonForCreateDto);

            _pokemonRepository.AddPokemon(pokemon);

            var pokemonDto         = _mapper.Map <PokemonDto>(pokemon);
            var genericApiResponse = new GenericApiResponse <Core.Entities.Pokemon> {
                Data = pokemon
            };

            return(Created($"{ControllerRoute}/{nameof(GetPokemon)}", genericApiResponse));
        }
Exemplo n.º 6
0
        public async Task AddToUserPokeCollection(int id, PokemonForCreationDto pokemonForUpdateDto)
        {
            var userFromRepo = await _repo.GetUser(id);

            if (userFromRepo == null)
            {
                return;
            }

            var pokemon = _mapper.Map <Pokemon>(pokemonForUpdateDto);

            userFromRepo.PokeCollection.Add(pokemon);

            var res = await _repo.SaveAll();

            if (!res)
            {
                throw new Exception($"Updating user {id}. Failed to save to database!");
            }
        }
Exemplo n.º 7
0
        public async Task AddToUserPokeCollectionByDiscordId(ulong discordId, PokemonForCreationDto pokemonForCreationDto)
        {
            var userFromRepo = await _repo.GetUserByDiscordId(discordId);

            if (userFromRepo == null)
            {
                return;
            }

            var pokemon = _mapper.Map <Pokemon>(pokemonForCreationDto);

            userFromRepo.PokeCollection.Add(pokemon);

            var res = await _repo.SaveAll();

            if (!res)
            {
                throw new Exception($"Updating user {discordId}. Failed to save to database!");
            }
            System.Console.WriteLine($"{pokemon.Name} successfully added to {userFromRepo.Id}'s inventory");
        }