This repository has been archived by the owner on Dec 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpokemon.py
117 lines (96 loc) · 3.02 KB
/
pokemon.py
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
import json
from enum import auto
import utils
with open("data/pokedex.json", "r", encoding="utf-8") as f:
pokedex_by_id = json.load(f)
pokedex_by_name = {value: key for key, value in pokedex_by_id.items()}
with open("data/categories.json", "r", encoding="utf-8") as f:
categories = json.load(f)
class Generation(utils.OrderedEnum):
GEN1 = 1
GEN2 = 2
GEN3 = 3
GEN4 = 4
GEN5 = 5
GEN6 = 6
GEN7 = 7
@staticmethod
def from_pokemon_id(id):
if not (1 <= id <= len(pokedex_by_id)):
raise KeyError
if id >= 722:
return Generation.GEN7
if id >= 650:
return Generation.GEN6
if id >= 494:
return Generation.GEN5
if id >= 387:
return Generation.GEN4
if id >= 252:
return Generation.GEN3
if id >= 152:
return Generation.GEN2
else:
return Generation.GEN1
def __str__(self):
return f"{self.value}G"
class Category(utils.OrderedEnum):
Common = auto()
Uncommon = auto()
Rare = auto()
Mythical = auto()
Legendary = auto()
@staticmethod
def from_pokemon_id(id):
id = str(id)
if id not in pokedex_by_id:
raise KeyError
name = pokedex_by_id[id]
return Category.from_pokemon_name(name)
@staticmethod
def from_pokemon_name(name):
if name in categories['Legendary']:
return Category.Legendary
if name in categories['Mythical']:
return Category.Mythical
if name in categories['Rare']:
return Category.Rare
if name in categories['Uncommon']:
return Category.Uncommon
return Category.Common
@staticmethod
def parse(arg):
if not isinstance(arg, str):
return
if arg.isdigit():
return Category(int(arg))
else:
return Category[arg.title()]
def __str__(self):
return self.name
class Pokemon:
def __init__(self, id, name, alolan=False):
self.id = id
self.name = name
self.alolan = alolan
self.generation = Generation.from_pokemon_id(self.id)
self.category = Category.from_pokemon_name(str(self))
def __repr__(self):
variant = "Alolan " if self.alolan else ""
return f"{variant}{self.name} [{self.category}] (#{str(self.id).zfill(3)} {self.generation})"
def __str__(self):
variant = "Alolan " if self.alolan else ""
return f"{variant}{self.name}"
def to_dict(self):
return {key: (value.to_dict() if callable(getattr(value, "to_dict", None)) else value) for
key, value in self.__dict__.items()}
@classmethod
def from_id(cls, id):
name = pokedex_by_id[str(id)]
return cls(int(id), name)
@classmethod
def from_name(cls, name):
alolan = "Alolan" in name
name = name.replace("Alolan", "").strip()
id = int(pokedex_by_name[name])
return cls(id, name, alolan=alolan)