feat: add release command

Add the release command to release a Pokemon back into the wild.
This commit is contained in:
Dan Anglin 2024-09-21 21:58:49 +01:00
parent 37d890c800
commit 85d75a3943
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
3 changed files with 46 additions and 0 deletions

View file

@ -0,0 +1,38 @@
package commands
import (
"errors"
"fmt"
"codeflow.dananglin.me.uk/apollo/pokedex/internal/poketrainer"
)
func ReleaseFunc(trainer *poketrainer.Trainer) CommandFunc {
return func(args []string) error {
if args == nil {
return errors.New("the name of the Pokemon has not been specified")
}
if len(args) != 1 {
return fmt.Errorf(
"unexpected number of Pokemon names: want 1; got %d",
len(args),
)
}
pokemonName := args[0]
if _, caught := trainer.GetPokemonFromPokedex(pokemonName); !caught {
return fmt.Errorf(
"you haven't caught a %s",
pokemonName,
)
}
trainer.RemovePokemonFromPokedex(pokemonName)
fmt.Printf("%s was released back into the wild.\n", pokemonName)
return nil
}
}

View file

@ -48,6 +48,10 @@ func (t *Trainer) GetPokemonFromPokedex(name string) (pokeapi.Pokemon, bool) {
return details, ok
}
func (t *Trainer) RemovePokemonFromPokedex(name string) {
delete(t.pokedex, name)
}
func (t *Trainer) ListAllPokemonFromPokedex() {
if len(t.pokedex) == 0 {
fmt.Println("You have no Pokemon in your Pokedex.")

View file

@ -63,6 +63,10 @@ func run() {
description: "List the names of all the Pokemon in your Pokedex",
callback: commands.PokedexFunc(trainer),
},
"release": {
description: "Release a Pokemon back into the wild",
callback: commands.ReleaseFunc(trainer),
},
"visit": {
description: "Visit a location area",
callback: commands.VisitFunc(client, trainer),