-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
199 lines (154 loc) · 4.06 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
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
package main
import (
"bufio"
"errors"
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/fadhilradh/pokedex-cli/config"
)
type cliCommand struct {
name string
description string
callback func(*config.Config, ...string) error
}
func commandHelp(cfg *config.Config, params ...string) error {
fmt.Println("Available commands:")
fmt.Println()
for _, cmd := range mainCommands() {
fmt.Printf("%s : %s\n", cmd.name, cmd.description)
}
fmt.Println()
return nil
}
func commandExit(cfg *config.Config, params ...string) error {
os.Exit(0)
return nil
}
func commandMap(cfg *config.Config, params ...string) error {
nextUrl := cfg.NextLocURL
maps, err := cfg.Client.ListLocations(nextUrl)
if err != nil {
return err
}
for _, loc := range maps.Results {
fmt.Println(loc.Name)
}
fmt.Println()
cfg.NextLocURL = maps.Next
cfg.PrevLocURL = maps.Previous
return nil
}
func commandMapBack(cfg *config.Config, params ...string) error {
prevUrl := cfg.PrevLocURL
if prevUrl == nil {
fmt.Println("Oops. There is no previous map")
} else {
maps, err := cfg.Client.ListLocations(prevUrl)
if err != nil {
return err
}
for _, loc := range maps.Results {
fmt.Println(loc.Name)
}
fmt.Println()
cfg.NextLocURL = maps.Next
cfg.PrevLocURL = maps.Previous
}
return nil
}
func commandExplore(cfg *config.Config, args ...string) error {
areaName := args[0]
locDetail, err := cfg.Client.GetLocDetail(areaName)
if err != nil {
return err
}
// TODO : change to cases.Title
fmt.Println("\nExploring " + strings.ReplaceAll(strings.Title(areaName), "-", " ") + "... \n")
rand.NewSource(time.Now().UnixNano())
randIdx := rand.Intn(len(locDetail.PokemonEncounters))
pokemonName := locDetail.PokemonEncounters[randIdx].Pokemon.Name
pokemonDetail, err := cfg.Client.GetPokemon(pokemonName)
if err != nil {
return err
}
cfg.CurrentPokemonEncountered = pokemonDetail
// TODO : change to cases.Title
fmt.Printf("A wild %s encountered ! \n \n", strings.Title(pokemonName))
fmt.Println(
`What will you do ?
- battle
- run
[type one of the commands above to continue]
`)
scanner := bufio.NewScanner(os.Stdin)
GetInput(scanner, "vs "+pokemonName+" > ", EncounterCommands, false)
return nil
}
func commandCatch(cfg *config.Config, args ...string) error {
if len(args) != 1 {
return errors.New("you must provide a pokemon name")
}
pokemon, err := cfg.Client.GetPokemon(args[0])
if err != nil {
return err
}
chance := rand.Intn(pokemon.BaseExperience)
fmt.Printf("Throwing a Pokeball at %s...\n", pokemon.Name)
if chance > 40 {
fmt.Printf("%s escaped!", pokemon.Name)
fmt.Println()
return nil
}
cfg.CaughtPokemons[pokemon.Name] = pokemon
fmt.Printf("%s was caught ! \n", pokemon.Name)
fmt.Printf("You may now inspect it with 'inspect %s' command. \n", pokemon.Name)
return nil
}
func commandInspect(cfg *config.Config, args ...string) error {
if len(args) != 1 {
return errors.New("you must provide a pokemon name")
}
pokemon, exist := cfg.CaughtPokemons[args[0]]
if !exist {
fmt.Printf("You have not caught %s yet", args[0])
fmt.Println()
return nil
}
fmt.Printf("Name : %s \n", pokemon.Name)
fmt.Printf("Weight : %d kg \n ", pokemon.Weight)
fmt.Printf("Height : %d cm \n", pokemon.Height)
fmt.Println("Stats")
for _, stat := range pokemon.Stats {
fmt.Printf("%s : %d \n", stat.Stat.Name, stat.BaseStat)
}
return nil
}
func commandPokedex(cfg *config.Config, args ...string) error {
pokemons := cfg.CaughtPokemons
if len(pokemons) == 0 {
fmt.Println("You have no pokemon. Catch them !")
return nil
}
fmt.Println("Here is your pokemon collection :")
for _, v := range cfg.CaughtPokemons {
fmt.Println("- ", v.Name)
}
return nil
}
func commandBattle(cfg *config.Config, params ...string) error {
fmt.Println("Battle ...")
return nil
}
func commandRun(cfg *config.Config, params ...string) error {
pokemonExp := cfg.CurrentPokemonEncountered.BaseExperience
randNum := rand.Intn(pokemonExp)
if randNum > 36 {
fmt.Println("Can't escape !")
return nil
}
fmt.Println("Running away ...")
return nil
}