added playcount

This commit is contained in:
polaris
2024-07-01 13:42:57 -04:00
parent 7be756270e
commit 8be325d845
2 changed files with 105 additions and 83 deletions

View File

@ -20,7 +20,10 @@ import { generateShareToken } from "@/app/(sharing)/[token]/token";
import { chuni_score_playlog } from "@/prisma/schemas/artemis/generated/artemis"; import { chuni_score_playlog } from "@/prisma/schemas/artemis/generated/artemis";
import { chuni_static_music } from "@/prisma/schemas/artemis/generated/artemis"; import { chuni_static_music } from "@/prisma/schemas/artemis/generated/artemis";
export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [ type includesPlayCount = chuni_score_playlog &
chuni_static_music & { playCount: number };
export const columns: ColumnDef<includesPlayCount>[] = [
{ {
accessorKey: "title", accessorKey: "title",
header: "Title", header: "Title",
@ -47,14 +50,10 @@ export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [
cell: ({ row }) => ( cell: ({ row }) => (
<div> <div>
<div className="font-medium"> <div className="font-medium">
<span className="space-x-2 pl-2"> {!row.original.isAllJustice && row.original.isFullCombo && (
{!row.original.isAllJustice && row.original.isFullCombo && ( <span>Full Combo</span>
<span className="">Full Combo</span> )}
)} {row.original.isAllJustice && <span>All Justice</span>}
{row.original.isAllJustice && (
<span className=" ">All Justice</span>
)}
</span>
</div> </div>
</div> </div>
), ),
@ -62,7 +61,9 @@ export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [
{ {
accessorKey: "Attempts", accessorKey: "Attempts",
header: "Attempts", header: "Attempts",
cell: ({ row }) => <div className="font-medium">{row.original.title}</div>, cell: ({ row }) => (
<div className="font-medium">{row.original.playCount}</div>
),
}, },
{ {
id: "actions", id: "actions",

View File

@ -14,97 +14,118 @@ type LinkSharingToken = {
export async function getSongsWithTitles(userId: number) { export async function getSongsWithTitles(userId: number) {
try { try {
// Fetch songs played by the user // Fetch songs played by the user
const songs: ChuniScorePlaylog[] = const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({
await artemis.chuni_score_playlog.findMany({ where: {
where: { user: userId,
user: userId, },
}, orderBy: {
orderBy: { userPlayDate: "desc",
userPlayDate: "desc", },
}, select: {
select: { id: true,
id: true, maxCombo: true,
maxCombo: true, userPlayDate: true,
userPlayDate: true, isFullCombo: true,
isFullCombo: true, playerRating: true,
playerRating: true, isAllJustice: true,
isAllJustice: true, score: true,
score: true, judgeHeaven: true,
judgeHeaven: true, judgeGuilty: true,
judgeGuilty: true, judgeJustice: true,
judgeJustice: true, judgeAttack: true,
judgeAttack: true, judgeCritical: true,
judgeCritical: true, isClear: true,
isClear: true, skillId: true,
skillId: true, skillEffect: true,
skillEffect: true, skillLevel: true,
skillLevel: true, isNewRecord: true,
isNewRecord: true, musicId: true,
musicId: true, level: true,
level: true, rateAir: true,
rateAir: true, rateHold: true,
rateHold: true, rateFlick: true,
rateFlick: true, rateSlide: true,
rateSlide: true, rateTap: true,
rateTap: true, romVersion: true,
romVersion: true, eventId: true,
eventId: true, characterId: true,
characterId: true, charaIllustId: true,
charaIllustId: true, track: true,
track: true, isContinue: true,
isContinue: true, isFreeToPlay: true,
isFreeToPlay: true, playKind: true,
playKind: true, playDate: true,
playDate: true, orderId: true,
orderId: true, sortNumber: true,
sortNumber: true, user: true,
user: true, placeId: true,
placeId: true, ticketId: true,
ticketId: true, },
}, });
});
// Extract unique musicIds from the fetched songs // Extract unique musicIds from the fetched songs
const chuniScorePlaylogMusicId = songs const chuniScorePlaylogMusicId = songs
.map((song) => song.musicId) .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 // Fetch static music information for the extracted musicIds
const staticMusicInfo: ChuniStaticMusic[] = const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({
await artemis.chuni_static_music.findMany({ where: {
where: { songId: {
songId: { in: chuniScorePlaylogMusicId,
in: chuniScorePlaylogMusicId,
},
}, },
select: { },
songId: true, select: {
title: true, songId: true,
artist: true, title: true,
chartId: true, artist: true,
level: true, chartId: true,
genre: true, level: true,
worldsEndTag: 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<number, number>);
// Map each song with its corresponding static music information and play count
const songsWithTitles = songs.map((song) => { const songsWithTitles = songs.map((song) => {
// Find static info corresponding to the song's musicId and level (chartId) // Find static info corresponding to the song's musicId and level (chartId)
const staticInfo = staticMusicInfo.find( const staticInfo = staticMusicInfo.find(
(chuniStatiMusic) => (chuniStaticMusic) =>
chuniStatiMusic.songId === song.musicId && chuniStaticMusic.songId === song.musicId &&
chuniStatiMusic.chartId === song.level 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 { return {
...song, ...song,
title: staticInfo?.title || "Unknown Title", title: staticInfo?.title || "Unknown Title",
artist: staticInfo?.artist || "Unknown Artist", artist: staticInfo?.artist || "Unknown Artist",
genre: staticInfo?.genre || "Unknown Genre", genre: staticInfo?.genre || "Unknown Genre",
level: staticInfo?.level || "Unknown Level", level: staticInfo?.level || "Unknown Level",
chartlevel: song.level || "unkownlevel", chartlevel: song.level || "Unknown Level",
playCount: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0,
}; };
}); });