-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
340 lines (262 loc) · 6.45 KB
/
main.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"bufio"
"errors"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"time"
"github.com/logan-bobo/pokedex-cli/internal/cache"
"github.com/logan-bobo/pokedex-cli/internal/pokeapi"
)
type pokedex struct {
entities map[string]pokeapi.Pokemon
}
func newPokedex() *pokedex {
p := pokedex{
entities: map[string]pokeapi.Pokemon{},
}
return &p
}
type cliCommand struct {
name string
description string
config *config
callback func(*config, *cache.Cache, *pokedex, string) error
}
type config struct {
next string
previous string
}
func buildCommandInterface() map[string]cliCommand {
conf := config{}
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
config: &conf,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
config: &conf,
},
"map": {
name: "map",
description: "Show the next 20 locations",
callback: mapNext,
config: &conf,
},
"mapb": {
name: "mapb",
description: "Show the previous 20 locations",
callback: mapPrevious,
config: &conf,
},
"explore": {
name: "explore",
description: "Show all pokemon in an area",
callback: exploreLocation,
config: &conf,
},
"catch": {
name: "catch",
description: "Attempt to catch a pokemon",
callback: catchPokemon,
config: &conf,
},
"inspect": {
name: "inspect",
description: "Inspect a pokemon",
callback: inspectPokemon,
config: &conf,
},
"pokedex": {
name: "pokedex",
description: "List all pokemon in pokedex",
callback: showPokedex,
config: &conf,
},
}
}
func commandExit(conf *config, cache *cache.Cache, pokedex *pokedex, location string) error {
fmt.Println("Goodbye!")
os.Exit(0)
return nil
}
func commandHelp(conf *config, cache *cache.Cache, pokedex *pokedex, location string) error {
fmt.Println("Welcome to the Pokedex!")
return nil
}
func mapNext(conf *config, cache *cache.Cache, pokedex *pokedex, location string) error {
var locations pokeapi.Locations
var err error
if conf.next == "" {
locations, err = pokeapi.GetLocations("0", cache)
if err != nil {
return err
}
} else {
u, err := url.Parse(conf.next)
if err != nil {
return err
}
query := u.Query()
offset, ok := query["offset"]
if !ok {
return errors.New(fmt.Sprintf("Offset not found in URL: %v", conf.next))
}
locations, err = pokeapi.GetLocations(offset[0], cache)
if err != nil {
return err
}
}
for _, location := range locations.Results {
fmt.Println(location.Name)
}
conf.next = locations.Next
previous, ok := locations.Previous.(string)
if ok {
conf.previous = previous
}
return nil
}
func mapPrevious(conf *config, cache *cache.Cache, pokedex *pokedex, location string) error {
var locations pokeapi.Locations
var err error
if conf.previous == "" {
fmt.Println("No location to go back to...")
return err
} else {
u, err := url.Parse(conf.previous)
if err != nil {
return err
}
query := u.Query()
offset, ok := query["offset"]
if !ok {
return errors.New(fmt.Sprintf("Offset not in URL: %v", conf.previous))
}
locations, err = pokeapi.GetLocations(offset[0], cache)
if err != nil {
return err
}
}
for _, location := range locations.Results {
fmt.Println(location.Name)
}
previous, ok := locations.Previous.(string)
if ok {
conf.previous = previous
}
conf.next = locations.Next
return nil
}
func exploreLocation(conf *config, cache *cache.Cache, pokedex *pokedex, location string) error {
locations, err := pokeapi.ExploreLocation(location, cache)
if err != nil {
return err
}
fmt.Println("Found Pokemon...")
for _, pokemon := range locations.PokemonEncounters {
fmt.Printf("- %v \n", pokemon.Pokemon.Name)
}
return nil
}
func catchPokemon(conf *config, cache *cache.Cache, pokedex *pokedex, name string) error {
catch := false
pokemon, err := pokeapi.GetPokemon(name, cache)
if err != nil {
return err
}
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
roll := rand.Intn(100)
if pokemon.BaseExperience > 75 {
if roll > 75 {
catch = true
fmt.Printf("Caught %v \n", pokemon.Name)
} else {
fmt.Printf("Failed to catch %v \n", pokemon.Name)
}
} else if pokemon.BaseExperience > 50 {
if roll > 50 {
catch = true
fmt.Printf("Caught %v \n", pokemon.Name)
} else {
fmt.Printf("Failed to catch %v \n", pokemon.Name)
}
} else if pokemon.BaseExperience > 25 {
if roll > 25 {
catch = true
fmt.Printf("Caught %v \n", pokemon.Name)
} else {
fmt.Printf("Failed to catch %v \n", pokemon.Name)
}
} else if pokemon.BaseExperience > 0 {
catch = true
fmt.Printf("Caught %v \n", pokemon.Name)
}
if catch {
_, ok := pokedex.entities[pokemon.Name]
if !ok {
pokedex.entities[pokemon.Name] = pokemon
} else {
fmt.Println("Pokemon already registered in your pokedex")
}
}
return nil
}
func inspectPokemon(conf *config, cache *cache.Cache, pokedex *pokedex, name string) error {
pokemon, ok := pokedex.entities[name]
if !ok {
fmt.Println("You have not caught this pokemon")
} else {
fmt.Printf("Name: %v \n Height: %v \n Weight: %v \n Stats:\n",
pokemon.Name, pokemon.Height, pokemon.Weight,
)
for _, item := range pokemon.Stats {
fmt.Printf(" - %v: %v \n", item.Stat.Name, item.BaseStat)
}
fmt.Println("Types:")
for _, item := range pokemon.Types {
fmt.Printf(" - %v \n", item.Type.Name)
}
}
return nil
}
func showPokedex(conf *config, cahce *cache.Cache, pokedex *pokedex, name string) error {
fmt.Println("Listing Pokemon: ")
for pokemon, _ := range pokedex.entities {
fmt.Printf("- %v \n", pokemon)
}
return nil
}
func main() {
cliCommands := buildCommandInterface()
scanner := bufio.NewScanner(os.Stdin)
cache := cache.NewCache(60 * time.Second)
pokedex := newPokedex()
for {
fmt.Print("Pokedex -> ")
scanner.Scan()
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
inputRaw := scanner.Text()
inputSplit := strings.Split(inputRaw, " ")
command, ok := cliCommands[inputSplit[0]]
if !ok {
fmt.Println("Command not found")
continue
}
if len(inputSplit) == 1 {
command.callback(command.config, cache, pokedex, "")
} else {
command.callback(command.config, cache, pokedex, inputSplit[1])
}
}
}