Skip to content

Commit

Permalink
WebHost: allow deleting Rooms and Seeds, as well as their associated …
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 authored Apr 2, 2024
1 parent 5e57920 commit 3c564d7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
13 changes: 12 additions & 1 deletion WebHostLib/autolauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import threading
import time
import typing
from uuid import UUID
from datetime import timedelta, datetime

from pony.orm import db_session, select, commit
Expand Down Expand Up @@ -62,6 +63,16 @@ def autohost(config: dict):
def keep_running():
try:
with Locker("autohost"):
# delete unowned user-content
with db_session:
# >>> bool(uuid.UUID(int=0))
# True
rooms = Room.select(lambda room: room.owner == UUID(int=0)).delete(bulk=True)
seeds = Seed.select(lambda seed: seed.owner == UUID(int=0) and not seed.rooms).delete(bulk=True)
slots = Slot.select(lambda slot: not slot.seed).delete(bulk=True)
# Command gets deleted by ponyorm Cascade Delete, as Room is Required
if rooms or seeds or slots:
logging.info(f"{rooms} Rooms, {seeds} Seeds and {slots} Slots have been deleted.")
run_guardian()
while 1:
time.sleep(0.1)
Expand Down Expand Up @@ -191,6 +202,6 @@ def guard():
guardian = threading.Thread(name="Guardian", target=guard)


from .models import Room, Generation, STATE_QUEUED, STATE_STARTED, STATE_ERROR, db, Seed
from .models import Room, Generation, STATE_QUEUED, STATE_STARTED, STATE_ERROR, db, Seed, Slot
from .customserver import run_server_process, get_static_server_data
from .generate import gen_game
4 changes: 4 additions & 0 deletions WebHostLib/templates/userContent.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ <h2>Your Rooms</h2>
<th class="center">Players</th>
<th>Created (UTC)</th>
<th>Last Activity (UTC)</th>
<th>Mark for deletion</th>
</tr>
</thead>
<tbody>
Expand All @@ -35,6 +36,7 @@ <h2>Your Rooms</h2>
<td>{{ room.seed.slots|length }}</td>
<td>{{ room.creation_time.strftime("%Y-%m-%d %H:%M") }}</td>
<td>{{ room.last_activity.strftime("%Y-%m-%d %H:%M") }}</td>
<td><a href="{{ url_for("disown_room", room=room.id) }}">Delete next maintenance.</td>
</tr>
{% endfor %}
</tbody>
Expand All @@ -51,6 +53,7 @@ <h2>Your Seeds</h2>
<th>Seed</th>
<th class="center">Players</th>
<th>Created (UTC)</th>
<th>Mark for deletion</th>
</tr>
</thead>
<tbody>
Expand All @@ -60,6 +63,7 @@ <h2>Your Seeds</h2>
<td>{% if seed.multidata %}{{ seed.slots|length }}{% else %}1{% endif %}
</td>
<td>{{ seed.creation_time.strftime("%Y-%m-%d %H:%M") }}</td>
<td><a href="{{ url_for("disown_seed", seed=seed.id) }}">Delete next maintenance.</td>
</tr>
{% endfor %}
</tbody>
Expand Down
28 changes: 27 additions & 1 deletion WebHostLib/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import zlib

from io import BytesIO
from flask import request, flash, redirect, url_for, session, render_template
from flask import request, flash, redirect, url_for, session, render_template, abort
from markupsafe import Markup
from pony.orm import commit, flush, select, rollback
from pony.orm.core import TransactionIntegrityError
Expand Down Expand Up @@ -219,3 +219,29 @@ def user_content():
rooms = select(room for room in Room if room.owner == session["_id"])
seeds = select(seed for seed in Seed if seed.owner == session["_id"])
return render_template("userContent.html", rooms=rooms, seeds=seeds)


@app.route("/disown_seed/<suuid:seed>", methods=["GET"])
def disown_seed(seed):
seed = Seed.get(id=seed)
if not seed:
return abort(404)
if seed.owner != session["_id"]:
return abort(403)

seed.owner = 0

return redirect(url_for("user_content"))


@app.route("/disown_room/<suuid:room>", methods=["GET"])
def disown_room(room):
room = Room.get(id=room)
if not room:
return abort(404)
if room.owner != session["_id"]:
return abort(403)

room.owner = 0

return redirect(url_for("user_content"))

0 comments on commit 3c564d7

Please sign in to comment.