Replace None-returning method with a pair, one that returns bool and one that always returns a game.

This commit is contained in:
Daniel Johnson 2024-01-01 09:23:51 -05:00
parent f7991b3553
commit df3175d91f
4 changed files with 25 additions and 16 deletions

View file

@ -119,7 +119,7 @@ class GameActions(BaseGameActions):
@property
def is_game_running(self):
for game in self.games:
if game.is_db_stored and self.application.get_running_game_by_id(game.id):
if game.is_db_stored and self.application.is_game_running_by_id(game.id):
return True
return False
@ -247,7 +247,7 @@ class GameActions(BaseGameActions):
"""Launch a game"""
for game in self.games:
if game.is_installed and game.is_db_stored:
if not self.application.get_running_game_by_id(game.id):
if not self.application.is_game_running_by_id(game.id):
game.launch(self.window)
def get_running_games(self):

View file

@ -25,6 +25,7 @@ import tempfile
from datetime import datetime, timedelta
from gettext import gettext as _
from typing import List
import gi
@ -865,15 +866,26 @@ class Application(Gtk.Application):
def get_launch_ui_delegate(self):
return self.launch_ui_delegate
def get_running_game_ids(self):
def get_running_game_ids(self) -> List[str]:
"""Returns the ids of the games presently running."""
return [game.id for game in self.running_games]
def get_running_game_by_id(self, game_id):
def is_game_running_by_id(self, game_id: str) -> bool:
"""True if the ID is the ID of a game that is running."""
if game_id:
for game in self.running_games:
if game.id == str(game_id):
return game
return None
return True
return False
def get_game_by_id(self, game_id: str) -> Game:
"""Returns the game with the ID given; if it's running this is the running
game instance, and if not it's a fresh copy off the database."""
for game in self.running_games:
if game.id == str(game_id):
return game
return Game(game_id)
@staticmethod
def get_lutris_action(url):

View file

@ -74,11 +74,10 @@ class GameView:
def _get_games_by_ids(self, game_ids: List[str]) -> List[Game]:
"""Resolves a list of game-ids to a list of game objects,
looking up running games, service games and all that."""
application = Gio.Application.get_default()
def _get_game_by_id(id_to_find: str) -> Game:
running = application.get_running_game_by_id(id_to_find) if application else None
return running or Game(id_to_find)
application = Gio.Application.get_default()
return application.get_game_by_id(id_to_find) if application else Game(id_to_find)
games = []
for game_id in game_ids:
@ -86,15 +85,13 @@ class GameView:
db_game = get_game_for_service(self.service.id, game_id)
if db_game:
game = _get_game_by_id(db_game["id"])
if db_game["id"]:
games.append(_get_game_by_id(db_game["id"]))
else:
db_game = ServiceGameCollection.get_game(self.service.id, game_id)
game = Game.create_empty_service_game(db_game, self.service)
games.append(Game.create_empty_service_game(db_game, self.service))
elif game_id:
game = _get_game_by_id(game_id)
else:
continue
games.append(game)
games.append(_get_game_by_id(game_id))
return games

View file

@ -51,7 +51,7 @@ class GameBar(Gtk.Box):
if game:
game_id = game["id"]
if game_id:
self.game = application.get_running_game_by_id(game_id) or Game(game_id)
self.game = application.get_game_by_id(game_id)
else:
self.game = Game.create_empty_service_game(db_game, self.service)
self.update_view()