-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
196 lines (178 loc) · 4.67 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)
func commandHelp(cfg *Config, args ...string) error {
commands := "Usage: \n"
for _, command := range getCommands() {
commands += fmt.Sprintf("%s: %s\n", command.name, command.description)
}
fmt.Println(commands)
return nil
}
func commandExit(cfg *Config, args ...string) error {
fmt.Println("Closing the Pokedex... Goodbye!")
os.Exit(0)
return nil
}
func commandMap(cfg *Config, args ...string) error {
if len(args) != 0 {
return fmt.Errorf("can't use any arguments with this command")
}
if cfg.Next == "" {
cfg.Next = "https://pokeapi.co/api/v2/location-area/?offset=0&limit=20"
}
var body []byte
var currentLocations area
var twentyLocations string
body, ok := cfg.cache.Get(cfg.Next)
if !ok {
body, _ = fetchAndCache(cfg, cfg.Next)
}
err := json.Unmarshal(body, ¤tLocations)
if err != nil {
fmt.Println(err)
}
cfg.Next = currentLocations.Next
cfg.Previous = currentLocations.Previous
for _, location := range currentLocations.Results {
twentyLocations += location.Name + "\n"
}
fmt.Println(twentyLocations)
return nil
}
func commandMapb(cfg *Config, args ...string) error {
if len(args) != 0 {
return fmt.Errorf("can't use any arguments with this command")
}
if cfg.Previous == "" {
return fmt.Errorf("can't go backwards")
}
var currentLocations area
var body []byte
var twentyLocations string
body, ok := cfg.cache.Get(cfg.Previous)
if !ok {
body, _ = fetchAndCache(cfg, cfg.Previous)
}
err := json.Unmarshal(body, ¤tLocations)
if err != nil {
fmt.Println(err)
}
cfg.Next = currentLocations.Next
cfg.Previous = currentLocations.Previous
for _, location := range currentLocations.Results {
twentyLocations += location.Name + "\n"
}
fmt.Println(twentyLocations)
return nil
}
func commandExplore(cfg *Config, args ...string) error {
switch len(args) {
case 0:
return fmt.Errorf("can't use explore without a location")
case 1:
var pokemons string
var area area_info
var body []byte
location := args[0]
fmt.Printf("Exploring %s... \n", location)
url := "https://pokeapi.co/api/v2/location-area/" + location
body, ok := cfg.cache.Get(url)
if !ok {
body, _ = fetchAndCache(cfg, url)
}
fmt.Println("Found Pokemon(s):")
err := json.Unmarshal(body, &area)
if err != nil {
fmt.Println(err)
}
for _, pokemonStruct := range area.PokemonEncounters {
pokemons += pokemonStruct.Pokemon.Name + "\n"
}
fmt.Println(pokemons)
return nil
default:
return fmt.Errorf("can't explore multiple location at the same time")
}
}
func commandCatch(cfg *Config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("need a target")
}
var pokemon Pokemon
var body []byte
url := "https://pokeapi.co/api/v2/pokemon/" + strings.ToLower(args[0])
_, ok := cfg.caughtPokemons[strings.ToLower(args[0])]
if !ok {
if body, ok = cfg.cache.Get(url); !ok {
body, _ = fetchAndCache(cfg, url)
}
err := json.Unmarshal(body, &pokemon)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Throwing a pokeball at %s...\n", strings.ToLower(args[0]))
if pokemon.Caught() {
fmt.Printf("%s was caught! \n", pokemon.Name)
cfg.caughtPokemons[pokemon.Name] = pokemon
} else {
fmt.Printf("%s escaped!\n", pokemon.Name)
}
} else {
fmt.Println("pokemon already caught")
}
return nil
}
func commandInspect(cfg *Config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("need a pokemon to inspect")
}
pokemon, ok := cfg.caughtPokemons[args[0]]
if !ok {
fmt.Println("you have not caught that pokemon")
} else {
var typesUnorderedList string
var statUnorderedList string
for _, typeStruct := range pokemon.Types {
typesUnorderedList += fmt.Sprintf(" - %s\n", typeStruct.Type.Name)
}
for _, statStruct := range pokemon.Stats {
statUnorderedList += fmt.Sprintf(" -%s: %d\n", statStruct.Stat.Name, statStruct.BaseStat)
}
fmt.Printf("Name: %s\n"+
"Height: %v\n"+
"Weight: %v\n"+
"Stats:\n"+
"%s"+
"Types:\n"+
"%s\n", pokemon.Name, pokemon.Height, pokemon.Weight, statUnorderedList, typesUnorderedList)
}
return nil
}
func commandPokedex(cfg *Config, args ...string) error {
if len(args) != 0 {
return fmt.Errorf("can't use any arguments with this command")
}
var pokemonUnorderedList string
for _, pokemon := range cfg.caughtPokemons {
pokemonUnorderedList += " - " + pokemon.Name + "\n"
}
fmt.Printf("Your Pokedex:\n%s", pokemonUnorderedList)
return nil
}
func fetchAndCache(cfg *Config, url string) ([]byte, error) {
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
body, _ := io.ReadAll(res.Body)
defer res.Body.Close()
cfg.cache.Add(url, body)
return body, nil
}