mirror of
https://github.com/mwisnowski/mtg_python_deckbuilder.git
synced 2026-04-05 20:57:16 +02:00
feat: smart land bases — auto land count, mana profile, slot earmarking, and backfill (#63)
This commit is contained in:
parent
ac6c9f4daa
commit
0ab2183277
21 changed files with 1408 additions and 51 deletions
|
|
@ -25,6 +25,7 @@ from .include_exclude_utils import (
|
|||
collapse_duplicates
|
||||
)
|
||||
from .phases.phase1_commander import CommanderSelectionMixin
|
||||
from .phases.phase2_lands_analysis import LandAnalysisMixin
|
||||
from .phases.phase2_lands_basics import LandBasicsMixin
|
||||
from .phases.phase2_lands_staples import LandStaplesMixin
|
||||
from .phases.phase2_lands_kindred import LandKindredMixin
|
||||
|
|
@ -68,6 +69,7 @@ if not any(isinstance(h, logging_util.logging.StreamHandler) for h in logger.han
|
|||
@dataclass
|
||||
class DeckBuilder(
|
||||
CommanderSelectionMixin,
|
||||
LandAnalysisMixin,
|
||||
LandBasicsMixin,
|
||||
LandStaplesMixin,
|
||||
LandKindredMixin,
|
||||
|
|
@ -540,6 +542,73 @@ class DeckBuilder(
|
|||
logger.info(f"Land Step {step}: begin")
|
||||
m()
|
||||
logger.info(f"Land Step {step}: complete (current land count {self._current_land_count() if hasattr(self, '_current_land_count') else 'n/a'})")
|
||||
# Backfill step: if the builder still falls short of the land target after all steps,
|
||||
# pad with basics so the deck always reaches the configured ideal.
|
||||
self._backfill_basics_to_target()
|
||||
|
||||
def run_land_step9(self) -> None:
|
||||
"""Land Step 9: Backfill basics to target if any steps fell short."""
|
||||
self._backfill_basics_to_target()
|
||||
|
||||
def _backfill_basics_to_target(self) -> None:
|
||||
"""Add basic lands to reach ideal_counts['lands'] if the build fell short.
|
||||
|
||||
In the spells-first web build path the deck may already be at 100 cards by the time
|
||||
this runs. When that happens a direct add would be removed by the stage safety clamp,
|
||||
so we instead *swap*: remove the last-inserted non-land, non-locked card before adding
|
||||
each basic. The net deck size stays at 100 so the clamp is never triggered.
|
||||
"""
|
||||
if not hasattr(self, 'ideal_counts') or not self.ideal_counts:
|
||||
return
|
||||
land_target = self.ideal_counts.get('lands', 0)
|
||||
shortfall = land_target - self._current_land_count()
|
||||
if shortfall <= 0:
|
||||
return
|
||||
colors = [c for c in getattr(self, 'color_identity', []) if c in ('W', 'U', 'B', 'R', 'G')]
|
||||
color_basic_map = {'W': 'Plains', 'U': 'Island', 'B': 'Swamp', 'R': 'Mountain', 'G': 'Forest'}
|
||||
usable_basics = [color_basic_map[c] for c in colors if c in color_basic_map]
|
||||
if not usable_basics:
|
||||
usable_basics = ['Wastes']
|
||||
|
||||
# Build locked-card set so we never remove a user-locked card during a swap.
|
||||
locks_lower: set[str] = set()
|
||||
try:
|
||||
for attr in ('locked_cards', '_locked_cards', '_lock_names'):
|
||||
v = getattr(self, attr, None)
|
||||
if isinstance(v, (list, set)):
|
||||
locks_lower = {str(n).strip().lower() for n in v}
|
||||
break
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self.output_func(f"\nLand Backfill: {shortfall} slot(s) below target; adding basics to reach {land_target}.")
|
||||
added = 0
|
||||
for i in range(shortfall):
|
||||
basic = usable_basics[i % len(usable_basics)]
|
||||
total_cards = sum(int(e.get('Count', 1)) for e in self.card_library.values())
|
||||
if total_cards < 100:
|
||||
self.add_card(basic, card_type='Land', role='basic', sub_role='basic', added_by='lands_backfill')
|
||||
added += 1
|
||||
else:
|
||||
# Deck is at the 100-card limit. Swap: remove the lowest-priority non-land card
|
||||
# (the last-inserted unlocked non-land in the library) then add the basic.
|
||||
removed_name: Optional[str] = None
|
||||
for name in reversed(list(self.card_library.keys())):
|
||||
if name.strip().lower() in locks_lower:
|
||||
continue
|
||||
entry = self.card_library.get(name) or {}
|
||||
ctype = str(entry.get('Card Type', '') or '').lower()
|
||||
if 'land' in ctype:
|
||||
continue
|
||||
if self._decrement_card(name):
|
||||
removed_name = name
|
||||
break
|
||||
if removed_name is not None:
|
||||
self.add_card(basic, card_type='Land', role='basic', sub_role='basic', added_by='lands_backfill')
|
||||
added += 1
|
||||
else:
|
||||
break # No removable non-land found; stop backfilling
|
||||
self.output_func(f" Land Count Now : {self._current_land_count()} / {land_target} ({added} added)")
|
||||
|
||||
def _generate_recommendations(self, base_stem: str, limit: int):
|
||||
"""Silently build a full (non-owned-filtered) deck with same choices and export top recommendations.
|
||||
|
|
@ -2183,6 +2252,9 @@ class DeckBuilder(
|
|||
value = self._prompt_int_with_default(f"{prompt} ", current_default, minimum=0, maximum=200)
|
||||
self.ideal_counts[key] = value
|
||||
|
||||
# Smart land analysis — runs after defaults are seeded so env overrides still win
|
||||
self.run_land_analysis()
|
||||
|
||||
# Basic validation adjustments
|
||||
# Ensure basic_lands <= lands
|
||||
if self.ideal_counts['basic_lands'] > self.ideal_counts['lands']:
|
||||
|
|
|
|||
|
|
@ -170,6 +170,20 @@ BUDGET_TOTAL_TOLERANCE: Final[float] = 0.10 # End-of-build review threshold (10
|
|||
DEFAULT_RAMP_COUNT: Final[int] = 8 # Default number of ramp pieces
|
||||
DEFAULT_LAND_COUNT: Final[int] = 35 # Default total land count
|
||||
DEFAULT_BASIC_LAND_COUNT: Final[int] = 15 # Default minimum basic lands
|
||||
|
||||
# Smart land analysis thresholds (Roadmap 14)
|
||||
CURVE_FAST_THRESHOLD: Final[float] = 3.0 # Commander CMC below this → fast deck
|
||||
CURVE_SLOW_THRESHOLD: Final[float] = 4.0 # Commander CMC above this → slow deck
|
||||
LAND_COUNT_FAST: Final[int] = 33 # Land target for fast decks
|
||||
LAND_COUNT_MID: Final[int] = 35 # Land target for mid decks (same as default)
|
||||
LAND_COUNT_SLOW_BASE: Final[int] = 37 # Base land target for slow decks (may increase with color count)
|
||||
LAND_COUNT_SLOW_MAX: Final[int] = 39 # Maximum land target for slow, many-color decks
|
||||
BASICS_HEAVY_RATIO: Final[float] = 0.60 # Fraction of land target used as basics in basics-heavy profile
|
||||
BASICS_FIXING_PER_COLOR: Final[int] = 2 # Basics per color in fixing-heavy profile (minimal basics)
|
||||
BASICS_MIN_HEADROOM: Final[int] = 5 # Minimum gap between basic_lands and total lands
|
||||
BUDGET_FORCE_BASICS_THRESHOLD: Final[float] = 50.0 # Budget below this (3+ colors) forces basics-heavy
|
||||
# Profile offsets applied to existing bracket-based ETB tapped threshold in Step 8
|
||||
PROFILE_TAPPED_THRESHOLD_OFFSETS: Final[Dict[str, int]] = {'fast': -4, 'mid': 0, 'slow': 4}
|
||||
DEFAULT_NON_BASIC_LAND_SLOTS: Final[int] = 10 # Default number of non-basic land slots to reserve
|
||||
DEFAULT_BASICS_PER_COLOR: Final[int] = 5 # Default number of basic lands to add per color
|
||||
|
||||
|
|
|
|||
|
|
@ -602,10 +602,120 @@ def compute_spell_pip_weights(card_library: Dict[str, dict], color_identity: Ite
|
|||
return {c: (pip_counts[c] / total_colored) for c in pip_counts}
|
||||
|
||||
|
||||
def compute_pip_density(card_library: Dict[str, dict], color_identity: Iterable[str]) -> Dict[str, Dict[str, int]]:
|
||||
"""Compute raw pip counts per color broken down by multiplicity.
|
||||
|
||||
Extends ``compute_spell_pip_weights`` with a full breakdown instead of
|
||||
normalized weights, and adds Phyrexian mana handling (``{WP}`` etc.).
|
||||
|
||||
Returns a dict keyed by color letter, each value being::
|
||||
{'single': int, 'double': int, 'triple': int, 'phyrexian': int}
|
||||
|
||||
'single' = cards with exactly 1 pip of this color in their cost
|
||||
'double' = cards with exactly 2 pips
|
||||
'triple' = cards with 3+ pips
|
||||
'phyrexian' = cards where the Phyrexian version of this color appears
|
||||
|
||||
Non-land spells only. Hybrid symbols credit 0.5 weight to each component
|
||||
(same as compute_spell_pip_weights) but are only reflected in the totals,
|
||||
not in the single/double/triple buckets (which track whole-pip occurrences).
|
||||
"""
|
||||
COLORS = set(COLOR_LETTERS)
|
||||
pip_colors_identity = [c for c in color_identity if c in COLORS]
|
||||
result: Dict[str, Dict[str, int]] = {
|
||||
c: {'single': 0, 'double': 0, 'triple': 0, 'phyrexian': 0}
|
||||
for c in COLOR_LETTERS
|
||||
}
|
||||
for entry in card_library.values():
|
||||
ctype = str(entry.get('Card Type', ''))
|
||||
if 'land' in ctype.lower():
|
||||
continue
|
||||
mana_cost = entry.get('Mana Cost') or entry.get('mana_cost') or ''
|
||||
if not isinstance(mana_cost, str):
|
||||
continue
|
||||
# Count pips per color for this card
|
||||
card_pips: Dict[str, float] = {c: 0.0 for c in COLOR_LETTERS}
|
||||
card_phyrexian: Dict[str, bool] = {c: False for c in COLOR_LETTERS}
|
||||
for match in re.findall(r'\{([^}]+)\}', mana_cost):
|
||||
sym = match.upper()
|
||||
if len(sym) == 1 and sym in card_pips:
|
||||
card_pips[sym] += 1
|
||||
elif '/' in sym:
|
||||
parts = [p for p in sym.split('/') if p in card_pips]
|
||||
if parts:
|
||||
weight_each = 1 / len(parts)
|
||||
for p in parts:
|
||||
card_pips[p] += weight_each
|
||||
elif sym.endswith('P') and len(sym) == 2:
|
||||
# Phyrexian mana: {WP}, {UP}, etc.
|
||||
base = sym[0]
|
||||
if base in card_pips:
|
||||
card_phyrexian[base] = True
|
||||
# Accumulate into buckets
|
||||
for c in COLOR_LETTERS:
|
||||
pips = card_pips[c]
|
||||
if card_phyrexian[c]:
|
||||
result[c]['phyrexian'] += 1
|
||||
if pips >= 3:
|
||||
result[c]['triple'] += 1
|
||||
elif pips >= 2:
|
||||
result[c]['double'] += 1
|
||||
elif pips >= 1:
|
||||
result[c]['single'] += 1
|
||||
# Zero out colors not in identity (irrelevant for analysis)
|
||||
for c in COLOR_LETTERS:
|
||||
if c not in pip_colors_identity:
|
||||
result[c] = {'single': 0, 'double': 0, 'triple': 0, 'phyrexian': 0}
|
||||
return result
|
||||
|
||||
|
||||
def analyze_curve(commander_mana_value: float, color_count: int) -> Dict[str, Any]:
|
||||
"""Estimate deck speed and derive an optimal land target from commander CMC.
|
||||
|
||||
Uses commander mana value as a proxy for deck speed — a reliable signal
|
||||
in Commander: low-CMC commanders rarely lead slow, high-land-count decks.
|
||||
|
||||
Args:
|
||||
commander_mana_value: The commander's converted mana cost.
|
||||
color_count: Number of colors in the deck's color identity (1-5).
|
||||
|
||||
Returns:
|
||||
dict with keys:
|
||||
speed_category: 'fast' | 'mid' | 'slow'
|
||||
land_target: recommended total land count (33-39)
|
||||
basic_target: recommended minimum basic land count
|
||||
"""
|
||||
fast_threshold = getattr(bc, 'CURVE_FAST_THRESHOLD', 3.0)
|
||||
slow_threshold = getattr(bc, 'CURVE_SLOW_THRESHOLD', 4.0)
|
||||
if commander_mana_value < fast_threshold:
|
||||
speed = 'fast'
|
||||
land_target = getattr(bc, 'LAND_COUNT_FAST', 33)
|
||||
elif commander_mana_value > slow_threshold:
|
||||
speed = 'slow'
|
||||
base = getattr(bc, 'LAND_COUNT_SLOW_BASE', 37)
|
||||
slow_max = getattr(bc, 'LAND_COUNT_SLOW_MAX', 39)
|
||||
# More colors = more fixing needed = slightly more lands
|
||||
land_target = min(base + max(0, color_count - 3), slow_max)
|
||||
else:
|
||||
speed = 'mid'
|
||||
land_target = getattr(bc, 'LAND_COUNT_MID', 35)
|
||||
# Basic target: ~40% of land target for mid/slow, ~50% for fast (fewer fixing lands needed)
|
||||
basics_ratio = 0.50 if speed == 'fast' else 0.40
|
||||
basic_target = max(color_count * 2, int(round(land_target * basics_ratio)))
|
||||
basic_target = min(basic_target, land_target - getattr(bc, 'BASICS_MIN_HEADROOM', 5))
|
||||
return {
|
||||
'speed_category': speed,
|
||||
'land_target': land_target,
|
||||
'basic_target': max(basic_target, color_count),
|
||||
}
|
||||
|
||||
|
||||
|
||||
__all__ = [
|
||||
'compute_color_source_matrix',
|
||||
'compute_spell_pip_weights',
|
||||
'compute_pip_density',
|
||||
'analyze_curve',
|
||||
'parse_theme_tags',
|
||||
'normalize_theme_list',
|
||||
'multi_face_land_info',
|
||||
|
|
|
|||
385
code/deck_builder/phases/phase2_lands_analysis.py
Normal file
385
code/deck_builder/phases/phase2_lands_analysis.py
Normal file
|
|
@ -0,0 +1,385 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from .. import builder_constants as bc
|
||||
from .. import builder_utils as bu
|
||||
|
||||
"""Phase 2 (pre-step): Smart land base analysis (Roadmap 14, M1).
|
||||
|
||||
LandAnalysisMixin.run_land_analysis() is called from run_deck_build_step2()
|
||||
AFTER ideal_counts defaults are seeded, so ENABLE_SMART_LANDS, LAND_PROFILE,
|
||||
and LAND_COUNT env overrides win over the calculated values.
|
||||
|
||||
Responsibilities:
|
||||
- compute_pip_density(): delegate to builder_utils
|
||||
- analyze_curve(): delegate to builder_utils
|
||||
- determine_profile(): basics / mid / fixing rules from Profile Definitions
|
||||
- run_land_analysis(): orchestrates analysis, sets ideal_counts, self._land_profile
|
||||
"""
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class LandAnalysisMixin:
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Public entry point — called from run_deck_build_step2()
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def run_land_analysis(self) -> None:
|
||||
"""Analyse the commander and color identity to set a smart land profile.
|
||||
|
||||
Sets:
|
||||
self._land_profile 'basics' | 'mid' | 'fixing' (default: 'mid')
|
||||
self._speed_category 'fast' | 'mid' | 'slow'
|
||||
self._land_report_data dict persisted for M3 diagnostics export
|
||||
Mutates:
|
||||
self.ideal_counts['lands'] and self.ideal_counts['basic_lands']
|
||||
(only when ENABLE_SMART_LANDS=1; env overrides honoured after)
|
||||
"""
|
||||
if not os.environ.get('ENABLE_SMART_LANDS'):
|
||||
return
|
||||
|
||||
try:
|
||||
self._run_land_analysis_inner()
|
||||
except Exception as exc:
|
||||
logger.warning('run_land_analysis failed (%s); defaulting to mid profile', exc)
|
||||
self._land_profile = 'mid'
|
||||
self._speed_category = 'mid'
|
||||
|
||||
def _run_land_analysis_inner(self) -> None:
|
||||
color_identity = getattr(self, 'color_identity', []) or []
|
||||
colors = [c for c in color_identity if c in ('W', 'U', 'B', 'R', 'G')]
|
||||
color_count = len(colors)
|
||||
|
||||
# --- Card pool DataFrame (available at step 2; card_library is still empty) ---
|
||||
pool_df = getattr(self, '_combined_cards_df', None)
|
||||
|
||||
# --- Curve analysis: commander CMC + pool average CMC (weighted) ---
|
||||
_cdict = getattr(self, 'commander_dict', None) or {}
|
||||
commander_cmc = float(_cdict.get('CMC') or _cdict.get('Mana Value') or 3.5)
|
||||
effective_cmc = commander_cmc
|
||||
avg_pool_cmc: Optional[float] = None
|
||||
if pool_df is not None and not pool_df.empty:
|
||||
try:
|
||||
non_land = pool_df[~pool_df['type'].str.lower().str.contains('land', na=False)]
|
||||
if not non_land.empty and 'manaValue' in non_land.columns:
|
||||
avg_pool_cmc = float(non_land['manaValue'].mean())
|
||||
# Weight commander CMC more heavily (it's the clearest intent signal)
|
||||
effective_cmc = commander_cmc * 0.6 + avg_pool_cmc * 0.4
|
||||
except Exception as exc:
|
||||
logger.debug('Pool average CMC failed (%s); using commander CMC only', exc)
|
||||
curve_stats = bu.analyze_curve(effective_cmc, color_count)
|
||||
speed: str = curve_stats['speed_category']
|
||||
# Apply the speed-based offset relative to the user's configured ideal land count.
|
||||
# e.g. if the user set 40 lands: fast gets 38, mid stays 40, slow gets 42-44.
|
||||
# This respects custom ideals instead of always using the hardcoded 33/35/37-39.
|
||||
mid_default = getattr(bc, 'LAND_COUNT_MID', 35)
|
||||
_user_land_base = int((getattr(self, 'ideal_counts', None) or {}).get('lands', mid_default))
|
||||
_speed_offset = curve_stats['land_target'] - mid_default
|
||||
land_target: int = max(1, _user_land_base + _speed_offset)
|
||||
_orig_land_target = curve_stats['land_target']
|
||||
basic_target: int = (
|
||||
max(color_count, int(round(curve_stats['basic_target'] * land_target / _orig_land_target)))
|
||||
if _orig_land_target > 0
|
||||
else curve_stats['basic_target']
|
||||
)
|
||||
|
||||
# --- Pip density analysis from pool (card_library is empty at step 2) ---
|
||||
pip_density: Dict[str, Dict[str, int]] = {}
|
||||
try:
|
||||
if pool_df is not None and not pool_df.empty:
|
||||
# Convert pool to minimal dict format for compute_pip_density
|
||||
records = pool_df[['manaCost', 'type']].fillna('').to_dict('records')
|
||||
pool_dict = {
|
||||
str(i): {
|
||||
'Mana Cost': str(r.get('manaCost') or ''),
|
||||
'Card Type': str(r.get('type') or ''),
|
||||
}
|
||||
for i, r in enumerate(records)
|
||||
}
|
||||
pip_density = bu.compute_pip_density(pool_dict, colors)
|
||||
else:
|
||||
# Fallback for tests / headless contexts without a loaded DataFrame
|
||||
card_library = getattr(self, 'card_library', {})
|
||||
pip_density = bu.compute_pip_density(card_library, colors)
|
||||
except Exception as exc:
|
||||
logger.warning('compute_pip_density failed (%s); profile from curve only', exc)
|
||||
|
||||
# --- Profile determination ---
|
||||
profile = self._determine_profile(pip_density, color_count)
|
||||
|
||||
# --- Budget override ---
|
||||
budget_total = getattr(self, 'budget_total', None)
|
||||
if budget_total is not None and color_count >= 3:
|
||||
budget_threshold = getattr(bc, 'BUDGET_FORCE_BASICS_THRESHOLD', 50.0)
|
||||
if float(budget_total) < budget_threshold:
|
||||
prev_profile = profile
|
||||
profile = 'basics'
|
||||
self.output_func(
|
||||
f'[Smart Lands] Budget ${budget_total:.0f} < ${budget_threshold:.0f} '
|
||||
f'with {color_count} colors: forcing basics-heavy profile '
|
||||
f'(was {prev_profile}).'
|
||||
)
|
||||
|
||||
# --- LAND_PROFILE env override (highest priority) ---
|
||||
env_profile = os.environ.get('LAND_PROFILE', '').strip().lower()
|
||||
if env_profile in ('basics', 'mid', 'fixing'):
|
||||
profile = env_profile
|
||||
|
||||
# --- Compute basic count for profile ---
|
||||
basics = self._basics_for_profile(profile, color_count, land_target)
|
||||
|
||||
# --- LAND_COUNT env override ---
|
||||
env_land_count = os.environ.get('LAND_COUNT', '').strip()
|
||||
if env_land_count.isdigit():
|
||||
land_target = int(env_land_count)
|
||||
# Re-clamp basics against (possibly overridden) land target
|
||||
min_headroom = getattr(bc, 'BASICS_MIN_HEADROOM', 5)
|
||||
basics = min(basics, land_target - min_headroom)
|
||||
basics = max(basics, color_count)
|
||||
|
||||
# --- Apply to ideal_counts ---
|
||||
ideal: Dict[str, int] = getattr(self, 'ideal_counts', {})
|
||||
ideal['lands'] = land_target
|
||||
ideal['basic_lands'] = basics
|
||||
|
||||
# --- Pip summary for reporting ---
|
||||
total_double = sum(v.get('double', 0) for v in pip_density.values())
|
||||
total_triple = sum(v.get('triple', 0) for v in pip_density.values())
|
||||
# Pips were a deciding factor when they pushed profile away from the default
|
||||
pip_was_deciding = (
|
||||
(color_count >= 3 and (total_double >= 15 or total_triple >= 3))
|
||||
or (color_count <= 2 and total_double < 5 and total_triple == 0)
|
||||
)
|
||||
|
||||
# --- Persist analysis state ---
|
||||
self._land_profile = profile
|
||||
self._speed_category = speed
|
||||
self._land_report_data: Dict[str, Any] = {
|
||||
'profile': profile,
|
||||
'speed_category': speed,
|
||||
'commander_cmc': commander_cmc,
|
||||
'effective_cmc': effective_cmc,
|
||||
'avg_pool_cmc': avg_pool_cmc,
|
||||
'color_count': color_count,
|
||||
'land_target': land_target,
|
||||
'basic_target': basics,
|
||||
'pip_density': pip_density,
|
||||
'total_double_pips': total_double,
|
||||
'total_triple_pips': total_triple,
|
||||
'pip_was_deciding': pip_was_deciding,
|
||||
'budget_total': budget_total,
|
||||
'env_overrides': {
|
||||
'LAND_PROFILE': env_profile or None,
|
||||
'LAND_COUNT': env_land_count or None,
|
||||
},
|
||||
}
|
||||
|
||||
rationale = self._build_rationale(profile, speed, commander_cmc, effective_cmc, color_count, pip_density, budget_total)
|
||||
self._land_report_data['rationale'] = rationale
|
||||
|
||||
self.output_func(
|
||||
f'\n[Smart Lands] Profile: {profile} | Speed: {speed} | '
|
||||
f'Lands: {land_target} | Basics: {basics}'
|
||||
)
|
||||
self.output_func(f' Rationale: {rationale}')
|
||||
|
||||
# --- Earmark land slots: scale non-land ideals to fit within the remaining budget ---
|
||||
# Commander takes 1 slot, so there are 99 slots for non-commander cards.
|
||||
# If non-land ideal counts sum to more than (99 - land_target), the spell phases
|
||||
# will fill those slots first (in spells-first builds) leaving no room for lands.
|
||||
self._earmark_land_slots(land_target)
|
||||
|
||||
def _earmark_land_slots(self, land_target: int) -> None:
|
||||
"""Scale non-land ideal_counts down so they fit within 99 - land_target slots.
|
||||
|
||||
This ensures the spell phases never consume the slots reserved for lands,
|
||||
making backfill unnecessary in the normal case.
|
||||
"""
|
||||
NON_LAND_KEYS = ['creatures', 'ramp', 'removal', 'wipes', 'card_advantage', 'protection']
|
||||
# 99 = total deck slots minus commander
|
||||
deck_slots = getattr(bc, 'DECK_NON_COMMANDER_SLOTS', 99)
|
||||
budget = deck_slots - land_target
|
||||
if budget <= 0:
|
||||
return
|
||||
ideal: Dict[str, int] = getattr(self, 'ideal_counts', {})
|
||||
current_sum = sum(int(ideal.get(k, 0)) for k in NON_LAND_KEYS)
|
||||
if current_sum <= budget:
|
||||
return # already fits; nothing to do
|
||||
|
||||
# Scale each key down proportionally (floor), then top up from the largest key first.
|
||||
scale = budget / current_sum
|
||||
new_vals: Dict[str, int] = {}
|
||||
for k in NON_LAND_KEYS:
|
||||
new_vals[k] = max(0, int(int(ideal.get(k, 0)) * scale))
|
||||
remainder = budget - sum(new_vals.values())
|
||||
# Distribute leftover slots to the largest keys first (preserves relative proportion)
|
||||
for k in sorted(NON_LAND_KEYS, key=lambda x: -int(ideal.get(x, 0))):
|
||||
if remainder <= 0:
|
||||
break
|
||||
new_vals[k] += 1
|
||||
remainder -= 1
|
||||
# Apply and report
|
||||
adjustments: list[str] = []
|
||||
for k in NON_LAND_KEYS:
|
||||
old = int(ideal.get(k, 0))
|
||||
new = new_vals[k]
|
||||
if old != new:
|
||||
ideal[k] = new
|
||||
adjustments.append(f'{k}: {old}→{new}')
|
||||
if adjustments:
|
||||
self.output_func(
|
||||
f' [Smart Lands] Earmarked {land_target} land slots; '
|
||||
f'scaled non-land targets to fit {budget} remaining: {", ".join(adjustments)}'
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Profile determination
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _determine_profile(
|
||||
self,
|
||||
pip_density: Dict[str, Dict[str, int]],
|
||||
color_count: int,
|
||||
) -> str:
|
||||
"""Determine the land profile from pip density and color count.
|
||||
|
||||
Rules (in priority order):
|
||||
1. 5-color → fixing
|
||||
2. 1-color → basics
|
||||
3. High pip density (≥15 double-pips or ≥3 triple-pips) AND 3+ colors → fixing
|
||||
4. Low pip density (<5 double-pips, 0 triple-pips) AND 1-2 colors → basics
|
||||
5. Otherwise → mid
|
||||
"""
|
||||
if color_count >= 5:
|
||||
return 'fixing'
|
||||
if color_count <= 1:
|
||||
return 'basics'
|
||||
|
||||
total_double = sum(v.get('double', 0) for v in pip_density.values())
|
||||
total_triple = sum(v.get('triple', 0) for v in pip_density.values())
|
||||
|
||||
if color_count >= 3 and (total_double >= 15 or total_triple >= 3):
|
||||
return 'fixing'
|
||||
if color_count <= 2 and total_double < 5 and total_triple == 0:
|
||||
return 'basics'
|
||||
return 'mid'
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Basics count per profile
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _basics_for_profile(self, profile: str, color_count: int, land_target: int) -> int:
|
||||
min_headroom = getattr(bc, 'BASICS_MIN_HEADROOM', 5)
|
||||
if profile == 'basics':
|
||||
ratio = getattr(bc, 'BASICS_HEAVY_RATIO', 0.60)
|
||||
count = int(round(land_target * ratio))
|
||||
elif profile == 'fixing':
|
||||
per_color = getattr(bc, 'BASICS_FIXING_PER_COLOR', 2)
|
||||
count = max(color_count * per_color, color_count)
|
||||
else: # mid
|
||||
# Default ratio preserved — same as current behavior
|
||||
count = getattr(bc, 'DEFAULT_BASIC_LAND_COUNT', 15)
|
||||
# Clamp
|
||||
count = min(count, land_target - min_headroom)
|
||||
count = max(count, color_count)
|
||||
return count
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Rationale string
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _build_rationale(
|
||||
self,
|
||||
profile: str,
|
||||
speed: str,
|
||||
commander_cmc: float,
|
||||
effective_cmc: float,
|
||||
color_count: int,
|
||||
pip_density: Dict[str, Dict[str, int]],
|
||||
budget: Optional[float],
|
||||
) -> str:
|
||||
total_double = sum(v.get('double', 0) for v in pip_density.values())
|
||||
total_triple = sum(v.get('triple', 0) for v in pip_density.values())
|
||||
if abs(effective_cmc - commander_cmc) >= 0.2:
|
||||
cmc_label = f'commander CMC {commander_cmc:.0f}, effective {effective_cmc:.1f} (with pool avg)'
|
||||
else:
|
||||
cmc_label = f'commander CMC {commander_cmc:.1f}'
|
||||
parts = [
|
||||
f'{color_count}-color identity',
|
||||
f'{cmc_label} ({speed} deck)',
|
||||
]
|
||||
if pip_density:
|
||||
parts.append(f'{total_double} double-pips, {total_triple} triple-or-more-pips')
|
||||
if budget is not None:
|
||||
parts.append(f'budget ${budget:.0f}')
|
||||
profile_desc = {
|
||||
'basics': 'basics-heavy (minimal fixing)',
|
||||
'mid': 'balanced (moderate fixing)',
|
||||
'fixing': 'fixing-heavy (extensive duals/fetches)',
|
||||
}.get(profile, profile)
|
||||
return f'{profile_desc} — {", ".join(parts)}'
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Post-build diagnostics (M3) — called from build_deck_summary()
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def generate_diagnostics(self) -> None:
|
||||
"""Update _land_report_data with post-build actuals from card_library.
|
||||
|
||||
Runs after all land/spell phases have added cards so card_library is
|
||||
fully populated. Safe to call even when ENABLE_SMART_LANDS is off —
|
||||
initialises _land_report_data with basic actuals if missing.
|
||||
"""
|
||||
if not hasattr(self, '_land_report_data'):
|
||||
self._land_report_data = {}
|
||||
|
||||
library = getattr(self, 'card_library', {})
|
||||
if not library:
|
||||
return
|
||||
|
||||
# Build a name → row dict for type/oracle text lookups
|
||||
df = getattr(self, '_combined_cards_df', None)
|
||||
name_to_row: Dict[str, Any] = {}
|
||||
if df is not None and not getattr(df, 'empty', True):
|
||||
try:
|
||||
for _, row in df.iterrows():
|
||||
nm = str(row.get('name', '') or '')
|
||||
if nm and nm not in name_to_row:
|
||||
name_to_row[nm] = row.to_dict()
|
||||
except Exception as exc:
|
||||
logger.debug('generate_diagnostics: df scan failed (%s)', exc)
|
||||
|
||||
total_lands = 0
|
||||
tapped_count = 0
|
||||
fixing_count = 0
|
||||
basic_count = 0
|
||||
|
||||
for name, info in library.items():
|
||||
ctype = str(info.get('Card Type', '') or '')
|
||||
if 'land' not in ctype.lower():
|
||||
continue
|
||||
total_lands += 1
|
||||
if 'basic' in ctype.lower():
|
||||
basic_count += 1
|
||||
row = name_to_row.get(name, {})
|
||||
tline = str(row.get('type', ctype) or ctype).lower()
|
||||
text_field = str(row.get('text', '') or '').lower()
|
||||
tapped_flag, _ = bu.tapped_land_penalty(tline, text_field)
|
||||
if tapped_flag:
|
||||
tapped_count += 1
|
||||
if bu.is_color_fixing_land(tline, text_field):
|
||||
fixing_count += 1
|
||||
|
||||
tapped_pct = round(tapped_count / total_lands * 100, 1) if total_lands else 0.0
|
||||
self._land_report_data.update({
|
||||
'actual_land_count': total_lands,
|
||||
'actual_tapped_count': tapped_count,
|
||||
'actual_fixing_count': fixing_count,
|
||||
'actual_basic_count': basic_count,
|
||||
'tapped_pct': tapped_pct,
|
||||
})
|
||||
|
|
@ -19,6 +19,10 @@ class LandOptimizationMixin:
|
|||
bracket_level = getattr(self, 'bracket_level', None)
|
||||
threshold_map = getattr(bc, 'TAPPED_LAND_MAX_THRESHOLDS', {5:6,4:8,3:10,2:12,1:14})
|
||||
threshold = threshold_map.get(bracket_level, 10)
|
||||
# Smart Lands M2: tighten tapped threshold for fast profiles, loosen for slow.
|
||||
# _land_profile defaults to 'mid' (offset 0) when ENABLE_SMART_LANDS is off.
|
||||
profile_offsets = getattr(bc, 'PROFILE_TAPPED_THRESHOLD_OFFSETS', {'fast': -4, 'mid': 0, 'slow': 4})
|
||||
threshold += profile_offsets.get(getattr(self, '_land_profile', 'mid'), 0)
|
||||
|
||||
name_to_row = {}
|
||||
for _, row in df.iterrows():
|
||||
|
|
|
|||
|
|
@ -480,6 +480,12 @@ class ReportingMixin:
|
|||
}
|
||||
"""
|
||||
# Build lookup to enrich type and mana values
|
||||
# M3 (Roadmap 14): update _land_report_data with post-build actuals
|
||||
try:
|
||||
if hasattr(self, 'generate_diagnostics'):
|
||||
self.generate_diagnostics()
|
||||
except Exception as _exc: # pragma: no cover - diagnostics only
|
||||
logger.debug('generate_diagnostics failed: %s', _exc)
|
||||
full_df = getattr(self, '_full_cards_df', None)
|
||||
combined_df = getattr(self, '_combined_cards_df', None)
|
||||
snapshot = full_df if full_df is not None else combined_df
|
||||
|
|
@ -599,12 +605,22 @@ class ReportingMixin:
|
|||
dfc_details: list[dict] = []
|
||||
dfc_extra_total = 0
|
||||
|
||||
# Pip distribution (counts and weights) for non-land spells only
|
||||
pip_counts = {c: 0 for c in ('W','U','B','R','G')}
|
||||
# Pip distribution (counts and weights) for non-land spells only.
|
||||
# pip_counts and pip_weights are derived from compute_pip_density(); the
|
||||
# pip_cards map (color → card list for UI cross-highlighting) is built here
|
||||
# since it is specific to the reporting layer and not needed elsewhere.
|
||||
from .. import builder_utils as _bu
|
||||
pip_density = _bu.compute_pip_density(self.card_library, getattr(self, 'color_identity', []) or [])
|
||||
# Flatten density buckets into a single float per color (single + double*2 + triple*3 + phyrexian)
|
||||
# so that pip_counts stays numerically compatible with pip_weights downstream.
|
||||
pip_counts: Dict[str, float] = {}
|
||||
for c in ('W', 'U', 'B', 'R', 'G'):
|
||||
d = pip_density[c]
|
||||
pip_counts[c] = float(d['single'] + d['double'] * 2 + d['triple'] * 3 + d['phyrexian'])
|
||||
total_pips = sum(pip_counts.values())
|
||||
# For UI cross-highlighting: map color -> list of cards that have that color pip in their cost
|
||||
pip_cards: Dict[str, list] = {c: [] for c in ('W','U','B','R','G')}
|
||||
pip_cards: Dict[str, list] = {c: [] for c in ('W', 'U', 'B', 'R', 'G')}
|
||||
import re as _re_local
|
||||
total_pips = 0.0
|
||||
for name, info in self.card_library.items():
|
||||
ctype = str(info.get('Card Type', ''))
|
||||
if 'land' in ctype.lower():
|
||||
|
|
@ -612,35 +628,24 @@ class ReportingMixin:
|
|||
mana_cost = info.get('Mana Cost') or info.get('mana_cost') or ''
|
||||
if not isinstance(mana_cost, str):
|
||||
continue
|
||||
# Track which colors appear for this card's mana cost for card listing
|
||||
colors_for_card = set()
|
||||
colors_for_card: set = set()
|
||||
for match in _re_local.findall(r'\{([^}]+)\}', mana_cost):
|
||||
sym = match.upper()
|
||||
if len(sym) == 1 and sym in pip_counts:
|
||||
pip_counts[sym] += 1
|
||||
total_pips += 1
|
||||
if len(sym) == 1 and sym in pip_cards:
|
||||
colors_for_card.add(sym)
|
||||
elif '/' in sym:
|
||||
parts = [p for p in sym.split('/') if p in pip_counts]
|
||||
if parts:
|
||||
weight_each = 1 / len(parts)
|
||||
for p in parts:
|
||||
pip_counts[p] += weight_each
|
||||
total_pips += weight_each
|
||||
colors_for_card.add(p)
|
||||
elif sym.endswith('P') and len(sym) == 2: # e.g. WP (Phyrexian) -> treat as that color
|
||||
for p in [p for p in sym.split('/') if p in pip_cards]:
|
||||
colors_for_card.add(p)
|
||||
elif sym.endswith('P') and len(sym) == 2:
|
||||
base = sym[0]
|
||||
if base in pip_counts:
|
||||
pip_counts[base] += 1
|
||||
total_pips += 1
|
||||
if base in pip_cards:
|
||||
colors_for_card.add(base)
|
||||
if colors_for_card:
|
||||
cnt = int(info.get('Count', 1))
|
||||
for c in colors_for_card:
|
||||
pip_cards[c].append({'name': name, 'count': cnt})
|
||||
if total_pips <= 0:
|
||||
# Fallback to even distribution across color identity
|
||||
colors = [c for c in ('W','U','B','R','G') if c in (getattr(self, 'color_identity', []) or [])]
|
||||
colors = [c for c in ('W', 'U', 'B', 'R', 'G') if c in (getattr(self, 'color_identity', []) or [])]
|
||||
if colors:
|
||||
share = 1 / len(colors)
|
||||
for c in colors:
|
||||
|
|
@ -766,6 +771,10 @@ class ReportingMixin:
|
|||
'colors': list(getattr(self, 'color_identity', []) or []),
|
||||
'include_exclude_summary': include_exclude_summary,
|
||||
}
|
||||
# M3 (Roadmap 14): attach smart-land diagnostics when available
|
||||
land_report_data = getattr(self, '_land_report_data', None)
|
||||
if land_report_data:
|
||||
summary_payload['land_report'] = dict(land_report_data)
|
||||
|
||||
try:
|
||||
commander_meta = self.get_commander_export_metadata()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue