hanabi-league/hanabi-league

137 lines
4.3 KiB
Python
Executable file

#! python3
import argparse
import sys
import verboselogs
sys.path.append('src/')
import config
import constants
import database
import log_setup
import fetch_players
import fetch_games
import ratings
import stats
import render_site
from log_setup import logger
def subcommand_init(force: bool, no_fetch_variants: bool):
tables = database.get_existing_tables()
if len(tables) > 0 and not force:
logger.info(
'Database tables "{}" exist already, aborting. To force re-initialization, use the --force options'
.format(", ".join(tables))
)
return
if len(tables) > 0:
logger.info(
"WARNING: This will drop all existing tables from the database and re-initialize them."
)
response = input("Do you wish to continue? [y/N] ")
if response not in ["y", "Y", "yes"]:
logger.info("Database initialization aborted.")
return
database.init_database()
logger.info("Successfully initialized database tables")
if not no_fetch_variants:
database.fetch_and_initialize_variants()
database.initialize_variant_base_ratings()
logger.info("Successfully initialized variants and base ratings.")
def subcommand_generate_config():
config.create_db_config()
config.create_config()
def subcommand_fetch(target: str, refresh: bool = False):
if target in ["all", "players"]:
fetch_players.fetch_players_interactive()
if target in ["all", "games"]:
games = fetch_games.fetch_games_for_all_players()
fetch_games.store_new_games(games)
if target in ["all", "game-details"]:
fetch_games.fetch_all_game_details(refresh)
def subcommand_process_ratings():
logger.info("Processing rating of all games.")
ratings.process_rating_of_all_games()
def subcommand_process_stats():
logger.info("Analysing all games in database.")
stats.analyze_all_games()
logger.info("Processing stats for all users.")
stats.update_user_statistics()
def subcommand_generate_site():
logger.info("Generating website.")
render_site.render_all()
def subcommand_run():
subcommand_fetch("games")
subcommand_fetch("game-details")
subcommand_process_ratings()
subcommand_process_stats()
subcommand_generate_site()
def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog=constants.APP_NAME,
description="Data collection and analysis tool for the Hanabi League"
)
parser.add_argument('--verbose', '-v', help='Enable verbose logging to console', action='store_true')
subparsers = parser.add_subparsers(dest='command', required=True, help='select subcommand')
init_parser = subparsers.add_parser('init', help='Initialize database.')
init_parser.add_argument('--force', '-f', help='Force initialization (Drops existing tables)', action='store_true')
init_parser.add_argument('--no-fetch-variants', '-n', help='Do not fetch and initialize variants', action='store_true')
subparsers.add_parser('generate-config', help='Generate config file at default location')
subparsers.add_parser('process-ratings', help="Process ratings of all games.")
subparsers.add_parser('process-stats', help="Process statistics for all players.")
subparsers.add_parser('generate-site', help="Generate the website from the DB.")
subparsers.add_parser('run', help="Run the automatic suite: Fetch + process games and render site.")
fetch_parser = subparsers.add_parser('fetch', help='Fetch new data.')
fetch_parser.add_argument(dest='target', choices=['all', 'players', 'games', 'game-details'], default='all')
fetch_parser.add_argument('--refresh', action='store_true')
return parser
def main():
parser = get_parser()
args = parser.parse_args()
subcommand_func = {
'init': subcommand_init,
'generate-config': subcommand_generate_config,
'process-ratings': subcommand_process_ratings,
'process-stats': subcommand_process_stats,
'generate-site': subcommand_generate_site,
'fetch': subcommand_fetch,
'run': subcommand_run,
}[args.command]
if args.verbose:
log_setup.logger_manager.set_console_level(verboselogs.VERBOSE)
del args.command
del args.verbose
subcommand_func(**vars(args))
if __name__ == "__main__":
main()