daphnis/app/(sharing)/[token]/[id]/actions.ts

157 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-07-23 20:15:27 +00:00
"use server";
import { artemis, daphnis } from "@/lib/prisma";
import type * as Prisma from "@prisma/client";
2024-08-21 17:34:23 +00:00
type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient;
2024-07-23 20:15:27 +00:00
type LinkSharingToken = {
2024-08-21 17:34:23 +00:00
playlogId: number;
};
2024-07-23 20:15:27 +00:00
export async function getSongsWithTitles(userId: number) {
2024-08-21 17:34:23 +00:00
try {
const songs: ChuniScorePlaylog[] =
await artemis.chuni_score_playlog.findMany({
2024-07-23 20:15:27 +00:00
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,
},
});
2024-08-21 17:34:23 +00:00
const chuniScorePlaylogMusicId = songs
.map((song) => song.musicId)
.filter((id): id is number => id !== null);
const staticMusicInfo: ChuniStaticMusic[] =
await artemis.chuni_static_music.findMany({
2024-07-23 20:15:27 +00:00
where: {
songId: {
in: chuniScorePlaylogMusicId,
},
},
select: {
songId: true,
title: true,
artist: true,
chartId: true,
level: true,
genre: true,
worldsEndTag: true,
2024-08-21 17:34:23 +00:00
jacketPath: true,
2024-07-23 20:15:27 +00:00
},
});
2024-08-21 17:34:23 +00:00
const playCounts = await artemis.chuni_score_playlog.groupBy({
by: ["musicId"],
_count: {
musicId: true,
},
where: {
user: userId,
musicId: {
in: chuniScorePlaylogMusicId,
2024-07-23 20:15:27 +00:00
},
2024-08-21 17:34:23 +00:00
},
});
const playCountMap = playCounts.reduce(
(map, item) => {
2024-07-23 20:15:27 +00:00
if (item.musicId !== null) {
map[item.musicId] = item._count.musicId;
}
return map;
2024-08-21 17:34:23 +00:00
},
{} as Record<number, number>,
);
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level,
);
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",
playCount: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0,
jacketPath: staticInfo?.jacketPath || "",
};
});
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
2024-07-23 20:15:27 +00:00
}
2024-08-21 17:34:23 +00:00
}
2024-07-23 20:15:27 +00:00
export async function generatePlaylogId(playlogid: number) {
2024-08-21 17:34:23 +00:00
try {
const tokens = (await daphnis.linkSharingToken.findMany({
where: {
playlogId: playlogid,
},
select: {
playlogId: true,
},
})) as LinkSharingToken[];
const playlogIds: number[] = tokens.map((token) => token.playlogId);
return playlogIds;
} catch (error) {
console.error("Error fetching playlogIds:", error);
throw error;
}
}