daphnis/lib/api.ts

222 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-06-29 05:22:22 +00:00
"use server";
import { getAuth } from "@/auth/queries/getauth";
2024-06-29 07:02:21 +00:00
import { artemis, daphnis } from "@/lib/prisma";
2024-06-29 21:49:55 +00:00
import type * as Prisma from "@prisma/client";
2024-06-29 06:37:50 +00:00
2024-06-29 07:02:21 +00:00
type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient;
2024-06-29 21:49:55 +00:00
2024-06-29 07:02:21 +00:00
type LinkSharingToken = {
playlogId: number;
};
2024-06-29 05:22:22 +00:00
export async function getSongsWithTitles(userId: number) {
try {
2024-07-01 17:42:57 +00:00
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,
},
});
2024-06-29 05:22:22 +00:00
2024-06-29 21:52:19 +00:00
const chuniScorePlaylogMusicId = songs
2024-06-29 05:22:22 +00:00
.map((song) => song.musicId)
2024-07-01 17:42:57 +00:00
.filter((id): id is number => id !== null);
2024-06-29 05:22:22 +00:00
2024-07-01 17:42:57 +00:00
const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({
where: {
songId: {
in: chuniScorePlaylogMusicId,
2024-06-29 05:22:22 +00:00
},
2024-07-01 17:42:57 +00:00
},
select: {
songId: true,
title: true,
artist: true,
chartId: true,
level: true,
genre: true,
worldsEndTag: true,
2024-07-01 18:19:20 +00:00
jacketPath: true, // Add jacketPath to the selection
2024-07-01 17:42:57 +00:00
},
});
const playCounts = await artemis.chuni_score_playlog.groupBy({
by: ['musicId'],
_count: {
musicId: true,
},
where: {
user: userId,
musicId: {
in: chuniScorePlaylogMusicId,
2024-06-29 21:49:55 +00:00
},
2024-07-01 17:42:57 +00:00
},
});
const playCountMap = playCounts.reduce((map, item) => {
if (item.musicId !== null) {
map[item.musicId] = item._count.musicId;
}
return map;
}, {} as Record<number, number>);
2024-06-29 05:22:22 +00:00
2024-06-29 21:49:55 +00:00
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
2024-07-01 17:42:57 +00:00
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level
2024-06-29 21:49:55 +00:00
);
return {
...song,
title: staticInfo?.title || "Unknown Title",
artist: staticInfo?.artist || "Unknown Artist",
genre: staticInfo?.genre || "Unknown Genre",
level: staticInfo?.level || "Unknown Level",
2024-07-01 17:42:57 +00:00
chartlevel: song.level || "Unknown Level",
playCount: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0,
2024-07-01 18:19:20 +00:00
jacketPath: staticInfo?.jacketPath || "",
2024-06-29 21:49:55 +00:00
};
});
2024-06-29 05:22:22 +00:00
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}
2024-07-01 18:19:20 +00:00
2024-06-29 07:12:29 +00:00
export async function generatePlaylogId(playlogid: number) {
2024-06-29 05:22:22 +00:00
try {
2024-06-29 21:49:55 +00:00
const tokens = (await daphnis.linkSharingToken.findMany({
2024-06-29 05:22:22 +00:00
where: {
playlogId: playlogid,
},
select: {
playlogId: true,
},
2024-06-29 21:49:55 +00:00
})) as LinkSharingToken[];
2024-06-29 05:22:22 +00:00
2024-06-29 07:02:21 +00:00
const playlogIds: number[] = tokens.map((token) => token.playlogId);
2024-06-29 05:22:22 +00:00
2024-06-29 07:02:21 +00:00
return playlogIds;
2024-06-29 05:22:22 +00:00
} catch (error) {
console.error("Error fetching playlogIds:", error);
throw error;
}
}
export const getUsername = async () => {
2024-06-29 07:02:21 +00:00
const { user } = await getAuth();
2024-06-29 05:22:22 +00:00
if (user) {
2024-06-29 06:37:50 +00:00
return await daphnis.user.findFirst({
2024-06-29 05:22:22 +00:00
where: {
id: user.id,
username: user.username,
},
});
}
2024-06-29 07:02:21 +00:00
return null;
2024-06-29 05:22:22 +00:00
};
export async function getAllAimeCards() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
2024-06-29 06:37:50 +00:00
const aimeUser = await daphnis.user.findMany({
2024-06-29 05:22:22 +00:00
where: {
accessCode: user.accessCode,
},
});
return aimeUser;
}
2024-07-21 17:24:31 +00:00
export async function getAvatarParts(avatarAccessoryId: number) {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const avatarParts = await artemis.chuni_static_avatar.findMany({
where: {
avatarAccessoryId: avatarAccessoryId,
},
select: {
name: true,
category: true,
version: true,
iconPath: true,
texturePath: true,
}
});
return avatarParts;
}
2024-06-29 05:22:22 +00:00
export async function verifyAimeCodeAgainstArtemis() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const aimeUser = await artemis.aime_card.findFirst({
where: {
access_code: user.accessCode,
},
});
return aimeUser;
}