diff --git a/code/deck_builder/phases/phase5_color_balance.py b/code/deck_builder/phases/phase5_color_balance.py index c2bd1fb..bbb2085 100644 --- a/code/deck_builder/phases/phase5_color_balance.py +++ b/code/deck_builder/phases/phase5_color_balance.py @@ -45,12 +45,13 @@ class ColorBalanceMixin: Uses the color source matrix to aggregate counts for each color. """ matrix = self._compute_color_source_matrix() - counts = {c:0 for c in ['W','U','B','R','G']} + # Track only WUBRG here; ignore colorless 'C' and any other markers for this computation. + counts = {c: 0 for c in ['W', 'U', 'B', 'R', 'G']} for name, colors in matrix.items(): entry = self.card_library.get(name, {}) - copies = entry.get('Count',1) + copies = entry.get('Count', 1) for c, v in colors.items(): - if v: + if v and c in counts: counts[c] += copies return counts diff --git a/code/headless_runner.py b/code/headless_runner.py index 73253f2..db09e5f 100644 --- a/code/headless_runner.py +++ b/code/headless_runner.py @@ -4,9 +4,45 @@ import argparse import json import os from typing import Any, Dict, List, Optional +import time + +import sys +import os from deck_builder.builder import DeckBuilder from deck_builder import builder_constants as bc +from file_setup.setup import initial_setup +from tagging import tagger + +def _is_stale(file1: str, file2: str) -> bool: + """Return True if file2 is missing or older than file1.""" + if not os.path.isfile(file2): + return True + if not os.path.isfile(file1): + return True + return os.path.getmtime(file2) < os.path.getmtime(file1) + +def _ensure_data_ready(): + cards_csv = os.path.join("csv_files", "cards.csv") + tagging_json = os.path.join("csv_files", ".tagging_complete.json") + # If cards.csv is missing, run full setup+tagging + if not os.path.isfile(cards_csv): + print("cards.csv not found, running full setup and tagging...") + initial_setup() + tagger.run_tagging() + _write_tagging_flag(tagging_json) + # If tagging_complete is missing or stale, run tagging + elif not os.path.isfile(tagging_json) or _is_stale(cards_csv, tagging_json): + print(".tagging_complete.json missing or stale, running tagging...") + tagger.run_tagging() + _write_tagging_flag(tagging_json) + +def _write_tagging_flag(tagging_json): + import json + from datetime import datetime + os.makedirs(os.path.dirname(tagging_json), exist_ok=True) + with open(tagging_json, 'w', encoding='utf-8') as f: + json.dump({'tagged_at': datetime.now().isoformat(timespec='seconds')}, f) def run( command_name: str = "", @@ -273,6 +309,7 @@ def _resolve_value( def _main() -> int: + _ensure_data_ready() parser = _build_arg_parser() args = parser.parse_args() # Optional config discovery (no prompts)