generated from templates/go-generic
feat: add release command
Add the release command to release a Pokemon back into the wild.
This commit is contained in:
parent
37d890c800
commit
85d75a3943
3 changed files with 46 additions and 0 deletions
38
internal/commands/release.go
Normal file
38
internal/commands/release.go
Normal 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
|
||||
}
|
||||
}
|
|
@ -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.")
|
||||
|
|
4
main.go
4
main.go
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue