expand CLI
This commit is contained in:
parent
dee13076e4
commit
295458f3f6
4 changed files with 55 additions and 16 deletions
|
@ -13,6 +13,9 @@ import database
|
||||||
import log_setup
|
import log_setup
|
||||||
import fetch_players
|
import fetch_players
|
||||||
import fetch_games
|
import fetch_games
|
||||||
|
import ratings
|
||||||
|
import stats
|
||||||
|
import render_site
|
||||||
|
|
||||||
from log_setup import logger
|
from log_setup import logger
|
||||||
|
|
||||||
|
@ -56,6 +59,21 @@ def subcommand_fetch(target: str):
|
||||||
fetch_games.fetch_all_game_details()
|
fetch_games.fetch_all_game_details()
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_process_ratings():
|
||||||
|
logger.info("Processing rating of all games.")
|
||||||
|
ratings.process_rating_of_all_games()
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_process_stats():
|
||||||
|
logger.info("Processing stats for all users.")
|
||||||
|
stats.update_user_statistics()
|
||||||
|
|
||||||
|
|
||||||
|
def subcommand_generate_site():
|
||||||
|
logger.info("Generating website.")
|
||||||
|
render_site.render_all()
|
||||||
|
|
||||||
|
|
||||||
def get_parser() -> argparse.ArgumentParser:
|
def get_parser() -> argparse.ArgumentParser:
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
prog=constants.APP_NAME,
|
prog=constants.APP_NAME,
|
||||||
|
@ -70,6 +88,9 @@ def get_parser() -> argparse.ArgumentParser:
|
||||||
init_parser.add_argument('--no-fetch-variants', '-n', help='Do not fetch and initialize variants', 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('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.")
|
||||||
|
|
||||||
fetch_parser = subparsers.add_parser('fetch', help='Fetch new data.')
|
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(dest='target', choices=['all', 'players', 'games', 'game-details'], default='all')
|
||||||
|
@ -84,6 +105,9 @@ def main():
|
||||||
subcommand_func = {
|
subcommand_func = {
|
||||||
'init': subcommand_init,
|
'init': subcommand_init,
|
||||||
'generate-config': subcommand_generate_config,
|
'generate-config': subcommand_generate_config,
|
||||||
|
'process-ratings': subcommand_process_ratings,
|
||||||
|
'process-stats': subcommand_process_stats,
|
||||||
|
'generate-site': subcommand_generate_site,
|
||||||
'fetch': subcommand_fetch
|
'fetch': subcommand_fetch
|
||||||
}[args.command]
|
}[args.command]
|
||||||
|
|
||||||
|
|
|
@ -28,9 +28,6 @@ session = requests_cache.CachedSession(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
print(platformdirs.user_cache_dir(constants.APP_NAME) + 'hanab.live.requests-cache')
|
|
||||||
|
|
||||||
|
|
||||||
class GameInfo:
|
class GameInfo:
|
||||||
def __init__(self, game_id: int, num_players: int, variant_id: int, seed: str, score: int, num_turns: int,
|
def __init__(self, game_id: int, num_players: int, variant_id: int, seed: str, score: int, num_turns: int,
|
||||||
user_ids: List[int], normalized_usernames: List[str]):
|
user_ids: List[int], normalized_usernames: List[str]):
|
||||||
|
|
|
@ -263,5 +263,7 @@ def process_rating_of_all_games():
|
||||||
# However, since we have such a small number of games, this is fast enough without problems,
|
# However, since we have such a small number of games, this is fast enough without problems,
|
||||||
# and makes the program structure easier, therefore avoiding mistakes and improving granularity
|
# and makes the program structure easier, therefore avoiding mistakes and improving granularity
|
||||||
# of our database updates.
|
# of our database updates.
|
||||||
|
i = 0
|
||||||
while process_rating_of_next_game():
|
while process_rating_of_next_game():
|
||||||
pass
|
i += 1
|
||||||
|
logger.info("Processed rating of all games ({} many)".format(i))
|
||||||
|
|
|
@ -437,21 +437,18 @@ def build_unique_variants(variant_rows: List[VariantRow]):
|
||||||
return [row for row in variant_rows if row.num_players == config.config_manager.get_config().min_player_count]
|
return [row for row in variant_rows if row.num_players == config.config_manager.get_config().min_player_count]
|
||||||
|
|
||||||
|
|
||||||
def render_leaderboard():
|
def render_main_site(env: jinja2.Environment, out_dir: Path):
|
||||||
rating_lists = get_rating_lists()
|
rating_lists = get_rating_lists()
|
||||||
streak_lists = get_streak_list()
|
streak_lists = get_streak_list()
|
||||||
leaders = get_leaders(rating_lists, streak_lists)
|
leaders = get_leaders(rating_lists, streak_lists)
|
||||||
|
|
||||||
variant_rows: List[VariantRow] = get_variant_rows()
|
variant_rows: List[VariantRow] = get_variant_rows()
|
||||||
variant_stats, variant_stats_by_player, variant_names = build_variant_stats_by_variant(variant_rows)
|
|
||||||
|
|
||||||
leaderboards = {
|
leaderboards = {
|
||||||
'Player Rating': rating_lists,
|
'Player Rating': rating_lists,
|
||||||
'Maximum Streak (Current Streak)': streak_lists,
|
'Maximum Streak (Current Streak)': streak_lists,
|
||||||
}
|
}
|
||||||
|
|
||||||
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
|
|
||||||
|
|
||||||
template = env.get_template('main.html')
|
template = env.get_template('main.html')
|
||||||
rendered_html = template.render(
|
rendered_html = template.render(
|
||||||
leaders=leaders,
|
leaders=leaders,
|
||||||
|
@ -464,17 +461,26 @@ def render_leaderboard():
|
||||||
# variants=variants,
|
# variants=variants,
|
||||||
)
|
)
|
||||||
|
|
||||||
out_dir = Path(constants.WEBSITE_OUTPUT_DIRECTORY)
|
|
||||||
|
|
||||||
output_file = out_dir / 'index.html'
|
output_file = out_dir / 'index.html'
|
||||||
output_file.parent.mkdir(exist_ok=True, parents=True)
|
output_file.parent.mkdir(exist_ok=True, parents=True)
|
||||||
with open(output_file, 'w') as f:
|
with open(output_file, 'w') as f:
|
||||||
f.write(rendered_html)
|
f.write(rendered_html)
|
||||||
|
|
||||||
|
# Copy CSS to output directory
|
||||||
|
shutil.copytree('css', 'build/css', dirs_exist_ok=True)
|
||||||
|
shutil.copytree('deps/tabulator/dist/css', 'build/css', dirs_exist_ok=True)
|
||||||
|
shutil.copytree('deps/tabulator/dist/js', 'build/js', dirs_exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
|
def render_variant_pages(env: jinja2.Environment, out_dir: Path):
|
||||||
|
variant_template = env.get_template('variant.html')
|
||||||
|
|
||||||
games = get_games()
|
games = get_games()
|
||||||
grouped_games_var = group_games_by_var_id(games)
|
grouped_games_var = group_games_by_var_id(games)
|
||||||
|
|
||||||
variant_template = env.get_template('variant.html')
|
variant_rows: List[VariantRow] = get_variant_rows()
|
||||||
|
variant_stats, variant_stats_by_player, variant_names = build_variant_stats_by_variant(variant_rows)
|
||||||
|
|
||||||
for variant_id, by_player_stats in variant_stats_by_player.items():
|
for variant_id, by_player_stats in variant_stats_by_player.items():
|
||||||
rendered_var = variant_template.render(
|
rendered_var = variant_template.render(
|
||||||
total_games_played=get_total_games(),
|
total_games_played=get_total_games(),
|
||||||
|
@ -492,6 +498,8 @@ def render_leaderboard():
|
||||||
with open(output_file, 'w') as f:
|
with open(output_file, 'w') as f:
|
||||||
f.write(rendered_var)
|
f.write(rendered_var)
|
||||||
|
|
||||||
|
|
||||||
|
def render_player_pages(env: jinja2.Environment, out_dir: Path):
|
||||||
player_stats = get_player_stats()
|
player_stats = get_player_stats()
|
||||||
|
|
||||||
player_template = env.get_template('player.html')
|
player_template = env.get_template('player.html')
|
||||||
|
@ -510,12 +518,20 @@ def render_leaderboard():
|
||||||
with open(output_file, 'w') as f:
|
with open(output_file, 'w') as f:
|
||||||
f.write(rendered_player)
|
f.write(rendered_player)
|
||||||
|
|
||||||
# Copy CSS to output directory
|
|
||||||
shutil.copytree('css', 'build/css', dirs_exist_ok=True)
|
def get_env_and_out_dir():
|
||||||
shutil.copytree('deps/tabulator/dist/css', 'build/css', dirs_exist_ok=True)
|
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
|
||||||
shutil.copytree('deps/tabulator/dist/js', 'build/js', dirs_exist_ok=True)
|
out_dir = Path(constants.WEBSITE_OUTPUT_DIRECTORY)
|
||||||
|
return env, out_dir
|
||||||
|
|
||||||
|
|
||||||
|
def render_all():
|
||||||
|
env, out_dir = get_env_and_out_dir()
|
||||||
|
render_main_site(env, out_dir)
|
||||||
|
render_player_pages(env, out_dir)
|
||||||
|
render_variant_pages(env, out_dir)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
render_leaderboard()
|
render_all()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue