Skip to content

Commit

Permalink
add explore command
Browse files Browse the repository at this point in the history
  • Loading branch information
adamthiede committed May 25, 2024
1 parent 8fa7cbb commit 94a545a
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 7 deletions.
2 changes: 1 addition & 1 deletion command_exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"os"
)

func commandExit(cfg *config) error {
func commandExit(cfg *config, args ...string) error {
os.Exit(0)
return nil
}
26 changes: 26 additions & 0 deletions command_explore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"errors"
"fmt"
//"encoding/json"
//"github.com/adamthiede/bootdev-pokedex/internal/pokeapi"
)

func commandExplore(cfg *config, args ...string) error {
if len(args) != 1 {
return errors.New("no location area provided")
}
locationAreaName := args[0]
locationArea, err := cfg.pokeApiClient.GetLocationArea(locationAreaName)
if err != nil {
return err
}

fmt.Printf("Pokemon in \"%s\" area:\n", args[0])
for _, encounter := range locationArea.PokemonEncounters {
fmt.Printf(" - %s\n", encounter.Pokemon.Name)
}

return nil
}
2 changes: 1 addition & 1 deletion command_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func commandHelp(cfg *config) error {
func commandHelp(cfg *config, args ...string) error {
fmt.Println("Available commands:")
for _, command := range getCommands() {
fmt.Println(">", command.name, ":", command.description)
Expand Down
4 changes: 2 additions & 2 deletions command_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
//"github.com/adamthiede/bootdev-pokedex/internal/pokeapi"
)

func commandMap(cfg *config) error {
func commandMap(cfg *config, args ...string) error {
resp, err := cfg.pokeApiClient.ListLocationAreas(cfg.nextLocationAreaURL)
if err != nil {
return err
Expand All @@ -22,7 +22,7 @@ func commandMap(cfg *config) error {
return nil
}

func commandMapb(cfg *config) error {
func commandMapb(cfg *config, args ...string) error {
if cfg.prevLocationAreaURL == nil {
return errors.New("no previous page")
}
Expand Down
2 changes: 1 addition & 1 deletion command_reap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
)

func commandReap(cfg *config) error {
func commandReap(cfg *config, args ...string) error {
cfg.pokeApiClient.InvalidateCache()
fmt.Println("invalidating cache.")
return nil
Expand Down
44 changes: 44 additions & 0 deletions internal/pokeapi/location_area_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,47 @@ func (c *Client) ListLocationAreas(pageURL *string) (LocationAreasResp, error) {
return locationAreasResp, nil

}

func (c *Client) GetLocationArea(locationAreaName string) (LocationArea, error) {
endpoint := "/location-area/" + locationAreaName
fullURL := baseURL + endpoint

cacheData, ok := c.cache.Get(fullURL)
if ok {
fmt.Println("[cache]")
locationArea := LocationArea{}
err := json.Unmarshal(cacheData, &locationArea)
if err != nil {
return LocationArea{}, err
}
return locationArea, nil
}

req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return LocationArea{}, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return LocationArea{}, err
}
defer resp.Body.Close()
if resp.StatusCode > 399 {
return LocationArea{}, fmt.Errorf("bad status code: %v", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return LocationArea{}, err
}

locationArea := LocationArea{}
err = json.Unmarshal(data, &locationArea)
if err != nil {
return LocationArea{}, err
}

c.cache.Add(fullURL, data)

return locationArea, nil

}
53 changes: 53 additions & 0 deletions internal/pokeapi/types_location_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,56 @@ type LocationAreasResp struct {
URL string `json:"url"`
} `json:"results"`
}

type LocationArea struct {
EncounterMethodRates []struct {
EncounterMethod struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"encounter_method"`
VersionDetails []struct {
Rate int `json:"rate"`
Version struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"version"`
} `json:"version_details"`
} `json:"encounter_method_rates"`
GameIndex int `json:"game_index"`
ID int `json:"id"`
Location struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"location"`
Name string `json:"name"`
Names []struct {
Language struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"language"`
Name string `json:"name"`
} `json:"names"`
PokemonEncounters []struct {
Pokemon struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"pokemon"`
VersionDetails []struct {
EncounterDetails []struct {
Chance int `json:"chance"`
ConditionValues []any `json:"condition_values"`
MaxLevel int `json:"max_level"`
Method struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"method"`
MinLevel int `json:"min_level"`
} `json:"encounter_details"`
MaxChance int `json:"max_chance"`
Version struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"version"`
} `json:"version_details"`
} `json:"pokemon_encounters"`
}
13 changes: 11 additions & 2 deletions repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
type cliCommand struct {
name string
description string
callback func(*config) error
callback func(*config, ...string) error
}

type pokemon struct {
Expand Down Expand Up @@ -39,6 +39,11 @@ func getCommands() map[string]cliCommand {
description: "Get previous 20 locations",
callback: commandMapb,
},
"explore": {
name: "explore {location_area}",
description: "get pokemon for a region",
callback: commandExplore,
},
"reap": {
name: "reap",
description: "invalidate cache",
Expand All @@ -65,6 +70,10 @@ func startRepl(cfg *config) {

// we don't yet use the rest of cleanedInput
command := cleanedInput[0]
args := []string{}
if len(cleanedInput) > 1 {
args = cleanedInput[1:]
}

// get commands and check if input is in commands
commands := getCommands()
Expand All @@ -75,7 +84,7 @@ func startRepl(cfg *config) {
continue
}

err = commands[command].callback(cfg)
err = commands[command].callback(cfg, args...)
if err != nil {
fmt.Println(err)
}
Expand Down

0 comments on commit 94a545a

Please sign in to comment.