From c2c25149b3026c3aae5ea6a648baedfe362f7688 Mon Sep 17 00:00:00 2001 From: NeumPhis <205-NeumPhis@users.noreply.dev.s-ul.eu> Date: Wed, 27 Nov 2019 01:49:05 +0000 Subject: [PATCH] chuni: Add recent rating response to fix rating calculation --- src/chunithm/handler/getUserRecentRating.ts | 12 +++++++++--- src/chunithm/proto/userRecentRating.ts | 13 +++++++++++++ src/chunithm/repo/userPlaylog.ts | 1 + src/chunithm/sql/userPlaylog.ts | 14 +++++++++++++- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/chunithm/handler/getUserRecentRating.ts b/src/chunithm/handler/getUserRecentRating.ts index df6a5ab..e2a3d86 100644 --- a/src/chunithm/handler/getUserRecentRating.ts +++ b/src/chunithm/handler/getUserRecentRating.ts @@ -1,16 +1,22 @@ import { Repositories } from "../repo"; import { GetUserRecentRatingRequest } from "../request/getUserRecentRating"; import { GetUserRecentRatingResponse } from "../response/getUserRecentRating"; +import { readAimeId } from "../proto/base"; +import { writeUserRecentRatingFromLog } from "../proto/userRecentRating"; export default async function getUserRecentRating( rep: Repositories, req: GetUserRecentRatingRequest ): Promise { - // I don't really know what subset of data to return here. + const aimeId = readAimeId(req.userId); + + const profileId = await rep.userData().lookup(aimeId); + // Return recent 30 plays to calculate rating + const items = await rep.userPlaylog().loadLatest(profileId, 30); return { userId: req.userId, - length: "0", - userRecentRatingList: [], + length: items.length.toString(), + userRecentRatingList: items.map(writeUserRecentRatingFromLog), }; } diff --git a/src/chunithm/proto/userRecentRating.ts b/src/chunithm/proto/userRecentRating.ts index e818f1f..e55303f 100644 --- a/src/chunithm/proto/userRecentRating.ts +++ b/src/chunithm/proto/userRecentRating.ts @@ -1,3 +1,4 @@ +import { UserPlaylogItem } from "./../model/userPlaylog"; import { Crush, writeObject } from "./base"; import { UserRecentRatingItem } from "../model/userRecentRating"; @@ -19,3 +20,15 @@ export function writeUserRecentRating( ): UserRecentRatingJson { return writeObject(obj); } + +export function writeUserRecentRatingFromLog( + obj: UserPlaylogItem +): UserRecentRatingJson { + return { + musicId: obj.musicId.toString(), + difficultId: obj.level.toString(), + // game version not saved in play log, just return a fixed version now + romVersionCode: "1030000", + score: obj.score.toString(), + }; +} diff --git a/src/chunithm/repo/userPlaylog.ts b/src/chunithm/repo/userPlaylog.ts index 60c7e48..6053227 100644 --- a/src/chunithm/repo/userPlaylog.ts +++ b/src/chunithm/repo/userPlaylog.ts @@ -7,4 +7,5 @@ export interface UserPlaylogRepository { // is nice to have. save(profileId: Id, obj: UserPlaylogItem): Promise; + loadLatest(profileId: Id, size: number): Promise; } diff --git a/src/chunithm/sql/userPlaylog.ts b/src/chunithm/sql/userPlaylog.ts index c377701..cb1c285 100644 --- a/src/chunithm/sql/userPlaylog.ts +++ b/src/chunithm/sql/userPlaylog.ts @@ -7,7 +7,7 @@ import { UserPlaylogRepository } from "../repo/userPlaylog"; import { Transaction } from "../../sql"; import { T, createSqlMapper } from "../../sql/util"; -const { writeRow } = createSqlMapper({ +const { readRow, writeRow } = createSqlMapper({ orderId: T.number, sortNumber: T.number, placeId: T.number, @@ -72,4 +72,16 @@ export class SqlUserPlaylogRepository implements UserPlaylogRepository { return this._txn.modify(stmt); } + + async loadLatest(profileId: Id, size: number): Promise { + const stmt = sql + .select("music_id", "score", "level","user_play_date") + .from("cm_user_playlog") + .where("profile_id", profileId) + .limit(size).order("user_play_date DESC"); + + const rows = await this._txn.fetchRows(stmt); + + return rows.map(readRow); + } }