-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
268 lines (217 loc) · 5.6 KB
/
cli.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"fmt"
"math/rand"
"os"
"github.com/mehkij/pokedex/internal/pokeapi"
"github.com/mehkij/pokedex/internal/pokecache"
)
type cliCommand struct {
name string
description string
callback func(*config, []string) error
}
type config struct {
pokeapiClient pokeapi.Client
nextAreaURL *string
prevAreaURL *string
pokeCache pokecache.Cache
pokedex map[string]pokeapi.PokemonRes
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: callbackHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: callbackExit,
},
"map": {
name: "map",
description: "Displays the next 20 location areas in the Pokemon world",
callback: callbackMap,
},
"mapb": {
name: "mapb",
description: "Displays the previous 20 location areas in the Pokemon world",
callback: callbackMapb,
},
"explore": {
name: "explore",
description: "Displays the available Pokemon in the given area",
callback: callbackExplore,
},
"catch": {
name: "catch",
description: "Attempt to catch a given Pokemon",
callback: callbackCatch,
},
"inspect": {
name: "inspect",
description: "Inspect a Pokemon you have caught previously",
callback: callbackInspect,
},
"pokedex": {
name: "pokedex",
description: "Lists the Pokemon you have caught",
callback: callbackPokedex,
},
}
}
func callbackHelp(config *config, params []string) error {
c := getCommands()
if len(params) > 1 {
_, ok := c[params[1]]
if ok {
fmt.Println("Usage:")
fmt.Printf("%s: %s\n", c[params[1]].name, c[params[1]].description)
return nil
} else {
fmt.Println("unknown command")
return nil
}
}
fmt.Println("Welcome to the Pokedex!")
fmt.Println("Usage:")
fmt.Println("")
for _, val := range c {
fmt.Printf("%s: %s\n", val.name, val.description)
}
return nil
}
func callbackExit(config *config, params []string) error {
if len(params) > 1 {
fmt.Println("too many arguments")
return nil
}
os.Exit(0)
return nil
}
func callbackMap(config *config, params []string) error {
if len(params) > 1 {
fmt.Println("too many arguments")
return nil
}
// Guards against empty config
if config.nextAreaURL == nil && config.prevAreaURL != nil {
fmt.Println("You are on the last page!")
return nil
}
res, err := config.pokeapiClient.ListLocationAreas(config.pokeCache, config.nextAreaURL)
if err != nil {
return err
}
for _, location := range res.Results {
fmt.Println("- ", location.Name)
}
config.nextAreaURL = res.Next
config.prevAreaURL = res.Previous
return nil
}
func callbackMapb(config *config, params []string) error {
if len(params) > 1 {
fmt.Println("too many arguments")
return nil
}
if config.prevAreaURL == nil {
fmt.Println("You are on the first page!")
return nil
}
res, err := config.pokeapiClient.ListLocationAreas(config.pokeCache, config.prevAreaURL)
if err != nil {
return err
}
for _, location := range res.Results {
fmt.Println("- ", location.Name)
}
config.nextAreaURL = res.Next
config.prevAreaURL = res.Previous
return nil
}
func callbackExplore(config *config, params []string) error {
// If len == 1, user only passed command name with no arguments
if len(params) == 1 {
fmt.Println("not enough arguments")
return nil
}
// params[0] is always the name of the command
URL := fmt.Sprintf("https://pokeapi.co/api/v2/location-area/%s/", params[1])
res, err := config.pokeapiClient.ListPokemonEncounters(config.pokeCache, &URL)
if err != nil {
fmt.Println(err)
return err
}
if len(res.PokemonEncounters) == 0 {
fmt.Println("no Pokemon in this area")
return nil
}
fmt.Println("Found Pokemon:")
for _, encounter := range res.PokemonEncounters {
fmt.Println("- ", encounter.Pokemon.Name)
}
return nil
}
func callbackCatch(config *config, params []string) error {
if len(params) == 1 {
fmt.Println("not enough arguments")
return nil
}
_, ok := config.pokedex[params[1]]
if ok {
fmt.Printf("You already have %s!\n", params[1])
return nil
}
// params[0] is always the name of the command
URL := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%s/", params[1])
res, err := config.pokeapiClient.GetPokemon(config.pokeCache, &URL)
if err != nil {
return err
}
fmt.Printf("Throwing a Pokeball at %v...\n", res.Name)
if rand.Float64()*float64(res.BaseExperience) > float64(res.BaseExperience)/2 {
fmt.Printf("You caught %v!\n", res.Name)
config.pokedex[res.Name] = res
} else {
fmt.Printf("Drats! The %v got away!\n", res.Name)
}
return nil
}
func callbackInspect(config *config, params []string) error {
if len(params) == 1 {
fmt.Println("not enough arguments")
return nil
}
pokemon, ok := config.pokedex[params[1]]
if ok {
fmt.Printf("Name: %s\nHeight: %d\nWeight: %d\n", pokemon.Name, pokemon.Height, pokemon.Weight)
fmt.Println("Stats:")
for _, stat := range pokemon.Stats {
fmt.Printf("- %s: %v\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println("Types:")
for _, t := range pokemon.Types {
fmt.Printf("- %s\n", t.Type.Name)
}
} else {
fmt.Println("You have not caught this Pokemon yet!")
}
return nil
}
func callbackPokedex(config *config, params []string) error {
if len(params) > 1 {
fmt.Println("too many arguments")
return nil
}
if len(config.pokedex) == 0 {
fmt.Println("You haven't caught any Pokemon yet!")
return nil
}
for _, pokemon := range config.pokedex {
fmt.Printf("- %s\n", pokemon.Name)
}
return nil
}