2024-09-21 16:53:11 +01:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"slices"
|
|
|
|
|
2024-09-22 13:17:47 +01:00
|
|
|
"codeflow.dananglin.me.uk/apollo/pokecli/internal/pokeclient"
|
|
|
|
"codeflow.dananglin.me.uk/apollo/pokecli/internal/poketrainer"
|
2024-09-21 16:53:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func MapFunc(client *pokeclient.Client, trainer *poketrainer.Trainer) CommandFunc {
|
|
|
|
return func(_ []string) error {
|
|
|
|
url := trainer.NextLocationArea()
|
|
|
|
if url == nil {
|
|
|
|
url = new(string)
|
|
|
|
*url = pokeclient.LocationAreaPath
|
|
|
|
}
|
|
|
|
|
|
|
|
return printResourceList(client, *url, trainer.UpdateLocationAreas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MapBFunc(client *pokeclient.Client, trainer *poketrainer.Trainer) CommandFunc {
|
|
|
|
return func(_ []string) error {
|
|
|
|
url := trainer.PreviousLocationArea()
|
|
|
|
if url == nil {
|
|
|
|
return fmt.Errorf("no previous locations available")
|
|
|
|
}
|
|
|
|
|
|
|
|
return printResourceList(client, *url, trainer.UpdateLocationAreas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func printResourceList(
|
|
|
|
client *pokeclient.Client,
|
|
|
|
url string,
|
|
|
|
updateStateFunc func(previous *string, next *string),
|
|
|
|
) error {
|
|
|
|
list, err := client.GetNamedAPIResourceList(url)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get the list of resources: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if updateStateFunc != nil {
|
|
|
|
updateStateFunc(list.Previous, list.Next)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, location := range slices.All(list.Results) {
|
|
|
|
fmt.Println(location.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|