checkpoint: add inspect command

This commit is contained in:
Dan Anglin 2024-09-20 10:36:06 +01:00
parent ccb341fef4
commit c413e3149e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

60
main.go
View file

@ -67,14 +67,19 @@ func run() {
},
"explore": {
name: "explore",
description: "Lists all the pokemon in a given area",
description: "Lists all the Pokemon in a given area",
callback: exploreFunc(client),
},
"catch": {
name: "catch",
description: "Catches a pokemon and adds them to your Pokedex",
description: "Catches a Pokemon and adds them to your Pokedex",
callback: catchFunc(client),
},
"inspect": {
name: "inspect",
description: "Inspects a Pokemon from your Pokedex",
callback: inspectFunc(),
},
}
summaries := summaryMap(commandMap)
@ -245,6 +250,53 @@ func catchFunc(client *pokeclient.Client) callbackFunc {
}
}
func inspectFunc() callbackFunc {
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]
pokemon, ok := dexter[pokemonName]
if !ok {
return fmt.Errorf("you have not caught %s", pokemonName)
}
info := fmt.Sprintf(
"Name: %s\nHeight: %d\nWeight: %d\nStats:",
pokemon.Name,
pokemon.Height,
pokemon.Weight,
)
for _, stat := range slices.All(pokemon.Stats) {
info += fmt.Sprintf(
"\n - %s: %d",
stat.Stat.Name,
stat.BaseStat,
)
}
info += "\nTypes:"
for _, pType := range slices.All(pokemon.Types) {
info += "\n - " + pType.Type.Name
}
fmt.Println(info)
return nil
}
}
func printResourceList(client *pokeclient.Client, url string, state *State) error {
list, err := client.GetNamedAPIResourceList(url)
if err != nil {
@ -286,11 +338,11 @@ func parseArgs(input string) (string, []string) {
}
func catchPokemon(chance int) bool {
if chance == 100 {
if chance >= 100 {
return true
}
if chance == 0 {
if chance <= 0 {
return false
}