#!/usr/bin/env python3 """ Manual script to distribute packs to all human-controlled teams """ import argparse import asyncio import logging import os import sys from api_calls import db_get, db_post, DB_URL # Set up logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) logger = logging.getLogger("manual_pack_dist") def check_environment(): """Check that required environment variables are set""" api_token = os.getenv("API_TOKEN") database_env = os.getenv("DATABASE", "dev") if not api_token: logger.error("ERROR: API_TOKEN environment variable is not set!") logger.error("Please set it with: export API_TOKEN='your-token-here'") sys.exit(1) logger.info(f"Using database environment: {database_env}") logger.info(f"API URL: {DB_URL}") logger.info( f"API Token: {'*' * (len(api_token) - 4)}{api_token[-4:] if len(api_token) > 4 else '****'}" ) logger.info("") async def distribute_packs(num_packs: int = 5, exclude_team_abbrev: list[str] = None): """Distribute packs to all human-controlled teams Args: num_packs: Number of packs to give to each team (default: 5) exclude_team_abbrev: List of team abbreviations to exclude (default: None) """ if exclude_team_abbrev is None: exclude_team_abbrev = [] # Convert to uppercase for case-insensitive matching exclude_team_abbrev = [abbrev.upper() for abbrev in exclude_team_abbrev] # Step 1: Get current season logger.info("Fetching current season...") current = await db_get("current") if current is None: logger.error( "Failed to fetch current season from API. Check your API_TOKEN and DATABASE environment variables." ) logger.error(f"API URL: {DB_URL}") logger.error( "Make sure the API endpoint is accessible and your token is valid." ) raise ValueError("Could not connect to the database API") logger.info(f"Current season: {current['season']}") # Step 2: Get all teams for current season logger.info(f"Fetching all teams for season {current['season']}...") all_teams = await db_get("teams", params=[("season", current["season"])]) if all_teams is None: logger.error("Failed to fetch teams from API") raise ValueError("Could not fetch teams from the database API") logger.info(f"Found {all_teams['count']} total teams") # Step 3: Filter for human-controlled teams only qualifying_teams = [] for team in all_teams["teams"]: if not team["is_ai"] and "gauntlet" not in team["abbrev"].lower(): # Check if team is in exclusion list if team["abbrev"].upper() in exclude_team_abbrev: logger.info(f" - {team['abbrev']}: {team['sname']} (EXCLUDED)") continue qualifying_teams.append(team) logger.info(f" - {team['abbrev']}: {team['sname']}") logger.info(f"\nFound {len(qualifying_teams)} human-controlled teams") if exclude_team_abbrev: logger.info(f"Excluded teams: {', '.join(exclude_team_abbrev)}") # Step 4: Distribute packs pack_type_id = 1 # Standard packs logger.info(f"\nDistributing {num_packs} standard packs to each team...") for team in qualifying_teams: logger.info( f"Giving {num_packs} packs to {team['abbrev']} ({team['sname']})..." ) # Create pack payload pack_payload = { "packs": [ { "team_id": team["id"], "pack_type_id": pack_type_id, "pack_cardset_id": None, } for _ in range(num_packs) ] } try: await db_post("packs", payload=pack_payload) logger.info(f" āœ“ Successfully gave packs to {team['abbrev']}") except Exception as e: logger.error(f" āœ— Failed to give packs to {team['abbrev']}: {e}") logger.info( f"\nšŸŽ‰ All done! Distributed {num_packs} packs to {len(qualifying_teams)} teams" ) if __name__ == "__main__": parser = argparse.ArgumentParser( description="Distribute packs to all human-controlled teams", formatter_class=argparse.RawDescriptionHelpFormatter, epilog=""" Examples: # Give 5 packs to all teams (default) python manual_pack_distribution.py # Give 10 packs to all teams python manual_pack_distribution.py --num-packs 10 # Give 5 packs, excluding certain teams python manual_pack_distribution.py --exclude-team-abbrev NYY BOS # Give 3 packs, excluding one team python manual_pack_distribution.py --num-packs 3 --exclude-team-abbrev LAD Environment Variables: API_TOKEN - Required: API authentication token DATABASE - Optional: 'dev' (default) or 'prod' """, ) parser.add_argument( "--num-packs", type=int, default=5, help="Number of packs to give to each team (default: 5)", ) parser.add_argument( "--exclude-team-abbrev", nargs="*", default=[], help="Team abbreviations to exclude (space-separated, e.g., NYY BOS LAD)", ) args = parser.parse_args() # Check environment before running check_environment() asyncio.run( distribute_packs( num_packs=args.num_packs, exclude_team_abbrev=args.exclude_team_abbrev ) )