diff --git a/command_catch.go b/command_catch.go deleted file mode 100644 index 3607960..0000000 --- a/command_catch.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "math/rand" - "net/http" -) - -func commandCatch(p *PokedexConfig, a *string) error { - if *a == "" { - return fmt.Errorf("you need to specify a Pokemon to catch") - } - var pokemonName string - var baseUrl string - for _, encounter := range p.LocalPokemons.PokemonEncounters { - if encounter.PokemonSummary.Name == *a { - pokemonName = *a - baseUrl = encounter.PokemonSummary.DataUrl - } - } - if pokemonName == "" { - baseUrl = "https://pokeapi.co/api/v2/pokemon/" + *a - } - // if pokemonName != *a { - // fmt.Println("this pokemon is not present in the current area") - // return nil - // } - - body, exists := p.Cache.Get(baseUrl) - if !exists { - client := http.Client{} - - resp, err := client.Get(baseUrl) - if err != nil { - return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) - } - body, err = io.ReadAll(resp.Body) - defer resp.Body.Close() - if resp.StatusCode > 299 { - return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) - } - if err != nil { - return fmt.Errorf("could not read request body! Err: %w", err) - } - p.Cache.Add(baseUrl, body) - } - var pokemon PokemonDetails - if err := json.Unmarshal(body, &pokemon); err != nil { - return fmt.Errorf("could not unmarshal Pokemon data! err: %w", err) - } - fmt.Printf("Throwing a Pokeball at %s...\n", *a) - catchSuccess := rand.Intn(pokemon.BaseExperience) - if 2*catchSuccess >= pokemon.BaseExperience { - fmt.Printf("%s was caught!\n", pokemon.Name) - p.CaughtPokemon = append(p.CaughtPokemon, pokemon) - return nil - } - fmt.Printf("%s escaped!\n", *a) - - return nil -} diff --git a/command_exit.go b/command_exit.go deleted file mode 100644 index ae0e7b5..0000000 --- a/command_exit.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "fmt" - "os" -) - -func commandExit(p *PokedexConfig, a *string) error { - fmt.Println("Closing the Pokedex... Goodbye!") - os.Exit(0) - return nil -} diff --git a/command_explore.go b/command_explore.go deleted file mode 100644 index 8abad7c..0000000 --- a/command_explore.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "net/http" -) - -func commandExplore(p *PokedexConfig, a *string) error { - if *a == "" { - return fmt.Errorf("you need to specify an area to explore") - } - baseUrl := "https://pokeapi.co/api/v2/location-area/" + *a + "/" - body, exists := p.Cache.Get(baseUrl) - if !exists { - client := http.Client{} - - resp, err := client.Get(baseUrl) - if err != nil { - return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) - } - - body, err = io.ReadAll(resp.Body) - defer resp.Body.Close() - if resp.StatusCode > 299 { - return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) - } - if err != nil { - return fmt.Errorf("could not read request body! Err: %w", err) - } - p.Cache.Add(baseUrl, body) - } - if err := json.Unmarshal(body, &p.LocalPokemons); err != nil { - return fmt.Errorf("could not unmarshal response! Err: %w", err) - } - - for _, pokemon := range p.LocalPokemons.PokemonEncounters { - fmt.Println(pokemon.PokemonSummary.Name) - } - - return nil -} diff --git a/command_help.go b/command_help.go deleted file mode 100644 index 6eac89d..0000000 --- a/command_help.go +++ /dev/null @@ -1,14 +0,0 @@ -package main - -import ( - "fmt" -) - -func commandHelp(p *PokedexConfig, a *string) error { - fmt.Print("Pokedex CLI - available commands:\n\n") - for _, v := range getCommands() { - fmt.Printf("%s %s\n", v.name, v.description) - } - fmt.Println() - return nil -} diff --git a/command_inspect.go b/command_inspect.go deleted file mode 100644 index 95b943e..0000000 --- a/command_inspect.go +++ /dev/null @@ -1,45 +0,0 @@ -package main - -import ( - "fmt" - - "gopkg.in/yaml.v2" -) - -type PokemonInspectOutput struct { - Name string - Height int - Weight int - Stats map[string]int - Types []string -} - -func commandInspect(p *PokedexConfig, a *string) error { - found := false - for _, pokemon := range p.CaughtPokemon { - if pokemon.Name == *a { - found = true - stats := map[string]int{} - types := []string{} - for _, pokeStat := range pokemon.PokeStats { - stats[pokeStat.PokeStat.Name] = pokeStat.BaseStat - } - for _, pokeType := range pokemon.PokeTypes { - types = append(types, pokeType.PokeType.Name) - } - pokemonInspectOutput := PokemonInspectOutput{ - Name: pokemon.Name, - Height: pokemon.Height, - Weight: pokemon.Weight, - Stats: stats, - Types: types, - } - data, _ := yaml.Marshal(pokemonInspectOutput) - fmt.Println(string(data)) - } - } - if !found { - fmt.Println("you have not caught that pokemon") - } - return nil -} diff --git a/command_map.go b/command_map.go deleted file mode 100644 index 20ec578..0000000 --- a/command_map.go +++ /dev/null @@ -1,48 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "net/http" -) - -func commandMap(p *PokedexConfig, a *string) error { - var baseUrl string - if p.LocationData.Next == nil { - baseUrl = "https://pokeapi.co/api/v2/location-area/" - } else { - baseUrl = *p.LocationData.Next - } - - var body []byte - // Check if respones is available in cache - if resp, exists := p.Cache.Get(baseUrl); exists { - body = resp - } else { - client := &http.Client{} - - resp, err := client.Get(baseUrl) - if err != nil { - return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) - } - - body, err = io.ReadAll(resp.Body) - defer resp.Body.Close() - if resp.StatusCode > 299 { - return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) - } - if err != nil { - return fmt.Errorf("could not read request body! Err: %w", err) - } - p.Cache.Add(baseUrl, body) - } - - if err := json.Unmarshal(body, &p.LocationData); err != nil { - return fmt.Errorf("could not unmarshal response! Err: %w", err) - } - for _, location := range p.LocationData.Locations { - fmt.Println(location["name"]) - } - return nil -} diff --git a/command_mapb.go b/command_mapb.go deleted file mode 100644 index 5f6eca7..0000000 --- a/command_mapb.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -import ( - "encoding/json" - "fmt" - "io" - "net/http" -) - -func commandMapb(p *PokedexConfig, a *string) error { - var baseUrl string - if p.LocationData.Previous == nil { - fmt.Println("you're on the first page") - return nil - } else { - baseUrl = *p.LocationData.Previous - } - var body []byte - if resp, exists := p.Cache.Get(baseUrl); exists { - body = resp - } else { - client := &http.Client{} - - resp, err := client.Get(baseUrl) - if err != nil { - return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) - } - - body, err = io.ReadAll(resp.Body) - defer resp.Body.Close() - if resp.StatusCode > 299 { - return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) - } - if err != nil { - return fmt.Errorf("could not read request body! Err: %w", err) - } - - p.Cache.Add(baseUrl, body) - } - if err := json.Unmarshal(body, &p.LocationData); err != nil { - return fmt.Errorf("could not unmarshal response! Err: %w", err) - } - for _, location := range p.LocationData.Locations { - fmt.Println(location["name"]) - } - return nil -} diff --git a/command_pokedex.go b/command_pokedex.go deleted file mode 100644 index e087a1e..0000000 --- a/command_pokedex.go +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import "fmt" - -func commandPokedex(p *PokedexConfig, a *string) error { - if len(p.CaughtPokemon) == 0 { - fmt.Println("Your Pokedex is empty.") - return nil - } - fmt.Println("Your Pokedex:") - for _, pokemon := range p.CaughtPokemon { - fmt.Println(" - ", pokemon.Name) - } - - return nil -} diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..4fab860 --- /dev/null +++ b/commands.go @@ -0,0 +1,244 @@ +package main + +import ( + "encoding/json" + "fmt" + "io" + "math/rand" + "net/http" + "os" + + "gopkg.in/yaml.v2" +) + +type PokemonInspectOutput struct { + Name string + Height int + Weight int + Stats map[string]int + Types []string +} + +func commandHelp(p *PokedexConfig, a *string) error { + fmt.Print("Pokedex CLI - available commands:\n\n") + for _, v := range getCommands() { + fmt.Printf("%s %s\n", v.name, v.description) + } + fmt.Println() + return nil +} + +func commandExit(p *PokedexConfig, a *string) error { + fmt.Println("Closing the Pokedex... Goodbye!") + os.Exit(0) + return nil +} + +func commandMap(p *PokedexConfig, a *string) error { + var baseUrl string + if p.LocationData.Next == nil { + baseUrl = "https://pokeapi.co/api/v2/location-area/" + } else { + baseUrl = *p.LocationData.Next + } + + var body []byte + + if resp, exists := p.Cache.Get(baseUrl); exists { + body = resp + } else { + client := &http.Client{} + + resp, err := client.Get(baseUrl) + if err != nil { + return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) + } + + body, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if resp.StatusCode > 299 { + return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) + } + if err != nil { + return fmt.Errorf("could not read request body! Err: %w", err) + } + p.Cache.Add(baseUrl, body) + } + + if err := json.Unmarshal(body, &p.LocationData); err != nil { + return fmt.Errorf("could not unmarshal response! Err: %w", err) + } + for _, location := range p.LocationData.Locations { + fmt.Println(location["name"]) + } + return nil +} + +func commandMapb(p *PokedexConfig, a *string) error { + var baseUrl string + if p.LocationData.Previous == nil { + fmt.Println("you're on the first page") + return nil + } else { + baseUrl = *p.LocationData.Previous + } + var body []byte + if resp, exists := p.Cache.Get(baseUrl); exists { + body = resp + } else { + client := &http.Client{} + + resp, err := client.Get(baseUrl) + if err != nil { + return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) + } + + body, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if resp.StatusCode > 299 { + return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) + } + if err != nil { + return fmt.Errorf("could not read request body! Err: %w", err) + } + + p.Cache.Add(baseUrl, body) + } + if err := json.Unmarshal(body, &p.LocationData); err != nil { + return fmt.Errorf("could not unmarshal response! Err: %w", err) + } + for _, location := range p.LocationData.Locations { + fmt.Println(location["name"]) + } + return nil +} + +func commandExplore(p *PokedexConfig, a *string) error { + if *a == "" { + return fmt.Errorf("you need to specify an area to explore") + } + baseUrl := "https://pokeapi.co/api/v2/location-area/" + *a + "/" + body, exists := p.Cache.Get(baseUrl) + if !exists { + client := http.Client{} + + resp, err := client.Get(baseUrl) + if err != nil { + return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) + } + + body, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if resp.StatusCode > 299 { + return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) + } + if err != nil { + return fmt.Errorf("could not read request body! Err: %w", err) + } + p.Cache.Add(baseUrl, body) + } + if err := json.Unmarshal(body, &p.LocalPokemons); err != nil { + return fmt.Errorf("could not unmarshal response! Err: %w", err) + } + + for _, pokemon := range p.LocalPokemons.PokemonEncounters { + fmt.Println(pokemon.PokemonSummary.Name) + } + + return nil +} + +func commandCatch(p *PokedexConfig, a *string) error { + if *a == "" { + return fmt.Errorf("you need to specify a Pokemon to catch") + } + var pokemonName string + var baseUrl string + for _, encounter := range p.LocalPokemons.PokemonEncounters { + if encounter.PokemonSummary.Name == *a { + pokemonName = *a + baseUrl = encounter.PokemonSummary.DataUrl + } + } + + if pokemonName != *a { + fmt.Println("this pokemon is not present in the current area") + return nil + } + + body, exists := p.Cache.Get(baseUrl) + if !exists { + client := http.Client{} + + resp, err := client.Get(baseUrl) + if err != nil { + return fmt.Errorf("could not make request to Pokedex API! Err: %w", err) + } + body, err = io.ReadAll(resp.Body) + defer resp.Body.Close() + if resp.StatusCode > 299 { + return fmt.Errorf("request returned non-200 code! Code: %v Body: %v", resp.StatusCode, body) + } + if err != nil { + return fmt.Errorf("could not read request body! Err: %w", err) + } + p.Cache.Add(baseUrl, body) + } + var pokemon PokemonDetails + if err := json.Unmarshal(body, &pokemon); err != nil { + return fmt.Errorf("could not unmarshal Pokemon data! err: %w", err) + } + fmt.Printf("Throwing a Pokeball at %s...\n", *a) + catchSuccess := rand.Intn(pokemon.BaseExperience) + if 2*catchSuccess >= pokemon.BaseExperience { + fmt.Printf("%s was caught!\n", pokemon.Name) + p.CaughtPokemon = append(p.CaughtPokemon, pokemon) + return nil + } + fmt.Printf("%s escaped!\n", *a) + + return nil +} + +func commandPokedex(p *PokedexConfig, a *string) error { + if len(p.CaughtPokemon) == 0 { + fmt.Println("Your Pokedex is empty.") + return nil + } + fmt.Println("Your Pokedex:") + for _, pokemon := range p.CaughtPokemon { + fmt.Println(" - ", pokemon.Name) + } + + return nil +} + +func commandInspect(p *PokedexConfig, a *string) error { + found := false + for _, pokemon := range p.CaughtPokemon { + if pokemon.Name == *a { + found = true + stats := map[string]int{} + types := []string{} + for _, pokeStat := range pokemon.PokeStats { + stats[pokeStat.PokeStat.Name] = pokeStat.BaseStat + } + for _, pokeType := range pokemon.PokeTypes { + types = append(types, pokeType.PokeType.Name) + } + pokemonInspectOutput := PokemonInspectOutput{ + Name: pokemon.Name, + Height: pokemon.Height, + Weight: pokemon.Weight, + Stats: stats, + Types: types, + } + data, _ := yaml.Marshal(pokemonInspectOutput) + fmt.Println(string(data)) + } + } + if !found { + fmt.Println("you have not caught that pokemon") + } + return nil +}