checkpoint: add pokedex command

This commit is contained in:
Dan Anglin 2024-09-20 10:48:43 +01:00
parent c413e3149e
commit 9d7ee5fede
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

25
main.go
View file

@ -80,6 +80,11 @@ func run() {
description: "Inspects a Pokemon from your Pokedex", description: "Inspects a Pokemon from your Pokedex",
callback: inspectFunc(), callback: inspectFunc(),
}, },
"pokedex": {
name: "pokedex",
description: "Lists the names of all the Pokemon in your Pokedex",
callback: pokedexFunc(),
},
} }
summaries := summaryMap(commandMap) summaries := summaryMap(commandMap)
@ -241,7 +246,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc {
if caught := catchPokemon(chance); caught { if caught := catchPokemon(chance); caught {
dexter[pokemonName] = pokemon dexter[pokemonName] = pokemon
fmt.Printf("%s was caught!\n", pokemonName) fmt.Printf("%s was caught!\nYou may now inspect it with the inspect command.\n", pokemonName)
} else { } else {
fmt.Printf("%s escaped!\n", pokemonName) fmt.Printf("%s escaped!\n", pokemonName)
} }
@ -297,6 +302,24 @@ func inspectFunc() callbackFunc {
} }
} }
func pokedexFunc() 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)
}
return nil
}
}
func printResourceList(client *pokeclient.Client, url string, state *State) error { func printResourceList(client *pokeclient.Client, url string, state *State) error {
list, err := client.GetNamedAPIResourceList(url) list, err := client.GetNamedAPIResourceList(url)
if err != nil { if err != nil {