-
Notifications
You must be signed in to change notification settings - Fork 18
/
pokedex.go
79 lines (69 loc) · 1.52 KB
/
pokedex.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
package main
import (
"strconv"
"fmt"
"encoding/json"
"strings"
"errors"
)
const (
POKEDEX_POKEMON_ICON_URL string = "https://ugc.pokevision.com/images/pokemon/%v.png"
)
var (
// Rattata
// Pidgey
// Weedle
// Caterpie
// Zubat
COMMON_POKEMON []int = []int{19, 16, 13, 10, 41}
)
type PokedexPokemon struct {
Index int
Name string
}
type Pokedex struct {
index map[int]PokedexPokemon
}
func LoadPokedex(locale string) (Pokedex, error) {
pokedex := Pokedex{}
file, err := Asset("pokedex." + locale + ".json")
if err != nil {
return pokedex, err
}
var unparsed map[string]interface{}
err = json.Unmarshal(file, &unparsed)
if err != nil {
return pokedex, err
}
pokedex.index = make(map[int]PokedexPokemon)
for key, value := range unparsed {
index, err := strconv.Atoi(key)
if err != nil {
return Pokedex{}, err
}
pokedex.index[index] = PokedexPokemon{index, value.(string)}
}
return pokedex, nil
}
func (dex *Pokedex) Get(index int) PokedexPokemon {
return dex.index[index]
}
func (dex *Pokedex) GetByName(name string) (PokedexPokemon, error) {
for _, pokedexPokemon := range dex.index {
if strings.ToLower(pokedexPokemon.Name) == strings.ToLower(name) {
return pokedexPokemon, nil
}
}
return PokedexPokemon{}, errors.New("Unknown Pokémon named " + name)
}
func (p *PokedexPokemon) Icon() string {
return fmt.Sprintf(POKEDEX_POKEMON_ICON_URL, p.Index)
}
func (p *PokedexPokemon) IsCommon() bool {
for _, index := range COMMON_POKEMON {
if p.Index == index {
return true
}
}
return false
}