2019-12-08 22:43:49 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import re
|
|
|
|
from typing import Any, Dict
|
2021-05-20 00:38:00 +02:00
|
|
|
from flask import Blueprint, request, Response, url_for, abort
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-11-01 04:29:56 +01:00
|
|
|
from bemani.common import DBConstants, GameConstants
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.data import UserID
|
|
|
|
from bemani.frontend.app import loginrequired, jsonify, render_react
|
|
|
|
from bemani.frontend.museca.museca import MusecaFrontend
|
|
|
|
from bemani.frontend.templates import templates_location
|
|
|
|
from bemani.frontend.static import static_location
|
2021-05-20 00:38:00 +02:00
|
|
|
from bemani.frontend.types import g
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
museca_pages = Blueprint(
|
2022-10-15 20:56:30 +02:00
|
|
|
"museca_pages",
|
2019-12-08 22:43:49 +01:00
|
|
|
__name__,
|
2022-10-15 20:56:30 +02:00
|
|
|
url_prefix=f"/{GameConstants.MUSECA.value}",
|
2019-12-08 22:43:49 +01:00
|
|
|
template_folder=templates_location,
|
|
|
|
static_folder=static_location,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/scores")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewnetworkscores() -> Response:
|
|
|
|
# Only load the last 100 results for the initial fetch, so we can render faster
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
network_scores = frontend.get_network_scores(limit=100)
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(network_scores["attempts"]) > 10:
|
|
|
|
network_scores["attempts"] = frontend.round_to_ten(network_scores["attempts"])
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Global MÚSECA Scores",
|
|
|
|
"museca/scores.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"attempts": network_scores["attempts"],
|
|
|
|
"songs": frontend.get_all_songs(),
|
|
|
|
"players": network_scores["players"],
|
|
|
|
"shownames": True,
|
|
|
|
"shownewrecords": False,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listnetworkscores"),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
|
|
|
"individual_score": url_for("museca_pages.viewtopscores", musicid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/scores/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listnetworkscores() -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return frontend.get_network_scores()
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/scores/<int:userid>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewscores(userid: UserID) -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
info = frontend.get_latest_player_info([userid]).get(userid)
|
|
|
|
if info is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
scores = frontend.get_scores(userid, limit=100)
|
|
|
|
if len(scores) > 10:
|
|
|
|
scores = frontend.round_to_ten(scores)
|
|
|
|
|
|
|
|
return render_react(
|
2020-01-07 22:29:07 +01:00
|
|
|
f'{info["name"]}\'s MÚSECA Scores',
|
2022-10-15 20:56:30 +02:00
|
|
|
"museca/scores.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"attempts": scores,
|
|
|
|
"songs": frontend.get_all_songs(),
|
|
|
|
"players": {},
|
|
|
|
"shownames": False,
|
|
|
|
"shownewrecords": True,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listscores", userid=userid),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
|
|
|
"individual_score": url_for("museca_pages.viewtopscores", musicid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/scores/<int:userid>/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listscores(userid: UserID) -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"attempts": frontend.get_scores(userid),
|
|
|
|
"players": {},
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/records")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewnetworkrecords() -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
network_records = frontend.get_network_records()
|
|
|
|
versions = {version: name for (game, version, name) in frontend.all_games()}
|
|
|
|
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Global MÚSECA Records",
|
|
|
|
"museca/records.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"records": network_records["records"],
|
|
|
|
"songs": frontend.get_all_songs(),
|
|
|
|
"players": network_records["players"],
|
|
|
|
"versions": versions,
|
|
|
|
"shownames": True,
|
|
|
|
"showpersonalsort": False,
|
|
|
|
"filterempty": False,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listnetworkrecords"),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
|
|
|
"individual_score": url_for("museca_pages.viewtopscores", musicid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/records/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listnetworkrecords() -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return frontend.get_network_records()
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/records/<int:userid>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewrecords(userid: UserID) -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
info = frontend.get_latest_player_info([userid]).get(userid)
|
|
|
|
if info is None:
|
|
|
|
abort(404)
|
|
|
|
versions = {version: name for (game, version, name) in frontend.all_games()}
|
|
|
|
|
|
|
|
return render_react(
|
2020-01-07 22:29:07 +01:00
|
|
|
f'{info["name"]}\'s MÚSECA Records',
|
2022-10-15 20:56:30 +02:00
|
|
|
"museca/records.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"records": frontend.get_records(userid),
|
|
|
|
"songs": frontend.get_all_songs(),
|
|
|
|
"players": {},
|
|
|
|
"versions": versions,
|
|
|
|
"shownames": False,
|
|
|
|
"showpersonalsort": True,
|
|
|
|
"filterempty": True,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listrecords", userid=userid),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
|
|
|
"individual_score": url_for("museca_pages.viewtopscores", musicid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/records/<int:userid>/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listrecords(userid: UserID) -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"records": frontend.get_records(userid),
|
|
|
|
"players": {},
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/topscores/<int:musicid>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewtopscores(musicid: int) -> Response:
|
|
|
|
# We just want to find the latest mix that this song exists in
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
versions = sorted(
|
|
|
|
[version for (game, version, name) in frontend.all_games()],
|
|
|
|
reverse=True,
|
|
|
|
)
|
|
|
|
name = None
|
|
|
|
artist = None
|
|
|
|
difficulties = [0, 0, 0, 0, 0]
|
|
|
|
|
|
|
|
for version in versions:
|
2022-11-01 04:29:56 +01:00
|
|
|
for omniadd in [0, DBConstants.OMNIMIX_VERSION_BUMP]:
|
2020-12-06 03:21:28 +01:00
|
|
|
for chart in [0, 1, 2, 3, 4]:
|
2022-10-15 20:56:30 +02:00
|
|
|
details = g.data.local.music.get_song(
|
|
|
|
GameConstants.MUSECA, version + omniadd, musicid, chart
|
|
|
|
)
|
2020-12-06 03:21:28 +01:00
|
|
|
if details is not None:
|
|
|
|
if name is None:
|
|
|
|
name = details.name
|
|
|
|
if artist is None:
|
|
|
|
artist = details.artist
|
|
|
|
if difficulties[chart] == 0:
|
2022-10-15 20:56:30 +02:00
|
|
|
difficulties[chart] = details.data.get_int("difficulty")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if name is None:
|
|
|
|
# Not a real song!
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
top_scores = frontend.get_top_scores(musicid)
|
|
|
|
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
f"Top MÚSECA Scores for {artist} - {name}",
|
|
|
|
"museca/topscores.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": name,
|
|
|
|
"artist": artist,
|
|
|
|
"difficulties": difficulties,
|
|
|
|
"players": top_scores["players"],
|
|
|
|
"topscores": top_scores["topscores"],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listtopscores", musicid=musicid),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/topscores/<int:musicid>/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listtopscores(musicid: int) -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return frontend.get_top_scores(musicid)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/players")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewplayers() -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"All MÚSECA Players",
|
|
|
|
"museca/allplayers.react.js",
|
|
|
|
{"players": frontend.get_all_players()},
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listplayers"),
|
|
|
|
"player": url_for("museca_pages.viewplayer", userid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/players/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listplayers() -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"players": frontend.get_all_players(),
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/players/<int:userid>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewplayer(userid: UserID) -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
info = frontend.get_all_player_info([userid])[userid]
|
|
|
|
if not info:
|
|
|
|
abort(404)
|
|
|
|
latest_version = sorted(info.keys(), reverse=True)[0]
|
|
|
|
|
|
|
|
return render_react(
|
2020-01-07 22:29:07 +01:00
|
|
|
f'{info[latest_version]["name"]}\'s MÚSECA Profile',
|
2022-10-15 20:56:30 +02:00
|
|
|
"museca/player.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"playerid": userid,
|
|
|
|
"own_profile": userid == g.userID,
|
|
|
|
"player": info,
|
|
|
|
"versions": {
|
|
|
|
version: name for (game, version, name) in frontend.all_games()
|
|
|
|
},
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("museca_pages.listplayer", userid=userid),
|
|
|
|
"records": url_for("museca_pages.viewrecords", userid=userid),
|
|
|
|
"scores": url_for("museca_pages.viewscores", userid=userid),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/players/<int:userid>/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def listplayer(userid: UserID) -> Dict[str, Any]:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
info = frontend.get_all_player_info([userid])[userid]
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"player": info,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/options")
|
2019-12-08 22:43:49 +01:00
|
|
|
@loginrequired
|
|
|
|
def viewsettings() -> Response:
|
|
|
|
frontend = MusecaFrontend(g.data, g.config, g.cache)
|
|
|
|
userid = g.userID
|
|
|
|
info = frontend.get_all_player_info([userid])[userid]
|
|
|
|
if not info:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"MÚSECA Game Settings",
|
|
|
|
"museca/settings.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"player": info,
|
|
|
|
"versions": {
|
|
|
|
version: name for (game, version, name) in frontend.all_games()
|
|
|
|
},
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"updatename": url_for("museca_pages.updatename"),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@museca_pages.route("/options/name/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@loginrequired
|
|
|
|
def updatename() -> Dict[str, Any]:
|
2022-10-15 20:56:30 +02:00
|
|
|
version = int(request.get_json()["version"])
|
|
|
|
name = request.get_json()["name"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(g.userID)
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Grab profile and update name
|
|
|
|
profile = g.data.local.user.get_profile(GameConstants.MUSECA, version, user.id)
|
|
|
|
if profile is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find profile to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
if len(name) == 0 or len(name) > 8:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid profile name!")
|
|
|
|
if (
|
|
|
|
re.match(
|
|
|
|
"^[" + "0-9" + "A-Z" + "!?#$&*-. " + "]*$",
|
|
|
|
name,
|
|
|
|
)
|
|
|
|
is None
|
|
|
|
):
|
|
|
|
raise Exception("Invalid profile name!")
|
|
|
|
profile.replace_str("name", name)
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.user.put_profile(GameConstants.MUSECA, version, user.id, profile)
|
|
|
|
|
|
|
|
# Return that we updated
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"version": version,
|
|
|
|
"name": name,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|