2023-10-01 01:54:23 +00:00
|
|
|
import json
|
2024-01-09 20:54:34 +00:00
|
|
|
from typing import List
|
|
|
|
from starlette.routing import Route
|
|
|
|
from starlette.responses import Response, RedirectResponse
|
2023-10-01 01:54:23 +00:00
|
|
|
import yaml
|
|
|
|
import jinja2
|
|
|
|
from os import path
|
2024-01-09 08:07:04 +00:00
|
|
|
from starlette.requests import Request
|
2023-10-01 01:54:23 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
from core.frontend import FE_Base, UserSession
|
2023-10-01 01:54:23 +00:00
|
|
|
from core.config import CoreConfig
|
|
|
|
from titles.idac.database import IDACData
|
|
|
|
from titles.idac.schema.profile import *
|
|
|
|
from titles.idac.schema.item import *
|
|
|
|
from titles.idac.config import IDACConfig
|
|
|
|
from titles.idac.const import IDACConstants
|
|
|
|
|
|
|
|
|
|
|
|
class IDACFrontend(FE_Base):
|
|
|
|
def __init__(
|
|
|
|
self, cfg: CoreConfig, environment: jinja2.Environment, cfg_dir: str
|
|
|
|
) -> None:
|
|
|
|
super().__init__(cfg, environment)
|
|
|
|
self.data = IDACData(cfg)
|
|
|
|
self.game_cfg = IDACConfig()
|
|
|
|
if path.exists(f"{cfg_dir}/{IDACConstants.CONFIG_NAME}"):
|
|
|
|
self.game_cfg.update(
|
|
|
|
yaml.safe_load(open(f"{cfg_dir}/{IDACConstants.CONFIG_NAME}"))
|
|
|
|
)
|
2024-01-09 20:54:34 +00:00
|
|
|
#self.nav_name = "頭文字D THE ARCADE"
|
|
|
|
self.nav_name = "IDAC"
|
2023-10-01 01:54:23 +00:00
|
|
|
# TODO: Add version list
|
|
|
|
self.version = IDACConstants.VER_IDAC_SEASON_2
|
|
|
|
|
|
|
|
self.ticket_names = {
|
|
|
|
3: "car_dressup_points",
|
|
|
|
5: "avatar_points",
|
|
|
|
25: "full_tune_tickets",
|
|
|
|
34: "full_tune_fragments",
|
|
|
|
}
|
2024-01-09 20:54:34 +00:00
|
|
|
|
|
|
|
def get_routes(self) -> List[Route]:
|
|
|
|
return [
|
|
|
|
Route("/", self.render_GET)
|
|
|
|
]
|
2023-10-01 01:54:23 +00:00
|
|
|
|
2024-01-09 19:42:17 +00:00
|
|
|
async def generate_all_tables_json(self, user_id: int):
|
2023-10-01 01:54:23 +00:00
|
|
|
json_export = {}
|
|
|
|
|
|
|
|
idac_tables = {
|
|
|
|
profile,
|
|
|
|
config,
|
|
|
|
avatar,
|
|
|
|
rank,
|
|
|
|
stock,
|
|
|
|
theory,
|
|
|
|
car,
|
|
|
|
ticket,
|
|
|
|
story,
|
|
|
|
episode,
|
|
|
|
difficulty,
|
|
|
|
course,
|
|
|
|
trial,
|
|
|
|
challenge,
|
|
|
|
theory_course,
|
|
|
|
theory_partner,
|
|
|
|
theory_running,
|
|
|
|
vs_info,
|
|
|
|
stamp,
|
|
|
|
timetrial_event
|
|
|
|
}
|
|
|
|
|
|
|
|
for table in idac_tables:
|
|
|
|
sql = select(table).where(
|
|
|
|
table.c.user == user_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# check if the table has a version column
|
|
|
|
if "version" in table.c:
|
|
|
|
sql = sql.where(table.c.version == self.version)
|
|
|
|
|
|
|
|
# lol use the profile connection for items, dirty hack
|
2024-01-09 19:42:17 +00:00
|
|
|
result = await self.data.profile.execute(sql)
|
2023-10-01 01:54:23 +00:00
|
|
|
data_list = result.fetchall()
|
|
|
|
|
|
|
|
# add the list to the json export with the correct table name
|
|
|
|
json_export[table.name] = []
|
|
|
|
for data in data_list:
|
|
|
|
tmp = data._asdict()
|
|
|
|
tmp.pop("id")
|
|
|
|
tmp.pop("user")
|
|
|
|
json_export[table.name].append(tmp)
|
|
|
|
|
|
|
|
return json.dumps(json_export, indent=4, default=str, ensure_ascii=False)
|
|
|
|
|
2024-01-09 19:42:17 +00:00
|
|
|
async def render_GET(self, request: Request) -> bytes:
|
2024-01-09 20:54:34 +00:00
|
|
|
uri: str = request.url.path
|
2023-10-01 01:54:23 +00:00
|
|
|
|
|
|
|
template = self.environment.get_template(
|
2024-01-09 20:54:34 +00:00
|
|
|
"titles/idac/templates/idac_index.jinja"
|
2023-10-01 01:54:23 +00:00
|
|
|
)
|
2024-01-09 20:54:34 +00:00
|
|
|
usr_sesh = self.validate_session(request)
|
|
|
|
if not usr_sesh:
|
|
|
|
usr_sesh = UserSession()
|
|
|
|
user_id = usr_sesh.user_id
|
2023-10-03 15:00:59 +00:00
|
|
|
# user_id = usr_sesh.user_id
|
2023-10-01 01:54:23 +00:00
|
|
|
|
|
|
|
# profile export
|
|
|
|
if uri.startswith("/game/idac/export"):
|
2023-10-03 15:00:59 +00:00
|
|
|
if user_id == 0:
|
2024-01-09 20:54:34 +00:00
|
|
|
return RedirectResponse(b"/game/idac", request)
|
2023-10-01 01:54:23 +00:00
|
|
|
|
|
|
|
# set the file name, content type and size to download the json
|
2024-01-09 19:42:17 +00:00
|
|
|
content = await self.generate_all_tables_json(user_id).encode("utf-8")
|
2023-10-01 01:54:23 +00:00
|
|
|
|
2023-10-03 15:00:59 +00:00
|
|
|
self.logger.info(f"User {user_id} exported their IDAC data")
|
2024-01-09 20:54:34 +00:00
|
|
|
return Response(
|
|
|
|
content,
|
|
|
|
200,
|
|
|
|
{'content-disposition': 'attachment; filename=idac_profile.json'},
|
|
|
|
"application/octet-stream"
|
|
|
|
)
|
2023-10-01 01:54:23 +00:00
|
|
|
|
|
|
|
profile_data, tickets, rank = None, None, None
|
2023-10-03 15:00:59 +00:00
|
|
|
if user_id > 0:
|
2024-01-09 19:42:17 +00:00
|
|
|
profile_data = await self.data.profile.get_profile(user_id, self.version)
|
|
|
|
ticket_data = await self.data.item.get_tickets(user_id)
|
|
|
|
rank = await self.data.profile.get_profile_rank(user_id, self.version)
|
2023-10-01 01:54:23 +00:00
|
|
|
|
2024-01-10 02:03:58 +00:00
|
|
|
if ticket_data:
|
|
|
|
tickets = {
|
|
|
|
self.ticket_names[ticket["ticket_id"]]: ticket["ticket_cnt"]
|
|
|
|
for ticket in ticket_data
|
|
|
|
}
|
2023-10-01 01:54:23 +00:00
|
|
|
|
2024-01-09 20:54:34 +00:00
|
|
|
return Response(template.render(
|
2023-10-01 01:54:23 +00:00
|
|
|
title=f"{self.core_config.server.name} | {self.nav_name}",
|
|
|
|
game_list=self.environment.globals["game_list"],
|
|
|
|
profile=profile_data,
|
|
|
|
tickets=tickets,
|
|
|
|
rank=rank,
|
|
|
|
sesh=vars(usr_sesh),
|
|
|
|
active_page="idac",
|
2024-01-13 22:15:02 +00:00
|
|
|
), media_type="text/html; charset=utf-8")
|