-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_loader.py
More file actions
201 lines (165 loc) · 7.18 KB
/
data_loader.py
File metadata and controls
201 lines (165 loc) · 7.18 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
# src/tft_analyzer/data_loader.py
import requests
from requests.exceptions import RequestException
import json
from pathlib import Path
from typing import Dict, List, Any
TFT_DATA_URL = "https://raw.communitydragon.org/latest/cdragon/tft/en_us.json"
DATA_DIR = Path(__file__).parent / "data"
TFT_DATA_FILE = DATA_DIR / "tft_data.json"
CONSTANTS_FILE = DATA_DIR / "game_constants.json"
def download_tft_data(url: str = TFT_DATA_URL, output_path: Path = TFT_DATA_FILE):
"""Downloads TFT data from the given URL and saves it to the output path."""
try:
print("Downloading latest TFT data...")
response = requests.get(url, timeout=30)
response.raise_for_status()
data = response.json()
DATA_DIR.mkdir(exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
print(f"Successfully downloaded TFT data to {output_path}")
except RequestException as e:
print(f"Error downloading TFT data: {e}. Will try to use existing file.")
def load_tft_data(path: Path = TFT_DATA_FILE) -> Dict[str, Any]:
"""Loads the TFT data from the specified local JSON file."""
if not path.exists():
download_tft_data()
if not path.exists():
raise FileNotFoundError(
f"TFT data file not found at '{path}' and could not be downloaded. "
f"Please check your internet connection and try again, or manually "
f"place the data file at '{path}'."
)
with open(path, 'r', encoding='utf-8') as f:
return json.load(f)
def get_champion_image_url(asset_path: str) -> str:
"""Converts a CommunityDragon asset path to a CDN image URL."""
if not asset_path:
return ""
base_url = "https://raw.communitydragon.org/latest/game/"
return base_url + asset_path.lower().replace(".tex", ".png")
def get_champion_data(tft_data: Dict[str, Any]) -> Dict[str, Dict[str, Any]]:
"""Parses the TFT data and returns a dictionary of champion data."""
champions = {}
if "sets" not in tft_data:
raise ValueError("TFT data does not contain 'sets' key.")
# Get the latest set
latest_set = tft_data["sets"][str(max(map(int, tft_data["sets"].keys())))]
if "champions" not in latest_set:
raise ValueError("Latest set data does not contain 'champions' key.")
for champ in latest_set["champions"]:
# Skip dummy/test champions and spawned units
if champ.get("isSpawn", False) or not champ.get("name"):
continue
cost = champ.get("cost")
name = champ.get("name", "").strip()
traits = champ.get("traits", [])
# Ensure traits is a list
if not isinstance(traits, list):
traits = []
# Only include playable champions: cost 1-5 with at least one trait
if not name or not isinstance(cost, int) or cost < 1 or cost > 5 or not traits:
continue
# Sanitize null stat values to 0
raw_stats = champ.get("stats", {})
stats = {k: (v if isinstance(v, (int, float)) else 0) for k, v in raw_stats.items()}
champions[name] = {
"cost": cost,
"traits": traits,
"stats": stats,
"damageType": champ.get("damageType", 1),
"roles": champ.get("roles", []),
"icon": champ.get("icon", ""),
"squareIcon": champ.get("squareIcon", ""),
"apiName": champ.get("apiName", ""),
}
return champions
def get_trait_data(tft_data: Dict[str, Any]) -> Dict[str, Any]:
"""Parses the TFT data and returns a dictionary of trait data."""
traits = {}
if "sets" not in tft_data:
raise ValueError("TFT data does not contain 'sets' key.")
# Get the latest set
latest_set = tft_data["sets"][str(max(map(int, tft_data["sets"].keys())))]
if "traits" not in latest_set:
raise ValueError("Latest set data does not contain 'traits' key.")
for trait in latest_set["traits"]:
name = trait.get("name")
if name:
traits[name] = {
"effects": trait.get("effects", [])
}
return traits
def get_item_data(tft_data: Dict[str, Any]) -> Dict[str, Any]:
"""Parses the TFT data and returns a dictionary of item data."""
items = {}
# Items are usually at the top level
for item in tft_data.get("items", []):
name = item.get("name")
if name:
items[name] = {
"id": item.get("id"),
"desc": item.get("desc"),
"effects": item.get("effects", {}),
"icon": item.get("icon")
}
return items
def get_champions_by_tier(tft_data: Dict[str, Any]) -> Dict[str, List[str]]:
"""Parses the TFT data and returns a dictionary of champion names by tier."""
champions_by_tier = {f"{i}-cost": [] for i in range(1, 6)}
champ_data = get_champion_data(tft_data)
for name, data in champ_data.items():
cost = data["cost"]
if 1 <= cost <= 5:
champions_by_tier[f"{cost}-cost"].append(name)
return champions_by_tier
def save_split_data(tft_data: Dict[str, Any]):
"""Splits and saves the data into separate JSON files."""
print("Splitting data into categories...")
save_map = {
"champions.json": get_champion_data(tft_data),
"traits.json": get_trait_data(tft_data),
"items.json": get_item_data(tft_data)
}
for filename, data in save_map.items():
path = DATA_DIR / filename
with open(path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4)
print(f"Saved {path}")
def save_game_constants():
"""
Saves static game rules (Pool Sizes, Shop Odds) to a JSON file.
These values are not typically available in the standard en_us.json,
so we maintain them here as a configurable source of truth.
"""
constants = {
"pool_sizes": {
"1": 29, "2": 22, "3": 18, "4": 12, "5": 10
},
"shop_odds": {
"1": {"1": 100, "2": 0, "3": 0, "4": 0, "5": 0},
"2": {"1": 100, "2": 0, "3": 0, "4": 0, "5": 0},
"3": {"1": 75, "2": 25, "3": 0, "4": 0, "5": 0},
"4": {"1": 55, "2": 30, "3": 15, "4": 0, "5": 0},
"5": {"1": 45, "2": 33, "3": 20, "4": 2, "5": 0},
"6": {"1": 25, "2": 40, "3": 30, "4": 5, "5": 0},
"7": {"1": 19, "2": 30, "3": 35, "4": 15, "5": 1},
"8": {"1": 18, "2": 25, "3": 32, "4": 22, "5": 3},
"9": {"1": 10, "2": 20, "3": 25, "4": 35, "5": 10},
"10": {"1": 5, "2": 10, "3": 20, "4": 40, "5": 25}
}
}
if not CONSTANTS_FILE.exists():
print("Generating default game constants file...")
with open(CONSTANTS_FILE, 'w', encoding='utf-8') as f:
json.dump(constants, f, indent=4)
print(f"Saved {CONSTANTS_FILE}")
if __name__ == "__main__":
# This makes the script runnable to manually update data
download_tft_data()
# Example of loading and parsing
data = load_tft_data()
# Split the data for the analyzer
save_split_data(data)
save_game_constants()