Skip to content

Commit

Permalink
Fix options pages sometimes displaying blank values in form fields (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LegendaryLinux authored Jun 1, 2024
1 parent 15e06e1 commit f3003ff
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion WebHostLib/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,42 @@ def test_ordered(obj):
def option_presets(game: str) -> Response:
world = AutoWorldRegister.world_types[game]

presets = {}
for preset_name, preset in world.web.options_presets.items():
presets[preset_name] = {}
for preset_option_name, preset_option in preset.items():
if preset_option == "random":
presets[preset_name][preset_option_name] = preset_option
continue

option = world.options_dataclass.type_hints[preset_option_name].from_any(preset_option)
if isinstance(option, Options.NamedRange) and isinstance(preset_option, str):
assert preset_option in option.special_range_names, \
f"Invalid preset value '{preset_option}' for '{preset_option_name}' in '{preset_name}'. " \
f"Expected {option.special_range_names.keys()} or {option.range_start}-{option.range_end}."

presets[preset_name][preset_option_name] = option.value
elif isinstance(option, Options.Range):
presets[preset_name][preset_option_name] = option.value
elif isinstance(preset_option, str):
# Ensure the option value is valid for Choice and Toggle options
assert option.name_lookup[option.value] == preset_option, \
f"Invalid option value '{preset_option}' for '{preset_option_name}' in preset '{preset_name}'. " \
f"Values must not be resolved to a different option via option.from_text (or an alias)."
# Use the name of the option
presets[preset_name][preset_option_name] = option.current_key
else:
# Use the name of the option
presets[preset_name][preset_option_name] = option.current_key

class SetEncoder(json.JSONEncoder):
def default(self, obj):
from collections.abc import Set
if isinstance(obj, Set):
return list(obj)
return json.JSONEncoder.default(self, obj)

json_data = json.dumps(world.web.options_presets, cls=SetEncoder)
json_data = json.dumps(presets, cls=SetEncoder)
response = Response(json_data)
response.headers["Content-Type"] = "application/json"
return response
Expand Down

0 comments on commit f3003ff

Please sign in to comment.