diff --git a/core/frontend.py b/core/frontend.py index bf50642..0ee2211 100644 --- a/core/frontend.py +++ b/core/frontend.py @@ -21,6 +21,7 @@ class IUserSession(Interface): userId = Attribute("User's ID") current_ip = Attribute("User's current ip address") permissions = Attribute("User's permission level") + ongeki_version = Attribute("User's selected Ongeki Version") class PermissionOffset(Enum): USER = 0 # Regular user @@ -36,6 +37,7 @@ class UserSession(object): self.userId = 0 self.current_ip = "0.0.0.0" self.permissions = 0 + self.ongeki_version = 7 class FrontendServlet(resource.Resource): diff --git a/core/frontend/index.jinja b/core/frontend/index.jinja index 7e4a1ca..3dacbe5 100644 --- a/core/frontend/index.jinja +++ b/core/frontend/index.jinja @@ -4,6 +4,7 @@ {{ title }} + diff --git a/titles/ongeki/__init__.py b/titles/ongeki/__init__.py index b887ba6..e4e0ce2 100644 --- a/titles/ongeki/__init__.py +++ b/titles/ongeki/__init__.py @@ -2,9 +2,11 @@ from titles.ongeki.index import OngekiServlet from titles.ongeki.const import OngekiConstants from titles.ongeki.database import OngekiData from titles.ongeki.read import OngekiReader +from titles.ongeki.frontend import OngekiFrontend index = OngekiServlet database = OngekiData reader = OngekiReader +frontend = OngekiFrontend game_codes = [OngekiConstants.GAME_CODE] current_schema_version = 5 diff --git a/titles/ongeki/base.py b/titles/ongeki/base.py index ace1d12..ccf24f5 100644 --- a/titles/ongeki/base.py +++ b/titles/ongeki/base.py @@ -978,35 +978,38 @@ class OngekiBase: """ Added in Bright """ - rival_list = self.data.profile.get_rivals(data["userId"]) - if rival_list is None or len(rival_list) < 1: + + rival_list = [] + user_rivals = self.data.profile.get_rivals(data["userId"]) + for rival in user_rivals: + tmp = {} + tmp["rivalUserId"] = rival[0] + rival_list.append(tmp) + + if user_rivals is None or len(rival_list) < 1: return { "userId": data["userId"], "length": 0, "userRivalList": [], } - return { "userId": data["userId"], "length": len(rival_list), - "userRivalList": rival_list._asdict(), + "userRivalList": rival_list, } - def handle_get_user_rival_data_api_reqiest(self, data: Dict) -> Dict: + def handle_get_user_rival_data_api_request(self, data: Dict) -> Dict: """ Added in Bright """ rivals = [] - for rival in data["userRivalList"]: name = self.data.profile.get_profile_name( rival["rivalUserId"], self.version ) if name is None: continue - - rivals.append({"rivalUserId": rival["rival"], "rivalUserName": name}) - + rivals.append({"rivalUserId": rival["rivalUserId"], "rivalUserName": name}) return { "userId": data["userId"], "length": len(rivals), @@ -1027,7 +1030,6 @@ class OngekiBase: for song in music["userMusicList"]: song["userRivalMusicDetailList"] = song["userMusicDetailList"] song.pop("userMusicDetailList") - return { "userId": data["userId"], "rivalUserId": rival_id, diff --git a/titles/ongeki/frontend.py b/titles/ongeki/frontend.py new file mode 100644 index 0000000..987776f --- /dev/null +++ b/titles/ongeki/frontend.py @@ -0,0 +1,87 @@ +import yaml +import jinja2 +from twisted.web.http import Request +from os import path +from twisted.web.util import redirectTo +from twisted.web.server import Session + +from core.frontend import FE_Base, IUserSession +from core.config import CoreConfig + +from titles.ongeki.config import OngekiConfig +from titles.ongeki.const import OngekiConstants +from titles.ongeki.database import OngekiData +from titles.ongeki.base import OngekiBase + + +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." + self.version_list = OngekiConstants.VERSION_NAMES + + def render_GET(self, request: Request) -> bytes: + template = self.environment.get_template( + "titles/ongeki/frontend/ongeki_index.jinja" + ) + sesh: Session = request.getSession() + usr_sesh = IUserSession(sesh) + self.version = usr_sesh.ongeki_version + if getattr(usr_sesh, "userId", 0) != 0: + profile_data =self.data.profile.get_profile_data(usr_sesh.userId, self.version) + rival_list = self.data.profile.get_rivals(usr_sesh.userId) + rival_data = { + "userRivalList": rival_list, + "userId": usr_sesh.userId + } + rival_info = OngekiBase.handle_get_user_rival_data_api_request(self, rival_data) + + return template.render( + 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"], + version_list=self.version_list, + version=self.version, + sesh=vars(usr_sesh) + ).encode("utf-16") + else: + return redirectTo(b"/gate/", request) + + def render_POST(self, request: Request): + uri = request.uri.decode() + sesh: Session = request.getSession() + usr_sesh = IUserSession(sesh) + if hasattr(usr_sesh, "userId"): + if uri == "/game/ongeki/rival.add": + rival_id = request.args[b"rivalUserId"][0].decode() + self.data.profile.put_rival(usr_sesh.userId, rival_id) + # self.logger.info(f"{usr_sesh.userId} added a rival") + return redirectTo(b"/game/ongeki/", request) + + elif uri == "/game/ongeki/rival.delete": + rival_id = request.args[b"rivalUserId"][0].decode() + self.data.profile.delete_rival(usr_sesh.userId, rival_id) + # self.logger.info(f"{response}") + return redirectTo(b"/game/ongeki/", request) + + 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) + return redirectTo(b"/game/ongeki/", request) + + else: + return b"Something went wrong" + else: + return b"User is not logged in" diff --git a/titles/ongeki/frontend/js/ongeki_scripts.js b/titles/ongeki/frontend/js/ongeki_scripts.js new file mode 100644 index 0000000..6de309b --- /dev/null +++ b/titles/ongeki/frontend/js/ongeki_scripts.js @@ -0,0 +1,24 @@ +function deleteRival(rivalUserId){ + + $(document).ready(function () { + $.post("/game/ongeki/rival.delete", + { + rivalUserId + }, + function(data,status){ + window.location.replace("/game/ongeki/") + }) + }); +} +function changeVersion(sel){ + + $(document).ready(function () { + $.post("/game/ongeki/version.change", + { + version: sel.value + }, + function(data,status){ + window.location.replace("/game/ongeki/") + }) + }); +} diff --git a/titles/ongeki/frontend/ongeki_index.jinja b/titles/ongeki/frontend/ongeki_index.jinja new file mode 100644 index 0000000..b7b5a90 --- /dev/null +++ b/titles/ongeki/frontend/ongeki_index.jinja @@ -0,0 +1,83 @@ +{% extends "core/frontend/index.jinja" %} +{% block content %} + +{% if sesh is defined and sesh["userId"] > 0 %} +
+
+
+ +
+
+

Profile

+

Version: + +

+
+
+
+
+

Name: {{ profile_data.userName if profile_data.userName is defined else "Profile not found" }}

+
+
+

ID: {{ profile_data.user if profile_data.user is defined else 'Profile not found' }}

+
+
+
+
+

Rivals

+
+
+ + + + + + + + + + {% for rival in rival_info%} + + + + + + {% endfor %} + +
IDNameDelete
{{rival.rivalUserId}}{{rival.rivalUserName}}
+
+ +
+ + +{% else %} +

Not Currently Logged In

+{% endif %} +{% endblock content %} \ No newline at end of file diff --git a/titles/ongeki/schema/profile.py b/titles/ongeki/schema/profile.py index f6eeef2..6071bad 100644 --- a/titles/ongeki/schema/profile.py +++ b/titles/ongeki/schema/profile.py @@ -3,7 +3,7 @@ from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, an from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON, BigInteger from sqlalchemy.engine.base import Connection from sqlalchemy.schema import ForeignKey -from sqlalchemy.sql import func, select +from sqlalchemy.sql import func, select, delete from sqlalchemy.engine import Row from sqlalchemy.dialects.mysql import insert @@ -269,7 +269,7 @@ class OngekiProfileData(BaseData): return None return row["userName"] - + def get_profile_preview(self, aime_id: int, version: int) -> Optional[Row]: sql = ( select([profile, option]) @@ -499,7 +499,7 @@ class OngekiProfileData(BaseData): def put_rival(self, aime_id: int, rival_id: int) -> Optional[int]: sql = insert(rival).values(user=aime_id, rivalUserId=rival_id) - conflict = sql.on_duplicate_key_update(rival=rival_id) + conflict = sql.on_duplicate_key_update(rivalUserId=rival_id) result = self.execute(conflict) if result is None: @@ -508,3 +508,10 @@ class OngekiProfileData(BaseData): ) return None return result.lastrowid + def delete_rival(self, aime_id: int, rival_id: int) -> Optional[int]: + sql = delete(rival).where(rival.c.user==aime_id, rival.c.rivalUserId==rival_id) + result = self.execute(sql) + if result is None: + self.logger.error(f"delete_rival: failed to delete! aime_id: {aime_id}, rival_id: {rival_id}") + else: + return result.rowcount \ No newline at end of file