Skip to content

Commit

Permalink
Only show download button if game file actually exists
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
luator committed Feb 3, 2025
1 parent 068fc26 commit 432e6c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 7 additions & 3 deletions comprl-web-reflex/comprl_web/pages/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}),
)
),
)
Expand Down
13 changes: 10 additions & 3 deletions comprl-web-reflex/comprl_web/protected_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""State for the protected pages."""

import dataclasses
import pathlib
from typing import Sequence

import reflex as rx
Expand All @@ -27,6 +28,7 @@ class GameInfo:
result: str
time: str
id: str
has_game_file: bool


class ProtectedState(reflex_local_auth.LocalAuthState):
Expand Down Expand Up @@ -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 = []
Expand All @@ -230,26 +235,28 @@ 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,
game.user2_.username,
result,
str(game.start_time.strftime("%Y-%m-%d %H:%M:%S")),
str(game.game_id),
game_file_exists,
)
)

self.total_items = self._get_num_user_games()

@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)

0 comments on commit 432e6c5

Please sign in to comment.