| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2022-01-11 22:01:54 +01:00
										 |  |  | import logging | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2022-06-12 23:33:14 +02:00
										 |  |  | import typing | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | import yaml | 
					
						
							|  |  |  | from jinja2 import Template | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  | import Options | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from Utils import __version__, local_path | 
					
						
							|  |  |  | from worlds.AutoWorld import AutoWorldRegister | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-11 22:01:54 +01:00
										 |  |  | handled_in_js = {"start_inventory", "local_items", "non_local_items", "start_hints", "start_location_hints", | 
					
						
							|  |  |  |                  "exclude_locations"} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | def create(): | 
					
						
							| 
									
										
										
										
											2022-08-23 01:07:17 +02:00
										 |  |  |     target_folder = local_path("WebHostLib", "static", "generated") | 
					
						
							| 
									
										
										
										
											2022-10-02 20:51:14 +02:00
										 |  |  |     yaml_folder = os.path.join(target_folder, "configs") | 
					
						
							|  |  |  |     os.makedirs(yaml_folder, exist_ok=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for file in os.listdir(yaml_folder): | 
					
						
							|  |  |  |         full_path: str = os.path.join(yaml_folder, file) | 
					
						
							|  |  |  |         if os.path.isfile(full_path): | 
					
						
							|  |  |  |             os.unlink(full_path) | 
					
						
							| 
									
										
										
										
											2021-12-28 18:43:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-12 23:33:14 +02:00
										 |  |  |     def dictify_range(option: typing.Union[Options.Range, Options.SpecialRange]): | 
					
						
							| 
									
										
										
										
											2022-11-01 18:07:56 -05:00
										 |  |  |         data = {option.default: 50} | 
					
						
							|  |  |  |         for sub_option in ["random", "random-low", "random-high"]: | 
					
						
							| 
									
										
										
										
											2022-10-09 17:16:59 -05:00
										 |  |  |             if sub_option != option.default: | 
					
						
							|  |  |  |                 data[sub_option] = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-01 18:07:56 -05:00
										 |  |  |         notes = {} | 
					
						
							| 
									
										
										
										
											2022-06-12 23:33:14 +02:00
										 |  |  |         for name, number in getattr(option, "special_range_names", {}).items(): | 
					
						
							| 
									
										
										
										
											2022-11-01 18:07:56 -05:00
										 |  |  |             notes[name] = f"equivalent to {number}" | 
					
						
							| 
									
										
										
										
											2022-06-12 23:33:14 +02:00
										 |  |  |             if number in data: | 
					
						
							|  |  |  |                 data[name] = data[number] | 
					
						
							|  |  |  |                 del data[number] | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 data[name] = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 22:14:18 +02:00
										 |  |  |         return data, notes | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |     def get_html_doc(option_type: type(Options.Option)) -> str: | 
					
						
							|  |  |  |         if not option_type.__doc__: | 
					
						
							|  |  |  |             return "Please document me!" | 
					
						
							|  |  |  |         return "\n".join(line.strip() for line in option_type.__doc__.split("\n")).strip() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 13:22:23 -05:00
										 |  |  |     weighted_settings = { | 
					
						
							|  |  |  |         "baseOptions": { | 
					
						
							|  |  |  |             "description": "Generated by https://archipelago.gg/", | 
					
						
							|  |  |  |             "name": "Player", | 
					
						
							|  |  |  |             "game": {}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         "games": {}, | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  |     for game_name, world in AutoWorldRegister.world_types.items(): | 
					
						
							| 
									
										
										
										
											2021-12-31 13:22:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-28 14:54:10 -07:00
										 |  |  |         all_options: typing.Dict[str, Options.AssembleOptions] = { | 
					
						
							|  |  |  |             **Options.per_game_common_options, | 
					
						
							|  |  |  |             **world.option_definitions | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-08-23 01:07:17 +02:00
										 |  |  |         with open(local_path("WebHostLib", "templates", "options.yaml")) as f: | 
					
						
							|  |  |  |             file_data = f.read() | 
					
						
							|  |  |  |         res = Template(file_data).render( | 
					
						
							| 
									
										
										
										
											2021-12-28 18:43:52 +01:00
										 |  |  |             options=all_options, | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  |             __version__=__version__, game=game_name, yaml_dump=yaml.dump, | 
					
						
							| 
									
										
										
										
											2022-10-09 17:16:59 -05:00
										 |  |  |             dictify_range=dictify_range, | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 01:07:17 +02:00
										 |  |  |         del file_data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 21:37:08 -04:00
										 |  |  |         with open(os.path.join(target_folder, 'configs', game_name + ".yaml"), "w") as f: | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |             f.write(res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Generate JSON files for player-settings pages | 
					
						
							|  |  |  |         player_settings = { | 
					
						
							| 
									
										
										
										
											2021-07-24 23:09:34 -04:00
										 |  |  |             "baseOptions": { | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                 "description": "Generated by https://archipelago.gg/", | 
					
						
							| 
									
										
										
										
											2021-09-16 17:15:25 -04:00
										 |  |  |                 "game": game_name, | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                 "name": "Player", | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         game_options = {} | 
					
						
							| 
									
										
										
										
											2021-12-28 18:43:52 +01:00
										 |  |  |         for option_name, option in all_options.items(): | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |             if option_name in handled_in_js: | 
					
						
							|  |  |  |                 pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             elif option.options: | 
					
						
							| 
									
										
										
										
											2021-11-20 17:37:08 +01:00
										 |  |  |                 game_options[option_name] = this_option = { | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                     "type": "select", | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |                     "displayName": option.display_name if hasattr(option, "display_name") else option_name, | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |                     "description": get_html_doc(option), | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                     "defaultValue": None, | 
					
						
							|  |  |  |                     "options": [] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-22 00:21:57 -04:00
										 |  |  |                 for sub_option_id, sub_option_name in option.name_lookup.items(): | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                     this_option["options"].append({ | 
					
						
							| 
									
										
										
										
											2021-08-31 17:28:46 -04:00
										 |  |  |                         "name": option.get_option_name(sub_option_id), | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                         "value": sub_option_name, | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if sub_option_id == option.default: | 
					
						
							|  |  |  |                         this_option["defaultValue"] = sub_option_name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-20 17:37:08 +01:00
										 |  |  |                 this_option["options"].append({ | 
					
						
							|  |  |  |                     "name": "Random", | 
					
						
							|  |  |  |                     "value": "random", | 
					
						
							|  |  |  |                 }) | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-12 05:48:52 +00:00
										 |  |  |                 if option.default == "random": | 
					
						
							|  |  |  |                     this_option["defaultValue"] = "random" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-06 11:25:37 +00:00
										 |  |  |             elif issubclass(option, Options.Range): | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  |                 game_options[option_name] = { | 
					
						
							|  |  |  |                     "type": "range", | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |                     "displayName": option.display_name if hasattr(option, "display_name") else option_name, | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |                     "description": get_html_doc(option), | 
					
						
							| 
									
										
										
										
											2022-06-12 05:48:52 +00:00
										 |  |  |                     "defaultValue": option.default if hasattr( | 
					
						
							|  |  |  |                         option, "default") and option.default != "random" else option.range_start, | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  |                     "min": option.range_start, | 
					
						
							|  |  |  |                     "max": option.range_end, | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-06 11:25:37 +00:00
										 |  |  |                 if issubclass(option, Options.SpecialRange): | 
					
						
							| 
									
										
										
										
											2022-06-12 23:33:14 +02:00
										 |  |  |                     game_options[option_name]["type"] = 'special_range' | 
					
						
							|  |  |  |                     game_options[option_name]["value_names"] = {} | 
					
						
							|  |  |  |                     for key, val in option.special_range_names.items(): | 
					
						
							|  |  |  |                         game_options[option_name]["value_names"][key] = val | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |             elif getattr(option, "verify_item_name", False): | 
					
						
							|  |  |  |                 game_options[option_name] = { | 
					
						
							|  |  |  |                     "type": "items-list", | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |                     "displayName": option.display_name if hasattr(option, "display_name") else option_name, | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |                     "description": get_html_doc(option), | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             elif getattr(option, "verify_location_name", False): | 
					
						
							|  |  |  |                 game_options[option_name] = { | 
					
						
							|  |  |  |                     "type": "locations-list", | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |                     "displayName": option.display_name if hasattr(option, "display_name") else option_name, | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |                     "description": get_html_doc(option), | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-06 11:25:37 +00:00
										 |  |  |             elif issubclass(option, Options.OptionList) or issubclass(option, Options.OptionSet): | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |                 if option.valid_keys: | 
					
						
							|  |  |  |                     game_options[option_name] = { | 
					
						
							|  |  |  |                         "type": "custom-list", | 
					
						
							| 
									
										
										
										
											2022-02-02 16:29:29 +01:00
										 |  |  |                         "displayName": option.display_name if hasattr(option, "display_name") else option_name, | 
					
						
							| 
									
										
										
										
											2022-08-21 00:58:46 +02:00
										 |  |  |                         "description": get_html_doc(option), | 
					
						
							| 
									
										
										
										
											2022-01-11 17:36:33 -05:00
										 |  |  |                         "options": list(option.valid_keys), | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-11 22:01:54 +01:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 logging.debug(f"{option} not exported to Web Settings.") | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |         player_settings["gameOptions"] = game_options | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 02:50:18 +02:00
										 |  |  |         os.makedirs(os.path.join(target_folder, 'player-settings'), exist_ok=True) | 
					
						
							| 
									
										
										
										
											2021-10-11 21:37:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with open(os.path.join(target_folder, 'player-settings', game_name + ".json"), "w") as f: | 
					
						
							| 
									
										
										
										
											2022-01-01 15:46:08 +01:00
										 |  |  |             json.dump(player_settings, f, indent=2, separators=(',', ': ')) | 
					
						
							| 
									
										
										
										
											2021-12-31 13:22:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-20 21:54:00 +01:00
										 |  |  |         if not world.hidden and world.web.settings_page is True: | 
					
						
							| 
									
										
										
										
											2022-01-01 03:12:32 +01:00
										 |  |  |             weighted_settings["baseOptions"]["game"][game_name] = 0 | 
					
						
							|  |  |  |             weighted_settings["games"][game_name] = {} | 
					
						
							| 
									
										
										
										
											2022-01-01 17:02:51 -05:00
										 |  |  |             weighted_settings["games"][game_name]["gameSettings"] = game_options | 
					
						
							| 
									
										
										
										
											2022-01-01 03:12:32 +01:00
										 |  |  |             weighted_settings["games"][game_name]["gameItems"] = tuple(world.item_names) | 
					
						
							|  |  |  |             weighted_settings["games"][game_name]["gameLocations"] = tuple(world.location_names) | 
					
						
							| 
									
										
										
										
											2021-12-31 13:22:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     with open(os.path.join(target_folder, 'weighted-settings.json'), "w") as f: | 
					
						
							| 
									
										
										
										
											2022-01-01 15:46:08 +01:00
										 |  |  |         json.dump(weighted_settings, f, indent=2, separators=(',', ': ')) |