2026-04-08 08:13:02 +00:00
|
|
|
import os
|
|
|
|
|
import json
|
|
|
|
|
from collections import defaultdict
|
2026-04-09 10:11:39 +00:00
|
|
|
import data.db_access as db_access
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
from tags.slayer import compute_slayer_for_career_player
|
2026-04-10 08:08:47 +00:00
|
|
|
from tags.objective_payload import compute_payload_tag
|
|
|
|
|
from tags.objective_domination import compute_dom_objective_for_career_player
|
2026-04-08 08:13:02 +00:00
|
|
|
from tags.sharpshooter import compute_sharpshooter_for_career_player
|
2026-04-10 08:08:47 +00:00
|
|
|
from tags.consistency import compute_consistency, compute_consistency_tag
|
|
|
|
|
from tags.clutch import compute_clutch, compute_clutch_raw
|
|
|
|
|
|
|
|
|
|
from data.league_metrics import compute_league_metrics, compute_league_clutch_metrics
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_career_database(output_path):
|
2026-04-10 08:08:47 +00:00
|
|
|
|
|
|
|
|
print("USING DB:", db_access.DB_PATH)
|
|
|
|
|
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
# 1. Load all players from the local SQLite DB
|
|
|
|
|
# ---------------------------------------------------------
|
2026-04-10 08:08:47 +00:00
|
|
|
player_rows = db_access.query("""
|
|
|
|
|
SELECT DISTINCT s.PlayerUUID, p.PlayerGameName
|
|
|
|
|
FROM stats s
|
|
|
|
|
LEFT JOIN players p ON p.PlayerUUID = s.PlayerUUID;
|
|
|
|
|
""")
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
if not player_rows:
|
|
|
|
|
print("No players found in local DB.")
|
|
|
|
|
return False
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
final_db = {"players": {}, "league_averages": {}}
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
# 2. Build per-player career stats from local DB
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
for row in player_rows:
|
|
|
|
|
pid = row["PlayerUUID"]
|
2026-04-10 08:08:47 +00:00
|
|
|
name = row["PlayerGameName"] or "Unknown"
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
matches = db_access.get_player_match_history(pid)
|
|
|
|
|
if not matches:
|
2026-04-08 08:13:02 +00:00
|
|
|
continue
|
2026-04-10 08:08:47 +00:00
|
|
|
|
|
|
|
|
#print("DEBUG MATCH ROW FOR", pid, ":", matches[0])
|
|
|
|
|
#break
|
|
|
|
|
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
career_raw = defaultdict(float)
|
|
|
|
|
|
|
|
|
|
for m in matches:
|
|
|
|
|
career_raw["kills"] += m["Kills"]
|
|
|
|
|
career_raw["deaths"] += m["Deaths"]
|
|
|
|
|
career_raw["damage"] += m["Damage"]
|
|
|
|
|
career_raw["shots"] += m["Shots"]
|
|
|
|
|
career_raw["shots_hit"] += m["ShotsHit"]
|
|
|
|
|
career_raw["headshots"] += m["Headshots"]
|
|
|
|
|
career_raw["PAY_PushTime"] += m["PAY_PushTime"]
|
|
|
|
|
career_raw["DOM_captures"] += m["DOM_Captures"]
|
|
|
|
|
career_raw["DOM_counters"] += m["DOM_Counters"]
|
2026-04-10 08:08:47 +00:00
|
|
|
career_raw["maps"] += 1
|
2026-04-09 10:11:39 +00:00
|
|
|
|
|
|
|
|
maps = max(1, career_raw["maps"])
|
|
|
|
|
KD = career_raw["kills"] / career_raw["deaths"] if career_raw["deaths"] > 0 else career_raw["kills"]
|
|
|
|
|
accuracy = (career_raw["shots_hit"] / career_raw["shots"]) if career_raw["shots"] > 0 else 0.0
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
derived = {
|
2026-04-09 10:11:39 +00:00
|
|
|
"kills_per_map": career_raw["kills"] / maps,
|
|
|
|
|
"deaths_per_map": career_raw["deaths"] / maps,
|
2026-04-10 08:08:47 +00:00
|
|
|
"push_time_per_season": career_raw["PAY_PushTime"],
|
2026-04-08 08:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final_db["players"][pid] = {
|
2026-04-09 10:11:39 +00:00
|
|
|
"name": name,
|
2026-04-08 08:13:02 +00:00
|
|
|
"career": {
|
2026-04-09 10:11:39 +00:00
|
|
|
"kills": career_raw["kills"],
|
|
|
|
|
"deaths": career_raw["deaths"],
|
2026-04-08 08:13:02 +00:00
|
|
|
"KD": KD,
|
|
|
|
|
"accuracy": accuracy,
|
2026-04-09 10:11:39 +00:00
|
|
|
"push_time": career_raw["PAY_PushTime"],
|
2026-04-13 07:56:38 +00:00
|
|
|
"DOM_Captures": int(career_raw["DOM_captures"]),
|
|
|
|
|
"DOM_Counters": int(career_raw["DOM_counters"]),
|
2026-04-09 10:11:39 +00:00
|
|
|
"maps": career_raw["maps"],
|
|
|
|
|
"damage": career_raw["damage"],
|
|
|
|
|
"shots": career_raw["shots"],
|
|
|
|
|
"shots_hit": career_raw["shots_hit"],
|
|
|
|
|
"headshots": career_raw["headshots"],
|
2026-04-08 08:13:02 +00:00
|
|
|
},
|
2026-04-13 07:56:38 +00:00
|
|
|
"DOM_Captures": int(career_raw["DOM_captures"]), # 👈 add this
|
|
|
|
|
"DOM_Counters": int(career_raw["DOM_counters"]), # 👈 and this
|
2026-04-08 08:13:02 +00:00
|
|
|
"derived": derived,
|
2026-04-09 10:11:39 +00:00
|
|
|
"matches": matches,
|
2026-04-08 08:13:02 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
2026-04-10 08:08:47 +00:00
|
|
|
# 3. PRECOMPUTE RAW CLUTCH VALUES (required for distribution)
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
2026-04-10 08:08:47 +00:00
|
|
|
all_players = list(final_db["players"].values())
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-10 08:08:47 +00:00
|
|
|
for pdata in all_players:
|
|
|
|
|
pdata["clutch_raw"] = compute_clutch_raw(pdata)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-10 08:08:47 +00:00
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
# 4. Compute league metrics for ALL TAGS
|
|
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
league_averages, distributions = compute_league_metrics(all_players)
|
|
|
|
|
clutch_averages, clutch_distribution = compute_league_clutch_metrics(all_players)
|
2026-04-09 10:11:39 +00:00
|
|
|
|
2026-04-10 08:08:47 +00:00
|
|
|
# Merge clutch averages into league averages
|
|
|
|
|
league_averages.update(clutch_averages)
|
2026-04-09 10:11:39 +00:00
|
|
|
|
|
|
|
|
final_db["league_averages"] = league_averages
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------
|
2026-04-10 08:08:47 +00:00
|
|
|
# 5. Compute all tags using distributions
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
|
|
|
|
for pid, pdata in final_db["players"].items():
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
# Slayer
|
2026-04-10 08:08:47 +00:00
|
|
|
pdata["slayer"] = compute_slayer_for_career_player(
|
|
|
|
|
pdata,
|
|
|
|
|
league_averages,
|
|
|
|
|
distributions["slayer"]
|
|
|
|
|
)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
# Sharpshooter
|
2026-04-10 08:08:47 +00:00
|
|
|
pdata["sharpshooter"] = compute_sharpshooter_for_career_player(
|
|
|
|
|
pdata["career"],
|
|
|
|
|
league_averages,
|
|
|
|
|
distributions["sharpshooter"]
|
|
|
|
|
)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
|
|
|
|
# Payload Objective Specialist
|
2026-04-10 08:08:47 +00:00
|
|
|
pdata["objective_payload"] = compute_payload_tag(
|
|
|
|
|
pdata,
|
|
|
|
|
league_averages,
|
|
|
|
|
distributions["payload"]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Domination Objective Specialist
|
2026-04-13 07:56:38 +00:00
|
|
|
pdata["objective_domination_components"] = compute_dom_objective_for_career_player(
|
2026-04-10 08:08:47 +00:00
|
|
|
pdata,
|
|
|
|
|
league_averages,
|
|
|
|
|
distributions["domination"]
|
2026-04-09 10:11:39 +00:00
|
|
|
)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
# Consistency
|
|
|
|
|
matches = pdata["matches"]
|
2026-04-08 08:13:02 +00:00
|
|
|
consistency_raw, components = compute_consistency(matches)
|
2026-04-10 08:08:47 +00:00
|
|
|
pdata["consistency"] = compute_consistency_tag(
|
|
|
|
|
components,
|
|
|
|
|
distributions["consistency"]
|
|
|
|
|
)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-10 08:08:47 +00:00
|
|
|
# Clutch (FINAL TAG)
|
|
|
|
|
pdata["clutch"] = compute_clutch(
|
|
|
|
|
pdata,
|
|
|
|
|
league_averages,
|
|
|
|
|
clutch_distribution
|
|
|
|
|
)
|
2026-04-08 08:13:02 +00:00
|
|
|
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
2026-04-10 08:08:47 +00:00
|
|
|
# 6. Save DB
|
2026-04-09 10:11:39 +00:00
|
|
|
# ---------------------------------------------------------
|
2026-04-08 08:13:02 +00:00
|
|
|
with open(output_path, "w", encoding="utf-8") as f:
|
|
|
|
|
json.dump(final_db, f, indent=2)
|
|
|
|
|
|
|
|
|
|
return True
|