-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
172 lines (142 loc) · 4.38 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
package main
import (
"errors"
"fmt"
"math/rand/v2"
"os"
"sort"
"strings"
"time"
)
type cliCommand struct {
name string
description string
callback func(*config, ...string) error
}
func commandExit(config *config, args ...string) error {
fmt.Printf("%sShutting down the Pokedex...", YELLOW)
time.Sleep(1 * time.Second)
fmt.Printf(" Goodbye!%s\n", RESET)
os.Exit(0)
return nil
}
func commandHelp(config *config, args ...string) error {
fmt.Printf("\n%sWelcome to the Pokedex!%s\n", YELLOW, RESET)
fmt.Printf("%sUsage:%s\n", GRAY, RESET)
fmt.Println()
// Sort mapped commands
commands := getCommands()
keys := make([]string, 0, len(commands))
for key := range commands {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
fmt.Printf("%s%s:%s %s\n", CYAN, commands[key].name, RESET, commands[key].description)
}
fmt.Println()
return nil
}
func commandMapf(config *config, args ...string) error {
locationPages, err := config.pokeapiClient.ListLocations(config.nextLocationsURL)
if err != nil {
return err
}
config.nextLocationsURL = locationPages.Next
config.prevLocationsURL = locationPages.Previous
for _, location := range locationPages.Results {
fmt.Printf("%s%s%s\n", BLUE, location.Name, RESET)
}
return nil
}
func commandMapb(config *config, args ...string) error {
if config.prevLocationsURL == nil {
return errors.New("you're on the first page")
}
locationsPage, err := config.pokeapiClient.ListLocations(config.prevLocationsURL)
if err != nil {
return err
}
config.nextLocationsURL = locationsPage.Next
config.prevLocationsURL = locationsPage.Previous
for _, location := range locationsPage.Results {
fmt.Printf("%s%s%s\n", BLUE, location.Name, RESET)
}
return nil
}
func commandExplore(config *config, args ...string) error {
if len(args) == 0 {
return errors.New("you must provide a location")
}
location, err := config.pokeapiClient.GetLocation(args[0])
if err != nil {
return fmt.Errorf("error fetching location: %w", err)
}
fmt.Printf("%sExploring %s...%s\n\n", YELLOW, location.Name, RESET)
time.Sleep(1 * time.Second)
fmt.Printf("%sFound Pokemon: %s\n", BLUE, RESET)
time.Sleep(1 * time.Second)
for _, enc := range location.PokemonEncounters {
fmt.Printf("%s - %s%s\n", CYAN, strings.ToUpper(enc.Pokemon.Name[:1])+enc.Pokemon.Name[1:], RESET)
time.Sleep(200 * time.Millisecond)
}
fmt.Println()
return nil
}
func commandCatch(config *config, args ...string) error {
if len(args) == 0 {
return errors.New("you must provide a pokemon")
}
pokemon, err := config.pokeapiClient.GetPokemon(args[0])
if err != nil {
return fmt.Errorf("error fetching pokemon: %w", err)
}
pokemonName := strings.ToUpper(pokemon.Name[:1]) + pokemon.Name[1:]
fmt.Printf("%sThrowing a %sPokeball %sat %s%s", WHITE, RED, WHITE, BLUE, pokemonName)
for range 3 {
time.Sleep(1 * time.Second)
fmt.Print(".")
}
fmt.Print("\n")
chance := rand.IntN(pokemon.BaseExperience)
if chance > 40 {
fmt.Printf("%s%s %sescaped!\n", BLUE, pokemonName, RESET)
return nil
}
fmt.Printf("%s%s %swas caught!\n", BLUE, pokemonName, RESET)
_, exists := config.caughtPokemon[pokemon.Name]
if !exists {
time.Sleep(1 * time.Second)
fmt.Printf("%s%s's data has been uploaded to the pokedex!%s\n\n", BLUE, pokemonName, RESET)
}
config.caughtPokemon[pokemon.Name] = pokemon
return nil
}
func commandInspect(config *config, args ...string) error {
if len(args) == 0 {
return errors.New("you must provide a pokemon")
}
pokemon, exists := config.caughtPokemon[args[0]]
if !exists {
return errors.New("you have not caught that pokemon yet")
}
fmt.Printf("%sName: %s%s\n", CYAN, RESET, strings.ToUpper(pokemon.Name[:1])+pokemon.Name[1:])
fmt.Printf("%sHeight: %s%v\n", CYAN, RESET, pokemon.Height)
fmt.Printf("%sWeight: %s%v\n", CYAN, RESET, pokemon.Weight)
fmt.Printf("%sStats: %s\n", CYAN, RESET)
for _, stat := range pokemon.Stats {
fmt.Printf(" %s- %s: %s%v\n", CYAN, stat.Stat.Name, RESET, stat.BaseStat)
}
fmt.Printf("%sTypes: %s\n", CYAN, RESET)
for _, typeInfo := range pokemon.Types {
fmt.Printf(" %s- %s%s\n", CYAN, RESET, typeInfo.Type.Name)
}
return nil
}
func commandPokedex(config *config, args ...string) error {
fmt.Printf("%sYour Pokedex:%s\n", BLUE, RESET)
for _, pokemon := range config.caughtPokemon {
fmt.Printf(" %s- %s%s\n", BLUE, RESET, strings.ToUpper(pokemon.Name[:1])+pokemon.Name[1:])
}
return nil
}