diff --git a/core/frontend.py b/core/frontend.py index b95662f..5e18b5d 100644 --- a/core/frontend.py +++ b/core/frontend.py @@ -169,7 +169,7 @@ class FE_Base(): ), media_type="text/html; charset=utf-8") if sesh is None: - resp.delete_cookie("DIANA_SESH") + resp.delete_cookie("ARTEMIS_SESH") return resp def get_routes(self) -> List[Route]: @@ -194,6 +194,7 @@ class FE_Base(): sesh.permissions = tk['permissions'] sesh.chunithm_version = tk['chunithm_version'] sesh.maimai_version = tk['maimai_version'] + sesh.ongeki_version = tk['ongeki_version'] if sesh.user_id <= 0: self.logger.error("User session failed to validate due to an invalid ID!") @@ -219,7 +220,7 @@ class FE_Base(): return UserSession() def validate_session(self, request: Request) -> Optional[UserSession]: - sesh = request.cookies.get('DIANA_SESH', "") + sesh = request.cookies.get('ARTEMIS_SESH', "") if not sesh: return None @@ -280,7 +281,7 @@ class FE_Gate(FE_Base): error=err, sesh=vars(UserSession()), ), media_type="text/html; charset=utf-8") - resp.delete_cookie("DIANA_SESH") + resp.delete_cookie("ARTEMIS_SESH") return resp async def render_login(self, request: Request): @@ -330,7 +331,7 @@ class FE_Gate(FE_Base): usr_sesh = self.encode_session(sesh) self.logger.debug(f"Created session with JWT {usr_sesh}") resp = RedirectResponse("/user/", 303) - resp.set_cookie("DIANA_SESH", usr_sesh) + resp.set_cookie("ARTEMIS_SESH", usr_sesh) return resp @@ -369,7 +370,7 @@ class FE_Gate(FE_Base): usr_sesh = self.encode_session(sesh) self.logger.debug(f"Created session with JWT {usr_sesh}") resp = RedirectResponse("/user/", 303) - resp.set_cookie("DIANA_SESH", usr_sesh) + resp.set_cookie("ARTEMIS_SESH", usr_sesh) return resp @@ -487,7 +488,7 @@ class FE_User(FE_Base): async def render_logout(self, request: Request): resp = RedirectResponse("/gate/", 303) - resp.delete_cookie("DIANA_SESH") + resp.delete_cookie("ARTEMIS_SESH") return resp async def edit_card(self, request: Request) -> RedirectResponse: diff --git a/titles/chuni/frontend.py b/titles/chuni/frontend.py index 724d447..68b5fce 100644 --- a/titles/chuni/frontend.py +++ b/titles/chuni/frontend.py @@ -39,12 +39,149 @@ class ChuniFrontend(FE_Base): usr_sesh = self.validate_session(request) if not usr_sesh: usr_sesh = UserSession() - - return Response(template.render( - title=f"{self.core_config.server.name} | {self.nav_name}", - game_list=self.environment.globals["game_list"], - sesh=vars(usr_sesh) - ), media_type="text/html; charset=utf-8") + + if usr_sesh.user_id > 0: + versions = await self.data.profile.get_all_profile_versions(usr_sesh.user_id) + profile = [] + if versions: + # chunithm_version is -1 means it is not initialized yet, select a default version from existing. + if usr_sesh.chunithm_version < 0: + usr_sesh.chunithm_version = versions[0] + profile = await self.data.profile.get_profile_data(usr_sesh.user_id, usr_sesh.chunithm_version) + + resp = Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name}", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + user_id=usr_sesh.user_id, + profile=profile, + version_list=ChuniConstants.VERSION_NAMES, + versions=versions, + cur_version=usr_sesh.chunithm_version + ), media_type="text/html; charset=utf-8") + + if usr_sesh.chunithm_version >= 0: + encoded_sesh = self.encode_session(usr_sesh) + resp.set_cookie("ARTEMIS_SESH", encoded_sesh) + return resp + + else: + return RedirectResponse("/gate/", 303) + + async def render_GET_rating(self, request: Request) -> bytes: + template = self.environment.get_template( + "titles/chuni/templates/chuni_rating.jinja" + ) + usr_sesh = self.validate_session(request) + if not usr_sesh: + usr_sesh = UserSession() + + if usr_sesh.user_id > 0: + if usr_sesh.chunithm_version < 0: + return RedirectResponse("/game/chuni/", 303) + profile = await self.data.profile.get_profile_data(usr_sesh.user_id, usr_sesh.chunithm_version) + rating = await self.data.profile.get_profile_rating(usr_sesh.user_id, usr_sesh.chunithm_version) + hot_list=[] + base_list=[] + if profile and rating: + song_records = [] + for song in rating: + music_chart = await self.data.static.get_music_chart(usr_sesh.chunithm_version, song.musicId, song.difficultId) + if music_chart: + if (song.score < 800000): + song_rating = 0 + elif (song.score >= 800000 and song.score < 900000): + song_rating = music_chart.level / 2 - 5 + elif (song.score >= 900000 and song.score < 925000): + song_rating = music_chart.level - 5 + elif (song.score >= 925000 and song.score < 975000): + song_rating = music_chart.level - 3 + elif (song.score >= 975000 and song.score < 1000000): + song_rating = (song.score - 975000) / 2500 * 0.1 + music_chart.level + elif (song.score >= 1000000 and song.score < 1005000): + song_rating = (song.score - 1000000) / 1000 * 0.1 + 1 + music_chart.level + elif (song.score >= 1005000 and song.score < 1007500): + song_rating = (song.score - 1005000) / 500 * 0.1 + 1.5 + music_chart.level + elif (song.score >= 1007500 and song.score < 1009000): + song_rating = (song.score - 1007500) / 100 * 0.01 + 2 + music_chart.level + elif (song.score >= 1009000): + song_rating = 2.15 + music_chart.level + song_rating = int(song_rating * 10 ** 2) / 10 ** 2 + song_records.append({ + "difficultId": song.difficultId, + "musicId": song.musicId, + "title": music_chart.title, + "level": music_chart.level, + "score": song.score, + "type": song.type, + "song_rating": song_rating, + }) + hot_list = [obj for obj in song_records if obj["type"] == "userRatingBaseHotList"] + base_list = [obj for obj in song_records if obj["type"] == "userRatingBaseList"] + return Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name}", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + profile=profile, + hot_list=hot_list, + base_list=base_list, + ), media_type="text/html; charset=utf-8") + else: + return RedirectResponse("/gate/", 303) + + async def render_GET_playlog(self, request: Request) -> bytes: + template = self.environment.get_template( + "titles/chuni/templates/chuni_playlog.jinja" + ) + usr_sesh = self.validate_session(request) + if not usr_sesh: + usr_sesh = UserSession() + + if usr_sesh.user_id > 0: + if usr_sesh.chunithm_version < 0: + return RedirectResponse("/game/chuni/", 303) + path_index = request.path_params.get('index') + if not path_index or int(path_index) < 1: + index = 0 + else: + index = int(path_index) - 1 # 0 and 1 are 1st page + user_id = usr_sesh.user_id + playlog_count = await self.data.score.get_user_playlogs_count(user_id) + if playlog_count < index * 20 : + return Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name}", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + playlog_count=0 + ), media_type="text/html; charset=utf-8") + playlog = await self.data.score.get_playlogs_limited(user_id, index, 20) + playlog_with_title = [] + for record in playlog: + music_chart = await self.data.static.get_music_chart(usr_sesh.chunithm_version, record.musicId, record.level) + if music_chart: + difficultyNum=music_chart.level + artist=music_chart.artist + title=music_chart.title + else: + difficultyNum=0 + artist="unknown" + title="musicid: " + str(record.musicId) + playlog_with_title.append({ + "raw": record, + "title": title, + "difficultyNum": difficultyNum, + "artist": artist, + }) + return Response(template.render( + title=f"{self.core_config.server.name} | {self.nav_name}", + game_list=self.environment.globals["game_list"], + sesh=vars(usr_sesh), + user_id=usr_sesh.user_id, + playlog=playlog_with_title, + playlog_count=playlog_count + ), media_type="text/html; charset=utf-8") + else: + return RedirectResponse("/gate/", 303) async def update_name(self, request: Request) -> bytes: usr_sesh = self.validate_session(request) @@ -80,4 +217,23 @@ class ChuniFrontend(FE_Base): if not await self.data.profile.update_name(usr_sesh, new_name_full): return RedirectResponse("/gate/?e=999", 303) - return RedirectResponse("/gate/?s=1", 303) \ No newline at end of file + return RedirectResponse("/game/chuni/?s=1", 303) + + async def version_change(self, request: Request): + usr_sesh = self.validate_session(request) + if not usr_sesh: + usr_sesh = UserSession() + + 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}") + 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}") + resp = RedirectResponse("/game/chuni/", 303) + resp.set_cookie("ARTEMIS_SESH", encoded_sesh) + return resp + else: + return RedirectResponse("/gate/", 303) diff --git a/titles/mai2/frontend.py b/titles/mai2/frontend.py index 6ce8828..635c2fa 100644 --- a/titles/mai2/frontend.py +++ b/titles/mai2/frontend.py @@ -67,8 +67,8 @@ class Mai2Frontend(FE_Base): if usr_sesh.maimai_version >= 0: encoded_sesh = self.encode_session(usr_sesh) - resp.delete_cookie("DIANA_SESH") - resp.set_cookie("DIANA_SESH", encoded_sesh) + resp.delete_cookie("ARTEMIS_SESH") + resp.set_cookie("ARTEMIS_SESH", encoded_sesh) return resp else: @@ -184,7 +184,7 @@ class Mai2Frontend(FE_Base): encoded_sesh = self.encode_session(usr_sesh) self.logger.info(f"Created session with JWT {encoded_sesh}") resp = RedirectResponse("/game/mai2/", 303) - resp.set_cookie("DIANA_SESH", encoded_sesh) + resp.set_cookie("ARTEMIS_SESH", encoded_sesh) return resp else: return RedirectResponse("/gate/", 303) \ No newline at end of file diff --git a/titles/ongeki/frontend.py b/titles/ongeki/frontend.py index 1cdec03..226f318 100644 --- a/titles/ongeki/frontend.py +++ b/titles/ongeki/frontend.py @@ -31,7 +31,8 @@ class OngekiFrontend(FE_Base): def get_routes(self) -> List[Route]: return [ - Route("/", self.render_GET) + Route("/", self.render_GET), + Route("/version.change", self.render_POST, methods=['POST']) ] async def render_GET(self, request: Request) -> bytes: @@ -69,29 +70,34 @@ class OngekiFrontend(FE_Base): return RedirectResponse("/gate/", 303) async def render_POST(self, request: Request): - uri = request.uri.decode() + uri = request.url.path + frm = await request.form() usr_sesh = self.validate_session(request) if not usr_sesh: usr_sesh = UserSession() if usr_sesh.user_id > 0: if uri == "/game/ongeki/rival.add": - rival_id = request.args[b"rivalUserId"][0].decode() + rival_id = frm.get("rivalUserId") 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) elif uri == "/game/ongeki/rival.delete": - rival_id = request.args[b"rivalUserId"][0].decode() + rival_id = frm.get("rivalUserId") await self.data.profile.delete_rival(usr_sesh.user_id, rival_id) # self.logger.info(f"{response}") return RedirectResponse(b"/game/ongeki/", 303) elif uri == "/game/ongeki/version.change": - ongeki_version=request.args[b"version"][0].decode() + ongeki_version=frm.get("version") if(ongeki_version.isdigit()): usr_sesh.ongeki_version=int(ongeki_version) - return RedirectResponse("/game/ongeki/", 303) + enc = self.encode_session(usr_sesh) + resp = RedirectResponse("/game/ongeki/", 303) + resp.delete_cookie('ARTEMIS_SESH') + resp.set_cookie('ARTEMIS_SESH', enc) + return resp else: Response("Something went wrong", status_code=500)