From 9b5283d389ccf1eea5f7f1b5fcea785f779fa32c Mon Sep 17 00:00:00 2001 From: Hay1tsme Date: Mon, 1 Jul 2024 18:26:39 -0400 Subject: [PATCH] mai2: add event editing for sysops --- titles/chuni/frontend.py | 4 +- titles/mai2/frontend.py | 127 +++++++++++++- titles/mai2/schema/static.py | 16 ++ .../templates/events/mai2_event_edit.jinja | 16 ++ .../mai2/templates/events/mai2_events.jinja | 156 ++++++++++++++++++ titles/mai2/templates/mai2_header.jinja | 7 +- 6 files changed, 315 insertions(+), 11 deletions(-) create mode 100644 titles/mai2/templates/events/mai2_event_edit.jinja create mode 100644 titles/mai2/templates/events/mai2_events.jinja diff --git a/titles/chuni/frontend.py b/titles/chuni/frontend.py index 0dbefac..510ae08 100644 --- a/titles/chuni/frontend.py +++ b/titles/chuni/frontend.py @@ -234,11 +234,11 @@ class ChuniFrontend(FE_Base): if usr_sesh.user_id > 0: form_data = await request.form() chunithm_version = form_data.get("version") - self.logger.info(f"version change to: {chunithm_version}") + self.logger.debug(f"version change to: {chunithm_version}") if(chunithm_version.isdigit()): usr_sesh.chunithm_version=int(chunithm_version) encoded_sesh = self.encode_session(usr_sesh) - self.logger.info(f"Created session with JWT {encoded_sesh}") + self.logger.debug(f"Created session with JWT {encoded_sesh}") resp = RedirectResponse("/game/chuni/", 303) resp.set_cookie("ARTEMIS_SESH", encoded_sesh) return resp diff --git a/titles/mai2/frontend.py b/titles/mai2/frontend.py index 635c2fa..976e2c4 100644 --- a/titles/mai2/frontend.py +++ b/titles/mai2/frontend.py @@ -5,8 +5,9 @@ from starlette.responses import Response, RedirectResponse from os import path import yaml import jinja2 +from datetime import datetime -from core.frontend import FE_Base, UserSession +from core.frontend import FE_Base, UserSession, PermissionOffset from core.config import CoreConfig from .database import Mai2Data from .config import Mai2Config @@ -32,6 +33,12 @@ class Mai2Frontend(FE_Base): Route("/", self.render_GET_playlog, methods=['GET']), Route("/{index}", self.render_GET_playlog, methods=['GET']), ]), + Mount("/events", routes=[ + Route("/", self.render_events, methods=['GET']), + Route("/{event_id:int}", self.render_event_edit, methods=['GET']), + Route("/update", self.update_event, methods=['POST']), + Route("/version.change", self.version_change, methods=['POST']), + ]), Route("/update.name", self.update_name, methods=['POST']), Route("/version.change", self.version_change, methods=['POST']), ] @@ -43,13 +50,15 @@ class Mai2Frontend(FE_Base): usr_sesh = self.validate_session(request) if not usr_sesh: usr_sesh = UserSession() + + incoming_ver = usr_sesh.maimai_version if usr_sesh.user_id > 0: versions = await self.data.profile.get_all_profile_versions(usr_sesh.user_id) profile = [] if versions: # maimai_version is -1 means it is not initialized yet, select a default version from existing. - if usr_sesh.maimai_version < 0: + if incoming_ver < 0: usr_sesh.maimai_version = versions[0]['version'] profile = await self.data.profile.get_profile_detail(usr_sesh.user_id, usr_sesh.maimai_version) versions = [x['version'] for x in versions] @@ -65,7 +74,7 @@ class Mai2Frontend(FE_Base): cur_version=usr_sesh.maimai_version ), media_type="text/html; charset=utf-8") - if usr_sesh.maimai_version >= 0: + if incoming_ver < 0: encoded_sesh = self.encode_session(usr_sesh) resp.delete_cookie("ARTEMIS_SESH") resp.set_cookie("ARTEMIS_SESH", encoded_sesh) @@ -80,12 +89,10 @@ class Mai2Frontend(FE_Base): ) usr_sesh = self.validate_session(request) if not usr_sesh: - print("wtf") usr_sesh = UserSession() if usr_sesh.user_id > 0: if usr_sesh.maimai_version < 0: - print(usr_sesh.maimai_version) return RedirectResponse("/game/mai2/", 303) path_index = request.path_params.get('index') if not path_index or int(path_index) < 1: @@ -175,6 +182,11 @@ class Mai2Frontend(FE_Base): if not usr_sesh: usr_sesh = UserSession() + if "/events/" in request.url.path: + resp = RedirectResponse("/game/mai2/events/", 303) + else: + resp = RedirectResponse("/game/mai2/", 303) + if usr_sesh.user_id > 0: form_data = await request.form() maimai_version = form_data.get("version") @@ -182,9 +194,108 @@ class Mai2Frontend(FE_Base): if(maimai_version.isdigit()): usr_sesh.maimai_version=int(maimai_version) encoded_sesh = self.encode_session(usr_sesh) - self.logger.info(f"Created session with JWT {encoded_sesh}") - resp = RedirectResponse("/game/mai2/", 303) + self.logger.debug(f"Created session with JWT {encoded_sesh}") resp.set_cookie("ARTEMIS_SESH", encoded_sesh) return resp else: - return RedirectResponse("/gate/", 303) \ No newline at end of file + return RedirectResponse("/gate/", 303) + + async def render_events(self, request: Request) -> Response: + usr_sesh = self.validate_session(request) + if not usr_sesh: + return RedirectResponse("/gate/", 303) + + if not self.test_perm(usr_sesh.permissions, PermissionOffset.SYSADMIN): + return RedirectResponse("/game/mai2/", 303) + + template = self.environment.get_template( + "titles/mai2/templates/events/mai2_events.jinja" + ) + + incoming_ver = usr_sesh.maimai_version + evts = [] + + if incoming_ver < 0: + usr_sesh.maimai_version = Mai2Constants.VER_MAIMAI_DX + + event_list = await self.data.static.get_game_events(usr_sesh.maimai_version) + self.logger.info(f"Get events for v{usr_sesh.maimai_version}") + + for event in event_list: + evts.append({ + "id": event['id'], + "version": event['version'], + "eventId": event['eventId'], + "eventType": event['type'], + "name": event['name'], + "startDate": event['startDate'].strftime("%x %X"), + "enabled": "true" if event['enabled'] else "false", + }) + + resp = Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name} Events", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + version_list=Mai2Constants.VERSION_STRING, + events=evts + ), media_type="text/html; charset=utf-8") + + if incoming_ver < 0: + encoded_sesh = self.encode_session(usr_sesh) + resp.delete_cookie("ARTEMIS_SESH") + resp.set_cookie("ARTEMIS_SESH", encoded_sesh) + + return resp + + async def render_event_edit(self, request: Request) -> Response: + usr_sesh = self.validate_session(request) + if not usr_sesh: + return RedirectResponse("/gate/", 303) + + if not self.test_perm(usr_sesh.permissions, PermissionOffset.SYSADMIN): + return RedirectResponse("/game/mai2/", 303) + + template = self.environment.get_template( + "titles/mai2/templates/events/mai2_event_edit.jinja" + ) + + evt_id = request.path_params.get("event_id") + + event_id = await self.data.static.get_event_by_id(evt_id) + if not event_id: + return RedirectResponse("/game/mai2/events/", 303) + + return Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name} Edit Event {evt_id}", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + user_id=usr_sesh.user_id, + version_list=Mai2Constants.VERSION_STRING, + cur_version=usr_sesh.maimai_version, + event=event_id._asdict() + ), media_type="text/html; charset=utf-8") + + async def update_event(self, request: Request) -> RedirectResponse: + usr_sesh = self.validate_session(request) + if not usr_sesh: + return RedirectResponse("/gate/", 303) + + if not self.test_perm(usr_sesh.permissions, PermissionOffset.SYSADMIN): + return RedirectResponse("/game/mai2/", 303) + + form_data = await request.form() + print(form_data) + event_id: int = form_data.get("evtId", None) + new_enabled: bool = bool(form_data.get("evtEnabled", False)) + try: + new_start_date: datetime = datetime.strptime(form_data.get("evtStart", None), "%Y-%m-%dT%H:%M:%S") + except: + new_start_date = None + + print(f"{event_id} {new_enabled} {new_start_date}") + if event_id is None or new_start_date is None: + return RedirectResponse("/game/mai2/events/?e=4", 303) + + await self.data.static.update_event_by_id(int(event_id), new_enabled, new_start_date) + + return RedirectResponse("/game/mai2/events/?s=1", 303) diff --git a/titles/mai2/schema/static.py b/titles/mai2/schema/static.py index e33e4ec..ddba0f8 100644 --- a/titles/mai2/schema/static.py +++ b/titles/mai2/schema/static.py @@ -7,6 +7,7 @@ from sqlalchemy.schema import ForeignKey from sqlalchemy.sql import func, select from sqlalchemy.engine import Row from sqlalchemy.dialects.mysql import insert +from datetime import datetime event = Table( "mai2_static_event", @@ -248,3 +249,18 @@ class Mai2StaticData(BaseData): if result is None: return None return result.fetchall() + + async def get_event_by_id(self, table_id: int) -> Optional[Row]: + result = await self.execute(event.select(event.c.id == table_id)) + if result: + return result.fetchone() + + async def get_events_by_event_id(self, event_id: int) -> Optional[List[Row]]: + result = await self.execute(event.select(event.c.eventId == event_id)) + if result: + return result.fetchall() + + async def update_event_by_id(self, table_id: int, is_enable: bool, start_date: datetime) -> None: + result = await self.execute(event.update(event.c.id == table_id).values(enabled=is_enable, startDate = start_date)) + if not result: + self.logger.error(f"Failed to update event {table_id} - {is_enable} {start_date}") diff --git a/titles/mai2/templates/events/mai2_event_edit.jinja b/titles/mai2/templates/events/mai2_event_edit.jinja new file mode 100644 index 0000000..df061f5 --- /dev/null +++ b/titles/mai2/templates/events/mai2_event_edit.jinja @@ -0,0 +1,16 @@ +{% extends "core/templates/index.jinja" %} +{% block content %} + +
+

Event {{ event.eventId }} for {{ version_list[event.version] }}: {{ event.name }}

+ +
+

+ +
+

+ +   + +
+{% endblock content %} diff --git a/titles/mai2/templates/events/mai2_events.jinja b/titles/mai2/templates/events/mai2_events.jinja new file mode 100644 index 0000000..a2e9325 --- /dev/null +++ b/titles/mai2/templates/events/mai2_events.jinja @@ -0,0 +1,156 @@ +{% extends "core/templates/index.jinja" %} +{% block content %} +

Events

+
+ +
+ + + + + + + + + + + + + + + {% if events is not defined or events|length == 0 %} + + + + {% endif %} +
Viewing all events
IDVersionEvent IDEvent TypeNameStart DateEnabledActions
No Events
+
+ +  + + +
+ +{% endblock content %} \ No newline at end of file diff --git a/titles/mai2/templates/mai2_header.jinja b/titles/mai2/templates/mai2_header.jinja index 4a4cb86..f226fbe 100644 --- a/titles/mai2/templates/mai2_header.jinja +++ b/titles/mai2/templates/mai2_header.jinja @@ -3,6 +3,9 @@ \ No newline at end of file