import logging
import os
import pathlib
import sys
import flask
from flask_cors import CORS
log = logging.getLogger()
pipe_stderr = logging.StreamHandler(sys.stderr)
pipe_stderr.setLevel(logging.DEBUG)
log.addHandler(pipe_stderr)
if getattr(sys, "frozen", False):
path_appdir = pathlib.Path(sys._MEIPASS)
else:
path_appdir = pathlib.Path.cwd()
log.critical(path_appdir)
## \internal
# \brief Signals whether we are a desktop application (as opposed to a Docker container).
desktop_mode = False
from . import gamedata
## \internal
# \brief The Flask instance. See Flask documentation.
app = flask.Flask("seagull-game", root_path=path_appdir, template_folder="templates", static_folder="static")
CORS(app)
orig_url_for = app.url_for
xml_namespaces = {
"items": "seagull:rules/items",
"upgrades": "seagull:rules/upgrades"
}
#REDIS_HOST="stub-implementation.example.net"
#REDIS_PORT=6379
#REDIS_USER="seagull"
#REDIS_PASS="i am not a real password"
#state_cache = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, username=REDIS_USER, password=REDIS_PASS)
# ham5 standing by...
## \internal
# \brief Returns CDN URLs for files not present within the container itself, or copies VFS data out so Flask etc can use it.
def url_for_override(endpoint, *posargs, _anchor=None, _method=None, _scheme=None, _external=None, self=app, **values):
if endpoint == "static":
if not gamedata.vfs.exists(f"static/{values["filename"]}"):
log.warning("requested {0} from local file, but it doesn't exist in this container. Redirecting to CDN...\n".format(values["filename"]))
return "https://cdn.otl-hga.net/seagull/" + values["filename"]
else:
gamedata.vfs.copy_out(f"static/{values["filename"]}", dest=path_appdir.as_posix())
return orig_url_for(endpoint, *posargs, _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external, **values)
else:
print(endpoint)
return orig_url_for(endpoint, *posargs, _anchor=_anchor, _method=_method, _scheme=_scheme, _external=_external, **values)
app.url_for = url_for_override
## \internal
# \brief Base Flask rendering context. Generated with render_base_context().
base_context = {}
base_context_live = False
## \brief Renders a dialog template and sends it to the client.
# \param dialog The dialog to render.
# \api{GET} /dialog/`