first commit

This commit is contained in:
Joren 2024-11-16 03:07:10 +01:00
commit 7903e67946
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
3 changed files with 248 additions and 0 deletions

22
go.mod Normal file
View File

@ -0,0 +1,22 @@
module pokeCLI
go 1.23.3
require (
github.com/muesli/termenv v0.15.2
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/qeesung/image2ascii v1.0.1
golang.org/x/text v0.20.0
)
require (
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/wayneashleyberry/terminal-dimensions v1.1.0 // indirect
golang.org/x/sys v0.7.0 // indirect
)

35
go.sum Normal file
View File

@ -0,0 +1,35 @@
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 h1:WWB576BN5zNSZc/M9d/10pqEx5VHNhaQ/yOVAkmj5Yo=
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qeesung/image2ascii v1.0.1 h1:Fe5zTnX/v/qNC3OC4P/cfASOXS501Xyw2UUcgrLgtp4=
github.com/qeesung/image2ascii v1.0.1/go.mod h1:kZKhyX0h2g/YXa/zdJR3JnLnJ8avHjZ3LrvEKSYyAyU=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/wayneashleyberry/terminal-dimensions v1.1.0 h1:EB7cIzBdsOzAgmhTUtTTQXBByuPheP/Zv1zL2BRPY6g=
github.com/wayneashleyberry/terminal-dimensions v1.1.0/go.mod h1:2lc/0eWCObmhRczn2SdGSQtgBooLUzIotkkEGXqghyg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

191
main.go Normal file
View File

@ -0,0 +1,191 @@
package main
import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"os"
"sort"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Pokemon struct {
Name string `json:"name"`
Weight int `json:"weight"`
Types []Type `json:"types"`
Sprites Sprites `json:"sprites"`
}
type Type struct {
TypeDetails TypeDetails `json:"type"`
}
type TypeDetails struct {
Name string `json:"name"`
URL string `json:"url"`
}
type DamageRelations struct {
DoubleDamageFrom []TypeDetails `json:"double_damage_from"`
HalfDamageFrom []TypeDetails `json:"half_damage_from"`
NoDamageFrom []TypeDetails `json:"no_damage_from"`
DoubleDamageTo []TypeDetails `json:"double_damage_to"`
HalfDamageTo []TypeDetails `json:"half_damage_to"`
NoDamageTo []TypeDetails `json:"no_damage_to"`
}
type Sprites struct {
BackDefault string `json:"back_default"`
BackFemale *string `json:"back_female"`
BackShiny string `json:"back_shiny"`
BackShinyFemale *string `json:"back_shiny_female"`
FrontDefault string `json:"front_default"`
FrontFemale *string `json:"front_female"`
FrontShiny string `json:"front_shiny"`
FrontShinyFemale *string `json:"front_shiny_female"`
}
func getPokemonData(pokemonName string) (*Pokemon, error) {
url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%s", strings.ToLower(pokemonName))
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch Pokémon data: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var pokemon Pokemon
if err := json.Unmarshal(body, &pokemon); err != nil {
return nil, fmt.Errorf("failed to parse Pokémon data: %w", err)
}
return &pokemon, nil
}
func getTypeDamageRelations(typeURL string) (*DamageRelations, error) {
resp, err := http.Get(typeURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch type damage relations: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var damageRelations struct {
DamageRelations DamageRelations `json:"damage_relations"`
}
if err := json.Unmarshal(body, &damageRelations); err != nil {
return nil, fmt.Errorf("failed to parse damage relations: %w", err)
}
return &damageRelations.DamageRelations, nil
}
func calculateCombinedWeaknesses(types []Type) map[string]float64 {
typeEffects := make(map[string]float64)
for _, pokeType := range types {
typeName := pokeType.TypeDetails.Name
typeURL := pokeType.TypeDetails.URL
damageRelations, err := getTypeDamageRelations(typeURL)
if err != nil {
fmt.Printf("Error fetching damage relations for type %s: %v\n", typeName, err)
continue
}
for _, dmgType := range damageRelations.DoubleDamageFrom {
typeEffects[dmgType.Name] += 1
}
for _, dmgType := range damageRelations.HalfDamageFrom {
typeEffects[dmgType.Name] -= 0.5
}
for _, dmgType := range damageRelations.NoDamageFrom {
typeEffects[dmgType.Name] = 0
}
}
combinedWeaknesses := make(map[string]float64)
for typeName, multiplier := range typeEffects {
combinedWeaknesses[typeName] = math.Pow(2, multiplier)
}
return combinedWeaknesses
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run pokecli.go <pokemon_name>")
os.Exit(1)
}
pokemonName := os.Args[1]
pokemon, err := getPokemonData(pokemonName)
if err != nil {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Name: %s\n", cases.Title(language.Und).String(pokemon.Name))
fmt.Printf("Weight: %d\n", pokemon.Weight)
fmt.Println("\nTypes:")
for _, pokeType := range pokemon.Types {
typeName := pokeType.TypeDetails.Name
typeURL := pokeType.TypeDetails.URL
fmt.Printf(" - %s\n", cases.Title(language.Und).String(typeName))
damageRelations, err := getTypeDamageRelations(typeURL)
if err != nil {
fmt.Printf("Error fetching damage relations for type %s: %v\n", typeName, err)
continue
}
if len(damageRelations.DoubleDamageTo) > 0 {
for _, dmgType := range damageRelations.DoubleDamageTo {
fmt.Printf(" * %s\n", cases.Title(language.English).String(dmgType.Name))
}
}
}
combinedWeaknesses := calculateCombinedWeaknesses(pokemon.Types)
fmt.Println("\nWeaknesses:")
type Weakness struct {
TypeName string
Multiplier float64
}
var weaknesses []Weakness
for typeName, multiplier := range combinedWeaknesses {
if multiplier > 1 {
weaknesses = append(weaknesses, Weakness{TypeName: typeName, Multiplier: multiplier})
}
}
sort.Slice(weaknesses, func(i, j int) bool {
return weaknesses[i].Multiplier > weaknesses[j].Multiplier
})
for _, weakness := range weaknesses {
fmt.Printf(" - %s: %.1fx\n", cases.Title(language.English).String(weakness.TypeName), weakness.Multiplier)
}
}