| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | from flask import request, flash, redirect, url_for, session, render_template | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 18:08:15 +02:00
										 |  |  | from Generate import roll_settings | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | from Utils import parse_yaml | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-27 04:05:42 +02:00
										 |  |  | @app.route('/mysterycheck', methods=['GET', 'POST']) | 
					
						
							|  |  |  | def mysterycheck(): | 
					
						
							|  |  |  |     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) | 
					
						
							|  |  |  |             if type(options) == str: | 
					
						
							|  |  |  |                 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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def get_yaml_data(file) -> Union[Dict[str, str], str]: | 
					
						
							|  |  |  |     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() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 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." | 
					
						
							|  |  |  |                     elif file.filename.endswith(".yaml"): | 
					
						
							|  |  |  |                         options[file.filename] = zfile.open(file, "r").read() | 
					
						
							|  |  |  |                     elif file.filename.endswith(".txt"): | 
					
						
							|  |  |  |                         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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  | def roll_options(options: Dict[str, Union[dict, str]]) -> Tuple[Dict[str, Union[str, bool]], Dict[str, dict]]: | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							|  |  |  |                 yaml_data = text | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 yaml_data = parse_yaml(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: | 
					
						
							| 
									
										
										
										
											2021-01-02 12:49:43 +01:00
										 |  |  |                 rolled_results[filename] = roll_settings(yaml_data, plando_options={"bosses"}) | 
					
						
							| 
									
										
										
										
											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 |