daphnis/components/scoreplaylog/action.ts

220 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-08-21 17:34:23 +00:00
"use server";
import { artemis } from "@/lib/prisma";
import { chuni_score_best } from "@/prisma/schemas/artemis/generated/artemis";
2024-08-21 17:34:23 +00:00
import type * as Prisma from "@prisma/client";
type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient;
export async function getSongsWithTitles(userId: number) {
try {
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 chuniScorePlaylogMusicId = songs
.map((song) => song.musicId)
.filter((id): id is number => id !== null);
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,
jacketPath: true,
},
});
2024-08-25 04:08:45 +00:00
const playCounts = await artemis.chuni_score_playlog.groupBy({
by: ["musicId", "level"],
where: {
musicId: { in: chuniScorePlaylogMusicId },
user: userId,
},
_count: {
_all: true,
},
});
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level,
);
2024-08-25 04:08:45 +00:00
const playCount =
playCounts.find(
(play) => play.musicId === song.musicId && play.level === song.level,
)?._count._all || 0;
return {
...song,
title: staticInfo?.title || "Unknown Title",
artist: staticInfo?.artist || "Unknown Artist",
genre: staticInfo?.genre || "Unknown Genre",
chartId: staticInfo?.chartId || "Unknown chartId",
level: staticInfo?.level || "Unknown Level",
chartlevel: song.level || "Unknown Level",
2024-08-25 04:08:45 +00:00
playCount,
jacketPath: staticInfo?.jacketPath || "",
};
});
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}
2024-08-21 17:34:23 +00:00
export async function searchSongWithTitle(
userId: number,
searchQuery: string = "",
) {
try {
const songs: ChuniScorePlaylog[] =
await artemis.chuni_score_playlog.findMany({
where: {
user: userId,
},
orderBy: {
userPlayDate: "desc",
},
select: {
id: true,
},
});
const chuniScorePlaylogMusicId = songs
.map((song) => song.musicId)
.filter((id): id is number => id !== null);
const staticMusicInfo: ChuniStaticMusic[] =
await artemis.chuni_static_music.findMany({
where: {
songId: {
in: chuniScorePlaylogMusicId,
},
title: {
contains: searchQuery,
},
},
select: {
songId: true,
2024-08-21 17:34:23 +00:00
title: true,
},
});
const chuniScoreBest: chuni_score_best[] =
await artemis.chuni_score_best.findMany({
where: {
musicId: {
in: chuniScorePlaylogMusicId,
},
2024-08-21 17:34:23 +00:00
},
select: {
id: true,
user: true,
musicId: true,
level: true,
playCount: true,
scoreMax: true,
resRequestCount: true,
resAcceptCount: true,
resSuccessCount: true,
missCount: true,
maxComboCount: true,
isFullCombo: true,
isAllJustice: true,
isSuccess: true,
fullChain: true,
maxChain: true,
scoreRank: true,
isLock: true,
ext1: true,
theoryCount: true,
},
});
2024-08-21 17:34:23 +00:00
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level,
);
const bestScore = chuniScoreBest.find(
(score) => score.musicId === song.musicId,
);
2024-08-21 17:34:23 +00:00
return {
...song,
title: staticInfo?.title || "Unknown Title",
playCount: bestScore?.playCount || 0,
2024-08-21 17:34:23 +00:00
};
});
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}