diff --git a/internal/trainer/trainer.go b/internal/trainer/trainer.go index 1174099..0152e8e 100644 --- a/internal/trainer/trainer.go +++ b/internal/trainer/trainer.go @@ -1,6 +1,11 @@ package trainer -import "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi" +import ( + "fmt" + "maps" + + "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi" +) type Trainer struct { previousLocationArea *string @@ -43,6 +48,20 @@ func (t *Trainer) GetPokemonFromPokedex(name string) (pokeapi.Pokemon, bool) { return details, ok } +func (t *Trainer) ListAllPokemonFromPokedex() { + if len(t.pokedex) == 0 { + fmt.Println("You have no Pokemon in your Pokedex.") + + return + } + + fmt.Println("Your Pokedex:") + + for name := range maps.All(t.pokedex) { + fmt.Println(" -", name) + } +} + func (t *Trainer) CurrentLocationAreaID() int { return t.currentLocationAreaID } diff --git a/main.go b/main.go index 3937925..2dc45d1 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,6 @@ import ( "strings" "time" - "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi" "codeflow.dananglin.me.uk/apollo/pokedex/internal/pokeclient" "codeflow.dananglin.me.uk/apollo/pokedex/internal/trainer" ) @@ -24,10 +23,6 @@ type command struct { type callbackFunc func(args []string) error -type pokedex map[string]pokeapi.Pokemon - -var dexter = make(pokedex) - func main() { run() } @@ -69,17 +64,17 @@ func run() { "catch": { name: "catch", description: "Catches a Pokemon and adds them to your Pokedex", - callback: catchFunc(client), + callback: catchFunc(client, trainer), }, "inspect": { name: "inspect", description: "Inspects a Pokemon from your Pokedex", - callback: inspectFunc(), + callback: inspectFunc(trainer), }, "pokedex": { name: "pokedex", description: "Lists the names of all the Pokemon in your Pokedex", - callback: pokedexFunc(), + callback: pokedexFunc(trainer), }, } @@ -212,7 +207,7 @@ func exploreFunc(client *pokeclient.Client) callbackFunc { } } -func catchFunc(client *pokeclient.Client) callbackFunc { +func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc { return func(args []string) error { if args == nil { return errors.New("the name of the Pokemon has not been specified") @@ -229,7 +224,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc { fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName) - pokemon, err := client.GetPokemon(pokemonName) + pokemonDetails, err := client.GetPokemon(pokemonName) if err != nil { return fmt.Errorf( "unable to get the information on %s: %w", @@ -241,7 +236,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc { chance := 50 if caught := catchPokemon(chance); caught { - dexter[pokemonName] = pokemon + trainer.AddPokemonToPokedex(pokemonName, pokemonDetails) fmt.Printf("%s was caught!\nYou may now inspect it with the inspect command.\n", pokemonName) } else { fmt.Printf("%s escaped!\n", pokemonName) @@ -251,7 +246,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc { } } -func inspectFunc() callbackFunc { +func inspectFunc(trainer *trainer.Trainer) callbackFunc { return func(args []string) error { if args == nil { return errors.New("the name of the Pokemon has not been specified") @@ -266,7 +261,7 @@ func inspectFunc() callbackFunc { pokemonName := args[0] - pokemon, ok := dexter[pokemonName] + pokemon, ok := trainer.GetPokemonFromPokedex(pokemonName) if !ok { return fmt.Errorf("you have not caught %s", pokemonName) } @@ -298,19 +293,9 @@ func inspectFunc() callbackFunc { } } -func pokedexFunc() callbackFunc { +func pokedexFunc(trainer *trainer.Trainer) callbackFunc { return func(_ []string) error { - if len(dexter) == 0 { - fmt.Println("You have no Pokemon in your Pokedex") - - return nil - } - - fmt.Println("Your Pokedex:") - - for name := range maps.All(dexter) { - fmt.Println(" -", name) - } + trainer.ListAllPokemonFromPokedex() return nil }