-
Couldn't load subscription status.
- Fork 16
Yaml option expansion #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 14 commits
2db9630
49da261
3f183a1
a418779
9f7d0ec
43e4b1f
e5f27a8
3b1145f
0380a7b
bacb3f9
563a782
a9372a6
5bb3551
4437028
54233ad
8449204
d7ccc93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| import os | ||
| import pkgutil | ||
| import json | ||
| from copy import deepcopy | ||
|
|
||
| from BaseClasses import MultiWorld, Item | ||
| from typing import Optional, List, TYPE_CHECKING, Union, get_args, get_origin | ||
|
|
@@ -55,27 +56,51 @@ def clamp(value, min, max): | |
| return value | ||
|
|
||
| def is_category_enabled(multiworld: MultiWorld, player: int, category_name: str) -> bool: | ||
| from .Data import category_table | ||
| """Check if a category has been disabled by a yaml option.""" | ||
| hook_result = before_is_category_enabled(multiworld, player, category_name) | ||
| if hook_result is not None: | ||
| return hook_result | ||
|
|
||
| category_data = category_table.get(category_name, {}) | ||
| return resolve_yaml_option(multiworld, player, category_data) | ||
| category_data = multiworld.worlds[player].category_table.get(category_name, {}) | ||
| resolve_option = resolve_yaml_option(multiworld, player, category_data) | ||
| return resolve_option or resolve_option is None | ||
|
|
||
| def resolve_yaml_option(multiworld: MultiWorld, player: int, data: dict) -> bool: | ||
| if "yaml_option" in data: | ||
| for option_name in data["yaml_option"]: | ||
| required = True | ||
| eval_1 = lambda x, t: x.value | ||
| target = 1 | ||
| if "<=" in option_name: | ||
| option_name, target = option_name.split("<=") | ||
| eval_1 = lambda x, t: x.value <= t | ||
| elif ">=" in option_name: | ||
| option_name, target = option_name.split(">=") | ||
| eval_1 = lambda x, t: x.value >= t | ||
| elif "<" in option_name: | ||
| option_name, target = option_name.split("<") | ||
| eval_1 = lambda x, t: x.value < t | ||
| elif ">" in option_name: | ||
| option_name, target = option_name.split(">") | ||
| eval_1 = lambda x, t: x.value > t | ||
| elif "=" in option_name: | ||
| option_name, target = option_name.split("=") | ||
axxroytovu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| eval_1 = lambda x, t: x.value == t | ||
| if option_name.startswith("!"): | ||
| option_name = option_name[1:] | ||
| required = False | ||
|
|
||
| eval_2 = lambda x, t: not eval_1(x, t) | ||
| else: | ||
| eval_2 = eval_1 | ||
|
|
||
| option_name = format_to_valid_identifier(option_name) | ||
| if is_option_enabled(multiworld, player, option_name) != required: | ||
| option = getattr(multiworld.worlds[player].options, option_name, None) | ||
| try: | ||
| target_eval = int(target) | ||
| except ValueError: | ||
| target_eval = option.options[target] | ||
| if not eval_2(option, target_eval): | ||
| return False | ||
| return True | ||
| return True | ||
| return None | ||
|
|
||
| def is_item_name_enabled(multiworld: MultiWorld, player: int, item_name: str) -> bool: | ||
| """Check if an item named 'item_name' has been disabled by a yaml option.""" | ||
|
|
@@ -85,13 +110,17 @@ def is_item_name_enabled(multiworld: MultiWorld, player: int, item_name: str) -> | |
|
|
||
| return is_item_enabled(multiworld, player, item) | ||
|
|
||
| def is_item_enabled(multiworld: MultiWorld, player: int, item: "ManualItem") -> bool: | ||
| def is_item_enabled(multiworld: MultiWorld, player: int, item: dict) -> bool: | ||
| """Check if an item has been disabled by a yaml option.""" | ||
| hook_result = before_is_item_enabled(multiworld, player, item) | ||
| if hook_result is not None: | ||
| return hook_result | ||
|
|
||
| return _is_manualobject_enabled(multiworld, player, item) | ||
|
|
||
| try_resolve = resolve_yaml_option(multiworld, player, item) | ||
| if try_resolve is None: | ||
| return _is_manualobject_enabled(multiworld, player, item) | ||
| else: | ||
| return try_resolve | ||
|
Comment on lines
+124
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if I understand this part correctly if the item says its enabled via your new yaml_option then categories are not checked at all is that what you mean to do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually there's a reason I originaly just put return _is_manualobject_enabled since the logic is the same for both loc and item maybe you could move your try_resolve call there so you dont have 2 copy of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Hmm. I could probably put an AND in there instead |
||
|
|
||
| def is_location_name_enabled(multiworld: MultiWorld, player: int, location_name: str) -> bool: | ||
| """Check if a location named 'location_name' has been disabled by a yaml option.""" | ||
|
|
@@ -101,25 +130,28 @@ def is_location_name_enabled(multiworld: MultiWorld, player: int, location_name: | |
|
|
||
| return is_location_enabled(multiworld, player, location) | ||
|
|
||
| def is_location_enabled(multiworld: MultiWorld, player: int, location: "ManualLocation") -> bool: | ||
| def is_location_enabled(multiworld: MultiWorld, player: int, location: dict) -> bool: | ||
axxroytovu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Check if a location has been disabled by a yaml option.""" | ||
| hook_result = before_is_location_enabled(multiworld, player, location) | ||
| if hook_result is not None: | ||
| return hook_result | ||
|
|
||
| return _is_manualobject_enabled(multiworld, player, location) | ||
|
|
||
| try_resolve = resolve_yaml_option(multiworld, player, location) | ||
| if try_resolve is None: | ||
| return _is_manualobject_enabled(multiworld, player, location) | ||
| else: | ||
| return try_resolve | ||
|
|
||
| def _is_manualobject_enabled(multiworld: MultiWorld, player: int, object: any) -> bool: | ||
| """Internal method: Check if a Manual Object has any category disabled by a yaml option. | ||
| \nPlease use the proper is_'item/location'_enabled or is_'item/location'_name_enabled methods instead. | ||
| """ | ||
| enabled = True | ||
| for category in object.get("category", []): | ||
| if not is_category_enabled(multiworld, player, category): | ||
| enabled = False | ||
| break | ||
|
|
||
| return enabled | ||
| resolve = is_category_enabled(multiworld, player, category) | ||
| if resolve == False: | ||
| return False | ||
| return True | ||
|
|
||
| def get_items_for_player(multiworld: MultiWorld, player: int, includePrecollected: bool = False) -> List[Item]: | ||
| """Return list of items of a player including placed items""" | ||
|
|
||
axxroytovu marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was wondering why you referenced it this way and was reminded once again that the
is_*_enabledhooks don't pass in world like all the other hooks, lol(Just an observation. This way is fine, and the hooks can be standardized later.)