6de22f7f1f
1 Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
cb471d8057 |
CLAUDE: Extract rarity cost adjustment logic into data-driven function
This commit eliminates 150+ lines of duplicated, error-prone nested if/elif logic by extracting rarity cost calculations into a lookup table and function. ## Changes Made 1. **Add RARITY_COST_ADJUSTMENTS lookup table** (creation_helpers.py) - Maps (old_rarity, new_rarity) → (cost_adjustment, minimum_cost) - Covers all 30 possible rarity transitions - Self-documenting with comments for each rarity tier - Single source of truth for all cost adjustments 2. **Add calculate_rarity_cost_adjustment() function** (creation_helpers.py) - Takes old_rarity, new_rarity, old_cost - Returns new cost with adjustments and minimums applied - Includes comprehensive docstring with examples - Handles edge cases (same rarity, undefined transitions) - Logs warnings for undefined transitions 3. **Update batters/creation.py** - Import calculate_rarity_cost_adjustment - Replace 75-line nested if/elif block with 7-line function call - Identical behavior, much cleaner code 4. **Update pitchers/creation.py** - Import calculate_rarity_cost_adjustment - Replace 75-line nested if/elif block with 7-line function call - Eliminates duplication between batters and pitchers 5. **Add comprehensive tests** (tests/test_rarity_cost_adjustments.py) - 22 tests covering all scenarios - Tests individual transitions (Diamond→Gold, Common→Bronze, etc.) - Tests all upward and downward transitions - Tests minimum cost enforcement - Tests edge cases (zero cost, very high cost, negative cost) - Tests symmetry (up then down returns close to original) ## Impact ### Lines Eliminated - **Batters:** 75 lines → 7 lines (89% reduction) - **Pitchers:** 75 lines → 7 lines (89% reduction) - **Total:** 150 lines of nested logic eliminated ### Benefits ✅ Eliminates 150+ lines of duplicated code ✅ Data-driven approach makes adjustments clear and modifiable ✅ Single source of truth prevents inconsistencies ✅ Independently testable business logic ✅ 22 comprehensive tests ensure correctness ✅ Easy to add new rarity tiers or modify costs ✅ Reduced risk of typos in magic numbers ## Test Results ✅ 22/22 new tests pass ✅ All existing tests still pass ✅ 100% backward compatible - identical behavior ## Files Modified - creation_helpers.py: +104 lines (table + function + docs) - batters/creation.py: -68 lines (replaced nested logic) - pitchers/creation.py: -68 lines (replaced nested logic) - tests/test_rarity_cost_adjustments.py: +174 lines (new tests) **Net change:** 150+ lines of complex logic replaced with simple, tested, data-driven approach. Part of ongoing refactoring to reduce code fragility. |