From 8be325d845b289eb1b7118aa080412987444f550 Mon Sep 17 00:00:00 2001 From: polaris Date: Mon, 1 Jul 2024 13:42:57 -0400 Subject: [PATCH] added playcount --- components/scoreplaylog/colums.tsx | 21 ++-- lib/api.ts | 167 ++++++++++++++++------------- 2 files changed, 105 insertions(+), 83 deletions(-) diff --git a/components/scoreplaylog/colums.tsx b/components/scoreplaylog/colums.tsx index 1d1ed3a..50a97bb 100644 --- a/components/scoreplaylog/colums.tsx +++ b/components/scoreplaylog/colums.tsx @@ -20,7 +20,10 @@ import { generateShareToken } from "@/app/(sharing)/[token]/token"; import { chuni_score_playlog } from "@/prisma/schemas/artemis/generated/artemis"; import { chuni_static_music } from "@/prisma/schemas/artemis/generated/artemis"; -export const columns: ColumnDef[] = [ +type includesPlayCount = chuni_score_playlog & + chuni_static_music & { playCount: number }; + +export const columns: ColumnDef[] = [ { accessorKey: "title", header: "Title", @@ -47,14 +50,10 @@ export const columns: ColumnDef[] = [ cell: ({ row }) => (
- - {!row.original.isAllJustice && row.original.isFullCombo && ( - Full Combo - )} - {row.original.isAllJustice && ( - All Justice - )} - + {!row.original.isAllJustice && row.original.isFullCombo && ( + Full Combo + )} + {row.original.isAllJustice && All Justice}
), @@ -62,7 +61,9 @@ export const columns: ColumnDef[] = [ { accessorKey: "Attempts", header: "Attempts", - cell: ({ row }) =>
{row.original.title}
, + cell: ({ row }) => ( +
{row.original.playCount}
+ ), }, { id: "actions", diff --git a/lib/api.ts b/lib/api.ts index 5b61999..8cdaefb 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -14,97 +14,118 @@ type LinkSharingToken = { export async function getSongsWithTitles(userId: number) { try { // Fetch songs played by the user - const songs: ChuniScorePlaylog[] = - await artemis.chuni_score_playlog.findMany({ - where: { - user: userId, - }, - orderBy: { - userPlayDate: "desc", - }, - select: { - id: true, - maxCombo: true, - userPlayDate: true, - isFullCombo: true, - playerRating: true, - isAllJustice: true, - score: true, - judgeHeaven: true, - judgeGuilty: true, - judgeJustice: true, - judgeAttack: true, - judgeCritical: true, - isClear: true, - skillId: true, - skillEffect: true, - skillLevel: true, - isNewRecord: true, - musicId: true, - level: true, - rateAir: true, - rateHold: true, - rateFlick: true, - rateSlide: true, - rateTap: true, - romVersion: true, - eventId: true, - characterId: true, - charaIllustId: true, - track: true, - isContinue: true, - isFreeToPlay: true, - playKind: true, - playDate: true, - orderId: true, - sortNumber: true, - user: true, - placeId: true, - ticketId: true, - }, - }); + const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({ + where: { + user: userId, + }, + orderBy: { + userPlayDate: "desc", + }, + select: { + id: true, + maxCombo: true, + userPlayDate: true, + isFullCombo: true, + playerRating: true, + isAllJustice: true, + score: true, + judgeHeaven: true, + judgeGuilty: true, + judgeJustice: true, + judgeAttack: true, + judgeCritical: true, + isClear: true, + skillId: true, + skillEffect: true, + skillLevel: true, + isNewRecord: true, + musicId: true, + level: true, + rateAir: true, + rateHold: true, + rateFlick: true, + rateSlide: true, + rateTap: true, + romVersion: true, + eventId: true, + characterId: true, + charaIllustId: true, + track: true, + isContinue: true, + isFreeToPlay: true, + playKind: true, + playDate: true, + orderId: true, + sortNumber: true, + user: true, + placeId: true, + ticketId: true, + }, + }); // Extract unique musicIds from the fetched songs const chuniScorePlaylogMusicId = songs .map((song) => song.musicId) - .filter((id) => id !== null) as number[]; + .filter((id): id is number => id !== null); - // Fetch static music information for the extrcted musicIds - const staticMusicInfo: ChuniStaticMusic[] = - await artemis.chuni_static_music.findMany({ - where: { - songId: { - in: chuniScorePlaylogMusicId, - }, + // Fetch static music information for the extracted musicIds + const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({ + where: { + songId: { + in: chuniScorePlaylogMusicId, }, - select: { - songId: true, - title: true, - artist: true, - chartId: true, - level: true, - genre: true, - worldsEndTag: true, - }, - }); + }, + select: { + songId: true, + title: true, + artist: true, + chartId: true, + level: true, + genre: true, + worldsEndTag: true, + }, + }); - // Map each song with its corresponding static music information + // Fetch play count for each musicId specific to the user + const playCounts = await artemis.chuni_score_playlog.groupBy({ + by: ['musicId'], + _count: { + musicId: true, + }, + where: { + user: userId, + musicId: { + in: chuniScorePlaylogMusicId, + }, + }, + }); + + // Create a map of musicId to play count + const playCountMap = playCounts.reduce((map, item) => { + if (item.musicId !== null) { + map[item.musicId] = item._count.musicId; + } + return map; + }, {} as Record); + + // Map each song with its corresponding static music information and play count const songsWithTitles = songs.map((song) => { // Find static info corresponding to the song's musicId and level (chartId) const staticInfo = staticMusicInfo.find( - (chuniStatiMusic) => - chuniStatiMusic.songId === song.musicId && - chuniStatiMusic.chartId === song.level + (chuniStaticMusic) => + chuniStaticMusic.songId === song.musicId && + chuniStaticMusic.chartId === song.level ); - // Return mapped song with title, artist, genre, and correct level + // Return mapped song with title, artist, genre, correct level, and play count return { ...song, title: staticInfo?.title || "Unknown Title", artist: staticInfo?.artist || "Unknown Artist", genre: staticInfo?.genre || "Unknown Genre", level: staticInfo?.level || "Unknown Level", - chartlevel: song.level || "unkownlevel", + chartlevel: song.level || "Unknown Level", + playCount: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0, }; });