checkpoint: new trainer package; removed state type

This commit is contained in:
Dan Anglin 2024-09-20 23:42:02 +01:00
parent 53aba1ffe7
commit eb4d49209e
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
2 changed files with 70 additions and 17 deletions

View file

@ -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
}

35
main.go
View file

@ -13,13 +13,9 @@ import (
"codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi" "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi"
"codeflow.dananglin.me.uk/apollo/pokedex/internal/pokeclient" "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 { type command struct {
name string name string
description string description string
@ -42,7 +38,7 @@ func run() {
10*time.Second, 10*time.Second,
) )
var state State trainer := trainer.NewTrainer()
commandMap := map[string]command{ commandMap := map[string]command{
"exit": { "exit": {
@ -58,12 +54,12 @@ func run() {
"map": { "map": {
name: "map", name: "map",
description: "Displays the next 20 locations in the Pokemon world", description: "Displays the next 20 locations in the Pokemon world",
callback: mapFunc(client, &state), callback: mapFunc(client, trainer),
}, },
"mapb": { "mapb": {
name: "map back", name: "map back",
description: "Displays the previous 20 locations in the Pokemon world", description: "Displays the previous 20 locations in the Pokemon world",
callback: mapBFunc(client, &state), callback: mapBFunc(client, trainer),
}, },
"explore": { "explore": {
name: "explore", name: "explore",
@ -158,26 +154,26 @@ func exitFunc(_ []string) error {
return nil return nil
} }
func mapFunc(client *pokeclient.Client, state *State) callbackFunc { func mapFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
return func(_ []string) error { return func(_ []string) error {
url := state.Next url := trainer.NextLocationArea()
if url == nil { if url == nil {
url = new(string) url = new(string)
*url = pokeclient.LocationAreaPath *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 { return func(_ []string) error {
url := state.Previous url := trainer.PreviousLocationArea()
if url == nil { if url == nil {
return fmt.Errorf("no previous locations available") 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) list, err := client.GetNamedAPIResourceList(url)
if err != nil { if err != nil {
return fmt.Errorf("unable to get the list of resources: %w", err) return fmt.Errorf("unable to get the list of resources: %w", err)
} }
state.Next = list.Next if updateStateFunc != nil {
state.Previous = list.Previous updateStateFunc(list.Previous, list.Next)
}
for _, location := range slices.All(list.Results) { for _, location := range slices.All(list.Results) {
fmt.Println(location.Name) fmt.Println(location.Name)