Compare commits

..

No commits in common. "a6f123d44710be8de82625ee0f4f4723a7b385eb" and "53aba1ffe7775e8b84b3ad5cfb8a244f25c61526" have entirely different histories.

2 changed files with 42 additions and 99 deletions

View file

@ -1,71 +0,0 @@
package trainer
import (
"fmt"
"maps"
"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) ListAllPokemonFromPokedex() {
if len(t.pokedex) == 0 {
fmt.Println("You have no Pokemon in your Pokedex.")
return
}
fmt.Println("Your Pokedex:")
for name := range maps.All(t.pokedex) {
fmt.Println(" -", name)
}
}
func (t *Trainer) CurrentLocationAreaID() int {
return t.currentLocationAreaID
}
func (t *Trainer) UpdateCurrentLocationAreaID(locationAreaID int) {
t.currentLocationAreaID = locationAreaID
}

70
main.go
View file

@ -11,10 +11,15 @@ import (
"strings"
"time"
"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
@ -23,6 +28,10 @@ type command struct {
type callbackFunc func(args []string) error
type pokedex map[string]pokeapi.Pokemon
var dexter = make(pokedex)
func main() {
run()
}
@ -33,7 +42,7 @@ func run() {
10*time.Second,
)
trainer := trainer.NewTrainer()
var state State
commandMap := map[string]command{
"exit": {
@ -49,12 +58,12 @@ func run() {
"map": {
name: "map",
description: "Displays the next 20 locations in the Pokemon world",
callback: mapFunc(client, trainer),
callback: mapFunc(client, &state),
},
"mapb": {
name: "map back",
description: "Displays the previous 20 locations in the Pokemon world",
callback: mapBFunc(client, trainer),
callback: mapBFunc(client, &state),
},
"explore": {
name: "explore",
@ -64,17 +73,17 @@ func run() {
"catch": {
name: "catch",
description: "Catches a Pokemon and adds them to your Pokedex",
callback: catchFunc(client, trainer),
callback: catchFunc(client),
},
"inspect": {
name: "inspect",
description: "Inspects a Pokemon from your Pokedex",
callback: inspectFunc(trainer),
callback: inspectFunc(),
},
"pokedex": {
name: "pokedex",
description: "Lists the names of all the Pokemon in your Pokedex",
callback: pokedexFunc(trainer),
callback: pokedexFunc(),
},
}
@ -149,26 +158,26 @@ func exitFunc(_ []string) error {
return nil
}
func mapFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
func mapFunc(client *pokeclient.Client, state *State) callbackFunc {
return func(_ []string) error {
url := trainer.NextLocationArea()
url := state.Next
if url == nil {
url = new(string)
*url = pokeclient.LocationAreaPath
}
return printResourceList(client, *url, trainer.UpdateLocationAreas)
return printResourceList(client, *url, state)
}
}
func mapBFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
func mapBFunc(client *pokeclient.Client, state *State) callbackFunc {
return func(_ []string) error {
url := trainer.PreviousLocationArea()
url := state.Previous
if url == nil {
return fmt.Errorf("no previous locations available")
}
return printResourceList(client, *url, trainer.UpdateLocationAreas)
return printResourceList(client, *url, state)
}
}
@ -207,7 +216,7 @@ func exploreFunc(client *pokeclient.Client) callbackFunc {
}
}
func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
func catchFunc(client *pokeclient.Client) callbackFunc {
return func(args []string) error {
if args == nil {
return errors.New("the name of the Pokemon has not been specified")
@ -224,7 +233,7 @@ func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
pokemonDetails, err := client.GetPokemon(pokemonName)
pokemon, err := client.GetPokemon(pokemonName)
if err != nil {
return fmt.Errorf(
"unable to get the information on %s: %w",
@ -236,7 +245,7 @@ func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc
chance := 50
if caught := catchPokemon(chance); caught {
trainer.AddPokemonToPokedex(pokemonName, pokemonDetails)
dexter[pokemonName] = pokemon
fmt.Printf("%s was caught!\nYou may now inspect it with the inspect command.\n", pokemonName)
} else {
fmt.Printf("%s escaped!\n", pokemonName)
@ -246,7 +255,7 @@ func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc
}
}
func inspectFunc(trainer *trainer.Trainer) callbackFunc {
func inspectFunc() callbackFunc {
return func(args []string) error {
if args == nil {
return errors.New("the name of the Pokemon has not been specified")
@ -261,7 +270,7 @@ func inspectFunc(trainer *trainer.Trainer) callbackFunc {
pokemonName := args[0]
pokemon, ok := trainer.GetPokemonFromPokedex(pokemonName)
pokemon, ok := dexter[pokemonName]
if !ok {
return fmt.Errorf("you have not caught %s", pokemonName)
}
@ -293,27 +302,32 @@ func inspectFunc(trainer *trainer.Trainer) callbackFunc {
}
}
func pokedexFunc(trainer *trainer.Trainer) callbackFunc {
func pokedexFunc() callbackFunc {
return func(_ []string) error {
trainer.ListAllPokemonFromPokedex()
if len(dexter) == 0 {
fmt.Println("You have no Pokemon in your Pokedex")
return nil
}
fmt.Println("Your Pokedex:")
for name := range maps.All(dexter) {
fmt.Println(" -", name)
}
return nil
}
}
func printResourceList(
client *pokeclient.Client,
url string,
updateStateFunc func(previous *string, next *string),
) error {
func printResourceList(client *pokeclient.Client, url string, state *State) 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)
}
state.Next = list.Next
state.Previous = list.Previous
for _, location := range slices.All(list.Results) {
fmt.Println(location.Name)