-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
192 lines (159 loc) · 4.06 KB
/
command.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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
)
type replCommand struct {
callback func(*config, ...string) error
name string
description string
}
func getCommands() map[string]replCommand {
return map[string]replCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"map": {
name: "map",
description: "Get the next page of locations",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "Get the previous page of locations",
callback: commandMapb,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
"explore": {
name: "explore <location_name>",
description: "List of all the Pokemon in a given area",
callback: commandExplore,
},
"catch": {
name: "catch <pokemon_name>",
description: "Try to catch the Pokemon",
callback: commandCatch,
},
"inspect": {
name: "inspect <pokemon_name>",
description: "Get info about a catched Pokemon",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "List all your caught Pokemons",
callback: commandPokedex,
},
}
}
func commandHelp(cfg *config, params ...string) error {
fmt.Print("\nWelcome to the Pokedex\nUsage:\n\n")
for _, c := range getCommands() {
fmt.Printf("%s: %s\n", c.name, c.description)
}
fmt.Println()
return nil
}
func commandExit(cfg *config, params ...string) error {
os.Exit(0)
return nil
}
func commandMap(cfg *config, params ...string) error {
locations, err := cfg.pokeapiClient.FetchLocations(cfg.nextLocationsURL)
if err != nil {
return err
}
cfg.nextLocationsURL = locations.Next
cfg.prevLocationsURL = locations.Previous
for _, result := range locations.LocationList {
fmt.Println(result.Name)
}
return nil
}
func commandMapb(cfg *config, params ...string) error {
locations, err := cfg.pokeapiClient.FetchLocations(cfg.prevLocationsURL)
if err != nil {
return err
}
cfg.nextLocationsURL = locations.Next
cfg.prevLocationsURL = locations.Previous
for _, location := range locations.LocationList {
fmt.Println(location.Name)
}
return nil
}
func commandExplore(cfg *config, params ...string) error {
if len(params) != 1 {
return errors.New("you must provide a Location name")
}
locationName := params[0]
fmt.Printf("Exploring %s...\n", locationName)
location, err := cfg.pokeapiClient.FetchOneLocation(locationName)
if err != nil {
return err
}
fmt.Println("Found Pokemon:")
for _, p := range location.PokemonEncounters {
fmt.Printf(" - %s\n", p.Pokemon.Name)
}
return nil
}
func commandCatch(cfg *config, params ...string) error {
if len(params) != 1 {
return errors.New("you must provide a Pokemon name")
}
name := params[0]
fmt.Printf("Throwing a Pokeball at %s...\n", name)
pokemon, err := cfg.pokeapiClient.FetchPokemonInfo(name)
if err != nil {
return err
}
const maxBaseExp = 608.0
catchProb := 1 - pokemon.BaseExp/maxBaseExp
if rand.Float64() >= catchProb {
fmt.Printf("%s escaped!\n", name)
} else {
fmt.Printf("%s was caught!\n", name)
fmt.Println("You may now inspect it with the inspect command.")
cfg.catchedPokemons[name] = pokemon
}
return nil
}
func commandInspect(cfg *config, params ...string) error {
if len(params) != 1 {
return errors.New("you must provide a Pokemon name")
}
name := params[0]
pokemon, ok := cfg.catchedPokemons[name]
if !ok {
fmt.Println("you have not caught that pokemon")
return nil
}
fmt.Printf("Name: %s\n", pokemon.Name)
fmt.Printf("Height: %d\n", pokemon.Height)
fmt.Printf("Weight: %d\n", pokemon.Weight)
fmt.Println("Stats:")
for _, s := range pokemon.Stats {
fmt.Printf(" -%s: %d\n", s.Stat.Name, s.BaseStat)
}
fmt.Println("Types:")
for _, t := range pokemon.Types {
fmt.Printf(" - %s\n", t.Type.Name)
}
return nil
}
func commandPokedex(cfg *config, params ...string) error {
fmt.Println("Your Pokedex:")
for _, p := range cfg.catchedPokemons {
fmt.Printf(" - %s\n", p.Name)
}
return nil
}