| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | import zipfile | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | from typing import * | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 00:40:51 +01:00
										 |  |  | from flask import request, flash, redirect, url_for, render_template, Markup | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from WebHostLib import app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | banned_zip_contents = (".sfc",) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def allowed_file(filename): | 
					
						
							|  |  |  |     return filename.endswith(('.txt', ".yaml", ".zip")) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  | from Generate import roll_settings, PlandoSettings | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  | from Utils import parse_yamls | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 15:17:47 +02:00
										 |  |  | @app.route('/check', methods=['GET', 'POST']) | 
					
						
							|  |  |  | def check(): | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  |     if request.method == 'POST': | 
					
						
							|  |  |  |         # check if the post request has the file part | 
					
						
							|  |  |  |         if 'file' not in request.files: | 
					
						
							|  |  |  |             flash('No file part') | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             file = request.files['file'] | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |             options = get_yaml_data(file) | 
					
						
							| 
									
										
										
										
											2022-12-06 00:40:51 +01:00
										 |  |  |             if isinstance(options, str): | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                 flash(options) | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  |                 results, _ = roll_options(options) | 
					
						
							| 
									
										
										
										
											2020-11-30 21:15:47 -05:00
										 |  |  |                 return render_template("checkResult.html", results=results) | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  |     return render_template("check.html") | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-05 15:17:47 +02:00
										 |  |  | @app.route('/mysterycheck') | 
					
						
							|  |  |  | def mysterycheck(): | 
					
						
							|  |  |  |     return redirect(url_for("check"), 301) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 00:40:51 +01:00
										 |  |  | def get_yaml_data(file) -> Union[Dict[str, str], str, Markup]: | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     options = {} | 
					
						
							|  |  |  |     # if user does not select file, browser also | 
					
						
							|  |  |  |     # submit an empty part without filename | 
					
						
							|  |  |  |     if file.filename == '': | 
					
						
							|  |  |  |         return 'No selected file' | 
					
						
							|  |  |  |     elif file and allowed_file(file.filename): | 
					
						
							|  |  |  |         if file.filename.endswith(".zip"): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             with zipfile.ZipFile(file, 'r') as zfile: | 
					
						
							|  |  |  |                 infolist = zfile.infolist() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 00:40:51 +01:00
										 |  |  |                 if any(file.filename.endswith(".archipelago") for file in infolist): | 
					
						
							|  |  |  |                     return Markup("Error: Your .zip file contains an .archipelago file. " | 
					
						
							|  |  |  |                                  'Did you mean to <a href="/uploads">host a game</a>?') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                 for file in infolist: | 
					
						
							|  |  |  |                     if file.filename.endswith(banned_zip_contents): | 
					
						
							|  |  |  |                         return "Uploaded data contained a rom file, which is likely to contain copyrighted material. Your file was deleted." | 
					
						
							| 
									
										
										
										
											2021-09-26 06:50:37 +02:00
										 |  |  |                     elif file.filename.endswith((".yaml", ".json", ".yml", ".txt")): | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                         options[file.filename] = zfile.open(file, "r").read() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             options = {file.filename: file.read()} | 
					
						
							|  |  |  |     if not options: | 
					
						
							|  |  |  |         return "Did not find a .yaml file to process." | 
					
						
							|  |  |  |     return options | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-22 17:20:24 +02:00
										 |  |  | def roll_options(options: Dict[str, Union[dict, str]], | 
					
						
							|  |  |  |                  plando_options: Set[str] = frozenset({"bosses", "items", "connections", "texts"})) -> \ | 
					
						
							|  |  |  |         Tuple[Dict[str, Union[str, bool]], Dict[str, dict]]: | 
					
						
							| 
									
										
										
										
											2022-07-03 14:11:52 +02:00
										 |  |  |     plando_options = PlandoSettings.from_set(set(plando_options)) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     results = {} | 
					
						
							|  |  |  |     rolled_results = {} | 
					
						
							|  |  |  |     for filename, text in options.items(): | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  |             if type(text) is dict: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 yaml_datas = (text, ) | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 yaml_datas = tuple(parse_yamls(text)) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |         except Exception as e: | 
					
						
							|  |  |  |             results[filename] = f"Failed to parse YAML data in {filename}: {e}" | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 if len(yaml_datas) == 1: | 
					
						
							|  |  |  |                     rolled_results[filename] = roll_settings(yaml_datas[0], | 
					
						
							| 
									
										
										
										
											2022-05-22 17:20:24 +02:00
										 |  |  |                                                              plando_options=plando_options) | 
					
						
							| 
									
										
										
										
											2022-04-12 10:57:29 +02:00
										 |  |  |                 else: | 
					
						
							|  |  |  |                     for i, yaml_data in enumerate(yaml_datas): | 
					
						
							|  |  |  |                         rolled_results[f"{filename}/{i + 1}"] = roll_settings(yaml_data, | 
					
						
							| 
									
										
										
										
											2022-05-22 17:20:24 +02:00
										 |  |  |                                                                               plando_options=plando_options) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |             except Exception as e: | 
					
						
							|  |  |  |                 results[filename] = f"Failed to generate mystery in {filename}: {e}" | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 results[filename] = True | 
					
						
							|  |  |  |     return results, rolled_results |