| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | import pickle | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | import random | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | import tempfile | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  | import zipfile | 
					
						
							| 
									
										
										
										
											2022-11-06 21:37:11 +01:00
										 |  |  | import concurrent.futures | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  | from collections import Counter | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  | from typing import Dict, Optional, Any, Union, List | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:27:40 +02:00
										 |  |  | from flask import request, flash, redirect, url_for, session, render_template | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from pony.orm import commit, db_session | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-06 11:32:49 +02:00
										 |  |  | from BaseClasses import seeddigits, get_seed | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  | from Generate import handle_name, PlandoOptions | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from Main import main as ERmain | 
					
						
							|  |  |  | from Utils import __version__ | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | from WebHostLib import app | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from worlds.alttp.EntranceRandomizer import parse_arguments | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  | from .check import get_yaml_data, roll_options | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from .models import Generation, STATE_ERROR, STATE_QUEUED, Seed, UUID | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  | from .upload import upload_zip_to_db | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  | def get_meta(options_source: dict, race: bool = False) -> Dict[str, Union[List[str], Dict[str, Any]]]: | 
					
						
							| 
									
										
										
										
											2022-05-04 20:03:19 -07:00
										 |  |  |     plando_options = { | 
					
						
							|  |  |  |         options_source.get("plando_bosses", ""), | 
					
						
							|  |  |  |         options_source.get("plando_items", ""), | 
					
						
							|  |  |  |         options_source.get("plando_connections", ""), | 
					
						
							|  |  |  |         options_source.get("plando_texts", "") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     plando_options -= {""} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |     server_options = { | 
					
						
							| 
									
										
										
										
											2021-11-17 16:58:43 +01:00
										 |  |  |         "hint_cost": int(options_source.get("hint_cost", 10)), | 
					
						
							| 
									
										
										
										
											2023-01-02 12:29:21 -06:00
										 |  |  |         "release_mode": options_source.get("release_mode", "goal"), | 
					
						
							| 
									
										
										
										
											2022-04-20 07:46:05 -07:00
										 |  |  |         "remaining_mode": options_source.get("remaining_mode", "disabled"), | 
					
						
							| 
									
										
										
										
											2021-11-17 16:58:43 +01:00
										 |  |  |         "collect_mode": options_source.get("collect_mode", "disabled"), | 
					
						
							| 
									
										
										
										
											2022-05-04 20:03:19 -07:00
										 |  |  |         "item_cheat": bool(int(options_source.get("item_cheat", 1))), | 
					
						
							|  |  |  |         "server_password": options_source.get("server_password", None), | 
					
						
							| 
									
										
										
										
											2021-11-17 16:58:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  |     generator_options = { | 
					
						
							|  |  |  |         "spoiler": int(options_source.get("spoiler", 0)), | 
					
						
							|  |  |  |         "race": race | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if race: | 
					
						
							|  |  |  |         server_options["item_cheat"] = False | 
					
						
							|  |  |  |         server_options["remaining_mode"] = "disabled" | 
					
						
							|  |  |  |         generator_options["spoiler"] = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         "server_options": server_options, | 
					
						
							|  |  |  |         "plando_options": list(plando_options), | 
					
						
							|  |  |  |         "generator_options": generator_options, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-17 16:58:43 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | @app.route('/generate', methods=['GET', 'POST']) | 
					
						
							|  |  |  | @app.route('/generate/<race>', methods=['GET', 'POST']) | 
					
						
							|  |  |  | def generate(race=False): | 
					
						
							|  |  |  |     if request.method == 'POST': | 
					
						
							|  |  |  |         # check if the post request has the file part | 
					
						
							|  |  |  |         if 'file' not in request.files: | 
					
						
							|  |  |  |             flash('No file part') | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2023-09-11 13:57:14 -07:00
										 |  |  |             files = request.files.getlist('file') | 
					
						
							|  |  |  |             options = get_yaml_data(files) | 
					
						
							| 
									
										
										
										
											2022-12-06 00:40:51 +01:00
										 |  |  |             if isinstance(options, str): | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                 flash(options) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  |                 meta = get_meta(request.form, race) | 
					
						
							|  |  |  |                 results, gen_options = roll_options(options, set(meta["plando_options"])) | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:19:36 +02:00
										 |  |  |                 if any(type(result) == str for result in results.values()): | 
					
						
							| 
									
										
										
										
											2020-11-30 21:15:47 -05:00
										 |  |  |                     return render_template("checkResult.html", results=results) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                 elif len(gen_options) > app.config["MAX_ROLL"]: | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |                     flash(f"Sorry, generating of multiworlds is limited to {app.config['MAX_ROLL']} players. " | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |                           f"If you have a larger group, please generate it yourself and upload it.") | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  |                 elif len(gen_options) >= app.config["JOB_THRESHOLD"]: | 
					
						
							|  |  |  |                     gen = Generation( | 
					
						
							|  |  |  |                         options=pickle.dumps({name: vars(options) for name, options in gen_options.items()}), | 
					
						
							|  |  |  |                         # convert to json compatible | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  |                         meta=json.dumps(meta), | 
					
						
							|  |  |  |                         state=STATE_QUEUED, | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  |                         owner=session["_id"]) | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  |                     commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     return redirect(url_for("wait_seed", seed=gen.id)) | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2020-12-08 21:30:07 -08:00
										 |  |  |                     try: | 
					
						
							|  |  |  |                         seed_id = gen_game({name: vars(options) for name, options in gen_options.items()}, | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  |                                            meta=meta, owner=session["_id"].int) | 
					
						
							| 
									
										
										
										
											2020-12-08 21:30:07 -08:00
										 |  |  |                     except BaseException as e: | 
					
						
							|  |  |  |                         from .autolauncher import handle_generation_failure | 
					
						
							|  |  |  |                         handle_generation_failure(e) | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  |                         return render_template("seedError.html", seed_error=(e.__class__.__name__ + ": " + str(e))) | 
					
						
							| 
									
										
										
										
											2020-12-08 21:30:07 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 20:48:58 +01:00
										 |  |  |                     return redirect(url_for("view_seed", seed=seed_id)) | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-30 20:06:03 -05:00
										 |  |  |     return render_template("generate.html", race=race, version=__version__) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 02:25:53 +01:00
										 |  |  | def gen_game(gen_options: dict, meta: Optional[Dict[str, Any]] = None, owner=None, sid=None): | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  |     if not meta: | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |         meta: Dict[str, Any] = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     meta.setdefault("server_options", {}).setdefault("hint_cost", 10) | 
					
						
							| 
									
										
										
										
											2023-06-26 07:30:58 +02:00
										 |  |  |     race = meta.setdefault("generator_options", {}).setdefault("race", False) | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-06 21:37:11 +01:00
										 |  |  |     def task(): | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  |         target = tempfile.TemporaryDirectory() | 
					
						
							|  |  |  |         playercount = len(gen_options) | 
					
						
							|  |  |  |         seed = get_seed() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if race: | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |             random.seed()  # use time-based random source | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             random.seed(seed) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  |         seedname = "W" + (f"{random.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits)) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         erargs = parse_arguments(['--multi', str(playercount)]) | 
					
						
							|  |  |  |         erargs.seed = seed | 
					
						
							| 
									
										
										
										
											2022-07-06 17:24:16 +02:00
										 |  |  |         erargs.name = {x: "" for x in range(1, playercount + 1)}  # only so it can be overwritten in mystery | 
					
						
							| 
									
										
										
										
											2023-06-26 07:30:58 +02:00
										 |  |  |         erargs.spoiler = meta["generator_options"].get("spoiler", 0) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  |         erargs.race = race | 
					
						
							|  |  |  |         erargs.outputname = seedname | 
					
						
							|  |  |  |         erargs.outputpath = target.name | 
					
						
							|  |  |  |         erargs.teams = 1 | 
					
						
							| 
									
										
										
										
											2023-01-17 17:25:59 +01:00
										 |  |  |         erargs.plando_options = PlandoOptions.from_set(meta.setdefault("plando_options", | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  |                                                                        {"bosses", "items", "connections", "texts"})) | 
					
						
							| 
									
										
										
										
											2023-07-20 15:40:31 -05:00
										 |  |  |         erargs.skip_prog_balancing = False | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  |         name_counter = Counter() | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  |         for player, (playerfile, settings) in enumerate(gen_options.items(), 1): | 
					
						
							|  |  |  |             for k, v in settings.items(): | 
					
						
							|  |  |  |                 if v is not None: | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  |                     if hasattr(erargs, k): | 
					
						
							|  |  |  |                         getattr(erargs, k)[player] = v | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         setattr(erargs, k, {player: v}) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if not erargs.name[player]: | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  |                 erargs.name[player] = os.path.splitext(os.path.split(playerfile)[-1])[0] | 
					
						
							|  |  |  |             erargs.name[player] = handle_name(erargs.name[player], player, name_counter) | 
					
						
							| 
									
										
										
										
											2022-02-14 04:58:21 +01:00
										 |  |  |         if len(set(erargs.name.values())) != len(erargs.name): | 
					
						
							|  |  |  |             raise Exception(f"Names have to be unique. Names: {Counter(erargs.name.values())}") | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |         ERmain(erargs, seed, baked_server_options=meta["server_options"]) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  |         return upload_to_db(target.name, sid, owner, race) | 
					
						
							| 
									
										
										
										
											2022-11-06 21:37:11 +01:00
										 |  |  |     thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=1) | 
					
						
							|  |  |  |     thread = thread_pool.submit(task) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         return thread.result(app.config["JOB_TIME"]) | 
					
						
							|  |  |  |     except concurrent.futures.TimeoutError as e: | 
					
						
							|  |  |  |         if sid: | 
					
						
							|  |  |  |             with db_session: | 
					
						
							|  |  |  |                 gen = Generation.get(id=sid) | 
					
						
							|  |  |  |                 if gen is not None: | 
					
						
							|  |  |  |                     gen.state = STATE_ERROR | 
					
						
							|  |  |  |                     meta = json.loads(gen.meta) | 
					
						
							|  |  |  |                     meta["error"] = ( | 
					
						
							|  |  |  |                             "Allowed time for Generation exceeded, please consider generating locally instead. " + | 
					
						
							|  |  |  |                             e.__class__.__name__ + ": " + str(e)) | 
					
						
							|  |  |  |                     gen.meta = json.dumps(meta) | 
					
						
							|  |  |  |                     commit() | 
					
						
							| 
									
										
										
										
											2020-11-12 19:50:13 +01:00
										 |  |  |     except BaseException as e: | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  |         if sid: | 
					
						
							|  |  |  |             with db_session: | 
					
						
							|  |  |  |                 gen = Generation.get(id=sid) | 
					
						
							|  |  |  |                 if gen is not None: | 
					
						
							|  |  |  |                     gen.state = STATE_ERROR | 
					
						
							| 
									
										
										
										
											2021-05-13 21:57:11 +02:00
										 |  |  |                     meta = json.loads(gen.meta) | 
					
						
							| 
									
										
										
										
											2021-10-11 00:46:18 +02:00
										 |  |  |                     meta["error"] = (e.__class__.__name__ + ": " + str(e)) | 
					
						
							| 
									
										
										
										
											2021-05-13 21:57:11 +02:00
										 |  |  |                     gen.meta = json.dumps(meta) | 
					
						
							|  |  |  |                     commit() | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  |         raise | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @app.route('/wait/<suuid:seed>') | 
					
						
							|  |  |  | def wait_seed(seed: UUID): | 
					
						
							|  |  |  |     seed_id = seed | 
					
						
							|  |  |  |     seed = Seed.get(id=seed_id) | 
					
						
							|  |  |  |     if seed: | 
					
						
							| 
									
										
										
										
											2021-11-25 20:48:58 +01:00
										 |  |  |         return redirect(url_for("view_seed", seed=seed_id)) | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  |     generation = Generation.get(id=seed_id) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not generation: | 
					
						
							|  |  |  |         return "Generation not found." | 
					
						
							|  |  |  |     elif generation.state == STATE_ERROR: | 
					
						
							| 
									
										
										
										
											2021-05-13 21:57:11 +02:00
										 |  |  |         return render_template("seedError.html", seed_error=generation.meta) | 
					
						
							| 
									
										
										
										
											2020-10-24 14:46:27 -04:00
										 |  |  |     return render_template("waitSeed.html", seed_id=seed_id) | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  | def upload_to_db(folder, sid, owner, race): | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     for file in os.listdir(folder): | 
					
						
							|  |  |  |         file = os.path.join(folder, file) | 
					
						
							| 
									
										
										
										
											2021-09-18 01:02:26 +02:00
										 |  |  |         if file.endswith(".zip"): | 
					
						
							|  |  |  |             with db_session: | 
					
						
							|  |  |  |                 with zipfile.ZipFile(file) as zfile: | 
					
						
							|  |  |  |                     res = upload_zip_to_db(zfile, owner, {"race": race}, sid) | 
					
						
							|  |  |  |                 if type(res) == "str": | 
					
						
							|  |  |  |                     raise Exception(res) | 
					
						
							|  |  |  |                 elif res: | 
					
						
							|  |  |  |                     seed = res | 
					
						
							|  |  |  |                     gen = Generation.get(id=seed.id) | 
					
						
							|  |  |  |                     if gen is not None: | 
					
						
							|  |  |  |                         gen.delete() | 
					
						
							|  |  |  |                     return seed.id | 
					
						
							|  |  |  |     raise Exception("Generation zipfile not found.") |