generated from templates/go-generic
checkpoint: use the trainer's new personal pokedex; remove global pokedex (dexter)
This commit is contained in:
parent
eb4d49209e
commit
a6f123d447
2 changed files with 30 additions and 26 deletions
|
@ -1,6 +1,11 @@
|
||||||
package trainer
|
package trainer
|
||||||
|
|
||||||
import "codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"maps"
|
||||||
|
|
||||||
|
"codeflow.dananglin.me.uk/apollo/pokedex/internal/api/pokeapi"
|
||||||
|
)
|
||||||
|
|
||||||
type Trainer struct {
|
type Trainer struct {
|
||||||
previousLocationArea *string
|
previousLocationArea *string
|
||||||
|
@ -43,6 +48,20 @@ func (t *Trainer) GetPokemonFromPokedex(name string) (pokeapi.Pokemon, bool) {
|
||||||
return details, ok
|
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 {
|
func (t *Trainer) CurrentLocationAreaID() int {
|
||||||
return t.currentLocationAreaID
|
return t.currentLocationAreaID
|
||||||
}
|
}
|
||||||
|
|
35
main.go
35
main.go
|
@ -11,7 +11,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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/pokeclient"
|
||||||
"codeflow.dananglin.me.uk/apollo/pokedex/internal/trainer"
|
"codeflow.dananglin.me.uk/apollo/pokedex/internal/trainer"
|
||||||
)
|
)
|
||||||
|
@ -24,10 +23,6 @@ type command struct {
|
||||||
|
|
||||||
type callbackFunc func(args []string) error
|
type callbackFunc func(args []string) error
|
||||||
|
|
||||||
type pokedex map[string]pokeapi.Pokemon
|
|
||||||
|
|
||||||
var dexter = make(pokedex)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
@ -69,17 +64,17 @@ func run() {
|
||||||
"catch": {
|
"catch": {
|
||||||
name: "catch",
|
name: "catch",
|
||||||
description: "Catches a Pokemon and adds them to your Pokedex",
|
description: "Catches a Pokemon and adds them to your Pokedex",
|
||||||
callback: catchFunc(client),
|
callback: catchFunc(client, trainer),
|
||||||
},
|
},
|
||||||
"inspect": {
|
"inspect": {
|
||||||
name: "inspect",
|
name: "inspect",
|
||||||
description: "Inspects a Pokemon from your Pokedex",
|
description: "Inspects a Pokemon from your Pokedex",
|
||||||
callback: inspectFunc(),
|
callback: inspectFunc(trainer),
|
||||||
},
|
},
|
||||||
"pokedex": {
|
"pokedex": {
|
||||||
name: "pokedex",
|
name: "pokedex",
|
||||||
description: "Lists the names of all the Pokemon in your Pokedex",
|
description: "Lists the names of all the Pokemon in your Pokedex",
|
||||||
callback: pokedexFunc(),
|
callback: pokedexFunc(trainer),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,7 +207,7 @@ func exploreFunc(client *pokeclient.Client) callbackFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func catchFunc(client *pokeclient.Client) callbackFunc {
|
func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
|
||||||
return func(args []string) error {
|
return func(args []string) error {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
return errors.New("the name of the Pokemon has not been specified")
|
return errors.New("the name of the Pokemon has not been specified")
|
||||||
|
@ -229,7 +224,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc {
|
||||||
|
|
||||||
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
|
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
|
||||||
|
|
||||||
pokemon, err := client.GetPokemon(pokemonName)
|
pokemonDetails, err := client.GetPokemon(pokemonName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"unable to get the information on %s: %w",
|
"unable to get the information on %s: %w",
|
||||||
|
@ -241,7 +236,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc {
|
||||||
chance := 50
|
chance := 50
|
||||||
|
|
||||||
if caught := catchPokemon(chance); caught {
|
if caught := catchPokemon(chance); caught {
|
||||||
dexter[pokemonName] = pokemon
|
trainer.AddPokemonToPokedex(pokemonName, pokemonDetails)
|
||||||
fmt.Printf("%s was caught!\nYou may now inspect it with the inspect command.\n", pokemonName)
|
fmt.Printf("%s was caught!\nYou may now inspect it with the inspect command.\n", pokemonName)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%s escaped!\n", pokemonName)
|
fmt.Printf("%s escaped!\n", pokemonName)
|
||||||
|
@ -251,7 +246,7 @@ func catchFunc(client *pokeclient.Client) callbackFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectFunc() callbackFunc {
|
func inspectFunc(trainer *trainer.Trainer) callbackFunc {
|
||||||
return func(args []string) error {
|
return func(args []string) error {
|
||||||
if args == nil {
|
if args == nil {
|
||||||
return errors.New("the name of the Pokemon has not been specified")
|
return errors.New("the name of the Pokemon has not been specified")
|
||||||
|
@ -266,7 +261,7 @@ func inspectFunc() callbackFunc {
|
||||||
|
|
||||||
pokemonName := args[0]
|
pokemonName := args[0]
|
||||||
|
|
||||||
pokemon, ok := dexter[pokemonName]
|
pokemon, ok := trainer.GetPokemonFromPokedex(pokemonName)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("you have not caught %s", pokemonName)
|
return fmt.Errorf("you have not caught %s", pokemonName)
|
||||||
}
|
}
|
||||||
|
@ -298,19 +293,9 @@ func inspectFunc() callbackFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func pokedexFunc() callbackFunc {
|
func pokedexFunc(trainer *trainer.Trainer) callbackFunc {
|
||||||
return func(_ []string) error {
|
return func(_ []string) error {
|
||||||
if len(dexter) == 0 {
|
trainer.ListAllPokemonFromPokedex()
|
||||||
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue