generated from templates/go-generic
Compare commits
No commits in common. "a6f123d44710be8de82625ee0f4f4723a7b385eb" and "53aba1ffe7775e8b84b3ad5cfb8a244f25c61526" have entirely different histories.
a6f123d447
...
53aba1ffe7
2 changed files with 42 additions and 99 deletions
|
@ -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
70
main.go
|
@ -11,10 +11,15 @@ 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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type State struct {
|
||||||
|
Previous *string
|
||||||
|
Next *string
|
||||||
|
}
|
||||||
|
|
||||||
type command struct {
|
type command struct {
|
||||||
name string
|
name string
|
||||||
description string
|
description string
|
||||||
|
@ -23,6 +28,10 @@ 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()
|
||||||
}
|
}
|
||||||
|
@ -33,7 +42,7 @@ func run() {
|
||||||
10*time.Second,
|
10*time.Second,
|
||||||
)
|
)
|
||||||
|
|
||||||
trainer := trainer.NewTrainer()
|
var state State
|
||||||
|
|
||||||
commandMap := map[string]command{
|
commandMap := map[string]command{
|
||||||
"exit": {
|
"exit": {
|
||||||
|
@ -49,12 +58,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, trainer),
|
callback: mapFunc(client, &state),
|
||||||
},
|
},
|
||||||
"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, trainer),
|
callback: mapBFunc(client, &state),
|
||||||
},
|
},
|
||||||
"explore": {
|
"explore": {
|
||||||
name: "explore",
|
name: "explore",
|
||||||
|
@ -64,17 +73,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, trainer),
|
callback: catchFunc(client),
|
||||||
},
|
},
|
||||||
"inspect": {
|
"inspect": {
|
||||||
name: "inspect",
|
name: "inspect",
|
||||||
description: "Inspects a Pokemon from your Pokedex",
|
description: "Inspects a Pokemon from your Pokedex",
|
||||||
callback: inspectFunc(trainer),
|
callback: inspectFunc(),
|
||||||
},
|
},
|
||||||
"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(trainer),
|
callback: pokedexFunc(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,26 +158,26 @@ func exitFunc(_ []string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc {
|
func mapFunc(client *pokeclient.Client, state *State) callbackFunc {
|
||||||
return func(_ []string) error {
|
return func(_ []string) error {
|
||||||
url := trainer.NextLocationArea()
|
url := state.Next
|
||||||
if url == nil {
|
if url == nil {
|
||||||
url = new(string)
|
url = new(string)
|
||||||
*url = pokeclient.LocationAreaPath
|
*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 {
|
return func(_ []string) error {
|
||||||
url := trainer.PreviousLocationArea()
|
url := state.Previous
|
||||||
if url == nil {
|
if url == nil {
|
||||||
return fmt.Errorf("no previous locations available")
|
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 {
|
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")
|
||||||
|
@ -224,7 +233,7 @@ func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc
|
||||||
|
|
||||||
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
|
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
|
||||||
|
|
||||||
pokemonDetails, err := client.GetPokemon(pokemonName)
|
pokemon, 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",
|
||||||
|
@ -236,7 +245,7 @@ func catchFunc(client *pokeclient.Client, trainer *trainer.Trainer) callbackFunc
|
||||||
chance := 50
|
chance := 50
|
||||||
|
|
||||||
if caught := catchPokemon(chance); caught {
|
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)
|
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)
|
||||||
|
@ -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 {
|
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")
|
||||||
|
@ -261,7 +270,7 @@ func inspectFunc(trainer *trainer.Trainer) callbackFunc {
|
||||||
|
|
||||||
pokemonName := args[0]
|
pokemonName := args[0]
|
||||||
|
|
||||||
pokemon, ok := trainer.GetPokemonFromPokedex(pokemonName)
|
pokemon, ok := dexter[pokemonName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("you have not caught %s", pokemonName)
|
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 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func printResourceList(
|
func printResourceList(client *pokeclient.Client, url string, state *State) error {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
if updateStateFunc != nil {
|
state.Next = list.Next
|
||||||
updateStateFunc(list.Previous, list.Next)
|
state.Previous = list.Previous
|
||||||
}
|
|
||||||
|
|
||||||
for _, location := range slices.All(list.Results) {
|
for _, location := range slices.All(list.Results) {
|
||||||
fmt.Println(location.Name)
|
fmt.Println(location.Name)
|
||||||
|
|
Loading…
Reference in a new issue