diff --git a/golang/pokemon/.gitignore b/golang/pokemon/.gitignore new file mode 100644 index 0000000..1fb9ef5 --- /dev/null +++ b/golang/pokemon/.gitignore @@ -0,0 +1 @@ +tmp/* diff --git a/golang/pokemon/go.mod b/golang/pokemon/go.mod new file mode 100644 index 0000000..000cc39 --- /dev/null +++ b/golang/pokemon/go.mod @@ -0,0 +1,5 @@ +module github.com/sakura2uuu/algorithm/golang/pokemon + +go 1.16 + +require github.com/manifoldco/promptui v0.8.0 diff --git a/golang/pokemon/go.sum b/golang/pokemon/go.sum new file mode 100644 index 0000000..04f549d --- /dev/null +++ b/golang/pokemon/go.sum @@ -0,0 +1,25 @@ +github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a h1:FaWFmfWdAUKbSCtOU2QjDaorUexogfaMgbipgYATUMU= +github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a/go.mod h1:UJSiEoRfvx3hP73CvoARgeLjaIOjybY9vj8PUPPFGeU= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a h1:weJVJJRzAJBFRlAiJQROKQs8oC9vOxvm4rZmBBk0ONw= +github.com/lunixbochs/vtclean v0.0.0-20180621232353-2d01aacdc34a/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI= +github.com/manifoldco/promptui v0.8.0 h1:R95mMF+McvXZQ7j1g8ucVZE1gLP3Sv6j9vlF9kyRqQo= +github.com/manifoldco/promptui v0.8.0/go.mod h1:n4zTdgP0vr0S3w7/O/g98U+e0gwLScEXGwov2nIKuGQ= +github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= +github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs= +github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b h1:MQE+LT/ABUuuvEZ+YQAMSXindAdUh7slEmAkup74op4= +golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/golang/pokemon/lib/importer/importer.go b/golang/pokemon/lib/importer/importer.go new file mode 100644 index 0000000..1b8ed39 --- /dev/null +++ b/golang/pokemon/lib/importer/importer.go @@ -0,0 +1,67 @@ +package importer + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +const ( + BASE_URL = `https://pokeapi.co/api/v2/pokemon?limit=2000` + CacheFile = `tmp/cache.txt` +) + +type Data struct { + Name string + URL string +} + +type Response struct { + Count int16 + Next string + Previous string + Results []Data +} + +func Import() { + urls := GetPokemonURLs() + data := GetPokemon(urls) + fmt.Println(data) +} + +func GetPokemon([]string) ([]string) { + + +} + +func GetPokemonURLs() ([]string) { + results := []Data{} + response := FetchData(BASE_URL) + + for (response.Next != "") { + results = append(results, response.Results...) + response = FetchData(response.Next) + } + results = append(results, response.Results...) + + urls := []string{} + for _, data := range results { + urls = append(urls, data.URL) + } + return urls +} + +func FetchData(url string) (Response) { + var response Response + + resp, err := http.Get(url) + if err != nil { + // handle error + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + + json.Unmarshal(body, &response) + return response +} diff --git a/golang/pokemon/lib/searcher/searcher.go b/golang/pokemon/lib/searcher/searcher.go new file mode 100644 index 0000000..a4bb0a5 --- /dev/null +++ b/golang/pokemon/lib/searcher/searcher.go @@ -0,0 +1,44 @@ +package searcher + +import ( + "bufio" + "fmt" + "os" + "regexp" +) + +const ( + CacheFile = `tmp/cache.txt` +) + +func Find(input string) (string) { + pattern := fmt.Sprintf(`\"name\"\:\"%s\"`, input) + + if Numeric(input) { + pattern = fmt.Sprintf(`\"id\"\:%s,`, input) + } + + return FindInLine(pattern) +} + +func Numeric(input string) (bool) { + return PatternMatched(input, `^[0-9]*$`) +} + +func PatternMatched(input string, pattern string) (bool) { + matched, _ := regexp.Match(pattern, []byte(input)) + return matched +} + +func FindInLine(pattern string) (string) { + f, _ := os.Open(CacheFile) + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + + if PatternMatched(line, pattern) { + return line + } + } + return `` +} diff --git a/golang/pokemon/main.go b/golang/pokemon/main.go new file mode 100644 index 0000000..d6ff7b7 --- /dev/null +++ b/golang/pokemon/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/manifoldco/promptui" + "github.com/sakura2uuu/algorithm/golang/pokemon/lib/searcher" + "github.com/sakura2uuu/algorithm/golang/pokemon/lib/importer" +) + +const ( + InfoColor = "\033[1;34m%s\033[0m" + NoticeColor = "\033[1;36m%s\033[0m" + WarningColor = "\033[1;33m%s\033[0m" + ErrorColor = "\033[1;31m%s\033[0m" + DebugColor = "\033[0;36m%s\033[0m" + ErrorLine = "\033[1;31m%s\033[0m" + NestedKeyLine = "\033[1;36m%s\033[0m" + KeyLine = "\033[1;34m%s\033[0m" + ValueLine = "\033[0;36m%s\033[0m" + InfoLine = "\033[1;34m%s\033[0m \033[0;36m%s\033[0m" + InfoLineWithNum = "\033[1;34m%s\033[0m \033[0;36m%v\033[0m" + DoubleInfoLine = "\033[1;34m%s\033[0m \033[0;36m%s\033[0m \033[1;34m%s\033[0m \033[0;36m%s\033[0m" +) + +type LocationAreaEncounter struct { + Location string + Methods []string +} + +type Pokemon struct { + ID int16 + Name string + Types []string + Stats []string + LocationAreaEncounters []LocationAreaEncounter +} + +func main() { + importer.Import() + + prompt := promptui.Prompt{ + Label: "Which Pokemon (ID or name)?", + } + + value, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + result := searcher.Find(value) + + if len(result) == 0 { + fmt.Printf(ErrorLine, "Not found") + fmt.Println("") + } else { + print(result) + } +} + +func print(result string) { + var pokemon Pokemon + json.Unmarshal([]byte(result), &pokemon) + + fmt.Printf(InfoLineWithNum, "ID: ", pokemon.ID) + fmt.Println("") + fmt.Printf(InfoLine, "Name: ", pokemon.Name) + fmt.Println("") + fmt.Printf(InfoLine, "Type: ", strings.Join(pokemon.Types[:], ", ")) + fmt.Println("") + fmt.Printf(NestedKeyLine, "Encounters") + fmt.Println("") + + encounters := []LocationAreaEncounter{} + for _, encounter := range pokemon.LocationAreaEncounters { + fmt.Println(encounter.Location) + if strings.HasPrefix(encounter.Location, "kanto-") { + encounters = append(encounters, encounter) + } + } + + if len(encounters) == 0 { + fmt.Printf(InfoLine, "Location: ", "-") + } else { + for _, encounter := range encounters { + fmt.Printf(DoubleInfoLine, "Location: ", encounter.Location, "Method: ", strings.Join(encounter.Methods[:], ", ")) + } + } + fmt.Println("") + + fmt.Printf(KeyLine, "Stats:") + fmt.Println("") + fmt.Printf(ValueLine, strings.Join(pokemon.Stats[:], ", ")) + fmt.Println("") +}