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 className="">Full Combo</span> <span>Full Combo</span>
)} )}
{row.original.isAllJustice && ( {row.original.isAllJustice && <span>All Justice</span>}
<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,8 +14,7 @@ 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,
}, },
@ -67,11 +66,10 @@ export async function getSongsWithTitles(userId: number) {
// 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,
@ -88,23 +86,46 @@ export async function getSongsWithTitles(userId: number) {
}, },
}); });
// 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,
}; };
}); });