2019-12-08 22:43:49 +01:00
|
|
|
import random
|
|
|
|
from typing import Dict, Tuple, Any, Optional
|
2021-05-20 00:38:00 +02:00
|
|
|
from flask import Blueprint, request, Response, render_template, url_for
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
from bemani.backend.base import Base
|
2022-10-15 20:56:30 +02:00
|
|
|
from bemani.common import (
|
|
|
|
CardCipher,
|
|
|
|
CardCipherException,
|
|
|
|
GameConstants,
|
|
|
|
RegionConstants,
|
|
|
|
ValidatedDict,
|
|
|
|
)
|
2021-05-20 00:38:00 +02:00
|
|
|
from bemani.data import Arcade, Machine, User, UserID, News, Event, Server, Client
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.data.api.client import APIClient, NotAuthorizedAPIException, APIException
|
2022-10-15 20:56:30 +02:00
|
|
|
from bemani.frontend.app import (
|
|
|
|
adminrequired,
|
|
|
|
jsonify,
|
|
|
|
valid_email,
|
|
|
|
valid_username,
|
|
|
|
valid_pin,
|
|
|
|
render_react,
|
|
|
|
)
|
2021-09-10 04:23:20 +02:00
|
|
|
from bemani.frontend.gamesettings import get_game_settings
|
2019-12-08 22:43:49 +01:00
|
|
|
from bemani.frontend.iidx.iidx import IIDXFrontend
|
|
|
|
from bemani.frontend.jubeat.jubeat import JubeatFrontend
|
|
|
|
from bemani.frontend.popn.popn import PopnMusicFrontend
|
|
|
|
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
|
|
|
|
|
|
|
admin_pages = Blueprint(
|
2022-10-15 20:56:30 +02:00
|
|
|
"admin_pages",
|
2019-12-08 22:43:49 +01:00
|
|
|
__name__,
|
2022-10-15 20:56:30 +02:00
|
|
|
url_prefix="/admin",
|
2019-12-08 22:43:49 +01:00
|
|
|
template_folder=templates_location,
|
|
|
|
static_folder=static_location,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def format_arcade(arcade: Arcade) -> Dict[str, Any]:
|
|
|
|
owners = []
|
|
|
|
for owner in arcade.owners:
|
|
|
|
user = g.data.local.user.get_user(owner)
|
|
|
|
if user is not None:
|
|
|
|
owners.append(user.username)
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": arcade.id,
|
|
|
|
"name": arcade.name,
|
|
|
|
"description": arcade.description,
|
|
|
|
"region": arcade.region,
|
2022-10-16 00:05:08 +02:00
|
|
|
"area": arcade.area or "",
|
2022-10-15 20:56:30 +02:00
|
|
|
"paseli_enabled": arcade.data.get_bool("paseli_enabled"),
|
|
|
|
"paseli_infinite": arcade.data.get_bool("paseli_infinite"),
|
|
|
|
"mask_services_url": arcade.data.get_bool("mask_services_url"),
|
|
|
|
"owners": owners,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_machine(machine: Machine) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": machine.id,
|
|
|
|
"pcbid": machine.pcbid,
|
|
|
|
"name": machine.name,
|
|
|
|
"description": machine.description,
|
|
|
|
"arcade": machine.arcade,
|
|
|
|
"port": machine.port,
|
|
|
|
"game": machine.game.value if machine.game else "any",
|
|
|
|
"version": machine.version,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 00:38:00 +02:00
|
|
|
def format_card(card: Tuple[str, Optional[UserID]]) -> Dict[str, Any]:
|
2019-12-08 22:43:49 +01:00
|
|
|
owner = None
|
|
|
|
if card[1] is not None:
|
|
|
|
user = g.data.local.user.get_user(card[1])
|
|
|
|
if user is not None:
|
|
|
|
owner = user.username
|
|
|
|
try:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"number": CardCipher.encode(card[0]),
|
|
|
|
"owner": owner,
|
|
|
|
"id": card[1],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
except CardCipherException:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"number": "????????????????",
|
|
|
|
"owner": owner,
|
|
|
|
"id": card[1],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_user(user: User) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": user.id,
|
|
|
|
"username": user.username,
|
|
|
|
"email": user.email,
|
|
|
|
"admin": user.admin,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_news(news: News) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": news.id,
|
|
|
|
"timestamp": news.timestamp,
|
|
|
|
"title": news.title,
|
|
|
|
"body": news.body,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_event(event: Event) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": event.id,
|
|
|
|
"timestamp": event.timestamp,
|
|
|
|
"userid": event.userid,
|
|
|
|
"arcadeid": event.arcadeid,
|
|
|
|
"type": event.type,
|
|
|
|
"data": event.data,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_client(client: Client) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": client.id,
|
|
|
|
"name": client.name,
|
|
|
|
"token": client.token,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def format_server(server: Server) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"id": server.id,
|
|
|
|
"uri": server.uri,
|
|
|
|
"token": server.token,
|
|
|
|
"allow_stats": server.allow_stats,
|
|
|
|
"allow_scores": server.allow_scores,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewsettings() -> Response:
|
2022-10-15 20:56:30 +02:00
|
|
|
return Response(
|
|
|
|
render_template(
|
|
|
|
"admin/settings.html",
|
|
|
|
**{
|
|
|
|
"title": "Network Settings",
|
|
|
|
"config": g.config,
|
|
|
|
"region": RegionConstants.LUT,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/events")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewevents() -> Response:
|
|
|
|
iidx = IIDXFrontend(g.data, g.config, g.cache)
|
|
|
|
jubeat = JubeatFrontend(g.data, g.config, g.cache)
|
|
|
|
pnm = PopnMusicFrontend(g.data, g.config, g.cache)
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Events",
|
|
|
|
"admin/events.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(limit=100)
|
|
|
|
],
|
|
|
|
"users": {
|
|
|
|
user.id: user.username for user in g.data.local.user.get_all_users()
|
|
|
|
},
|
|
|
|
"arcades": {
|
|
|
|
arcade.id: arcade.name
|
|
|
|
for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
},
|
|
|
|
"iidxsongs": iidx.get_all_songs(),
|
|
|
|
"jubeatsongs": jubeat.get_all_songs(),
|
|
|
|
"pnmsongs": pnm.get_all_songs(),
|
|
|
|
"iidxversions": {
|
|
|
|
version: name for (game, version, name) in iidx.all_games()
|
|
|
|
},
|
|
|
|
"jubeatversions": {
|
|
|
|
version: name for (game, version, name) in jubeat.all_games()
|
|
|
|
},
|
|
|
|
"pnmversions": {version: name for (game, version, name) in pnm.all_games()},
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("admin_pages.listevents", since=-1),
|
|
|
|
"backfill": url_for("admin_pages.backfillevents", until=-1),
|
|
|
|
"viewuser": url_for("admin_pages.viewuser", userid=-1),
|
|
|
|
"jubeatsong": url_for("jubeat_pages.viewtopscores", musicid=-1)
|
|
|
|
if GameConstants.JUBEAT in g.config.support
|
|
|
|
else None,
|
|
|
|
"iidxsong": url_for("iidx_pages.viewtopscores", musicid=-1)
|
|
|
|
if GameConstants.IIDX in g.config.support
|
|
|
|
else None,
|
|
|
|
"pnmsong": url_for("popn_pages.viewtopscores", musicid=-1)
|
|
|
|
if GameConstants.POPN_MUSIC in g.config.support
|
|
|
|
else None,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/events/backfill/<int:until>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def backfillevents(until: int) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(until_id=until, limit=1000)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/events/list/<int:since>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def listevents(since: int) -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(since_id=since)
|
|
|
|
],
|
|
|
|
"users": {user.id: user.username for user in g.data.local.user.get_all_users()},
|
|
|
|
"arcades": {
|
|
|
|
arcade.id: arcade.name for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
},
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/api")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewapi() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Data API",
|
|
|
|
"admin/api.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"clients": [
|
|
|
|
format_client(client) for client in g.data.local.api.get_all_clients()
|
|
|
|
],
|
|
|
|
"servers": [
|
|
|
|
format_server(server) for server in g.data.local.api.get_all_servers()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"addclient": url_for("admin_pages.addclient"),
|
|
|
|
"updateclient": url_for("admin_pages.updateclient"),
|
|
|
|
"removeclient": url_for("admin_pages.removeclient"),
|
|
|
|
"addserver": url_for("admin_pages.addserver"),
|
|
|
|
"updateserver": url_for("admin_pages.updateserver"),
|
|
|
|
"removeserver": url_for("admin_pages.removeserver"),
|
|
|
|
"queryserver": url_for("admin_pages.queryserver", serverid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/arcades")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewarcades() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Arcades",
|
|
|
|
"admin/arcades.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"arcades": [
|
|
|
|
format_arcade(arcade)
|
|
|
|
for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
],
|
|
|
|
"regions": RegionConstants.LUT,
|
|
|
|
"usernames": g.data.local.user.get_all_usernames(),
|
|
|
|
"paseli_enabled": g.config.paseli.enabled,
|
|
|
|
"paseli_infinite": g.config.paseli.infinite,
|
|
|
|
"default_region": g.config.server.region,
|
2022-10-16 00:05:08 +02:00
|
|
|
"default_area": g.config.server.area,
|
2022-10-15 20:56:30 +02:00
|
|
|
"mask_services_url": False,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"addarcade": url_for("admin_pages.addarcade"),
|
|
|
|
"updatearcade": url_for("admin_pages.updatearcade"),
|
|
|
|
"removearcade": url_for("admin_pages.removearcade"),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/pcbids")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewmachines() -> Response:
|
|
|
|
games: Dict[str, Dict[int, str]] = {}
|
|
|
|
for (game, version, name) in Base.all_games():
|
2021-08-19 21:21:22 +02:00
|
|
|
if game.value not in games:
|
|
|
|
games[game.value] = {}
|
|
|
|
games[game.value][version] = name
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Machines",
|
|
|
|
"admin/machines.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
|
|
|
"arcades": {
|
|
|
|
arcade.id: arcade.name
|
|
|
|
for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
},
|
|
|
|
"series": {
|
|
|
|
GameConstants.BISHI_BASHI.value: "BishiBashi",
|
|
|
|
GameConstants.DDR.value: "DDR",
|
|
|
|
GameConstants.IIDX.value: "IIDX",
|
|
|
|
GameConstants.JUBEAT.value: "Jubeat",
|
|
|
|
GameConstants.MGA.value: "Metal Gear Arcade",
|
|
|
|
GameConstants.MUSECA.value: "MÚSECA",
|
|
|
|
GameConstants.POPN_MUSIC.value: "Pop'n Music",
|
|
|
|
GameConstants.REFLEC_BEAT.value: "Reflec Beat",
|
|
|
|
GameConstants.SDVX.value: "SDVX",
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
2022-10-15 20:56:30 +02:00
|
|
|
"games": games,
|
|
|
|
"enforcing": g.config.server.enforce_pcbid,
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("admin_pages.listmachines"),
|
|
|
|
"generatepcbid": url_for("admin_pages.generatepcbid"),
|
|
|
|
"addpcbid": url_for("admin_pages.addpcbid"),
|
|
|
|
"updatepcbid": url_for("admin_pages.updatepcbid"),
|
|
|
|
"removepcbid": url_for("admin_pages.removepcbid"),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/cards")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewcards() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Cards",
|
|
|
|
"admin/cards.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": [format_card(card) for card in g.data.local.user.get_all_cards()],
|
|
|
|
"usernames": g.data.local.user.get_all_usernames(),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"addcard": url_for("admin_pages.addcard"),
|
|
|
|
"removecard": url_for("admin_pages.removecard"),
|
|
|
|
"viewuser": url_for("admin_pages.viewuser", userid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewusers() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Users",
|
|
|
|
"admin/users.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"users": [format_user(user) for user in g.data.local.user.get_all_users()],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"searchusers": url_for("admin_pages.searchusers"),
|
|
|
|
"viewuser": url_for("admin_pages.viewuser", userid=-1),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/news")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewnews() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"News",
|
|
|
|
"admin/news.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"news": [format_news(news) for news in g.data.local.network.get_all_news()],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"removenews": url_for("admin_pages.removenews"),
|
|
|
|
"addnews": url_for("admin_pages.addnews"),
|
|
|
|
"updatenews": url_for("admin_pages.updatenews"),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/gamesettings")
|
2021-09-10 04:23:20 +02:00
|
|
|
@adminrequired
|
|
|
|
def viewgamesettings() -> Response:
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"Game Settings",
|
|
|
|
"admin/gamesettings.react.js",
|
2021-09-10 04:23:20 +02:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"game_settings": get_game_settings(
|
|
|
|
g.data, g.data.local.machine.DEFAULT_SETTINGS_ARCADE
|
|
|
|
),
|
2021-09-10 04:23:20 +02:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"update_settings": url_for("admin_pages.updatesettings"),
|
2021-09-10 04:23:20 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>")
|
2019-12-08 22:43:49 +01:00
|
|
|
@adminrequired
|
|
|
|
def viewuser(userid: int) -> Response:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
|
|
|
|
def __format_card(card: str) -> str:
|
|
|
|
try:
|
|
|
|
return CardCipher.encode(card)
|
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
return "????????????????"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
cards = [__format_card(card) for card in g.data.local.user.get_cards(userid)]
|
|
|
|
arcades = g.data.local.machine.get_all_arcades()
|
|
|
|
return render_react(
|
2022-10-15 20:56:30 +02:00
|
|
|
"User",
|
|
|
|
"admin/user.react.js",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"user": {
|
|
|
|
"email": user.email,
|
|
|
|
"username": user.username,
|
|
|
|
},
|
|
|
|
"cards": cards,
|
|
|
|
"arcades": {arcade.id: arcade.name for arcade in arcades},
|
|
|
|
"balances": {
|
|
|
|
arcade.id: g.data.local.user.get_balance(userid, arcade.id)
|
|
|
|
for arcade in arcades
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
2022-10-15 20:56:30 +02:00
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(
|
|
|
|
userid=userid, event="paseli_transaction"
|
|
|
|
)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"refresh": url_for("admin_pages.listuser", userid=userid),
|
|
|
|
"removeusercard": url_for("admin_pages.removeusercard", userid=userid),
|
|
|
|
"addusercard": url_for("admin_pages.addusercard", userid=userid),
|
|
|
|
"updatebalance": url_for("admin_pages.updatebalance", userid=userid),
|
|
|
|
"updateusername": url_for("admin_pages.updateusername", userid=userid),
|
|
|
|
"updateemail": url_for("admin_pages.updateemail", userid=userid),
|
|
|
|
"updatepin": url_for("admin_pages.updatepin", userid=userid),
|
|
|
|
"updatepassword": url_for("admin_pages.updatepassword", userid=userid),
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/list")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def listuser(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def __format_card(card: str) -> str:
|
|
|
|
try:
|
|
|
|
return CardCipher.encode(card)
|
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
return "????????????????"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
cards = [__format_card(card) for card in g.data.local.user.get_cards(userid)]
|
|
|
|
arcades = g.data.local.machine.get_all_arcades()
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": cards,
|
|
|
|
"arcades": {arcade.id: arcade.name for arcade in arcades},
|
|
|
|
"balances": {
|
|
|
|
arcade.id: g.data.local.user.get_balance(userid, arcade.id)
|
|
|
|
for arcade in arcades
|
|
|
|
},
|
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(
|
|
|
|
userid=userid, event="paseli_transaction"
|
|
|
|
)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/arcades/list")
|
2021-08-29 03:47:45 +02:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def listmachines() -> Dict[str, Any]:
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
|
|
|
"arcades": {
|
|
|
|
arcade.id: arcade.name for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
},
|
2021-08-29 03:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/arcades/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatearcade() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this arcade up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["arcade"]
|
|
|
|
arcade = g.data.local.machine.get_arcade(new_values["id"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if arcade is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find arcade to update!")
|
|
|
|
|
|
|
|
arcade.name = new_values["name"]
|
|
|
|
arcade.description = new_values["description"]
|
|
|
|
arcade.region = new_values["region"]
|
2022-10-16 00:05:08 +02:00
|
|
|
arcade.area = new_values["area"] or None
|
2022-10-15 20:56:30 +02:00
|
|
|
arcade.data.replace_bool("paseli_enabled", new_values["paseli_enabled"])
|
|
|
|
arcade.data.replace_bool("paseli_infinite", new_values["paseli_infinite"])
|
|
|
|
arcade.data.replace_bool("mask_services_url", new_values["mask_services_url"])
|
2019-12-08 22:43:49 +01:00
|
|
|
owners = []
|
2022-10-15 20:56:30 +02:00
|
|
|
for owner in new_values["owners"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
ownerid = g.data.local.user.from_username(owner)
|
|
|
|
if ownerid is not None:
|
|
|
|
owners.append(ownerid)
|
|
|
|
owners = list(set(owners))
|
|
|
|
arcade.owners = owners
|
|
|
|
g.data.local.machine.put_arcade(arcade)
|
|
|
|
|
|
|
|
# Just return all arcades for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"arcades": [
|
|
|
|
format_arcade(arcade) for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/arcades/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addarcade() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this arcade up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["arcade"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(new_values["name"]) == 0:
|
|
|
|
raise Exception("Please name your new arcade!")
|
|
|
|
if len(new_values["description"]) == 0:
|
|
|
|
raise Exception("Please describe your new arcade!")
|
2019-12-08 22:43:49 +01:00
|
|
|
owners = []
|
2022-10-15 20:56:30 +02:00
|
|
|
for owner in new_values["owners"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
ownerid = g.data.local.user.from_username(owner)
|
|
|
|
if ownerid is not None:
|
|
|
|
owners.append(ownerid)
|
|
|
|
owners = list(set(owners))
|
|
|
|
|
|
|
|
g.data.local.machine.create_arcade(
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values["name"],
|
|
|
|
new_values["description"],
|
|
|
|
new_values["region"],
|
2022-10-16 00:05:08 +02:00
|
|
|
new_values["area"] or None,
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"paseli_enabled": new_values["paseli_enabled"],
|
|
|
|
"paseli_infinite": new_values["paseli_infinite"],
|
|
|
|
"mask_services_url": new_values["mask_services_url"],
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
owners,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Just return all arcades for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"arcades": [
|
|
|
|
format_arcade(arcade) for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/arcades/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removearcade() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this arcade up
|
2022-10-15 20:56:30 +02:00
|
|
|
arcadeid = request.get_json()["arcadeid"]
|
2019-12-08 22:43:49 +01:00
|
|
|
arcade = g.data.local.machine.get_arcade(arcadeid)
|
|
|
|
if arcade is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find arcade to delete!")
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.machine.destroy_arcade(arcadeid)
|
|
|
|
|
|
|
|
# Just return all arcades for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"arcades": [
|
|
|
|
format_arcade(arcade) for arcade in g.data.local.machine.get_all_arcades()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/clients/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updateclient() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this client up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["client"]
|
|
|
|
client = g.data.local.api.get_client(new_values["id"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if client is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find client to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(new_values["name"]) == 0:
|
|
|
|
raise Exception("Client names must be at least one character long!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
client.name = new_values["name"]
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.api.put_client(client)
|
|
|
|
|
|
|
|
# Just return all clients for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"clients": [
|
|
|
|
format_client(client) for client in g.data.local.api.get_all_clients()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/clients/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addclient() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this client up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["client"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(new_values["name"]) == 0:
|
|
|
|
raise Exception("Please name your new client!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
g.data.local.api.create_client(
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values["name"],
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Just return all clientss for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"clients": [
|
|
|
|
format_client(client) for client in g.data.local.api.get_all_clients()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/clients/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removeclient() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this client up
|
2022-10-15 20:56:30 +02:00
|
|
|
clientid = request.get_json()["clientid"]
|
2019-12-08 22:43:49 +01:00
|
|
|
client = g.data.local.api.get_client(clientid)
|
|
|
|
if client is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find client to delete!")
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.api.destroy_client(clientid)
|
|
|
|
|
|
|
|
# Just return all clients for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"clients": [
|
|
|
|
format_client(client) for client in g.data.local.api.get_all_clients()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/server/<int:serverid>/info")
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def queryserver(serverid: int) -> Dict[str, Any]:
|
|
|
|
# Attempt to look this server up
|
|
|
|
server = g.data.local.api.get_server(serverid)
|
|
|
|
if server is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find server to query!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
client = APIClient(server.uri, server.token, False, False)
|
|
|
|
try:
|
|
|
|
serverinfo = client.get_server_info()
|
|
|
|
info = {
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": serverinfo["name"],
|
|
|
|
"email": serverinfo["email"],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
2022-10-15 20:56:30 +02:00
|
|
|
info["status"] = (
|
|
|
|
"ok" if APIClient.API_VERSION in serverinfo["versions"] else "badversion"
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
except NotAuthorizedAPIException:
|
|
|
|
info = {
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": "unknown",
|
|
|
|
"email": "unknown",
|
|
|
|
"status": "badauth",
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
except APIException:
|
|
|
|
info = {
|
2022-10-15 20:56:30 +02:00
|
|
|
"name": "unknown",
|
|
|
|
"email": "unknown",
|
|
|
|
"status": "error",
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/servers/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updateserver() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this server up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["server"]
|
|
|
|
server = g.data.local.api.get_server(new_values["id"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if server is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find server to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(new_values["uri"]) == 0:
|
|
|
|
raise Exception("Please provide a valid connection URI for this server!")
|
|
|
|
if len(new_values["token"]) == 0 or len(new_values["token"]) > 64:
|
|
|
|
raise Exception("Please provide a valid connection token for this server!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
server.uri = new_values["uri"]
|
|
|
|
server.token = new_values["token"]
|
|
|
|
server.allow_stats = new_values["allow_stats"]
|
|
|
|
server.allow_scores = new_values["allow_scores"]
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.api.put_server(server)
|
|
|
|
|
|
|
|
# Just return all servers for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"servers": [
|
|
|
|
format_server(server) for server in g.data.local.api.get_all_servers()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/servers/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addserver() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this server up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values = request.get_json()["server"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if len(new_values["uri"]) == 0:
|
|
|
|
raise Exception("Please provide a connection URI for the new server!")
|
|
|
|
if len(new_values["token"]) == 0 or len(new_values["token"]) > 64:
|
|
|
|
raise Exception("Please provide a valid connection token for the new server!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
g.data.local.api.create_server(
|
2022-10-15 20:56:30 +02:00
|
|
|
new_values["uri"],
|
|
|
|
new_values["token"],
|
2019-12-08 22:43:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Just return all serverss for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"servers": [
|
|
|
|
format_server(server) for server in g.data.local.api.get_all_servers()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/servers/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removeserver() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this server up
|
2022-10-15 20:56:30 +02:00
|
|
|
serverid = request.get_json()["serverid"]
|
2019-12-08 22:43:49 +01:00
|
|
|
server = g.data.local.api.get_server(serverid)
|
|
|
|
if server is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find server to delete!")
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.api.destroy_server(serverid)
|
|
|
|
|
|
|
|
# Just return all servers for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"servers": [
|
|
|
|
format_server(server) for server in g.data.local.api.get_all_servers()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/pcbids/generate", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def generatepcbid() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this arcade up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_pcbid = request.get_json()["machine"]
|
|
|
|
if new_pcbid["arcade"] is not None:
|
|
|
|
arcade = g.data.local.machine.get_arcade(new_pcbid["arcade"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if arcade is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find arcade to link PCBID to!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Will be set by the game on boot.
|
2022-10-15 20:56:30 +02:00
|
|
|
name: str = "なし"
|
2021-08-20 20:10:07 +02:00
|
|
|
pcbid: Optional[str] = None
|
2019-12-08 22:43:49 +01:00
|
|
|
while pcbid is None:
|
|
|
|
# Generate a new PCBID, check for uniqueness
|
2022-10-15 20:56:30 +02:00
|
|
|
potential_pcbid = "01201000000000" + "".join(
|
|
|
|
[random.choice("0123456789ABCDEF") for _ in range(6)]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
if g.data.local.machine.get_machine(potential_pcbid) is None:
|
|
|
|
pcbid = potential_pcbid
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
g.data.local.machine.create_machine(
|
|
|
|
pcbid, name, new_pcbid["description"], new_pcbid["arcade"]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Just return all machines for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/pcbids/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addpcbid() -> Dict[str, Any]:
|
|
|
|
# Attempt to look this arcade up
|
2022-10-15 20:56:30 +02:00
|
|
|
new_pcbid = request.get_json()["machine"]
|
|
|
|
if new_pcbid["arcade"] is not None:
|
|
|
|
arcade = g.data.local.machine.get_arcade(new_pcbid["arcade"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if arcade is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find arcade to link PCBID to!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Verify that the PCBID is valid
|
2022-10-15 20:56:30 +02:00
|
|
|
potential_pcbid = "".join(
|
|
|
|
[c for c in new_pcbid["pcbid"].upper() if c in "0123456789ABCDEF"]
|
|
|
|
)
|
|
|
|
if len(potential_pcbid) != len(new_pcbid["pcbid"]):
|
2019-12-08 22:43:49 +01:00
|
|
|
raise Exception("Invalid characters in PCBID!")
|
|
|
|
if len(potential_pcbid) != 20:
|
|
|
|
raise Exception("PCBID has invalid length!")
|
|
|
|
|
|
|
|
if g.data.local.machine.get_machine(potential_pcbid) is not None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("PCBID already exists!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Will be set by the game on boot.
|
2022-10-15 20:56:30 +02:00
|
|
|
name = "なし"
|
|
|
|
g.data.local.machine.create_machine(
|
|
|
|
potential_pcbid, name, new_pcbid["description"], new_pcbid["arcade"]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Just return all machines for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/pcbids/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatepcbid() -> Dict[str, Any]:
|
2021-08-29 03:47:45 +02:00
|
|
|
# Attempt to look this machine up
|
2022-10-15 20:56:30 +02:00
|
|
|
machine = request.get_json()["machine"]
|
|
|
|
if machine["arcade"] is not None:
|
|
|
|
arcade = g.data.local.machine.get_arcade(machine["arcade"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if arcade is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find arcade to link PCBID to!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Make sure we don't duplicate port assignments
|
2022-10-15 20:56:30 +02:00
|
|
|
other_pcbid = g.data.local.machine.from_port(machine["port"])
|
|
|
|
if other_pcbid is not None and other_pcbid != machine["pcbid"]:
|
|
|
|
raise Exception(f"The specified port is already in use by '{other_pcbid}'!")
|
|
|
|
|
|
|
|
if machine["port"] < 1 or machine["port"] > 65535:
|
|
|
|
raise Exception("The specified port is out of range!")
|
|
|
|
|
|
|
|
current_machine = g.data.local.machine.get_machine(machine["pcbid"])
|
|
|
|
current_machine.description = machine["description"]
|
|
|
|
current_machine.arcade = machine["arcade"]
|
|
|
|
current_machine.port = machine["port"]
|
|
|
|
current_machine.game = (
|
|
|
|
None if machine["game"] == "any" else GameConstants(machine["game"])
|
|
|
|
)
|
|
|
|
current_machine.version = None if machine["game"] == "any" else machine["version"]
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.machine.put_machine(current_machine)
|
|
|
|
|
|
|
|
# Just return all machines for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/pcbids/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removepcbid() -> Dict[str, Any]:
|
2021-08-29 03:47:45 +02:00
|
|
|
# Attempt to look this machine up
|
2022-10-15 20:56:30 +02:00
|
|
|
pcbid = request.get_json()["pcbid"]
|
2019-12-08 22:43:49 +01:00
|
|
|
if g.data.local.machine.get_machine(pcbid) is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find PCBID to delete!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
g.data.local.machine.destroy_machine(pcbid)
|
|
|
|
|
|
|
|
# Just return all machines for ease of updating
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"machines": [
|
|
|
|
format_machine(machine)
|
|
|
|
for machine in g.data.local.machine.get_all_machines()
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/cards/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removecard() -> Dict[str, Any]:
|
|
|
|
# Grab card, convert it
|
2022-10-15 20:56:30 +02:00
|
|
|
card = request.get_json()["card"]
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
|
|
|
cardid = CardCipher.decode(card)
|
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid card number!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Make sure it is our card
|
|
|
|
userid = g.data.local.user.from_cardid(cardid)
|
|
|
|
|
|
|
|
# Remove it from the user's account
|
|
|
|
g.data.local.user.destroy_card(userid, cardid)
|
|
|
|
|
|
|
|
# Return new card list
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": [format_card(card) for card in g.data.local.user.get_all_cards()],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/cards/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addcard() -> Dict[str, Any]:
|
|
|
|
# Grab card, convert it
|
2022-10-15 20:56:30 +02:00
|
|
|
card = request.get_json()["card"]
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
cardid = CardCipher.decode(card["number"])
|
2019-12-08 22:43:49 +01:00
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid card number!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Make sure it is our card
|
2022-10-15 20:56:30 +02:00
|
|
|
userid = g.data.local.user.from_username(card["owner"])
|
2019-12-08 22:43:49 +01:00
|
|
|
if userid is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to add card to!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# See if it is already claimed
|
|
|
|
curuserid = g.data.local.user.from_cardid(cardid)
|
|
|
|
if curuserid is not None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("This card is already in use!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Add it to the user's account
|
|
|
|
g.data.local.user.add_card(userid, cardid)
|
|
|
|
|
|
|
|
# Return new card list
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": [format_card(card) for card in g.data.local.user.get_all_cards()],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/search", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def searchusers() -> Dict[str, Any]:
|
|
|
|
# Grab card, convert it
|
2022-10-15 20:56:30 +02:00
|
|
|
searchdetails = request.get_json()["user_search"]
|
|
|
|
if len(searchdetails["card"]) > 0:
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
2022-10-15 20:56:30 +02:00
|
|
|
cardid = CardCipher.decode(searchdetails["card"])
|
2019-12-08 22:43:49 +01:00
|
|
|
actual_userid = g.data.local.user.from_cardid(cardid)
|
|
|
|
if actual_userid is None:
|
|
|
|
# Force a non-match below
|
2021-05-20 00:38:00 +02:00
|
|
|
actual_userid = UserID(-1)
|
2019-12-08 22:43:49 +01:00
|
|
|
except CardCipherException:
|
2021-05-20 00:38:00 +02:00
|
|
|
actual_userid = UserID(-1)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
actual_userid = None
|
|
|
|
|
|
|
|
def match(user: User) -> bool:
|
|
|
|
if actual_userid is not None:
|
|
|
|
return user.id == actual_userid
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"users": [
|
|
|
|
format_user(user)
|
|
|
|
for user in g.data.local.user.get_all_users()
|
|
|
|
if match(user)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/balance/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatebalance(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
credits = request.get_json()["credits"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
arcades = g.data.local.machine.get_all_arcades()
|
|
|
|
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Update balances
|
|
|
|
for arcadeid in credits:
|
|
|
|
balance = g.data.local.user.update_balance(userid, arcadeid, credits[arcadeid])
|
|
|
|
if balance is not None:
|
|
|
|
g.data.local.network.put_event(
|
2022-10-15 20:56:30 +02:00
|
|
|
"paseli_transaction",
|
2019-12-08 22:43:49 +01:00
|
|
|
{
|
2022-10-15 20:56:30 +02:00
|
|
|
"delta": credits[arcadeid],
|
|
|
|
"balance": balance,
|
|
|
|
"reason": "admin adjustment",
|
2019-12-08 22:43:49 +01:00
|
|
|
},
|
|
|
|
userid=userid,
|
|
|
|
arcadeid=arcadeid,
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"arcades": {arcade.id: arcade.name for arcade in arcades},
|
|
|
|
"balances": {
|
|
|
|
arcade.id: g.data.local.user.get_balance(userid, arcade.id)
|
|
|
|
for arcade in arcades
|
|
|
|
},
|
|
|
|
"events": [
|
|
|
|
format_event(event)
|
|
|
|
for event in g.data.local.network.get_events(
|
|
|
|
userid=userid, event="paseli_transaction"
|
|
|
|
)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/username/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updateusername(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
username = request.get_json()["username"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if not valid_username(username):
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid username!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Make sure this user ID isn't taken
|
|
|
|
potential_userid = g.data.local.user.from_username(username)
|
|
|
|
if potential_userid is not None and potential_userid != userid:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("That username is already taken!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Update the user
|
|
|
|
user.username = username
|
|
|
|
g.data.local.user.put_user(user)
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"username": username,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/email/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updateemail(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
email = request.get_json()["email"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if not valid_email(email):
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid email!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Update the user
|
|
|
|
user.email = email
|
|
|
|
g.data.local.user.put_user(user)
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"email": email,
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/pin/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatepin(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
pin = request.get_json()["pin"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if not valid_pin(pin, "card"):
|
|
|
|
raise Exception("Invalid pin, must be exactly 4 digits!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Update the user
|
|
|
|
g.data.local.user.update_pin(userid, pin)
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/password/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatepassword(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
new1 = request.get_json()["new1"]
|
|
|
|
new2 = request.get_json()["new2"]
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Now, make sure that the passwords match
|
|
|
|
if new1 != new2:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Passwords do not match each other!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Now, make sure passwords are long enough
|
|
|
|
if len(new1) < 6:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Password is not long enough!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Update the user
|
|
|
|
g.data.local.user.update_password(userid, new1)
|
|
|
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/cards/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removeusercard(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
# Grab card, convert it
|
2022-10-15 20:56:30 +02:00
|
|
|
card = request.get_json()["card"]
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
|
|
|
cardid = CardCipher.decode(card)
|
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid card number!")
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Remove it from the user's account
|
|
|
|
g.data.local.user.destroy_card(userid, cardid)
|
|
|
|
|
|
|
|
# Return new card list
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": [
|
|
|
|
CardCipher.encode(card) for card in g.data.local.user.get_cards(userid)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/users/<int:userid>/cards/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addusercard(userid: int) -> Dict[str, Any]:
|
2021-05-20 00:38:00 +02:00
|
|
|
# Cast the userID.
|
|
|
|
userid = UserID(userid)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
# Grab card, convert it
|
2022-10-15 20:56:30 +02:00
|
|
|
card = request.get_json()["card"]
|
2019-12-08 22:43:49 +01:00
|
|
|
try:
|
|
|
|
cardid = CardCipher.decode(card)
|
|
|
|
except CardCipherException:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Invalid card number!")
|
2019-12-08 22:43:49 +01:00
|
|
|
user = g.data.local.user.get_user(userid)
|
|
|
|
# Make sure the user ID is valid
|
|
|
|
if user is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Cannot find user to update!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# See if it is already claimed
|
|
|
|
curuserid = g.data.local.user.from_cardid(cardid)
|
|
|
|
if curuserid is not None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("This card is already in use!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Add it to the user's account
|
|
|
|
g.data.local.user.add_card(userid, cardid)
|
|
|
|
|
|
|
|
# Return new card list
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"cards": [
|
|
|
|
CardCipher.encode(card) for card in g.data.local.user.get_cards(userid)
|
|
|
|
],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/news/add", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def addnews() -> Dict[str, Any]:
|
2022-10-15 20:56:30 +02:00
|
|
|
news = request.get_json()["news"]
|
|
|
|
if len(news["title"]) == 0:
|
|
|
|
raise Exception("Please provide a title!")
|
|
|
|
if len(news["body"]) == 0:
|
|
|
|
raise Exception("Please provide a body!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
g.data.local.network.create_news(news["title"], news["body"])
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"news": [format_news(news) for news in g.data.local.network.get_all_news()],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/news/remove", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def removenews() -> Dict[str, Any]:
|
2022-10-15 20:56:30 +02:00
|
|
|
newsid = request.get_json()["newsid"]
|
2019-12-08 22:43:49 +01:00
|
|
|
if g.data.local.network.get_news(newsid) is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise Exception("Unable to find entry to delete!")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
g.data.local.network.destroy_news(newsid)
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"news": [format_news(news) for news in g.data.local.network.get_all_news()],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/news/update", methods=["POST"])
|
2019-12-08 22:43:49 +01:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatenews() -> Dict[str, Any]:
|
2022-10-15 20:56:30 +02:00
|
|
|
new_news = request.get_json()["news"]
|
|
|
|
if g.data.local.network.get_news(new_news["id"]) is None:
|
|
|
|
raise Exception("Unable to find entry to update!")
|
|
|
|
if len(new_news["title"]) == 0:
|
|
|
|
raise Exception("Please provide a title!")
|
|
|
|
if len(new_news["body"]) == 0:
|
|
|
|
raise Exception("Please provide a body!")
|
|
|
|
|
|
|
|
news = g.data.local.network.get_news(new_news["id"])
|
|
|
|
news.title = new_news["title"]
|
|
|
|
news.body = new_news["body"]
|
2019-12-08 22:43:49 +01:00
|
|
|
g.data.local.network.put_news(news)
|
|
|
|
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"news": [format_news(news) for news in g.data.local.network.get_all_news()],
|
2019-12-08 22:43:49 +01:00
|
|
|
}
|
2021-09-10 04:23:20 +02:00
|
|
|
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
@admin_pages.route("/gamesettings/update", methods=["POST"])
|
2021-09-10 04:23:20 +02:00
|
|
|
@jsonify
|
|
|
|
@adminrequired
|
|
|
|
def updatesettings() -> Dict[str, Any]:
|
|
|
|
# Cast the ID for type safety.
|
|
|
|
arcadeid = g.data.local.machine.DEFAULT_SETTINGS_ARCADE
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
game = GameConstants(request.get_json()["game"])
|
|
|
|
version = request.get_json()["version"]
|
2021-09-10 04:23:20 +02:00
|
|
|
|
|
|
|
for setting_type, update_function in [
|
2022-10-15 20:56:30 +02:00
|
|
|
("bools", "replace_bool"),
|
|
|
|
("ints", "replace_int"),
|
|
|
|
("strs", "replace_str"),
|
|
|
|
("longstrs", "replace_str"),
|
2021-09-10 04:23:20 +02:00
|
|
|
]:
|
|
|
|
for game_setting in request.get_json()[setting_type]:
|
|
|
|
# Grab the value to update
|
2022-10-15 20:56:30 +02:00
|
|
|
category = game_setting["category"]
|
|
|
|
setting = game_setting["setting"]
|
|
|
|
new_value = game_setting["value"]
|
2021-09-10 04:23:20 +02:00
|
|
|
|
|
|
|
# Update the value
|
2022-10-15 20:56:30 +02:00
|
|
|
current_settings = g.data.local.machine.get_settings(
|
|
|
|
arcadeid, game, version, category
|
|
|
|
)
|
2021-09-10 04:23:20 +02:00
|
|
|
if current_settings is None:
|
|
|
|
current_settings = ValidatedDict()
|
|
|
|
|
|
|
|
getattr(current_settings, update_function)(setting, new_value)
|
|
|
|
|
|
|
|
# Save it back
|
2022-10-15 20:56:30 +02:00
|
|
|
g.data.local.machine.put_settings(
|
|
|
|
arcadeid, game, version, category, current_settings
|
|
|
|
)
|
2021-09-10 04:23:20 +02:00
|
|
|
|
|
|
|
# Return the updated value
|
|
|
|
return {
|
2022-10-15 20:56:30 +02:00
|
|
|
"game_settings": [
|
|
|
|
gs
|
|
|
|
for gs in get_game_settings(g.data, arcadeid)
|
|
|
|
if gs["game"] == game.value and gs["version"] == version
|
2021-09-10 04:23:20 +02:00
|
|
|
][0],
|
|
|
|
}
|