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_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",
header: "Title",
@ -47,14 +50,10 @@ export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [
cell: ({ row }) => (
<div>
<div className="font-medium">
<span className="space-x-2 pl-2">
{!row.original.isAllJustice && row.original.isFullCombo && (
<span className="">Full Combo</span>
<span>Full Combo</span>
)}
{row.original.isAllJustice && (
<span className=" ">All Justice</span>
)}
</span>
{row.original.isAllJustice && <span>All Justice</span>}
</div>
</div>
),
@ -62,7 +61,9 @@ export const columns: ColumnDef<chuni_score_playlog & chuni_static_music>[] = [
{
accessorKey: "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",

View File

@ -14,8 +14,7 @@ type LinkSharingToken = {
export async function getSongsWithTitles(userId: number) {
try {
// Fetch songs played by the user
const songs: ChuniScorePlaylog[] =
await artemis.chuni_score_playlog.findMany({
const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({
where: {
user: userId,
},
@ -67,11 +66,10 @@ export async function getSongsWithTitles(userId: number) {
// 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({
// Fetch static music information for the extracted musicIds
const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({
where: {
songId: {
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) => {
// 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,
};
});