newstyle DataPackage. Both versions in merged format for compatibility for now.

This commit is contained in:
Fabian Dill 2021-07-12 18:05:46 +02:00
parent 50a21fbd74
commit f456dba993
9 changed files with 60 additions and 38 deletions

View File

@ -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))

View File

@ -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

View File

@ -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):

View File

@ -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")
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)

View File

@ -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:

View File

@ -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] = {}

View File

@ -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:

View File

@ -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))

View File

@ -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"]