-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_handler.go
More file actions
168 lines (137 loc) · 3.42 KB
/
Copy pathcommand_handler.go
File metadata and controls
168 lines (137 loc) · 3.42 KB
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
package main
import (
"fmt"
"math/rand"
"os"
"errors"
"github.com/Toha764/pokedexcli/internal/pokeapi"
)
// - Quit
func commandExit(cfg *config, args ...string) error {
fmt.Print("Closing the Pokedex... Goodbye!")
os.Exit(0)
return nil
}
// - Help list
func commandHelp(cfg *config, args ...string) error {
fmt.Println("Welcome to the Pokedex!")
fmt.Printf("Usage: \n\n")
for _, c := range getCommands() {
fmt.Printf("%s: %s\n", c.name, c.description)
}
return nil
}
// - Next Map List
func commandMapNext(cfg *config, args ...string) error {
// fetches next 20 positions
resp, err1 := cfg.pokeapiClient.ListLocations(cfg.nextUrl)
if err1 != nil {
return fmt.Errorf("error occured: %v", err1)
}
cfg.nextUrl = resp.Next
cfg.prevUrl = resp.Previous
for _, result := range resp.Results {
fmt.Println(result.Name)
}
return nil
}
// - Previous Map List
func commandMapPrev(cfg *config, args ...string) error {
// fetches previous 20 positions
if cfg.prevUrl == nil {
fmt.Println("This is the first page")
return nil
}
resp, err2 := cfg.pokeapiClient.ListLocations(cfg.prevUrl)
if err2 != nil {
return fmt.Errorf("error occured: %v", err2)
}
cfg.nextUrl = resp.Next
cfg.prevUrl = resp.Previous
for _, result := range resp.Results {
fmt.Println(result.Name)
}
return nil
}
// - Explore
func commandExplore(cfg *config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("you must provide a location name")
}
name := args[0]
location, err := cfg.pokeapiClient.ListPokemon(&name)
if err != nil {
return err
}
fmt.Printf("Exploring %s...\n", location.Name)
fmt.Println("Found Pokemon: ")
for _, e := range location.PokemonEncounters {
fmt.Printf(" - %s\n", e.Pokemon.Name)
}
return nil
}
// - Catch
func commandCatch(cfg *config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("must provide a name for the pokemon to catch")
}
pokeName := args[0]
fmt.Printf("Throwing a Pokeball at %v...\n", pokeName)
pokemon, err := cfg.pokeapiClient.CatchPokemon(&pokeName)
if err != nil {
return err
}
if catchAttempt(pokemon) == false {
fmt.Printf("%v escaped!\n", pokeName)
return nil
}
fmt.Printf("%v was caught!\n", pokeName)
cfg.pokedex.Pokemons[pokeName] = pokemon
return nil
}
// - Inspect
func commandInspect(cfg *config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("must provide a pokemon name")
}
pokeName := args[0]
pokemon, ok := cfg.pokedex.Pokemons[pokeName]
if !ok {
return errors.New("you have not caught that pokemon")
}
fmt.Printf("Name: %v\nHeight: %v\nWeight: %v\n",
pokemon.Name,
pokemon.Height,
pokemon.Weight,
)
fmt.Println("Stats:")
for _, stat := range pokemon.Stats {
fmt.Printf(" -%s: %v\n", stat.Stat.Name, stat.BaseStat)
}
fmt.Println("Types:")
for _, typeInfo := range pokemon.Types {
fmt.Println(" -", typeInfo.Type.Name)
}
return nil
}
// - Pokedex
func commandPokedex(cfg *config, args... string) error {
if len(cfg.pokedex.Pokemons) == 0 {
return fmt.Errorf("no pokemon collected")
}
fmt.Println("Your Pokedex: ")
for _, pokemon := range cfg.pokedex.Pokemons {
fmt.Printf(" - %v\n",pokemon.Name)
}
return nil
}
// ----------- helper functions -----------
// - Catch Success or Fail
func catchAttempt(poke pokeapi.Pokemon) bool {
chance := 90.0 - (float64(poke.BaseExperience) / 3.0)
if chance < 10 {
chance = 10 // min chance
}
roll := rand.Float64() * 100
return roll < chance
}