From 432e6c5cc9c45577c84168a613ba6523f9bd13b5 Mon Sep 17 00:00:00 2001 From: Felix Kloss Date: Mon, 27 Jan 2025 15:02:44 +0100 Subject: [PATCH] Only show download button if game file actually exists This is mostly relevant for disconnected games, where no data is saved anymore. However, it might also be useful to avoid confusion if a game file is missing for other reasons. --- comprl-web-reflex/comprl_web/pages/games.py | 10 +++++++--- comprl-web-reflex/comprl_web/protected_state.py | 13 ++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/comprl-web-reflex/comprl_web/pages/games.py b/comprl-web-reflex/comprl_web/pages/games.py index 02fa860..da40833 100644 --- a/comprl-web-reflex/comprl_web/pages/games.py +++ b/comprl-web-reflex/comprl_web/pages/games.py @@ -19,9 +19,13 @@ def show_game(game: GameInfo) -> rx.Component: rx.table.cell(game.time), rx.table.cell(game.id), rx.table.cell( - rx.button( - "Download game data", - on_click=lambda: UserGamesState.download_game(game.id), + rx.cond( + game.has_game_file, + rx.button( + "Download game data", + on_click=lambda: UserGamesState.download_game(game.id), + ), + rx.text("No game data", align="center", style={"font-style": "italic"}), ) ), ) diff --git a/comprl-web-reflex/comprl_web/protected_state.py b/comprl-web-reflex/comprl_web/protected_state.py index ad7ff3a..d9043a4 100644 --- a/comprl-web-reflex/comprl_web/protected_state.py +++ b/comprl-web-reflex/comprl_web/protected_state.py @@ -1,6 +1,7 @@ """State for the protected pages.""" import dataclasses +import pathlib from typing import Sequence import reflex as rx @@ -27,6 +28,7 @@ class GameInfo: result: str time: str id: str + has_game_file: bool class ProtectedState(reflex_local_auth.LocalAuthState): @@ -217,6 +219,9 @@ def clear_search(self): self.search_id = "" self.load_user_games() + def _get_game_file_path(self, game_id: str) -> pathlib.Path: + return config.get_config().data_dir / "game_actions" / f"{game_id}.pkl" + @rx.event def load_user_games(self) -> None: self.user_games = [] @@ -230,6 +235,8 @@ def load_user_games(self) -> None: else: result = "Unknown" + game_file_exists = self._get_game_file_path(game.game_id).exists() + self.user_games.append( GameInfo( game.user1_.username, @@ -237,6 +244,7 @@ def load_user_games(self) -> None: result, str(game.start_time.strftime("%Y-%m-%d %H:%M:%S")), str(game.game_id), + game_file_exists, ) ) @@ -244,12 +252,11 @@ def load_user_games(self) -> None: @rx.event def download_game(self, game_id: str): - game_file_name = f"{game_id}.pkl" - game_file_path = config.get_config().data_dir / "game_actions" / game_file_name + game_file_path = self._get_game_file_path(game_id) try: data = game_file_path.read_bytes() except Exception: raise RuntimeError("Game file not found") from None - return rx.download(filename=game_file_name, data=data) + return rx.download(filename=game_file_path.name, data=data)