Skip to content

Commit

Permalink
Add timeout for library updates, fix tick updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rall3n committed Sep 3, 2020
1 parent 83aceba commit 764ba1c
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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():
Expand All @@ -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:
Expand All @@ -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():
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit 764ba1c

Please sign in to comment.