Paper Dynasty Card Creation - Baseball card generation system
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. |
||
|---|---|---|
| .claude/plans | ||
| batters | ||
| card-output | ||
| data-input | ||
| defenders | ||
| html work | ||
| logs | ||
| pitchers | ||
| scouting | ||
| scripts | ||
| tests | ||
| .gitignore | ||
| automated_data_fetcher.py | ||
| batter-deltas.csv | ||
| batting_stats.csv | ||
| check_cards.py | ||
| CLAUDE.md | ||
| creation_helpers.py | ||
| db_calls_card_creation.py | ||
| db_calls.py | ||
| exceptions.py | ||
| live_series_update.py | ||
| new-batters.csv | ||
| new-pitchers.csv | ||
| pitcher-deltas.csv | ||
| pitching_stats.csv | ||
| pkmn.json | ||
| post_raw_player_csv.py | ||
| PROMO_CARD_FIX.md | ||
| pull_pitching_stats.py | ||
| pybaseball_doodling.py | ||
| pytest.ini | ||
| README.txt | ||
| REFACTORING_SUMMARY.md | ||
| refresh_cards.py | ||
| requirements.txt | ||
| retrosheet_data.py | ||
| retrosheet.db | ||
| scouting_batters.py | ||
| scouting_pitchers.py | ||
| test_data_fetcher_demo.py | ||
#######
CARD CREATION PROCESS
#######
1) Download stats
FanGraphs / https://www.fangraphs.com/leaders/splits-leaderboards
- Batting
- vL Standard / vlhp-basic.csv
- vL Batted Balls / vlhp-rate.csv
- vR Standard / vrhp-basic.csv
- vR Batted Balls / vrhp-rate.csv
- Pitching
- vL Standard / vlhh-basic.csv
- vL Batted Balls / vlhh-rate.csv
- vR Standard / vrhh-basic.csv
- vR Batted Balls / vrhh-rate.csv
Baseball Reference
- running.csv
- https://www.baseball-reference.com/leagues/majors/2023-baserunning-batting.shtml
- Remove header lines
- pitching.csv
- https://www.baseball-reference.com/leagues/majors/2023-standard-pitching.shtml
2) Run Card Updates (Python Configuration)
3) Check Card Validity (Python Configuration)
#######
OLD DATA REQUIREMENTS
#######
- Add any new players to players.csv for import
- Create directory in /data-input in format `XXXX Season Cardset`
- Upload the following csv files:
- baserunning-data.csv
- https://www.baseball-reference.com/leagues/majors/2023-baserunning-batting.shtml
- Remove header lines
- batter-stats.csv
- https://www.fangraphs.com/leaders/splits-leaderboards
- Remove header lines
- 20 PA vL / 40 PA vR for Live || 50 PA vL / 75 PA vR for legacy seasons
- defense-X.csv (each position)
- https://www.baseball-reference.com/leagues/majors/2023-specialpos_p-fielding.shtml
- replace the `p` in `p-fielding` with 1b/2b/lf
- Column Changes (pre-2013)
- Catchers: add column between Rgood and RsbC
- 1b/2b/3b/ss: add 3 columns between Rgood and Rbnt
- defense-of.csv (don't forget combined OF)
- https://www.baseball-reference.com/leagues/majors/2023-specialpos_of-fielding.shtml
- replace the `p` in `p-fielding` with of
- pitcher-data.csv
- https://www.baseball-reference.com/leagues/majors/2023-standard-pitching.shtml
- pitcher-stats.csv
- https://www.fangraphs.com/leaders/splits-leaderboards
- Remove header lines
- 20 TBF vL / 40 TBF vR for Live || 50 TBF vL / 75 TBF vR for legacy seasons
#######
OLD CARD CREATION PROCESS
#######
1) Import new players for sba_id with `1. Import Players`
2) Confirm cardset exists; if not, create now
3) Create cards with `3. Card Creation`
4) Generate csv output with `4. Card Output`
5) Upload output files into Sheets for Component Studio import
6) Upload ratings output files into Sheets for PD Ratings Guide
7) Import cards into Component Studio
8) Export -> Download All from Component Studio
9) Rename image files to <first>.<last>.png