Skip to content

Commit

Permalink
game: Add more eager loading of Sheet.current_user (in stop_game())
Browse files Browse the repository at this point in the history
  • Loading branch information
mhthies committed Jun 28, 2020
1 parent 0faf4b8 commit 3ecbdb2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions qaqa_bot/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def stop_game(self, session: Session, chat_id: int) -> List[TranslatedMessage]:

logger.info("Marking game %s to stop at next opportunity.", game.id)
game.is_waiting_for_finish = True
sheet_infos = list(self._game_sheet_infos(game, session))
sheet_infos = list(self._game_sheet_infos(game, session, eager_current_user=True))

messages = self._finish_if_stopped_and_all_answered(game, sheet_infos, session)

Expand All @@ -497,7 +497,7 @@ def stop_game(self, session: Session, chat_id: int) -> List[TranslatedMessage]:
users_to_update = set()
for sheet_info in sheet_infos:
if not sheet_info.num_entries or sheet_info.last_entry.type == model.EntryType.ANSWER:
sheet_user: Optional[model.User] = sheet_info.sheet.current_user # TODO optimize?
sheet_user: Optional[model.User] = sheet_info.sheet.current_user
if sheet_user is not None:
logger.debug("Removing sheet %s from user %s's queue due to game stop.",
sheet_info.sheet.id, sheet_user.id)
Expand Down Expand Up @@ -930,6 +930,8 @@ def _finalize_game(self, game: model.Game, session: Session) -> List[Message]:
logger.info("Finalizing game %s ...", game.id)
messages = []
# Requery sheets with optimized loading of current_user
# TODO this is only required when called via `.stop_game_immediately()`. In all other cases, the sheets have
# already been called with a joinedload of the Sheet.current_user.
sheets: List[model.Sheet] = session.query(model.Sheet)\
.filter(model.Sheet.game == game)\
.populate_existing()\
Expand Down

0 comments on commit 3ecbdb2

Please sign in to comment.