From f456dba99373a5c2308da4d210ea5422ef9d71d0 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 12 Jul 2021 18:05:46 +0200 Subject: [PATCH] newstyle DataPackage. Both versions in merged format for compatibility for now. --- MultiServer.py | 12 +++---- Utils.py | 2 +- worlds/AutoWorld.py | 12 +++++++ worlds/__init__.py | 58 ++++++++++++++++----------------- worlds/alttp/__init__.py | 3 ++ worlds/factorio/Technologies.py | 2 +- worlds/factorio/__init__.py | 3 ++ worlds/hk/__init__.py | 3 ++ worlds/minecraft/__init__.py | 3 ++ 9 files changed, 60 insertions(+), 38 deletions(-) diff --git a/MultiServer.py b/MultiServer.py index fc472703..38d8b6ea 100644 --- a/MultiServer.py +++ b/MultiServer.py @@ -27,8 +27,7 @@ from fuzzywuzzy import process as fuzzy_process from worlds.AutoWorld import AutoWorldRegister proxy_worlds = {name: world(None, 0) for name, world in AutoWorldRegister.world_types.items()} -from worlds import network_data_package, lookup_any_item_id_to_name, lookup_any_item_name_to_id, \ - lookup_any_location_id_to_name, lookup_any_location_name_to_id +from worlds import network_data_package, lookup_any_item_id_to_name, lookup_any_location_id_to_name import Utils from Utils import get_item_name_from_id, get_location_name_from_id, \ version_tuple, restricted_loads, Version @@ -518,7 +517,7 @@ def notify_team(ctx: Context, team: int, text: str): def collect_hints(ctx: Context, team: int, slot: int, item: str) -> typing.List[NetUtils.Hint]: hints = [] - seeked_item_id = lookup_any_item_name_to_id[item] + seeked_item_id = proxy_worlds[ctx.games[slot]].item_name_to_id[item] for finding_player, check_data in ctx.locations.items(): for location_id, result in check_data.items(): item_id, receiving_player = result @@ -531,7 +530,7 @@ def collect_hints(ctx: Context, team: int, slot: int, item: str) -> typing.List[ def collect_hints_location(ctx: Context, team: int, slot: int, location: str) -> typing.List[NetUtils.Hint]: - seeked_location: int = lookup_any_location_name_to_id[location] + seeked_location: int = proxy_worlds[ctx.games[slot]].location_name_to_id[location] item_id, receiving_player = ctx.locations[slot].get(seeked_location, (None, None)) if item_id: found = seeked_location in ctx.location_checks[team, slot] @@ -1223,9 +1222,10 @@ class ServerCommandProcessor(CommonCommandProcessor): if usable: team, slot = self.ctx.player_name_lookup[seeked_player] item = " ".join(item_name) - item, usable, response = get_intended_text(item, proxy_worlds[self.ctx.games[slot]].item_names) + world = proxy_worlds[self.ctx.games[slot]] + item, usable, response = get_intended_text(item, world.item_names) if usable: - new_item = NetworkItem(lookup_any_item_name_to_id[item], -1, 0) + new_item = NetworkItem(world.item_name_to_id[item], -1, 0) get_received_items(self.ctx, team, slot).append(new_item) self.ctx.notify_all('Cheat console: sending "' + item + '" to ' + self.ctx.get_aliased_name(team, slot)) diff --git a/Utils.py b/Utils.py index 9a21c896..662a6b6e 100644 --- a/Utils.py +++ b/Utils.py @@ -13,7 +13,7 @@ class Version(typing.NamedTuple): build: int -__version__ = "0.1.4" +__version__ = "0.1.5" version_tuple = tuplize_version(__version__) import builtins diff --git a/worlds/AutoWorld.py b/worlds/AutoWorld.py index 063a8626..7a5cba34 100644 --- a/worlds/AutoWorld.py +++ b/worlds/AutoWorld.py @@ -9,6 +9,8 @@ class AutoWorldRegister(type): def __new__(cls, name, bases, dct): dct["all_names"] = dct["item_names"] | dct["location_names"] | set(dct.get("item_name_groups", {})) + dct["item_id_to_name"] = {code: name for name, code in dct["item_name_to_id"].items()} + dct["location_id_to_name"] = {code: name for name, code in dct["location_name_to_id"].items()} new_class = super().__new__(cls, name, bases, dct) if "game" in dct: AutoWorldRegister.world_types[dct["game"]] = new_class @@ -39,6 +41,16 @@ class World(metaclass=AutoWorldRegister): location_names: Set[str] = frozenset() # set of all potential location names all_names: Set[str] = frozenset() # gets automatically populated with all item, item group and location names + # map names to their IDs + item_name_to_id: Dict[str, int] = {} + location_name_to_id: Dict[str, int] = {} + + # reverse, automatically generated + item_id_to_name: Dict[int, str] = {} + location_id_to_name: Dict[int, str] = {} + + data_version = 1 # increment this every time something in your world's names/id mappings changes. + hint_blacklist: Set[str] = frozenset() # any names that should not be hintable def __init__(self, world: MultiWorld, player: int): diff --git a/worlds/__init__.py b/worlds/__init__.py index dd4c81bf..c72a1bef 100644 --- a/worlds/__init__.py +++ b/worlds/__init__.py @@ -5,36 +5,34 @@ __all__ = {"lookup_any_item_id_to_name", "lookup_any_location_id_to_name", "network_data_package"} -# all of the below should be moved to AutoWorld functionality -from .alttp.Items import lookup_id_to_name as alttp -from .hk.Items import lookup_id_to_name as hk -from .factorio import Technologies -from .minecraft.Items import lookup_id_to_name as mc - -lookup_any_item_id_to_name = {**alttp, **hk, **Technologies.lookup_id_to_name, **mc} -assert len(alttp) + len(hk) + len(Technologies.lookup_id_to_name) + len(mc) == len(lookup_any_item_id_to_name) -lookup_any_item_name_to_id = {name: id for id, name in lookup_any_item_id_to_name.items()} -# assert len(lookup_any_item_name_to_id) == len(lookup_any_item_id_to_name) # currently broken: Single Arrow - -from .alttp import Regions -from .hk import Locations -from .minecraft import Locations as Advancements - -lookup_any_location_id_to_name = {**Regions.lookup_id_to_name, **Locations.lookup_id_to_name, - **Technologies.lookup_id_to_name, **Advancements.lookup_id_to_name} -assert len(Regions.lookup_id_to_name) + len(Locations.lookup_id_to_name) + \ - len(Technologies.lookup_id_to_name) + len(Advancements.lookup_id_to_name) == \ - len(lookup_any_location_id_to_name) -lookup_any_location_name_to_id = {name: id for id, name in lookup_any_location_id_to_name.items()} -assert len(lookup_any_location_name_to_id) == len(lookup_any_location_id_to_name) - -network_data_package = {"lookup_any_location_id_to_name": lookup_any_location_id_to_name, - "lookup_any_item_id_to_name": lookup_any_item_id_to_name, - "version": 9} - -# end of TODO block - # import all submodules to trigger AutoWorldRegister for file in os.scandir(os.path.dirname(__file__)): if file.is_dir(): - importlib.import_module(f".{file.name}", "worlds") \ No newline at end of file + importlib.import_module(f".{file.name}", "worlds") + +from .AutoWorld import AutoWorldRegister +lookup_any_item_id_to_name = {} +lookup_any_location_id_to_name = {} +games = {} + +for world_name, world in AutoWorldRegister.world_types.items(): + games[world_name] = { + "item_name_to_id" : world.item_name_to_id, + "location_name_to_id": world.location_name_to_id, + "version": world.data_version, + # seems clients don't actually want this. Keeping it here in case someone changes their mind. + # "item_name_groups": {name: tuple(items) for name, items in world.item_name_groups.items()} + } + lookup_any_item_id_to_name.update(world.item_id_to_name) + lookup_any_location_id_to_name.update(world.location_id_to_name) + +network_data_package = { + "lookup_any_location_id_to_name": lookup_any_location_id_to_name, # legacy, to be removed + "lookup_any_item_id_to_name": lookup_any_item_id_to_name, # legacy, to be removed + "version": 10, # legacy, to be removed + "games": games, +} + +import json +with open("datapackagegroups.json", "w") as f: + json.dump(network_data_package, f, indent=4) \ No newline at end of file diff --git a/worlds/alttp/__init__.py b/worlds/alttp/__init__.py index 59dc60f0..c65ed00b 100644 --- a/worlds/alttp/__init__.py +++ b/worlds/alttp/__init__.py @@ -15,6 +15,9 @@ class ALTTPWorld(World): location_names = frozenset(lookup_name_to_id) hint_blacklist = {"Triforce"} + item_name_to_id = {name: data.item_code for name, data in item_table.items() if type(data.item_code) == int} + location_name_to_id = lookup_name_to_id + def collect(self, state: CollectionState, item: Item) -> bool: if item.name.startswith('Progressive '): if 'Sword' in item.name: diff --git a/worlds/factorio/Technologies.py b/worlds/factorio/Technologies.py index f5356536..0ab5b92e 100644 --- a/worlds/factorio/Technologies.py +++ b/worlds/factorio/Technologies.py @@ -345,7 +345,7 @@ progressive_rows["progressive-wall"] = ("stone-wall", "gate") progressive_rows["progressive-follower"] = ("defender", "distractor", "destroyer") progressive_rows["progressive-inserter"] = ("fast-inserter", "stack-inserter") -base_tech_table = tech_table.copy() # without progressive techs +base_tech_table = tech_table.copy() # without progressive techs base_technology_table = technology_table.copy() progressive_tech_table: Dict[str, int] = {} diff --git a/worlds/factorio/__init__.py b/worlds/factorio/__init__.py index 6d64a67c..622eaf7a 100644 --- a/worlds/factorio/__init__.py +++ b/worlds/factorio/__init__.py @@ -22,6 +22,9 @@ class Factorio(World): item_names = frozenset(tech_table) location_names = frozenset(base_tech_table) + item_name_to_id = tech_table + location_name_to_id = base_tech_table + def generate_basic(self): for tech_name in base_tech_table: if self.world.progressive: diff --git a/worlds/hk/__init__.py b/worlds/hk/__init__.py index 9f4f8404..af8b8aa4 100644 --- a/worlds/hk/__init__.py +++ b/worlds/hk/__init__.py @@ -18,6 +18,9 @@ class HKWorld(World): item_names: Set[str] = frozenset(item_table) location_names: Set[str] = frozenset(lookup_name_to_id) + item_name_to_id = {name: data.id for name, data in item_table.items()} + location_name_to_id = lookup_name_to_id + def generate_basic(self): # Link regions self.world.get_entrance('Hollow Nest S&Q', self.player).connect(self.world.get_region('Hollow Nest', self.player)) diff --git a/worlds/minecraft/__init__.py b/worlds/minecraft/__init__.py index 49ddb4e9..6e16bb9f 100644 --- a/worlds/minecraft/__init__.py +++ b/worlds/minecraft/__init__.py @@ -19,6 +19,9 @@ class MinecraftWorld(World): item_names = frozenset(item_table) location_names = frozenset(advancement_table) + item_name_to_id = {name: data.code for name, data in item_table.items()} + location_name_to_id = {name: data.id for name, data in advancement_table.items()} + def _get_mc_data(self): exits = ["Overworld Structure 1", "Overworld Structure 2", "Nether Structure 1", "Nether Structure 2", "The End Structure"]