| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  | import concurrent.futures | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-03-28 16:52:32 -07:00
										 |  |  | from collections import Counter | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  | from typing import Any, Dict, List, Optional, Union, Set | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  | from flask import flash, redirect, render_template, request, session, url_for | 
					
						
							| 
									
										
										
										
											2022-10-17 01:08:31 +02:00
										 |  |  | from pony.orm import commit, db_session | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  | from BaseClasses import get_seed, seeddigits | 
					
						
							|  |  |  | from Generate import PlandoOptions, handle_name | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  | from settings import ServerOptions, GeneratorOptions | 
					
						
							| 
									
										
										
										
											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]]]: | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  |     plando_options: Set[str] = set() | 
					
						
							|  |  |  |     for substr in ("bosses", "items", "connections", "texts"): | 
					
						
							|  |  |  |         if options_source.get(f"plando_{substr}", substr in GeneratorOptions.plando_options): | 
					
						
							|  |  |  |             plando_options.add(substr) | 
					
						
							| 
									
										
										
										
											2022-05-04 20:03:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 01:38:50 +02:00
										 |  |  |     server_options = { | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  |         "hint_cost": int(options_source.get("hint_cost", ServerOptions.hint_cost)), | 
					
						
							| 
									
										
										
										
											2024-12-12 15:36:56 -05:00
										 |  |  |         "release_mode": str(options_source.get("release_mode", ServerOptions.release_mode)), | 
					
						
							|  |  |  |         "remaining_mode": str(options_source.get("remaining_mode", ServerOptions.remaining_mode)), | 
					
						
							|  |  |  |         "collect_mode": str(options_source.get("collect_mode", ServerOptions.collect_mode)), | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  |         "item_cheat": bool(int(options_source.get("item_cheat", not ServerOptions.disable_item_cheat))), | 
					
						
							| 
									
										
										
										
											2024-12-12 15:36:56 -05:00
										 |  |  |         "server_password": str(options_source.get("server_password", None)), | 
					
						
							| 
									
										
										
										
											2021-11-17 16:58:43 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  |     generator_options = { | 
					
						
							| 
									
										
										
										
											2024-05-29 16:53:18 +02:00
										 |  |  |         "spoiler": int(options_source.get("spoiler", GeneratorOptions.spoiler)), | 
					
						
							|  |  |  |         "race": race, | 
					
						
							| 
									
										
										
										
											2023-04-17 13:19:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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) | 
					
						
							| 
									
										
										
										
											2024-05-19 20:21:46 +02:00
										 |  |  |                 return start_generation(options, meta) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-19 20:21:46 +02:00
										 |  |  | def start_generation(options: Dict[str, Union[dict, str]], meta: Dict[str, Any]): | 
					
						
							|  |  |  |     results, gen_options = roll_options(options, set(meta["plando_options"])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if any(type(result) == str for result in results.values()): | 
					
						
							|  |  |  |         return render_template("checkResult.html", results=results) | 
					
						
							|  |  |  |     elif len(gen_options) > app.config["MAX_ROLL"]: | 
					
						
							|  |  |  |         flash(f"Sorry, generating of multiworlds is limited to {app.config['MAX_ROLL']} players. " | 
					
						
							|  |  |  |               f"If you have a larger group, please generate it yourself and upload it.") | 
					
						
							| 
									
										
										
										
											2024-10-05 18:14:22 +02:00
										 |  |  |         return redirect(url_for(request.endpoint, **(request.view_args or {}))) | 
					
						
							| 
									
										
										
										
											2024-05-19 20:21:46 +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 | 
					
						
							|  |  |  |             meta=json.dumps(meta), | 
					
						
							|  |  |  |             state=STATE_QUEUED, | 
					
						
							|  |  |  |             owner=session["_id"]) | 
					
						
							|  |  |  |         commit() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect(url_for("wait_seed", seed=gen.id)) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             seed_id = gen_game({name: vars(options) for name, options in gen_options.items()}, | 
					
						
							|  |  |  |                                meta=meta, owner=session["_id"].int) | 
					
						
							|  |  |  |         except BaseException as e: | 
					
						
							|  |  |  |             from .autolauncher import handle_generation_failure | 
					
						
							|  |  |  |             handle_generation_failure(e) | 
					
						
							|  |  |  |             return render_template("seedError.html", seed_error=(e.__class__.__name__ + ": " + str(e))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect(url_for("view_seed", seed=seed_id)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2023-11-23 16:03:56 -06:00
										 |  |  |         erargs.skip_output = False | 
					
						
							| 
									
										
										
										
											2025-04-05 06:50:52 -07:00
										 |  |  |         erargs.spoiler_only = False | 
					
						
							| 
									
										
										
										
											2024-09-18 04:37:10 +02:00
										 |  |  |         erargs.csv_output = 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.") |