1
0
mirror of synced 2024-12-24 03:34:50 +01:00
bemaniutils/bemani/frontend/mga/endpoints.py

145 lines
4.1 KiB
Python
Raw Normal View History

2021-09-04 17:17:22 +02:00
# vim: set fileencoding=utf-8
import re
from typing import Any, Dict
from flask import Blueprint, request, Response, url_for, abort
from bemani.common import GameConstants
from bemani.data import UserID
from bemani.frontend.app import loginrequired, jsonify, render_react
from bemani.frontend.mga.mga import MetalGearArcadeFrontend
from bemani.frontend.templates import templates_location
from bemani.frontend.static import static_location
from bemani.frontend.types import g
mga_pages = Blueprint(
"mga_pages",
2021-09-04 17:17:22 +02:00
__name__,
url_prefix=f"/{GameConstants.MGA.value}",
2021-09-04 17:17:22 +02:00
template_folder=templates_location,
static_folder=static_location,
)
@mga_pages.route("/players")
2021-09-04 17:17:22 +02:00
@loginrequired
def viewplayers() -> Response:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
return render_react(
"All MGA Players",
"mga/allplayers.react.js",
{"players": frontend.get_all_players()},
2021-09-04 17:17:22 +02:00
{
"refresh": url_for("mga_pages.listplayers"),
"player": url_for("mga_pages.viewplayer", userid=-1),
2021-09-04 17:17:22 +02:00
},
)
@mga_pages.route("/players/list")
2021-09-04 17:17:22 +02:00
@jsonify
@loginrequired
def listplayers() -> Dict[str, Any]:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
return {
"players": frontend.get_all_players(),
2021-09-04 17:17:22 +02:00
}
@mga_pages.route("/players/<int:userid>")
2021-09-04 17:17:22 +02:00
@loginrequired
def viewplayer(userid: UserID) -> Response:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
djinfo = frontend.get_all_player_info([userid])[userid]
if not djinfo:
abort(404)
latest_version = sorted(djinfo.keys(), reverse=True)[0]
return render_react(
f'{djinfo[latest_version]["name"]}\'s MGA Profile',
"mga/player.react.js",
2021-09-04 17:17:22 +02:00
{
"playerid": userid,
"own_profile": userid == g.userID,
"player": djinfo,
"versions": {
version: name for (game, version, name) in frontend.all_games()
},
2021-09-04 17:17:22 +02:00
},
{
"refresh": url_for("mga_pages.listplayer", userid=userid),
2021-09-04 17:17:22 +02:00
},
)
@mga_pages.route("/players/<int:userid>/list")
2021-09-04 17:17:22 +02:00
@jsonify
@loginrequired
def listplayer(userid: UserID) -> Dict[str, Any]:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
djinfo = frontend.get_all_player_info([userid])[userid]
return {
"player": djinfo,
2021-09-04 17:17:22 +02:00
}
@mga_pages.route("/options")
2021-09-04 17:17:22 +02:00
@loginrequired
def viewsettings() -> Response:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
userid = g.userID
djinfo = frontend.get_all_player_info([userid])[userid]
if not djinfo:
abort(404)
return render_react(
"Metal Gear Arcade Game Settings",
"mga/settings.react.js",
2021-09-04 17:17:22 +02:00
{
"player": djinfo,
"versions": {
version: name for (game, version, name) in frontend.all_games()
},
2021-09-04 17:17:22 +02:00
},
{
"updatename": url_for("mga_pages.updatename"),
2021-09-04 17:17:22 +02:00
},
)
@mga_pages.route("/options/name/update", methods=["POST"])
2021-09-04 17:17:22 +02:00
@jsonify
@loginrequired
def updatename() -> Dict[str, Any]:
frontend = MetalGearArcadeFrontend(g.data, g.config, g.cache)
version = int(request.get_json()["version"])
name = request.get_json()["name"]
2021-09-04 17:17:22 +02:00
user = g.data.local.user.get_user(g.userID)
if user is None:
raise Exception("Unable to find user to update!")
2021-09-04 17:17:22 +02:00
# Grab profile and update dj name
profile = g.data.local.user.get_profile(GameConstants.MGA, version, user.id)
if profile is None:
raise Exception("Unable to find profile to update!")
if len(name) == 0 or len(name) > 8:
raise Exception("Invalid profile name!")
if (
re.match(
"^[" + "a-z" + "A-Z" + "0-9" + "@!?/=():*^[\\]#;\\-_{}$.+" + "]*$",
name,
)
is None
):
raise Exception("Invalid profile name!")
2021-09-04 17:17:22 +02:00
profile = frontend.update_name(profile, name)
g.data.local.user.put_profile(GameConstants.MGA, version, user.id, profile)
# Return that we updated
return {
"version": version,
"name": frontend.sanitize_name(name),
2021-09-04 17:17:22 +02:00
}