diff --git a/internal/trainer/trainer.go b/internal/trainer/trainer.go new file mode 100644 index 0000000..1174099 --- /dev/null +++ b/internal/trainer/trainer.go @@ -0,0 +1,52 @@ +package trainer + +import "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi" + +type Trainer struct { + previousLocationArea *string + nextLocationArea *string + currentLocationAreaID int + pokedex map[string]pokeapi.Pokemon +} + +func NewTrainer() *Trainer { + trainer := Trainer{ + previousLocationArea: nil, + nextLocationArea: nil, + currentLocationAreaID: 0, + pokedex: make(map[string]pokeapi.Pokemon), + } + + return &trainer +} + +func (t *Trainer) UpdateLocationAreas(previous, next *string) { + t.previousLocationArea = previous + t.nextLocationArea = next +} + +func (t *Trainer) PreviousLocationArea() *string { + return t.previousLocationArea +} + +func (t *Trainer) NextLocationArea() *string { + return t.nextLocationArea +} + +func (t *Trainer) AddPokemonToPokedex(name string, details pokeapi.Pokemon) { + t.pokedex[name] = details +} + +func (t *Trainer) GetPokemonFromPokedex(name string) (pokeapi.Pokemon, bool) { + details, ok := t.pokedex[name] + + return details, ok +} + +func (t *Trainer) CurrentLocationAreaID() int { + return t.currentLocationAreaID +} + +func (t *Trainer) UpdateCurrentLocationAreaID(locationAreaID int) { + t.currentLocationAreaID = locationAreaID +} diff --git a/main.go b/main.go index b44954e..3937925 100644 --- a/main.go +++ b/main.go @@ -13,13 +13,9 @@ import ( "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" ) -type State struct { - Previous *string - Next *string -} - type command struct { name string description string @@ -42,7 +38,7 @@ func run() { 10*time.Second, ) - var state State + trainer := trainer.NewTrainer() commandMap := map[string]command{ "exit": { @@ -58,12 +54,12 @@ func run() { "map": { name: "map", description: "Displays the next 20 locations in the Pokemon world", - callback: mapFunc(client, &state), + callback: mapFunc(client, trainer), }, "mapb": { name: "map back", description: "Displays the previous 20 locations in the Pokemon world", - callback: mapBFunc(client, &state), + callback: mapBFunc(client, trainer), }, "explore": { name: "explore", @@ -158,26 +154,26 @@ func exitFunc(_ []string) error { return nil } -func mapFunc(client *pokeclient.Client, state *State) callbackFunc { +func mapFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc { return func(_ []string) error { - url := state.Next + url := trainer.NextLocationArea() if url == nil { url = new(string) *url = pokeclient.LocationAreaPath } - return printResourceList(client, *url, state) + return printResourceList(client, *url, trainer.UpdateLocationAreas) } } -func mapBFunc(client *pokeclient.Client, state *State) callbackFunc { +func mapBFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc { return func(_ []string) error { - url := state.Previous + url := trainer.PreviousLocationArea() if url == nil { return fmt.Errorf("no previous locations available") } - return printResourceList(client, *url, state) + return printResourceList(client, *url, trainer.UpdateLocationAreas) } } @@ -320,14 +316,19 @@ func pokedexFunc() callbackFunc { } } -func printResourceList(client *pokeclient.Client, url string, state *State) error { +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) } - state.Next = list.Next - state.Previous = list.Previous + if updateStateFunc != nil { + updateStateFunc(list.Previous, list.Next) + } for _, location := range slices.All(list.Results) { fmt.Println(location.Name)