-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
209 lines (181 loc) Β· 4.56 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"cmp"
"errors"
"fmt"
"io"
"maps"
"os"
"slices"
"github.com/zoumas/pokedexcli/internal/cache"
"github.com/zoumas/pokedexcli/internal/pokeapi"
)
type config struct {
w io.Writer
cache *cache.Cache
pokemon map[string]*pokeapi.Pokemon
next string
previous string
args []string
}
type command struct {
callback func(cfg *config) error
name string
description string
}
func commands() map[string]command {
return map[string]command{
"exit": {
callback: commandExit,
name: "exit",
description: "Exit the Pokedex",
},
"help": {
callback: commandHelp,
name: "help",
description: "Displays a help message",
},
"map": {
callback: commandMap,
name: "map",
description: "Display the names of the next 20 location areas of the Pokemon world",
},
"mapb": {
callback: commandMapb,
name: "mapb",
description: "The opposite of map. Displays the previous 20 location areas",
},
"explore": {
callback: commandExplore,
name: "explore",
description: "See possible encounters for the given location area",
},
"catch": {
callback: commandCatch,
name: "catch",
description: "Attempt to catch a wild Pokemon",
},
"inspect": {
callback: commandInspect,
name: "inspect",
description: "See information about a caught Pokemon",
},
"pokedex": {
callback: commandPokedex,
name: "pokedex",
description: "List the names of all caught Pokemon",
},
}
}
func commandExit(cfg *config) error {
fmt.Fprintln(cfg.w, "Closing the Pokedex... Goodbye!")
os.Exit(0)
return nil
}
func commandHelp(cfg *config) error {
cmds := commands()
if len(cfg.args) != 0 {
cmd, ok := cmds[cfg.args[0]]
if !ok {
return fmt.Errorf("Unknown command: %q", cfg.args[0])
}
fmt.Fprintf(cfg.w, "%s: %s\n", cmd.name, cmd.description)
return nil
}
fmt.Fprintln(cfg.w, "Welcome to the Pokedex!")
fmt.Fprintln(cfg.w, "Usage:")
fmt.Fprintln(cfg.w)
for _, cmd := range cmds {
fmt.Fprintf(cfg.w, "%s: %s\n", cmd.name, cmd.description)
}
return nil
}
func commandMap(cfg *config) error {
if cfg.next == "" {
fmt.Fprintln(cfg.w,
"An unseen force prevents you from going forward. It seems this is the end...")
return nil
}
return listLocationsAreas(cfg, cfg.next)
}
func commandMapb(cfg *config) error {
if cfg.previous == "" {
fmt.Fprintln(cfg.w,
"A gust of wind blows leaves around π... There is nothing back there.")
return nil
}
return listLocationsAreas(cfg, cfg.previous)
}
func listLocationsAreas(cfg *config, url string) error {
l, err := pokeapi.GetLocationAreas(cfg.cache, url)
if err != nil {
return err
}
cfg.next = l.Next
cfg.previous = l.Previous
for _, r := range l.Results {
fmt.Fprintln(cfg.w, r.Name)
}
return nil
}
func commandExplore(cfg *config) error {
if len(cfg.args) == 0 {
return fmt.Errorf("There is nothing to explore...")
}
name := cfg.args[0]
fmt.Fprintf(cfg.w, "Exploring %s...\n", name)
l, err := pokeapi.GetLocationArea(cfg.cache, name)
if err != nil {
return fmt.Errorf("Something went wrong while exploring: %v", err)
}
for _, e := range l.PokemonEncounters {
fmt.Fprintln(cfg.w, "-", e.Pokemon.Name)
}
return nil
}
func commandCatch(cfg *config) error {
if len(cfg.args) == 0 {
return fmt.Errorf("There is nothing to catch...")
}
name := cfg.args[0]
p, err := pokeapi.GetPokemon(name)
if err != nil {
return errors.New("An unseen force prevents you from throwing a Pokeball...")
}
fmt.Fprintf(cfg.w, "Throwing a Pokeball at %s...\n", name)
cfg.pokemon[name] = p
fmt.Fprintf(cfg.w, "%s has been caught!\n", name)
return nil
}
func commandInspect(cfg *config) error {
if len(cfg.args) == 0 {
return fmt.Errorf("There is nothing to inspect...")
}
name := cfg.args[0]
p, ok := cfg.pokemon[name]
if !ok {
return fmt.Errorf("You haven't caught %s yet!", name)
}
fmt.Fprintf(cfg.w, "Name: %s\n", p.Name)
fmt.Fprintf(cfg.w, "Height: %d\n", p.Height)
fmt.Fprintf(cfg.w, "Weight: %d\n", p.Weight)
fmt.Fprintf(cfg.w, "Stats:\n")
for _, s := range p.Stats {
fmt.Fprintf(cfg.w, "\t- %s: %d\n", s.Stat.Name, s.BaseStat)
}
fmt.Fprintf(cfg.w, "Types:\n")
for _, t := range p.Types {
fmt.Fprintf(cfg.w, "\t- %s\n", t.Type.Name)
}
return nil
}
func commandPokedex(cfg *config) error {
xs := slices.Collect(maps.Values(cfg.pokemon))
slices.SortStableFunc(xs, func(a, b *pokeapi.Pokemon) int {
return cmp.Compare(a.Order, b.Order)
})
for _, x := range xs {
fmt.Fprintf(cfg.w, "%4d. %s\n", x.Order, x.Name)
}
return nil
}