-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
173 lines (162 loc) · 4.07 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
package main
import (
"errors"
"fmt"
"github.com/yuisofull/pokedexcli/pokeapi"
"math/rand"
"os"
)
type cliCommand struct {
name string
description string
callback func(cfg *config, param ...string) error
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
"map": {
name: "map",
description: "displays the next names of 20 location areas",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "displays the previous names of 20 location areas",
callback: commandMapBack,
},
"explore": {
name: "explore",
description: "displays Pokemon's names in a location area. Usage: explore <area_name>",
callback: commandExplore,
},
"catch": {
name: "catch",
description: "catch a pokemon. Usage: catch <pokemon_name>",
callback: commandCatch,
},
"inspect": {
name: "inspect",
description: "inspect a pokemon. Usage: inspect <pokemon_name>",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "list all pokemons you have caught",
callback: commandPokedex,
},
}
}
func commandPokedex(cfg *config, param ...string) error {
fmt.Println("Your Pokedex: ")
for pkm := range cfg.PokeDex {
fmt.Println(" - " + pkm)
}
return nil
}
func commandInspect(cfg *config, param ...string) error {
if len(param) == 0 {
return errors.New("missing 1 argument. Usage: inspect <pokemon_name>")
}
if pkm, exist := cfg.PokeDex[param[0]]; !exist {
fmt.Println("you have not caught that pokemon")
return nil
} else {
fmt.Printf(`Name: %s
Height: %d
Weight: %d
Stats:
`, pkm.Name, pkm.Height, pkm.Weight)
for _, stat := range pkm.Stats {
fmt.Printf(" -%s: %d\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println("Types:")
for _, ty := range pkm.Types {
fmt.Printf(" -%s\n", ty.Type.Name)
}
}
return nil
}
func commandCatch(cfg *config, param ...string) error {
if len(param) == 0 {
return errors.New("missing 1 argument. Usage: catch <pokemon_name>")
}
if _, exist := cfg.PokeDex[param[0]]; exist {
fmt.Println("you have already caught it!")
return nil
}
pkm, err := pokeapi.GetPokemon(&cfg.pokeapiClient, param[0])
if err != nil {
return err
}
fmt.Printf("Throwing a Pokeball at %s...\n", param[0])
r := rand.Intn(pkm.BaseExperience)
if r > 40 {
fmt.Printf("%s escaped!\n", param[0])
return nil
}
fmt.Printf("%s was caught!\n"+
"You may now inspect it with the inspect command.\n", param[0])
cfg.PokeDex[pkm.Name] = *pkm
return nil
}
func commandExplore(cfg *config, param ...string) error {
if len(param) == 0 {
return errors.New("missing 1 argument. Usage: explore <area_name>")
}
laInfo, err := pokeapi.GetLocationAreaInfo(&cfg.pokeapiClient, param[0])
if err != nil {
return err
}
fmt.Println(`Exploring ` + param[0] + `...
Found Pokemon: `)
for _, pokemon := range laInfo.PokemonEncounters {
fmt.Println(" - " + pokemon.Pokemon.Name)
}
return nil
}
func commandMapBack(cfg *config, param ...string) error {
la, err := pokeapi.GetPreviousLocationArea(&cfg.pokeapiClient)
if err != nil {
//fmt.Println("Cannot get the previous location area")
return err
}
for _, location := range la.Results {
fmt.Println(*location.Name)
}
return nil
}
func commandMap(cfg *config, param ...string) error {
la, err := pokeapi.GetNextLocationArea(&cfg.pokeapiClient)
if err != nil {
//fmt.Println("Cannot get the next location area")
return err
}
for _, location := range la.Results {
fmt.Println(*location.Name)
}
return nil
}
func commandExit(cfg *config, param ...string) error {
os.Exit(0)
return nil
}
func commandHelp(cfg *config, param ...string) error {
fmt.Println()
fmt.Println("Welcome to the Pokedex!")
fmt.Println("Usage:")
fmt.Println()
for _, cmd := range getCommands() {
fmt.Printf("%s: %s\n", cmd.name, cmd.description)
}
fmt.Println()
return nil
}