From 5d33b4b16412f9802427156800e5e37b4f1130c2 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Fri, 24 Apr 2020 05:29:02 +0200 Subject: [PATCH] introduce persistent data storage, which for now just caches rom -> server pairs --- MultiClient.py | 13 +++++++++++-- MultiServer.py | 1 + Utils.py | 24 +++++++++++++++++++++++- requirements.txt | 3 ++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/MultiClient.py b/MultiClient.py index 1d857117..58dc7dc2 100644 --- a/MultiClient.py +++ b/MultiClient.py @@ -589,13 +589,22 @@ async def server_loop(ctx : Context, address = None): logging.error('Already connected') return - if address is None: + if address is None: # set through CLI or BMBP address = ctx.server_address - + if address is None: # see if this is an old connection + try: + address = Utils.persistent_load()["servers"][ctx.auth] + except Exception as e: + logging.debug(f"Could not find cached server address. {e}") + else: + logging.info(f"Using server {address} from the last time this rom was loaded. \n" + f"Should this be in error, use /connect ") while not address: logging.info('Enter multiworld server address') address = await console_input(ctx) + Utils.persistent_store("servers", ctx.auth, address) + address = f"ws://{address}" if "://" not in address else address port = urllib.parse.urlparse(address).port or 38281 diff --git a/MultiServer.py b/MultiServer.py index a9a78bda..1e25f9ff 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -519,6 +519,7 @@ class ClientMessageProcessor(CommandProcessor): notify_all(self.ctx, get_players_string(self.ctx)) else: self.output(get_players_string(self.ctx)) + return True def _cmd_forfeit(self) -> bool: """Surrender and send your remaining items out to their recipients""" diff --git a/Utils.py b/Utils.py index 66ff6a9b..6f3eed1e 100644 --- a/Utils.py +++ b/Utils.py @@ -9,7 +9,7 @@ import sys import typing import functools -from yaml import load +from yaml import load, dump try: from yaml import CLoader as Loader @@ -212,6 +212,28 @@ def get_location_name_from_address(address): return Regions.lookup_id_to_name.get(address, f'Unknown location (ID:{address})') +def persistent_store(category, key, value): + path = local_path("_persistent_storage.yaml") + storage: dict = persistent_load() + category = storage.setdefault(category, {}) + category[key] = value + with open(path, "wt") as f: + f.write(dump(storage)) + + +def persistent_load(): + path = local_path("_persistent_storage.yaml") + storage: dict = {} + if os.path.exists(path): + try: + with open(path, "r") as f: + storage = parse_yaml(f.read()) + except Exception as e: + import logging + logging.debug(f"Could not read store: {e}") + return storage + + class ReceivedItem(typing.NamedTuple): item: int location: int diff --git a/requirements.txt b/requirements.txt index 94956255..1451d58c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ PyYAML>=5.3.1 fuzzywuzzy>=0.18.0 bsdiff4>=1.1.9 upnpy>=1.1.5 -prompt_toolkit>=3.0.5 \ No newline at end of file +prompt_toolkit>=3.0.5 +appdirs>=1.4.3 \ No newline at end of file