Update Webhost for generation on-site

uploads and tracker do not work yet
This commit is contained in:
Fabian Dill 2020-10-26 00:04:58 +01:00
parent 4de64eab84
commit 4dc84e1dac
6 changed files with 31 additions and 26 deletions

View File

@ -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)

View File

@ -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):

View File

@ -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/<suuid:seed_id>/<int:player_id>")
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)

View File

@ -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.")

View File

@ -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):

View File

@ -11,7 +11,7 @@
<ul>
{% for patch in patches|list|sort(attribute="player") %}
<li><a href="{{ url_for("download_patch", patch_id=patch.id, room_id=room.id) }}">
Patch for player {{ patch.player }} - {{ room.seed.multidata["names"][0][patch.player-1] }}</a></li>
Patch for player {{ patch.player_id }} - {{ patch.player_name }}</a></li>
{% endfor %}
</ul>
{% endif %}