-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
196 lines (160 loc) · 6.68 KB
/
data.py
File metadata and controls
196 lines (160 loc) · 6.68 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
import requests
import json
import os
from uuid import UUID
# File path for the cache
move_file = 'move_cache.json'
stats_file = 'stats_cache.json'
learnable_moves_file = 'learnable_moves_cache.json'
ailments = ["burn", "freeze", "paralysis", "poison", "sleep", "confusion", "trap", "none"]
# Function to load the cache from a file
def load_cache(cache_file):
if os.path.exists(cache_file):
with open(cache_file, 'r') as f:
return json.load(f)
return {}
# Function to save the cache to a file
def save_cache(cache_data, file):
with open(file, 'w') as f:
json.dump(cache_data, f)
# Function to get move data (with caching)
def get_move_data(move_name):
if not isinstance(move_name, str):
return move_name
# Load cache from file
cache = load_cache(move_file)
# Check if move data is already cached
if move_name in cache:
return cache[move_name]
# Fetch data from PokeAPI if not cached
url = f"https://pokeapi.co/api/v2/move/{move_name}/"
response = requests.get(url)
# Check if the response is valid JSON
try:
move_data = response.json()
except requests.exceptions.JSONDecodeError:
print(f"Error: Failed to fetch data for move '{move_name}'")
return None
# Extract only the relevant information
move_info = {
"name": move_data["name"],
"power": move_data.get("power") if move_data.get("power") is not None else 0, # Some moves may not have power
"max_pp": move_data.get("pp", 0), # Kepp track of max pp
"pp": move_data.get("pp", 0), # Some moves may not have pp
"category": move_data["damage_class"]["name"],
"type": move_data["type"]["name"],
"crit_chance": move_data["meta"]["crit_rate"] if move_data["meta"] is not None else 0,
"effect_chance": move_data["effect_chance"] if move_data["effect_chance"] is not None else 0,
"ailment": move_data["meta"]["ailment"]["name"] if move_data["meta"] and move_data["meta"]["ailment"]["name"] is not None else "none",
"stat_changes": move_data["stat_changes"],
"min_hits": move_data["meta"]["min_hits"] if move_data["meta"] and move_data["meta"]["min_hits"] is not None else 1,
"max_hits": move_data["meta"]["max_hits"] if move_data["meta"] and move_data["meta"]["max_hits"] is not None else 1,
"effect": move_data["effect_entries"][0]["short_effect"] if move_data["effect_entries"] else "None",
}
# Filter out moves with complicated effects
if move_info["ailment"] not in ailments or move_info['category'] != "status" and move_info['power'] == 0:
# Cache the move data
cache[move_name] = 0
save_cache(cache, move_file)
return 0
# Cache the move data
cache[move_name] = move_info
save_cache(cache, move_file)
return move_info
def get_pokemon_stats(pokemon_name):
# Load cache from file
cache = load_cache(stats_file)
# Check if move data is already cached
if pokemon_name in cache:
return cache[pokemon_name]
# Fetch data from PokeAPI if not cached
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_name}/"
response = requests.get(url)
# Check if the response is valid JSON
try:
pokemon_data = response.json()
except requests.exceptions.JSONDecodeError:
print(f"Error: Failed to fetch data for pokemon '{pokemon_name}'")
return None
# Extract only the relevant information
pokemon_info = {}
for stat in pokemon_data["stats"]:
stat_name = stat["stat"]["name"]
stat_value = stat["base_stat"]
pokemon_info[stat_name] = stat_value
pokemon_info["accuracy"] = 100
pokemon_info["evasion"] = 100
pokemon_info["id"] = pokemon_data["id"]
# Cache the move data
cache[pokemon_name] = pokemon_info
save_cache(cache, stats_file)
return pokemon_info
def get_pokemon_moves(pokemon_name):
# Load cache from file
cache = load_cache(learnable_moves_file)
# Check if move data is already cached
if pokemon_name in cache:
return cache[pokemon_name]
# Fetch data from PokeAPI if not cached
url = f"https://pokeapi.co/api/v2/pokemon/{pokemon_name}/"
response = requests.get(url)
# Check if the response is valid JSON
try:
pokemon_data = response.json()
except requests.exceptions.JSONDecodeError:
print(f"Error: Failed to fetch learnable moves for pokemon '{pokemon_name}'")
return None
# Extract only the relevant information
pokemon_info = []
for move in pokemon_data["moves"]:
move_info = get_move_data(move["move"]["name"])
if move_info == 0:
continue
if move_info["ailment"] not in ailments or move_info['category'] != "status" and move_info['power'] == 0:
continue
pokemon_info.append(move["move"]["name"])
# Cache the move data
cache[pokemon_name] = pokemon_info
save_cache(cache, learnable_moves_file)
return pokemon_info
#save player data function
def save_player_data(player, map_data, region_pokedex):
def convert_uuid_to_str(data):
if isinstance(data, dict):
return {key: convert_uuid_to_str(value) for key, value in data.items()}
elif isinstance(data, list):
return [convert_uuid_to_str(item) for item in data]
elif isinstance(data, UUID):
return str(data)
else:
return data
def remove_non_serializable(data):
if isinstance(data, dict):
return {key: remove_non_serializable(value) for key, value in data.items() if is_serializable(value)}
elif isinstance(data, list):
return [remove_non_serializable(item) for item in data if is_serializable(item)]
else:
return data
def is_serializable(value):
try:
json.dumps(value)
return True
except (TypeError, OverflowError):
return False
data = {
"pokemon_team": [remove_non_serializable(convert_uuid_to_str(pokemon.__dict__)) for pokemon in player.pokemon_team],
"inventory": remove_non_serializable(convert_uuid_to_str(player.inventory)),
"pc": [remove_non_serializable(convert_uuid_to_str(pokemon.__dict__)) for pokemon in player.pc],
"regions": map_data,
"region_pokedex": region_pokedex
}
with open("player_data.json", "w") as file:
json.dump(data, file, indent=4)
def delete_player_data():
try:
os.remove("player_data.json")
print("Player data deleted successfully.")
except FileNotFoundError:
print("No player data file found to delete.")
except Exception as e:
print(f"Error deleting player data: {e}")