expand CLI

This commit is contained in:
Maximilian Keßler 2023-12-22 12:23:22 +01:00
parent dee13076e4
commit 295458f3f6
Signed by: max
GPG key ID: BCC5A619923C0BA5
4 changed files with 55 additions and 16 deletions

View file

@ -13,6 +13,9 @@ import database
import log_setup
import fetch_players
import fetch_games
import ratings
import stats
import render_site
from log_setup import logger
@ -56,6 +59,21 @@ def subcommand_fetch(target: str):
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:
parser = argparse.ArgumentParser(
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')
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.add_argument(dest='target', choices=['all', 'players', 'games', 'game-details'], default='all')
@ -84,6 +105,9 @@ def main():
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
}[args.command]

View file

@ -28,9 +28,6 @@ session = requests_cache.CachedSession(
}
)
print(platformdirs.user_cache_dir(constants.APP_NAME) + 'hanab.live.requests-cache')
class GameInfo:
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]):

View file

@ -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,
# and makes the program structure easier, therefore avoiding mistakes and improving granularity
# of our database updates.
i = 0
while process_rating_of_next_game():
pass
i += 1
logger.info("Processed rating of all games ({} many)".format(i))

View file

@ -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]
def render_leaderboard():
def render_main_site(env: jinja2.Environment, out_dir: Path):
rating_lists = get_rating_lists()
streak_lists = get_streak_list()
leaders = get_leaders(rating_lists, streak_lists)
variant_rows: List[VariantRow] = get_variant_rows()
variant_stats, variant_stats_by_player, variant_names = build_variant_stats_by_variant(variant_rows)
leaderboards = {
'Player Rating': rating_lists,
'Maximum Streak (Current Streak)': streak_lists,
}
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
template = env.get_template('main.html')
rendered_html = template.render(
leaders=leaders,
@ -464,17 +461,26 @@ def render_leaderboard():
# variants=variants,
)
out_dir = Path(constants.WEBSITE_OUTPUT_DIRECTORY)
output_file = out_dir / 'index.html'
output_file.parent.mkdir(exist_ok=True, parents=True)
with open(output_file, 'w') as f:
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()
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():
rendered_var = variant_template.render(
total_games_played=get_total_games(),
@ -492,6 +498,8 @@ def render_leaderboard():
with open(output_file, 'w') as f:
f.write(rendered_var)
def render_player_pages(env: jinja2.Environment, out_dir: Path):
player_stats = get_player_stats()
player_template = env.get_template('player.html')
@ -510,12 +518,20 @@ def render_leaderboard():
with open(output_file, 'w') as f:
f.write(rendered_player)
# 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 get_env_and_out_dir():
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
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__":
render_leaderboard()
render_all()