WebHost: move new API hooks to WebWorld

This commit is contained in:
Fabian Dill
2023-08-28 13:49:14 +02:00
parent a1dcaf52e3
commit 96d4143030
5 changed files with 50 additions and 51 deletions

View File

@@ -45,7 +45,7 @@ def get_app():
for world in worlds.AutoWorldRegister.world_types.values():
try:
world.run_webhost_app_setup(app)
world.web.run_webhost_app_setup(app)
except Exception as e:
logging.exception(e)
@@ -130,14 +130,13 @@ if __name__ == "__main__":
for world in worlds.AutoWorldRegister.world_types.values():
try:
world.run_webhost_setup()
world.web.run_webhost_setup()
except Exception as e:
logging.exception(e)
del world, worlds
app = get_app()
del world, worlds
create_options_files()
create_ordered_tutorials_file()

View File

@@ -159,6 +159,16 @@ class WebWorld:
bug_report_page: Optional[str]
"""display a link to a bug report page, most likely a link to a GitHub issue page."""
# allows modification of webhost during startup, this is run once
@classmethod
def run_webhost_setup(cls):
pass
# allows modification of webhost during startup,
# this is run whenever a Flask app is created (per-thread/per-process)
@classmethod
def run_webhost_app_setup(cls, app: "Flask"):
pass
class World(metaclass=AutoWorldRegister):
"""A World object encompasses a game's Items, Locations, Rules and additional data or functionality required.
@@ -413,17 +423,6 @@ class World(metaclass=AutoWorldRegister):
res["checksum"] = data_package_checksum(res)
return res
# allows modification of webhost during startup, this is run once
@classmethod
def run_webhost_setup(cls):
pass
# allows modification of webhost during startup,
# this is run whenever a Flask app is created (per-thread/per-process)
@classmethod
def run_webhost_app_setup(cls, app: "Flask"):
pass
# any methods attached to this can be used as part of CollectionState,
# please use a prefix as all of them get clobbered together
class LogicMixin(metaclass=AutoLogicRegister):

View File

@@ -124,6 +124,14 @@ class ALTTPWeb(WebWorld):
tutorials = [setup_en, setup_de, setup_es, setup_fr, msu, msu_es, msu_fr, plando, oof_sound]
@classmethod
def run_webhost_setup(cls):
rom_file = get_base_rom_path()
if os.path.exists(rom_file):
from .Sprites import update_sprites
update_sprites()
else:
logging.warning("Could not update LttP sprites.")
class ALTTPWorld(World):
"""
@@ -808,15 +816,6 @@ class ALTTPWorld(World):
)
return slot_data
@classmethod
def run_webhost_setup(cls):
rom_file = get_base_rom_path()
if os.path.exists(rom_file):
from .Sprites import update_sprites
update_sprites()
else:
logging.warning("Could not update LttP sprites.")
def get_same_seed(world, seed_def: tuple) -> str:
seeds: typing.Dict[tuple, str] = getattr(world, "__named_seeds", {})
if seed_def in seeds:

View File

@@ -61,6 +61,35 @@ class FactorioWeb(WebWorld):
["Berserker, Farrak Kilhn"]
)]
@classmethod
def run_webhost_app_setup(cls, app):
from uuid import UUID
import pkgutil
from werkzeug.exceptions import abort
from flask import render_template_string
from WebHostLib import cache
from WebHostLib.tracker import (_get_multiworld_tracker_data, _get_inventory_data,
get_enabled_multiworld_trackers, multi_trackers)
multitracker_template = pkgutil.get_data(__name__, "data/web/templates/MultiTracker.html").decode()
@app.route('/tracker/<suuid:tracker>/Factorio')
@cache.memoize(timeout=60) # multisave is currently created up to every minute
def get_Factorio_multiworld_tracker(tracker: UUID):
data = _get_multiworld_tracker_data(tracker)
if not data:
abort(404)
data["inventory"] = _get_inventory_data(data)
data["enabled_multiworld_trackers"] = get_enabled_multiworld_trackers(data["room"], "Factorio")
data["item_name_to_id"] = Factorio.item_name_to_id
return render_template_string(multitracker_template, **data)
multi_trackers[Factorio.game] = get_Factorio_multiworld_tracker
class FactorioItem(Item):
game = "Factorio"
@@ -524,33 +553,6 @@ class Factorio(World):
all_items[name], self.player)
return item
@classmethod
def run_webhost_app_setup(cls, app):
from uuid import UUID
from werkzeug.exceptions import abort
from flask import render_template
from WebHostLib import cache
from WebHostLib.tracker import (_get_multiworld_tracker_data, _get_inventory_data,
get_enabled_multiworld_trackers, multi_trackers)
@app.route('/tracker/<suuid:tracker>/Factorio')
@cache.memoize(timeout=60) # multisave is currently created up to every minute
def get_Factorio_multiworld_tracker(tracker: UUID):
data = _get_multiworld_tracker_data(tracker)
if not data:
abort(404)
data["inventory"] = _get_inventory_data(data)
data["enabled_multiworld_trackers"] = get_enabled_multiworld_trackers(data["room"], "Factorio")
data["item_name_to_id"] = cls.location_name_to_id
return render_template("multiFactorioTracker.html", **data)
multi_trackers[cls.game] = get_Factorio_multiworld_tracker
class FactorioLocation(Location):
game: str = Factorio.game