generated from templates/go-generic
checkpoint: new trainer package; removed state type
This commit is contained in:
parent
53aba1ffe7
commit
eb4d49209e
2 changed files with 70 additions and 17 deletions
52
internal/trainer/trainer.go
Normal file
52
internal/trainer/trainer.go
Normal 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
35
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)
|
||||
|
|
Loading…
Reference in a new issue