2024-01-09 21:54:34 +01:00
|
|
|
from typing import List
|
|
|
|
from starlette.routing import Route
|
2023-08-27 20:18:56 +02:00
|
|
|
import yaml
|
|
|
|
import jinja2
|
2024-01-09 09:07:04 +01:00
|
|
|
from starlette.requests import Request
|
2024-01-09 21:54:34 +01:00
|
|
|
from starlette.responses import Response, RedirectResponse
|
2023-08-27 20:18:56 +02:00
|
|
|
from os import path
|
|
|
|
|
2024-01-09 21:54:34 +01:00
|
|
|
from core.frontend import FE_Base, UserSession
|
2023-08-27 20:18:56 +02:00
|
|
|
from core.config import CoreConfig
|
|
|
|
|
|
|
|
from titles.ongeki.config import OngekiConfig
|
|
|
|
from titles.ongeki.const import OngekiConstants
|
|
|
|
from titles.ongeki.database import OngekiData
|
2023-09-01 23:21:02 +02:00
|
|
|
from titles.ongeki.base import OngekiBase
|
2023-08-27 20:18:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class OngekiFrontend(FE_Base):
|
|
|
|
def __init__(
|
|
|
|
self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str
|
|
|
|
) -> None:
|
|
|
|
super().__init__(cfg, environment)
|
|
|
|
self.data = OngekiData(cfg)
|
|
|
|
self.game_cfg = OngekiConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/{OngekiConstants.CONFIG_NAME}"):
|
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{OngekiConstants.CONFIG_NAME}"))
|
|
|
|
)
|
|
|
|
self.nav_name = "O.N.G.E.K.I."
|
2023-09-08 23:55:18 +02:00
|
|
|
self.version_list = OngekiConstants.VERSION_NAMES
|
2024-01-09 21:54:34 +01:00
|
|
|
|
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
return [
|
|
|
|
Route("/", self.render_GET)
|
|
|
|
]
|
2023-09-01 23:21:02 +02:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def render_GET(self, request: Request) -> bytes:
|
2023-08-27 20:18:56 +02:00
|
|
|
template = self.environment.get_template(
|
2024-01-09 21:54:34 +01:00
|
|
|
"titles/ongeki/templates/ongeki_index.jinja"
|
2023-08-27 20:18:56 +02:00
|
|
|
)
|
2024-01-09 21:54:34 +01:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh:
|
|
|
|
usr_sesh = UserSession()
|
|
|
|
|
2023-09-08 23:55:18 +02:00
|
|
|
self.version = usr_sesh.ongeki_version
|
2024-01-10 03:16:22 +01:00
|
|
|
if usr_sesh.user_id > 0:
|
2024-03-30 01:09:22 +01:00
|
|
|
profile_data =await self.data.profile.get_profile_data(usr_sesh.user_id, self.version)
|
2024-01-09 21:54:34 +01:00
|
|
|
rival_list = await self.data.profile.get_rivals(usr_sesh.user_id)
|
2023-09-01 23:21:02 +02:00
|
|
|
rival_data = {
|
|
|
|
"userRivalList": rival_list,
|
2024-01-09 21:54:34 +01:00
|
|
|
"userId": usr_sesh.user_id
|
2023-09-01 23:21:02 +02:00
|
|
|
}
|
2024-01-10 03:16:22 +01:00
|
|
|
|
|
|
|
# Hay1tsme 01/09/2024: ??????????????????????????????????????????????????????????????
|
|
|
|
rival_info = await OngekiBase.handle_get_user_rival_data_api_request(self, rival_data)
|
2023-09-01 23:21:02 +02:00
|
|
|
|
2024-01-09 21:54:34 +01:00
|
|
|
return Response(template.render(
|
2023-09-01 23:21:02 +02:00
|
|
|
data=self.data.profile,
|
|
|
|
title=f"{self.core_config.server.name} | {self.nav_name}",
|
|
|
|
game_list=self.environment.globals["game_list"],
|
|
|
|
gachas=self.game_cfg.gachas.enabled_gachas,
|
|
|
|
profile_data=profile_data,
|
|
|
|
rival_info=rival_info["userRivalDataList"],
|
2023-09-08 23:55:18 +02:00
|
|
|
version_list=self.version_list,
|
|
|
|
version=self.version,
|
2023-09-01 23:21:02 +02:00
|
|
|
sesh=vars(usr_sesh)
|
2024-01-13 23:15:02 +01:00
|
|
|
), media_type="text/html; charset=utf-8")
|
2023-09-01 23:21:02 +02:00
|
|
|
else:
|
2024-01-09 21:54:34 +01:00
|
|
|
return RedirectResponse("/gate/", 303)
|
2023-09-01 23:21:02 +02:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def render_POST(self, request: Request):
|
2023-09-01 23:21:02 +02:00
|
|
|
uri = request.uri.decode()
|
2024-01-09 21:54:34 +01:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh:
|
|
|
|
usr_sesh = UserSession()
|
|
|
|
|
|
|
|
if usr_sesh.user_id > 0:
|
2023-09-01 23:21:02 +02:00
|
|
|
if uri == "/game/ongeki/rival.add":
|
|
|
|
rival_id = request.args[b"rivalUserId"][0].decode()
|
2024-01-09 21:54:34 +01:00
|
|
|
await self.data.profile.put_rival(usr_sesh.user_id, rival_id)
|
|
|
|
# self.logger.info(f"{usr_sesh.user_id} added a rival")
|
|
|
|
return RedirectResponse(b"/game/ongeki/", 303)
|
2023-09-08 23:55:18 +02:00
|
|
|
|
2023-09-01 23:21:02 +02:00
|
|
|
elif uri == "/game/ongeki/rival.delete":
|
2023-09-02 00:40:57 +02:00
|
|
|
rival_id = request.args[b"rivalUserId"][0].decode()
|
2024-01-09 21:54:34 +01:00
|
|
|
await self.data.profile.delete_rival(usr_sesh.user_id, rival_id)
|
2023-09-02 00:40:57 +02:00
|
|
|
# self.logger.info(f"{response}")
|
2024-01-09 21:54:34 +01:00
|
|
|
return RedirectResponse(b"/game/ongeki/", 303)
|
2023-09-08 23:55:18 +02:00
|
|
|
|
|
|
|
elif uri == "/game/ongeki/version.change":
|
|
|
|
ongeki_version=request.args[b"version"][0].decode()
|
|
|
|
if(ongeki_version.isdigit()):
|
|
|
|
usr_sesh.ongeki_version=int(ongeki_version)
|
2024-01-09 21:54:34 +01:00
|
|
|
return RedirectResponse("/game/ongeki/", 303)
|
2023-09-08 23:55:18 +02:00
|
|
|
|
2023-09-01 23:21:02 +02:00
|
|
|
else:
|
2024-01-09 21:54:34 +01:00
|
|
|
Response("Something went wrong", status_code=500)
|
2023-09-01 23:21:02 +02:00
|
|
|
else:
|
2024-01-09 21:54:34 +01:00
|
|
|
return RedirectResponse("/gate/", 303)
|