-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
162 lines (132 loc) · 3.64 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
package main
import (
"errors"
"fmt"
"math/rand"
"os"
)
func commandCatch(cfg *config, args ...string) error {
if len(args) != 1 {
return errors.New("no pokemon name provided")
}
pokemonName := args[0]
pokemon, err := cfg.pokeapiClient.GetPokemon(pokemonName)
if err != nil {
return err
}
// BaseExperience: higher for harder to catch pokemon,
// lower for easier to catch pokemon
// threshold is arbitrary number below which
// we "catch" a pokemon
const threshold = 50
randomNumber := rand.Intn(pokemon.BaseExperience)
fmt.Println(pokemon.BaseExperience, randomNumber, threshold)
if randomNumber > threshold {
fmt.Printf("Failed to catch %s!\n\n", pokemonName)
return nil
}
cfg.caughtPokemon[pokemonName] = pokemon
fmt.Printf("%s pokemon was caught!\n\n", pokemonName)
return nil
}
func commandExit(cfg *config, args ...string) error {
fmt.Println("Goodbye!")
os.Exit(0)
return nil
}
func commandExplore(cfg *config, args ...string) error {
if len(args) != 1 {
return errors.New("no location area specified")
}
locationAreaName := args[0]
locationArea, err := cfg.pokeapiClient.GetLocationArea(locationAreaName)
if err != nil {
return err
}
fmt.Printf("Pokemon in %s:\n", locationArea.Name)
for _, pokemon := range locationArea.PokemonEncounters {
fmt.Printf(" - %s\n", pokemon.Pokemon.Name)
}
fmt.Printf("\n")
return nil
}
func commandHelp(cfg *config, args ...string) error {
fmt.Printf("\nWelcome to the Pokedex Help Menu\n")
fmt.Printf("Here are the available commands:\n")
commandList := createCommandList()
for _, command := range commandList {
fmt.Printf(" - %s: %s\n", command.name, command.description)
}
fmt.Printf("\n")
return nil
}
func commandInspect(cfg *config, args ...string) error {
if len(args) != 1 {
return errors.New("no pokemon name provided")
}
pokemonName := args[0]
pokemon, ok := cfg.caughtPokemon[pokemonName]
if !ok {
return errors.New("you haven't caught this pokemon yet")
}
fmt.Printf("Name: %s\n", pokemon.Name)
fmt.Printf("Height: %v\n", pokemon.Height)
fmt.Printf("Weight: %v\n", pokemon.Weight)
fmt.Println("Stats:")
for _, stat := range pokemon.Stats {
fmt.Printf(" - %s: %v\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println("Types:")
for _, typ := range pokemon.Types {
fmt.Printf(" - %s\n", typ.Type.Name)
}
fmt.Printf("\n")
return nil
}
func commandMap(cfg *config, args ...string) error {
res, err := cfg.pokeapiClient.ListLocationAreas(cfg.nextLocationAreaURL)
if err != nil {
return err
}
fmt.Printf("\nLocation Areas:\n")
for _, area := range res.Results {
fmt.Printf(" - %s\n", area.Name)
}
fmt.Printf("\n")
cfg.nextLocationAreaURL = res.Next
cfg.previousLocationAreaURL = res.Previous
return nil
}
func commandMapBack(cfg *config, args ...string) error {
if cfg.previousLocationAreaURL == nil {
return errors.New("oops! you're on the first page")
}
res, err := cfg.pokeapiClient.ListLocationAreas(cfg.previousLocationAreaURL)
if err != nil {
return err
}
fmt.Printf("\nLocation Areas:\n")
for _, area := range res.Results {
fmt.Printf(" - %s\n", area.Name)
}
fmt.Printf("\n")
cfg.nextLocationAreaURL = res.Next
cfg.previousLocationAreaURL = res.Previous
return nil
}
func commandPokedex(cfg *config, args ...string) error {
fmt.Println("Pokemon in Pokedex:")
for _, pokemon := range cfg.caughtPokemon {
fmt.Printf(" %s\n", pokemon.Name)
fmt.Println(" stats:")
for _, stat := range pokemon.Stats {
fmt.Printf(" - %s: %v\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println(" types:")
for _, typ := range pokemon.Types {
fmt.Printf(" - %s\n", typ.Type.Name)
}
}
fmt.Printf("\n")
return nil
}