-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_catch.go
More file actions
87 lines (73 loc) · 1.67 KB
/
command_catch.go
File metadata and controls
87 lines (73 loc) · 1.67 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
package main
import (
"math/rand"
"encoding/json"
"fmt"
"io"
"net/http"
)
type PokemonDetails struct {
BaseExp int `json:"base_experience"`
Name string `json:"name"`
Height int `json:"height"`
Weight int `json:"weight"`
Stats []struct {
Base_stat int `json:"base_stat"`
Stat_name struct {
Name string `json:"name"`
} `json:"stat"`
} `json:"stats"`
Types []struct {
Type struct {
Name string `json:"name"`
} `json:"type"`
} `json:"types"`
}
var pokemon_caught = make(map[string]PokemonDetails)
func commandCatch(location *config, args []string) error {
if args == nil {
fmt.Println("Need to give pokemon name with catch command")
return nil
}
pokemon_name := args[0]
url := "https://pokeapi.co/api/v2/pokemon/" + pokemon_name
if _, ok := pokemon_caught[pokemon_name]; ok {
fmt.Println("Pokemon already in inventory!")
return nil
}
var data []byte
val, ok := location.TheCache.Get(url)
if ok {
data = val
} else {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
data, err = io.ReadAll(res.Body)
if err != nil {
return err
}
}
var poke_details PokemonDetails
if err := json.Unmarshal(data, &poke_details); err != nil {
fmt.Println(pokemon_name, "doesn't exist!")
return nil
}
caught := false
if rand.Intn(100) < int(85.0 - (float64(poke_details.BaseExp)/15.0)) {
caught = true
}
fmt.Printf("Throwing a Pokeball at %v...\n", poke_details.Name,)
if !caught {
fmt.Println(poke_details.Name, "escaped!")
return nil
}
fmt.Println(poke_details.Name, "was caught!")
pokemon_caught[pokemon_name] = poke_details
if !ok {
location.TheCache.Add(pokemon_name, data)
}
return nil
}