-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
281 lines (227 loc) · 9.76 KB
/
utils.py
File metadata and controls
281 lines (227 loc) · 9.76 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from pydantic import BaseModel
from fastapi import HTTPException
from typing import List, Dict, Tuple
import random
import csv
from logging_config import get_logger
import httpx
from collections import defaultdict
# __name__ will set logger name as the file name: 'utils'
logger = get_logger(__name__)
type_advantages = {}
# Define Pokemon class
class Pokemon(BaseModel):
name: str
level: int = 1
# Stats directly taken from pokeAPI
# TODO: later find out if can use type annotation
# to get attribute with dict{base_stat: int, effort: int}
hp: Dict[str, int]
attack: Dict[str, int]
defense: Dict[str, int]
special_attack: Dict[str, int]
special_defense: Dict[str, int]
speed: Dict[str, int]
types: List[str]
# updated stats based on custom formula
hp_updated: float = 0
attack_updated: float = 0
defense_updated: float = 0
special_attack_updated: float = 0
special_defense_updated: float = 0
speed_updated: float = 0
model_config = {
"json_schema_extra": {
"examples": [
{
"name": "FakeMon",
"level": 5,
"hp": {"base_stat": 10, "effort": 0},
"attack": {"base_stat": 30, "effort": 0},
"defense": {"base_stat": 35, "effort": 1},
"special_attack": {"base_stat": 25, "effort": 2},
"special_defense": {"base_stat": 10, "effort": 0},
"speed": {"base_stat": 10, "effort": 1},
"types": ["electric", "water"],
"hp_updated": 10,
"attack_updated": 15,
"defense_updated": 40,
"special_attack_updated": 30,
"special_defense_updated": 25,
"speed_updated": 20,
}
]
}
}
def update_stat(self, stat: Dict[str, int], base_modifier: int = 5) -> int:
IV = random.randint(0, 31)
base_stat = stat["base_stat"]
effort = stat["effort"]
updated_base_stat = int(
((2 * base_stat + IV + (effort / 4)) * (self.level / 100)) + base_modifier
)
return updated_base_stat
def stats_modifier(self):
# Higher "constant" (base_modifier) only for HP
self.hp_updated = self.update_stat(self.hp, base_modifier=10)
self.attack_updated = self.update_stat(self.attack)
self.defense_updated = self.update_stat(self.defense)
self.special_attack_updated = self.update_stat(self.special_attack)
self.special_defense_updated = self.update_stat(self.special_defense)
self.speed_updated = self.update_stat(self.speed)
async def parse_types_csv():
global type_advantages
filepath = "data/types.csv"
try:
with open(filepath, "r") as file:
csv_reader = csv.reader(file)
headers = next(csv_reader)
for row in csv_reader:
# Lower case to match with pokeapi
row_dict = {
header.lower(): float(value)
for header, value in zip(headers[1:], row[1:])
}
# Convert keys within the dictionary to lowercase
lowercase_key_row_dict = {k.lower(): v for k, v in row_dict.items()}
type_advantages[row[0].lower()] = lowercase_key_row_dict
except FileNotFoundError as exc:
logger.error(f"Error reading data in {filepath}: {str(exc)}")
raise
def clean_stat_names(stats: dict) -> dict:
"""
Pokeapi has "special-defense" and "special-attack".
To avoid syntax errors with python, converting "-" to "_"
"""
return {key.replace("-", "_"): value for key, value in stats.items()}
def revert_stat_names(stats: dict) -> dict:
return {key.replace("_", "-"): value for key, value in stats.items()}
def calculate_damage(level, attack_power, defense_power):
"""
Calculate damage based on the Pokémon's level, attack power, and defense power.
This is a simplified example and may need to be customized for your specific needs.
"""
# Placeholder for any modifiers to damage (e.g., critical hits, randomization)
modifier = 1
# Basic damage calculation
damage = (
((2 * level) / 5 + 2) * attack_power * (attack_power / defense_power) / 50 + 2
) * modifier
return damage
def extract_pokemon_base_stats(pokemon: Pokemon) -> dict:
pokemon_stats = vars(pokemon)
del pokemon_stats["name"]
del pokemon_stats["types"]
del pokemon_stats["level"]
# Collect the {stat}_updated attributes to delete
attrs_to_delete = [
stat for stat in pokemon_stats.keys() if stat.endswith("_updated")
]
# Delete the {stat}_updated attributes
for attr in attrs_to_delete:
del pokemon_stats[attr]
pokemon_stats = revert_stat_names(pokemon_stats)
return pokemon_stats
def get_preferred_cry(cries):
if 'latest' in cries and cries['latest']:
return cries['latest']
elif 'legacy' in cries and cries['legacy']:
return cries['legacy']
else:
return None
def get_generations(generations: dict) -> list:
"""
In pokeapi, stored as : generation-i, generation-ii, etc.
"""
generations = [key for key in generations.keys() if key.startswith("generation")]
return [gen.replace('generation-', '') for gen in generations]
async def get_pokemon(pokemon_name: str):
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_name}"
async with httpx.AsyncClient() as client:
response = await client.get(url)
if response.status_code == 200:
pokemon_data = response.json()
stats = {
stat["stat"]["name"]: {
"base_stat": stat["base_stat"],
"effort": stat["effort"],
}
for stat in pokemon_data["stats"]
}
stats = clean_stat_names(stats)
types = [t["type"]["name"] for t in pokemon_data["types"]]
pokemon = Pokemon(name=pokemon_name, **stats, types=types)
sprites = pokemon_data["sprites"]
cry = get_preferred_cry(pokemon_data["cries"])
weight = pokemon_data["weight"] / 10 #kg
height = pokemon_data["height"] / 10 #m
generations = get_generations(pokemon_data["sprites"]["versions"])
return pokemon, sprites, cry, weight, height, generations
else:
raise HTTPException(
status_code=response.status_code, detail="Pokemon not found"
)
def determine_attacker(pokemon1: Pokemon, pokemon2: Pokemon) -> Tuple[Pokemon, Pokemon]:
if pokemon1.speed_updated == pokemon2.speed_updated:
attacker = random.choice([pokemon1, pokemon2])
defender = pokemon2 if attacker == pokemon1 else pokemon1
else:
attacker, defender = (
(pokemon1, pokemon2) if pokemon1.speed_updated > pokemon2.speed_updated else (pokemon2, pokemon1)
)
return attacker, defender
def sanity_check_attacker(turn: int, prev_attacker_name: str, current_attacker_name: str):
if prev_attacker_name is current_attacker_name == prev_attacker_name and turn != 1:
raise ValueError(f"Attacker {current_attacker_name} is the same in consecutive rounds!")
def calculate_round_damage(attacker: Pokemon, defender: Pokemon, type_advantages: dict) -> float:
for _ in attacker.types:
# For now, take average of "physical" and "special" stats
attack_power = (
attacker.attack_updated + attacker.special_attack_updated
) / 2
defense_power = (
defender.defense_updated + defender.special_defense_updated
) / 2
damage = calculate_damage(attacker.level, attack_power, defense_power)
type_effectiveness = 1
for defending_type in defender.types:
effectiveness = 1
for attack_type in attacker.types:
# attacker effectiveness of each type across defender types
effectiveness *= type_advantages.get(attack_type, {}).get(
defending_type, 1
)
# Collect cumulative type effectiveness of attacker
type_effectiveness *= effectiveness
# Apply type effectiveness to the damage
damage *= type_effectiveness
return damage
def perform_attack(attacker: Pokemon, defender: Pokemon, type_advantages: dict):
# assuming one iteration is meant for the loop
damage = calculate_round_damage(attacker, defender, type_advantages)
defender.hp_updated -= damage
def battle_loop(pokemon1: Pokemon, pokemon2: Pokemon, type_advantages: dict) -> str:
attacker, defender = determine_attacker(pokemon1, pokemon2)
turn = 0
prev_attacker_name = attacker.name
while attacker.hp_updated > 0 and defender.hp_updated > 0:
turn += 1
sanity_check_attacker(turn, prev_attacker_name, attacker.name)
prev_attacker_name = attacker.name
logger.info(
f"Battle simulator --- This is round {turn}. The attacker is {attacker.name} \
and the defender is {defender.name}. The defender has HP {defender.hp_updated}"
)
perform_attack(attacker, defender, type_advantages)
attacker, defender = defender, attacker
return defender.name if attacker.hp_updated <= 0 else attacker.name
def battle_simulator(pokemon1: Pokemon, pokemon2: Pokemon, type_advantages: dict):
pokemon1.stats_modifier()
pokemon2.stats_modifier()
logger.info(
f"Battle simulator --- pokemon1: {pokemon1.name} has speed {pokemon1.speed_updated}"
)
logger.info(
f"Battle simulator --- pokemon2: {pokemon2.name} has speed {pokemon2.speed_updated}"
)
return battle_loop(pokemon1, pokemon2, type_advantages)