-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (165 loc) · 4.34 KB
/
main.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
package main
import (
"bufio"
"fmt"
"github.com/etiedem/pokedexcli/internal/pokeapi"
"github.com/etiedem/pokedexcli/internal/pokecache"
"github.com/etiedem/pokedexcli/internal/pokeconfig"
"math/rand"
"os"
"strings"
"time"
)
type cliCommand struct {
name string
description string
callback func(*pokeconfig.Config, *string, *pokecache.Cache) error
}
func getCommands() map[string]cliCommand {
return map[string]cliCommand{
"map": {
name: "map",
description: "Displays the names of the next 20 locations",
callback: commandMap,
},
"mapb": {
name: "mapb",
description: "Displays the names of the previous 20 locations",
callback: commandMapb,
},
"explore": {
name: "explore",
description: "Displays the names of all the pokemon in an area",
callback: commandExplore,
},
"catch": {
name: "catch",
description: "Attempt to catch a pokemon",
callback: commandCatch,
},
"inspect": {
name: "inspect",
description: "Inspect a pokemon you have caught",
callback: commandInspect,
},
"pokedex": {
name: "pokedex",
description: "List all of your pokemon",
callback: commandPokedex,
},
"help": {
name: "help",
description: "Displays a help message",
callback: commandHelp,
},
"exit": {
name: "exit",
description: "Exit the Pokedex",
callback: commandExit,
},
}
}
func commandExplore(c *pokeconfig.Config, param *string, cache *pokecache.Cache) error {
resp := pokeapi.SearchLocation(*param, cache)
fmt.Println("Found Pokemon:")
for _, pokemon := range resp.PokemonEncounters {
fmt.Printf(" - %s\n", pokemon.Pokemon.Name)
}
return nil
}
func commandCatch(c *pokeconfig.Config, param *string, cache *pokecache.Cache) error {
resp := pokeapi.GetPokemon(*param, cache)
r := rand.Intn(resp.BaseExperience)
fmt.Printf("Throwing a Pokeball at %s...\n", resp.Name)
if r > 40 {
fmt.Printf("%s escaped!\n", resp.Name)
return nil
}
fmt.Printf("%s was caught!\n", resp.Name)
c.Pokedex[resp.Name] = resp
return nil
}
func commandInspect(c *pokeconfig.Config, param *string, cache *pokecache.Cache) error {
pokemon, exist := c.Pokedex[*param]
if exist == false {
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 _, stat := range pokemon.Stats {
fmt.Printf(" - %s: %d\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println("Types:")
for _, t := range pokemon.Types {
fmt.Printf(" - %s\n", t.Type.Name)
}
return nil
}
func commandPokedex(c *pokeconfig.Config, _ *string, _ *pokecache.Cache) error {
fmt.Println("Your Pokedex:")
for name := range c.Pokedex {
fmt.Printf(" - %s\n", name)
}
return nil
}
func commandMap(c *pokeconfig.Config, param *string, cache *pokecache.Cache) error {
resp := pokeapi.GetLocations(c, "next", cache)
for _, location := range resp.Locations {
fmt.Println(location.Name)
}
return nil
}
func commandMapb(c *pokeconfig.Config, param *string, cache *pokecache.Cache) error {
resp := pokeapi.GetLocations(c, "previous", cache)
for _, location := range resp.Locations {
fmt.Println(location.Name)
}
return nil
}
func commandHelp(_ *pokeconfig.Config, _ *string, _ *pokecache.Cache) error {
fmt.Println()
fmt.Println("Welcome to the Pokedex!")
fmt.Printf("Usage:\n\n")
for _, command := range getCommands() {
fmt.Printf("%s: %s\n", command.name, command.description)
}
fmt.Println()
return nil
}
func commandExit(_ *pokeconfig.Config, _ *string, _ *pokecache.Cache) error {
os.Exit(0)
return nil
}
func cleanInput(data string) []string {
output := strings.ToLower(data)
words := strings.Fields(output)
return words
}
func main() {
commands := getCommands()
scanner := bufio.NewScanner(os.Stdin)
config := &pokeconfig.Config{Pokedex: make(map[string]pokeconfig.Pokemon)}
cache := pokecache.NewCache(time.Minute)
for {
fmt.Printf("pokedex > ")
scanner.Scan()
words := cleanInput(scanner.Text())
if len(words) == 0 {
continue
}
commandName := words[0]
command, exists := commands[commandName]
var param *string
if len(words) > 1 {
param = &words[1]
}
if exists {
command.callback(config, param, cache)
} else {
fmt.Println("Unknown command")
}
}
}