-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_callbacks.go
More file actions
144 lines (113 loc) · 3.02 KB
/
cmd_callbacks.go
File metadata and controls
144 lines (113 loc) · 3.02 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
package main
import (
"errors"
"fmt"
"math"
"math/rand"
"os"
)
const (
decayScalar = 1.0 / 160.0
)
func cmdHelp(cfg *config, _ ...string) error {
fmt.Println("Usage:")
fmt.Println("--------------------")
for _, cmd := range getCommands() {
fmt.Printf("%s: %s\n", cmd.name, cmd.description)
}
return nil
}
func cmdMapf(cfg *config, _ ...string) error {
respLocations, err := cfg.pokeapiClient.GetLocations(cfg.nextLocationsURL)
if err != nil {
return err
}
cfg.nextLocationsURL = respLocations.Next
cfg.prevLocationsURL = respLocations.Previous
for _, locations := range respLocations.Results {
fmt.Println(locations.Name)
}
return nil
}
func cmdMapb(cfg *config, _ ...string) error {
if cfg.prevLocationsURL == nil {
return errors.New("you're on the first page, can't go to previous page")
}
respLocations, err := cfg.pokeapiClient.GetLocations(cfg.prevLocationsURL)
if err != nil {
return err
}
cfg.nextLocationsURL = respLocations.Next
cfg.prevLocationsURL = respLocations.Previous
for _, locations := range respLocations.Results {
fmt.Println(locations.Name)
}
return nil
}
func cmdExplore(cfg *config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("please provide a location to explore")
}
locationName := args[0]
respLocationArea, err := cfg.pokeapiClient.GetLocationAreaPokemon(&locationName)
if err != nil {
return fmt.Errorf("please provide a valid location")
}
fmt.Printf("Exploring %s\n", locationName)
fmt.Println("Found pokemon:")
for _, encounter := range respLocationArea.PokemonEncounters {
fmt.Printf(" - %s\n", encounter.Pokemon.Name)
}
return nil
}
func cmdCatch(cfg *config, args ...string) error {
if len(args) != 1 {
return fmt.Errorf("please provide a valid name for a pokemon")
}
name := args[0]
pokemon, err := cfg.pokeapiClient.GetPokemon(&name)
if err != nil {
return fmt.Errorf("please provide a valid name for a pokemon")
}
roll := rand.Float64()
fmt.Printf("throwing a pokeball at %s...\n", name)
success := (roll < (math.Pow(0.5, float64(pokemon.BaseExperience)*decayScalar)))
if !success {
fmt.Printf("%s escaped!\n", name)
return nil
}
cfg.caughtPokemon[name] = pokemon
fmt.Printf("%s was caught!\n", name)
return nil
}
func cmdInspect(cfg *config, args ...string) error {
name := args[0]
pokemon, ok := cfg.caughtPokemon[name]
if !ok {
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 _, typ := range pokemon.Types {
fmt.Printf(" - %s\n", typ.Type.Name)
}
return nil
}
func cmdPokedex(cfg *config, _ ...string) error {
fmt.Println("Your Pokedex:")
for _, pokemon := range cfg.caughtPokemon {
fmt.Printf(" - %s\n", pokemon.Name)
}
return nil
}
func cmdExit(cfg *config, _ ...string) error {
os.Exit(0)
return nil
}