| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | import os | 
					
						
							|  |  |  | import tempfile | 
					
						
							|  |  |  | import random | 
					
						
							|  |  |  | import zlib | 
					
						
							|  |  |  | import json | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 19:27:40 +02:00
										 |  |  | from flask import request, flash, redirect, url_for, session, render_template | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from EntranceRandomizer import parse_arguments | 
					
						
							|  |  |  | from Main import main as ERmain | 
					
						
							|  |  |  | from Main import get_seed, seeddigits | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  | import pickle | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .models import * | 
					
						
							|  |  |  | from WebHostLib import app | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  | from .check import get_yaml_data, roll_options | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							|  |  |  |             file = request.files['file'] | 
					
						
							|  |  |  |             options = get_yaml_data(file) | 
					
						
							|  |  |  |             if type(options) == str: | 
					
						
							|  |  |  |                 flash(options) | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											2020-10-29 01:43:23 +01:00
										 |  |  |                 results, gen_options = roll_options(options) | 
					
						
							| 
									
										
										
										
											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"]: | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  |                     flash(f"Sorry, generating of multiworlds is limited to {app.config['MAX_ROLL']} players for now. " | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  |                         meta=pickle.dumps({"race": race}), state=STATE_QUEUED, | 
					
						
							|  |  |  |                         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()}, | 
					
						
							|  |  |  |                                            race=race, 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))) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 21:15:47 -05:00
										 |  |  |                     return redirect(url_for("viewSeed", seed=seed_id)) | 
					
						
							| 
									
										
										
										
											2020-09-09 01:41:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     return render_template("generate.html", race=race) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-18 01:18:37 +02:00
										 |  |  | def gen_game(gen_options, race=False, owner=None, sid=None): | 
					
						
							| 
									
										
										
										
											2020-08-18 02:06:35 +02:00
										 |  |  |     try: | 
					
						
							|  |  |  |         target = tempfile.TemporaryDirectory() | 
					
						
							|  |  |  |         playercount = len(gen_options) | 
					
						
							|  |  |  |         seed = get_seed() | 
					
						
							|  |  |  |         random.seed(seed) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if race: | 
					
						
							|  |  |  |             random.seed()  # reset to time-based random source | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         seedname = "M" + (f"{random.randint(0, pow(10, seeddigits) - 1)}".zfill(seeddigits)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         erargs = parse_arguments(['--multi', str(playercount)]) | 
					
						
							|  |  |  |         erargs.seed = seed | 
					
						
							|  |  |  |         erargs.name = {x: "" for x in range(1, playercount + 1)}  # only so it can be overwrittin in mystery | 
					
						
							|  |  |  |         erargs.create_spoiler = not race | 
					
						
							|  |  |  |         erargs.race = race | 
					
						
							|  |  |  |         erargs.skip_playthrough = race | 
					
						
							|  |  |  |         erargs.outputname = seedname | 
					
						
							|  |  |  |         erargs.outputpath = target.name | 
					
						
							|  |  |  |         erargs.teams = 1 | 
					
						
							|  |  |  |         erargs.progression_balancing = {} | 
					
						
							|  |  |  |         erargs.create_diff = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for player, (playerfile, settings) in enumerate(gen_options.items(), 1): | 
					
						
							|  |  |  |             for k, v in settings.items(): | 
					
						
							|  |  |  |                 if v is not None: | 
					
						
							|  |  |  |                     getattr(erargs, k)[player] = v | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if not erargs.name[player]: | 
					
						
							|  |  |  |                 erargs.name[player] = os.path.split(playerfile)[-1].split(".")[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         erargs.names = ",".join(erargs.name[i] for i in range(1, playercount + 1)) | 
					
						
							|  |  |  |         del (erargs.name) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         erargs.skip_progression_balancing = {player: not balanced for player, balanced in | 
					
						
							|  |  |  |                                              erargs.progression_balancing.items()} | 
					
						
							|  |  |  |         del (erargs.progression_balancing) | 
					
						
							|  |  |  |         ERmain(erargs, seed) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 00:09:47 +01:00
										 |  |  |         return upload_to_db(target.name, owner, sid, race) | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-11-12 19:50:13 +01:00
										 |  |  |                     gen.meta = (e.__class__.__name__ + ": "+ str(e)).encode() | 
					
						
							| 
									
										
										
										
											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: | 
					
						
							| 
									
										
										
										
											2020-11-30 21:15:47 -05:00
										 |  |  |         return redirect(url_for("viewSeed", 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: | 
					
						
							| 
									
										
										
										
											2020-12-08 21:30:07 -08:00
										 |  |  |         return render_template("seedError.html", seed_error=generation.meta.decode()) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-30 00:09:47 +01:00
										 |  |  | def upload_to_db(folder, owner, sid, race:bool): | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     patches = set() | 
					
						
							|  |  |  |     spoiler = "" | 
					
						
							| 
									
										
										
										
											2020-10-30 00:09:47 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |     multidata = None | 
					
						
							|  |  |  |     for file in os.listdir(folder): | 
					
						
							|  |  |  |         file = os.path.join(folder, file) | 
					
						
							|  |  |  |         if file.endswith(".bmbp"): | 
					
						
							| 
									
										
										
										
											2020-08-10 09:17:21 +02:00
										 |  |  |             player = int(file.split("P")[1].split(".")[0].split("_")[0]) | 
					
						
							| 
									
										
										
										
											2020-08-02 22:11:52 +02:00
										 |  |  |             patches.add(Patch(data=open(file, "rb").read(), player=player)) | 
					
						
							|  |  |  |         elif file.endswith(".txt"): | 
					
						
							|  |  |  |             spoiler = open(file, "rt").read() | 
					
						
							|  |  |  |         elif file.endswith("multidata"): | 
					
						
							| 
									
										
										
										
											2020-10-30 00:09:47 +01:00
										 |  |  |             multidata = file | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if not race or len(patches) > 1: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             multidata = json.loads(zlib.decompress(open(multidata, "rb").read())) | 
					
						
							|  |  |  |         except Exception as e: | 
					
						
							|  |  |  |             flash(e) | 
					
						
							|  |  |  |             raise e | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         multidata = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     with db_session: | 
					
						
							|  |  |  |         if sid: | 
					
						
							|  |  |  |             seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner, id=sid) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner) | 
					
						
							|  |  |  |         for patch in patches: | 
					
						
							|  |  |  |             patch.seed = seed | 
					
						
							|  |  |  |         if sid: | 
					
						
							|  |  |  |             gen = Generation.get(id=sid) | 
					
						
							|  |  |  |             if gen is not None: | 
					
						
							|  |  |  |                 gen.delete() | 
					
						
							|  |  |  |     return seed.id |