1st version completed
This commit is contained in:
parent
e51545a0c3
commit
402a056dbb
25
command_catch.go
Normal file
25
command_catch.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
func commandCatch(cfg *config, pokemonName string) error {
|
||||
pokemon, err := cfg.pokeClient.PokemonInfo(&pokemonName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Throwing a Pokeball at %s...\n", pokemonName)
|
||||
baseExp := pokemon.BaseExp
|
||||
catchRate := 500 - baseExp
|
||||
catchRoll := rand.Intn(500)
|
||||
if catchRoll <= catchRate {
|
||||
(*cfg.pokeDex)[pokemonName] = pokemon
|
||||
fmt.Println(pokemonName, "was caught!")
|
||||
} else {
|
||||
fmt.Println(pokemonName, "escaped!")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
func commandExit(cfg *config) error {
|
||||
func commandExit(cfg *config, arg string) error {
|
||||
fmt.Println("Closing the Pokedex... Goodbye!")
|
||||
os.Exit(0)
|
||||
return nil
|
||||
|
||||
20
command_explore.go
Normal file
20
command_explore.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func commandExplore(cfg *config, areaName string) error {
|
||||
encounters, err := cfg.pokeClient.ListEncounters(&areaName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Exploring %s...\n", encounters.Name)
|
||||
fmt.Println("Found Pokemon:")
|
||||
for _, p := range encounters.Encounters {
|
||||
fmt.Printf(" - %s\n", p.Pokemon.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -2,13 +2,13 @@ package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func commandHelp(cfg *config) error {
|
||||
func commandHelp(cfg *config, arg string) error {
|
||||
fmt.Println()
|
||||
fmt.Println("Welcome to the Pokedex!")
|
||||
fmt.Println("Usage:")
|
||||
fmt.Println()
|
||||
for _, c := range getCommands() {
|
||||
fmt.Printf("\t%v\t\t%v\n", c.name, c.description)
|
||||
fmt.Printf(" %v\t\t\t%v\n", c.name, c.description)
|
||||
}
|
||||
fmt.Println()
|
||||
return nil
|
||||
|
||||
28
command_inspect.go
Normal file
28
command_inspect.go
Normal file
@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func commandInspect(cfg *config, pokemonName string) error {
|
||||
pokemon, ok := (*cfg.pokeDex)[pokemonName]
|
||||
if !ok {
|
||||
return errors.New("you havent caught that pokemon yet")
|
||||
}
|
||||
fmt.Println("")
|
||||
fmt.Printf(" Name: %v\n", pokemon.Name)
|
||||
fmt.Printf(" Height: %v\n", pokemon.Height)
|
||||
fmt.Printf(" Weight: %v\n", pokemon.Weight)
|
||||
fmt.Println(" Stats:")
|
||||
for _, stat := range pokemon.Stats {
|
||||
fmt.Printf(" -%v: %v\n", stat.Stat.Name, stat.BaseStat)
|
||||
}
|
||||
fmt.Println(" Types:")
|
||||
for _, t := range pokemon.Types {
|
||||
fmt.Printf(" - %v\n", t.Type.Name)
|
||||
}
|
||||
fmt.Println("")
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func commandMap(cfg *config) error {
|
||||
func commandMap(cfg *config, arg string) error {
|
||||
// if cfg.nextLocationsURL == nil {
|
||||
// return errors.New("you're on the last page")
|
||||
// }
|
||||
@ -25,7 +25,7 @@ func commandMap(cfg *config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func commandMapb(cfg *config) error {
|
||||
func commandMapb(cfg *config, arg string) error {
|
||||
if cfg.prevLocationsURL == nil {
|
||||
return errors.New("you're on the first page")
|
||||
}
|
||||
|
||||
18
command_pokedex.go
Normal file
18
command_pokedex.go
Normal file
@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func commandPokedex(cfg *config, arg string) error {
|
||||
if len(*cfg.pokeDex) == 0 {
|
||||
return errors.New("you haven't caught any pokemon")
|
||||
}
|
||||
|
||||
fmt.Println(" Your Pokedex:")
|
||||
for k := range *cfg.pokeDex {
|
||||
fmt.Printf(" - %v\n", k)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
80
internal/pokeapi/encounter_list.go
Normal file
80
internal/pokeapi/encounter_list.go
Normal file
@ -0,0 +1,80 @@
|
||||
package pokeapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) ListEncounters(areaName *string) (RespShallowLocationExplore, error) {
|
||||
if areaName == nil {
|
||||
return RespShallowLocationExplore{}, errors.New("invalid location for exploration")
|
||||
}
|
||||
|
||||
url := "https://pokeapi.co/api/v2/location-area/" + *areaName
|
||||
data, ok := c.cache.Get(url)
|
||||
if !ok {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return RespShallowLocationExplore{}, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return RespShallowLocationExplore{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return RespShallowLocationExplore{}, err
|
||||
}
|
||||
c.cache.Add(url, data)
|
||||
}
|
||||
|
||||
var encounterResp RespShallowLocationExplore
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
if err := decoder.Decode(&encounterResp); err != nil {
|
||||
return RespShallowLocationExplore{}, err
|
||||
}
|
||||
|
||||
return encounterResp, nil
|
||||
}
|
||||
|
||||
func (c *Client) PokemonInfo(pokemon *string) (Pokemon, error) {
|
||||
if pokemon == nil {
|
||||
return Pokemon{}, errors.New("please enter the pokemon name")
|
||||
}
|
||||
url := "https://pokeapi.co/api/v2/pokemon/" + *pokemon
|
||||
data, ok := c.cache.Get(url)
|
||||
if !ok {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return Pokemon{}, err
|
||||
}
|
||||
|
||||
resp, err := c.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return Pokemon{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return Pokemon{}, err
|
||||
}
|
||||
c.cache.Add(url, data)
|
||||
}
|
||||
|
||||
var pokemonResp Pokemon
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
if err := decoder.Decode(&pokemonResp); err != nil {
|
||||
return Pokemon{}, err
|
||||
}
|
||||
|
||||
return pokemonResp, nil
|
||||
}
|
||||
@ -8,5 +8,51 @@ type RespShallowLocationExplore struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"pokemon"`
|
||||
}
|
||||
} `json:"pokemon_encounters"`
|
||||
}
|
||||
|
||||
type Pokemon struct {
|
||||
Abilities []struct {
|
||||
Ability struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"ability"`
|
||||
IsHidden bool `json:"is_hidden"`
|
||||
Slot int `json:"slot"`
|
||||
} `json:"abilities"`
|
||||
BaseExp int `json:"base_experience"`
|
||||
Cires struct {
|
||||
Latest string `json:"latest"`
|
||||
Legacy string `json:"legacy"`
|
||||
} `json:"cires"`
|
||||
Forms []struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"forms"`
|
||||
Height int `json:"height"`
|
||||
Id int `json:"id"`
|
||||
Moves []struct {
|
||||
Move struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"move"`
|
||||
} `json:"moves"`
|
||||
Name string `json:"name"`
|
||||
Order int `json:"order"`
|
||||
Stats []struct {
|
||||
BaseStat int `json:"base_stat"`
|
||||
Effort int `json:"effort"`
|
||||
Stat struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"stat"`
|
||||
} `json:"stats"`
|
||||
Types []struct {
|
||||
Slot int `json:"slot"`
|
||||
Type struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
} `json:"type"`
|
||||
} `json:"types"`
|
||||
Weight int `json:"weight"`
|
||||
}
|
||||
|
||||
2
main.go
2
main.go
@ -10,8 +10,10 @@ const prompt = "Pokedex > "
|
||||
|
||||
func main() {
|
||||
pokeClient := pokeapi.NewClient(5*time.Second, 5*time.Minute)
|
||||
catchedPokemon := map[string]pokeapi.Pokemon{}
|
||||
cfg := &config{
|
||||
pokeClient: pokeClient,
|
||||
pokeDex: &catchedPokemon,
|
||||
}
|
||||
startRepl(cfg)
|
||||
}
|
||||
|
||||
30
repl.go
30
repl.go
@ -31,6 +31,26 @@ func getCommands() map[string]cliCommand {
|
||||
description: "Displays the names of the previous 20 locations",
|
||||
callback: commandMapb,
|
||||
},
|
||||
"explore": {
|
||||
name: "explore <area_name>",
|
||||
description: "Displays the pokemon found in an area",
|
||||
callback: commandExplore,
|
||||
},
|
||||
"catch": {
|
||||
name: "catch <pokemon_name>",
|
||||
description: "Attempt to catch a pokemon",
|
||||
callback: commandCatch,
|
||||
},
|
||||
"inspect": {
|
||||
name: "inspect <pokemon_name>",
|
||||
description: "Displays info of a catched pokemon",
|
||||
callback: commandInspect,
|
||||
},
|
||||
"pokedex": {
|
||||
name: "pokedex",
|
||||
description: "Displays your caught pokemon",
|
||||
callback: commandPokedex,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,10 +66,15 @@ func startRepl(cfg *config) {
|
||||
}
|
||||
|
||||
inputCommand := words[0]
|
||||
commandArg := ""
|
||||
if len(words) > 1 {
|
||||
commandArg = words[1]
|
||||
}
|
||||
|
||||
command, exists := getCommands()[inputCommand]
|
||||
|
||||
if exists {
|
||||
err := command.callback(cfg)
|
||||
err := command.callback(cfg, commandArg)
|
||||
if err != nil {
|
||||
fmt.Printf("%v error: %v\n", command.name, err.Error())
|
||||
}
|
||||
@ -65,11 +90,12 @@ func startRepl(cfg *config) {
|
||||
type cliCommand struct {
|
||||
name string
|
||||
description string
|
||||
callback func(*config) error
|
||||
callback func(*config, string) error
|
||||
}
|
||||
|
||||
type config struct {
|
||||
pokeClient pokeapi.Client
|
||||
pokeDex *map[string]pokeapi.Pokemon
|
||||
nextLocationsURL *string
|
||||
prevLocationsURL *string
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user