| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | from Utils import __version__ | 
					
						
							|  |  |  | from jinja2 import Template | 
					
						
							|  |  |  | import yaml | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from worlds.AutoWorld import AutoWorldRegister | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  | import Options | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | target_folder = os.path.join("WebHostLib", "static", "generated") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  | def create(): | 
					
						
							| 
									
										
										
										
											2021-12-28 18:43:52 +01:00
										 |  |  |     os.makedirs(os.path.join(target_folder, 'configs'), exist_ok=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 22:14:18 +02:00
										 |  |  |     def dictify_range(option): | 
					
						
							|  |  |  |         data = {option.range_start: 0, option.range_end: 0, "random": 0, "random-low": 0, "random-high": 0, | 
					
						
							|  |  |  |                 option.default: 50} | 
					
						
							|  |  |  |         notes = { | 
					
						
							|  |  |  |             option.range_start: "minimum value", | 
					
						
							|  |  |  |             option.range_end: "maximum value" | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return data, notes | 
					
						
							| 
									
										
										
										
											2021-09-17 00:17:54 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def default_converter(default_value): | 
					
						
							|  |  |  |         if isinstance(default_value, (set, frozenset)): | 
					
						
							|  |  |  |             return list(default_value) | 
					
						
							|  |  |  |         return default_value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 18:43:52 +01:00
										 |  |  |         all_options = {**world.options, **Options.per_game_common_options} | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  |         res = Template(open(os.path.join("WebHostLib", "templates", "options.yaml")).read()).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, | 
					
						
							|  |  |  |             dictify_range=dictify_range, default_converter=default_converter, | 
					
						
							| 
									
										
										
										
											2021-07-22 18:21:31 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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(): | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |             if 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", | 
					
						
							| 
									
										
										
										
											2021-08-22 20:01:58 -04:00
										 |  |  |                     "displayName": option.displayname if hasattr(option, "displayname") else option_name, | 
					
						
							| 
									
										
										
										
											2021-07-24 21:27:56 -04:00
										 |  |  |                     "description": option.__doc__ if option.__doc__ else "Please document me!", | 
					
						
							|  |  |  |                     "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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  |             elif hasattr(option, "range_start") and hasattr(option, "range_end"): | 
					
						
							|  |  |  |                 game_options[option_name] = { | 
					
						
							|  |  |  |                     "type": "range", | 
					
						
							| 
									
										
										
										
											2021-08-22 20:01:58 -04:00
										 |  |  |                     "displayName": option.displayname if hasattr(option, "displayname") else option_name, | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  |                     "description": option.__doc__ if option.__doc__ else "Please document me!", | 
					
						
							| 
									
										
										
										
											2021-07-25 19:04:08 -04:00
										 |  |  |                     "defaultValue": option.default if hasattr(option, "default") else option.range_start, | 
					
						
							| 
									
										
										
										
											2021-07-25 18:18:15 -04:00
										 |  |  |                     "min": option.range_start, | 
					
						
							|  |  |  |                     "max": option.range_end, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-01-01 03:12:32 +01:00
										 |  |  |         if not world.hidden: | 
					
						
							|  |  |  |             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=(',', ': ')) |