From 4dc84e1dacf98371c96d7bccab5d44e79f8e01e4 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Mon, 26 Oct 2020 00:04:58 +0100 Subject: [PATCH] Update Webhost for generation on-site uploads and tracker do not work yet --- WebHostLib/__init__.py | 2 ++ WebHostLib/customserver.py | 6 ++++-- WebHostLib/downloads.py | 11 ++++------- WebHostLib/generate.py | 26 ++++++++++++++------------ WebHostLib/models.py | 10 ++++++---- WebHostLib/templates/macros.html | 2 +- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index 3f16f967..ad138bcc 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -48,6 +48,8 @@ app.config["PONY"] = { app.config["MAX_ROLL"] = 20 app.config["CACHE_TYPE"] = "simple" app.autoversion = True +app.config["HOSTNAME"] = "berserkermulti.world" + av = Autoversion(app) cache = Cache(app) Compress(app) diff --git a/WebHostLib/customserver.py b/WebHostLib/customserver.py index d755894d..858e8c28 100644 --- a/WebHostLib/customserver.py +++ b/WebHostLib/customserver.py @@ -9,12 +9,13 @@ import socket import threading import time import random +import zlib from .models import * from MultiServer import Context, server, auto_shutdown, ServerCommandProcessor, ClientMessageProcessor -from Utils import get_public_ipv4, get_public_ipv6 +from Utils import get_public_ipv4, get_public_ipv6, restricted_loads class CustomClientMessageProcessor(ClientMessageProcessor): @@ -73,7 +74,8 @@ class WebHostContext(Context): self.port = room.last_port else: self.port = get_random_port() - return self._load(room.seed.multidata, True) + + return self._load(restricted_loads(zlib.decompress(room.seed.multidata)), True) @db_session def init_save(self, enabled: bool = True): diff --git a/WebHostLib/downloads.py b/WebHostLib/downloads.py index b7a8c1c3..dbbb6160 100644 --- a/WebHostLib/downloads.py +++ b/WebHostLib/downloads.py @@ -15,12 +15,11 @@ def download_patch(room_id, patch_id): room = Room.get(id=room_id) last_port = room.last_port - pname = room.seed.multidata["names"][0][patch.player - 1] - patch_data = update_patch_data(patch.data, server="berserkermulti.world:" + str(last_port)) + patch_data = update_patch_data(patch.data, server=f"{app.config['HOSTNAME']}:{last_port}") patch_data = io.BytesIO(patch_data) - fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](room_id)}.apbp" + fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](room_id)}.apbp" return send_file(patch_data, as_attachment=True, attachment_filename=fname) @@ -31,17 +30,15 @@ def download_spoiler(seed_id): @app.route("/dl_raw_patch//") def download_raw_patch(seed_id, player_id): - patch = select(patch for patch in Patch if patch.player == player_id and patch.seed.id == seed_id).first() + patch = select(patch for patch in Patch if patch.player_id == player_id and patch.seed.id == seed_id).first() if not patch: return "Patch not found" else: import io - pname = patch.seed.multidata["names"][0][patch.player - 1] - patch_data = update_patch_data(patch.data, server="") patch_data = io.BytesIO(patch_data) - fname = f"P{patch.player}_{pname}_{app.jinja_env.filters['suuid'](seed_id)}.apbp" + fname = f"P{patch.player_id}_{patch.player_name}_{app.jinja_env.filters['suuid'](seed_id)}.apbp" return send_file(patch_data, as_attachment=True, attachment_filename=fname) diff --git a/WebHostLib/generate.py b/WebHostLib/generate.py index 19178822..ec89659f 100644 --- a/WebHostLib/generate.py +++ b/WebHostLib/generate.py @@ -1,8 +1,6 @@ import os import tempfile import random -import zlib -import json from flask import request, flash, redirect, url_for, session, render_template @@ -124,21 +122,23 @@ def upload_to_db(folder, owner, sid): for file in os.listdir(folder): file = os.path.join(folder, file) if file.endswith(".apbp"): - player = int(file.split("P")[1].split(".")[0].split("_")[0]) - patches.add(Patch(data=open(file, "rb").read(), player=player)) + player_text = file.split("_P", 1)[1] + player_name = player_text.split("_", 1)[1].split(".", 1)[0] + player_id = int(player_text.split(".", 1)[0].split("_", 1)[0]) + patches.add(Patch(data=open(file, "rb").read(), + player_id=player_id, player_name = player_name)) elif file.endswith(".txt"): - spoiler = open(file, "rt").read() - elif file.endswith("multidata"): - try: - multidata = json.loads(zlib.decompress(open(file, "rb").read())) - except Exception as e: - flash(e) + spoiler = open(file, "rt", encoding="utf-8-sig").read() + elif file.endswith(".multidata"): + multidata = open(file, "rb").read() if multidata: with db_session: if sid: - seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner, id=sid) + seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner, + id=sid, meta={"tags": ["generated"]}) else: - seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner) + seed = Seed(multidata=multidata, spoiler=spoiler, patches=patches, owner=owner, + meta={"tags": ["generated"]}) for patch in patches: patch.seed = seed if sid: @@ -146,3 +146,5 @@ def upload_to_db(folder, owner, sid): if gen is not None: gen.delete() return seed.id + else: + raise Exception("Multidata required, but not found.") diff --git a/WebHostLib/models.py b/WebHostLib/models.py index d5e0f2b8..069dfca2 100644 --- a/WebHostLib/models.py +++ b/WebHostLib/models.py @@ -11,8 +11,9 @@ STATE_ERROR = -1 class Patch(db.Entity): id = PrimaryKey(int, auto=True) - player = Required(int) - data = Required(buffer, lazy=True) + player_id = Required(int) + player_name = Required(str, 16) + data = Required(bytes, lazy=True) seed = Optional('Seed') @@ -23,7 +24,7 @@ class Room(db.Entity): owner = Required(UUID, index=True) commands = Set('Command') seed = Required('Seed', index=True) - multisave = Optional(Json, lazy=True) + multisave = Optional(buffer, lazy=True) show_spoiler = Required(int, default=0) # 0 -> never, 1 -> after completion, -> 2 always timeout = Required(int, default=lambda: 6 * 60 * 60) # seconds since last activity to shutdown tracker = Optional(UUID, index=True) @@ -33,11 +34,12 @@ class Room(db.Entity): class Seed(db.Entity): id = PrimaryKey(UUID, default=uuid4) rooms = Set(Room) - multidata = Optional(Json, lazy=True) + multidata = Required(bytes, lazy=True) owner = Required(UUID, index=True) creation_time = Required(datetime, default=lambda: datetime.utcnow()) patches = Set(Patch) spoiler = Optional(LongStr, lazy=True) + meta = Required(Json, lazy=True, default=lambda: {}) # additional meta information/tags class Command(db.Entity): diff --git a/WebHostLib/templates/macros.html b/WebHostLib/templates/macros.html index aaf9fa46..1bf4af28 100644 --- a/WebHostLib/templates/macros.html +++ b/WebHostLib/templates/macros.html @@ -11,7 +11,7 @@ {% endif %}