From 764ba1c05a3dcdf22a1df5b24601cacb2107897e Mon Sep 17 00:00:00 2001 From: Rallen Date: Thu, 3 Sep 2020 21:51:18 +0200 Subject: [PATCH] Add timeout for library updates, fix tick updates --- src/plugin.py | 51 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/plugin.py b/src/plugin.py index 696a53b..a9ae6ac 100644 --- a/src/plugin.py +++ b/src/plugin.py @@ -2,7 +2,7 @@ import sys import webbrowser -from pathlib import Path +from time import time from galaxy.api.plugin import Plugin, create_and_run_plugin from galaxy.api.consts import Platform, LicenseType, LocalGameState, OSCompatibility @@ -14,21 +14,24 @@ from authentication import create_next_step, START_URI, END_URI +LOCAL_GAMES_TIMEOUT = (1 * 60) +OWNED_GAMES_TIMEOUT = (10 * 60) + + class AmazonGamesPlugin(Plugin): _owned_games_db = None - _local_games_cache = {} - _owned_games_cache = {} - _client = None def __init__(self, reader, writer, token): super().__init__(Platform.Amazon, __version__, reader, writer, token) self.logger = logging.getLogger('amazonPlugin') self._client = AmazonGamesClient() - self.logger.info("Plugin __init__") + self._local_games_cache = None + self._owned_games_cache = None + self._owned_games_last_updated = self._local_games_last_updated = time() def _init_db(self): - if not self._owned_games_db and self._client.is_installed: + if not self._owned_games_db: self._owned_games_db = DBClient(self._client.owned_games_db_path) def _on_auth(self): @@ -48,7 +51,10 @@ def _get_owned_games(self): self.logger.exception('Failed to get owned games') return {} - def _update_owned_games_cache(self): + def _update_owned_games(self): + if (time() - self._owned_games_last_updated) < OWNED_GAMES_TIMEOUT: + return + owned_games = self._get_owned_games() for game_id in self._owned_games_cache.keys() - owned_games.keys(): @@ -58,6 +64,7 @@ def _update_owned_games_cache(self): self.add_game(owned_games[game_id]) self._owned_games_cache = owned_games + self._owned_games_last_updated = time() def _get_local_games(self): try: @@ -69,7 +76,10 @@ def _get_local_games(self): self.logger.exception('Failed to get local games') return {} - def _update_local_games_state(self): + def _update_local_games(self): + if (time() - self._local_games_last_updated) < LOCAL_GAMES_TIMEOUT: + return + local_games = self._get_local_games() for game_id in self._local_games_cache.keys() - local_games.keys(): @@ -81,6 +91,7 @@ def _update_local_games_state(self): self.update_local_game_status(local_game) self._local_games_cache = local_games + self._local_games_last_updated = time() @staticmethod def _scheme_command(command, game_id): @@ -108,24 +119,23 @@ async def pass_login_credentials(self, step, credentials, cookies): return create_next_step(START_URI.SPLASH, END_URI.SPLASH_CONTINUE) async def get_owned_games(self): + if self._owned_games_cache is None: + self._owned_games_cache = self._get_owned_games() return list(self._owned_games_cache.values()) async def get_local_games(self): - return [game for game in self._local_games_cache.values()] - - def handshake_complete(self): - self._client.update_install_location() - self._init_db() - if self._client.is_installed: - self._update_local_games_state() - self._update_owned_games_cache() + if self._local_games_cache is None: + self._local_games_cache = self._get_local_games() + return list(self._local_games_cache.values()) def tick(self): self._client.update_install_location() - self._init_db() if self._client.is_installed: - self._update_local_games_state() - self._update_local_games_state() + if self._owned_games_db and self._local_games_cache is not None: + self._update_local_games() + + if self._owned_games_cache is not None: + self._update_owned_games() async def launch_game(self, game_id): AmazonGamesPlugin._scheme_command('play', game_id) @@ -146,10 +156,11 @@ async def shutdown_platform_client(self): async def get_os_compatibility(self, game_id, context): return OSCompatibility.Windows + def main(): - logging.info("Running Amazon plugin") create_and_run_plugin(AmazonGamesPlugin, sys.argv) + # run plugin event loop if __name__ == "__main__": main()