2024-08-14 15:52:19 +00:00
|
|
|
"use server";
|
|
|
|
import { getAuth } from "@/auth/queries/getauth";
|
|
|
|
import { supportedVersionNumber } from "@/lib/helpers";
|
|
|
|
import { artemis } from "@/lib/prisma";
|
|
|
|
|
2024-08-15 02:27:17 +00:00
|
|
|
export async function getUserRatingBaseList() {
|
2024-08-14 15:52:19 +00:00
|
|
|
const { user } = await getAuth();
|
|
|
|
|
|
|
|
if (!user || !user.accessCode) {
|
|
|
|
throw new Error("User is not authenticated or accessCode is missing");
|
|
|
|
}
|
|
|
|
|
2024-08-15 02:27:17 +00:00
|
|
|
try {
|
2024-08-15 03:10:24 +00:00
|
|
|
const userRatingBaseList = await artemis.chuni_profile_rating.findMany({
|
|
|
|
where: {
|
|
|
|
user: user.UserId,
|
|
|
|
type: "userRatingBaseList",
|
|
|
|
version: supportedVersionNumber,
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
musicId: true,
|
|
|
|
score: true,
|
|
|
|
difficultId: true,
|
|
|
|
romVersionCode: true,
|
|
|
|
version: true,
|
|
|
|
index: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const baseListMusicIds = userRatingBaseList.map((entry) => entry.musicId!);
|
|
|
|
|
|
|
|
const staticMusicInfo = await artemis.chuni_static_music.findMany({
|
|
|
|
where: {
|
|
|
|
songId: {
|
|
|
|
in: baseListMusicIds,
|
|
|
|
},
|
2024-08-15 03:58:28 +00:00
|
|
|
version: supportedVersionNumber,
|
2024-08-15 03:10:24 +00:00
|
|
|
},
|
|
|
|
select: {
|
|
|
|
songId: true,
|
|
|
|
title: true,
|
|
|
|
artist: true,
|
|
|
|
chartId: true,
|
|
|
|
level: true,
|
|
|
|
genre: true,
|
|
|
|
worldsEndTag: true,
|
|
|
|
jacketPath: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-08-15 03:57:35 +00:00
|
|
|
// Map to join `musicId` and `difficultId` with static music info
|
|
|
|
// i hate it
|
|
|
|
const profileRatingToStaticMusic = new Map<
|
|
|
|
string,
|
|
|
|
(typeof staticMusicInfo)[0]
|
|
|
|
>(staticMusicInfo.map((info) => [`${info.songId}-${info.chartId}`, info]));
|
2024-08-15 03:10:24 +00:00
|
|
|
|
|
|
|
const songsWithTitles = userRatingBaseList.map((ratingListSong) => {
|
2024-08-15 03:57:35 +00:00
|
|
|
const MusicIdtoDiffId = `${ratingListSong.musicId}-${ratingListSong.difficultId}`;
|
|
|
|
const songInfo = profileRatingToStaticMusic.get(MusicIdtoDiffId);
|
2024-08-15 03:10:24 +00:00
|
|
|
|
2024-08-15 03:57:35 +00:00
|
|
|
const level = songInfo?.level ?? 0;
|
2024-08-15 03:10:24 +00:00
|
|
|
const score = ratingListSong.score ?? 0;
|
|
|
|
|
2024-08-15 03:58:28 +00:00
|
|
|
// Rating Formula
|
2024-08-15 03:10:24 +00:00
|
|
|
let rating = 0;
|
2024-08-15 03:57:35 +00:00
|
|
|
|
2024-08-15 03:10:24 +00:00
|
|
|
if (score >= 1009000) {
|
|
|
|
rating = level * 100 + 215;
|
|
|
|
} else if (score >= 1007500) {
|
|
|
|
rating = level * 100 + 200 + (score - 1007500) / 100;
|
|
|
|
} else if (score >= 1005000) {
|
|
|
|
rating = level * 100 + 150 + (score - 1005000) / 50;
|
|
|
|
} else if (score >= 1000000) {
|
|
|
|
rating = level * 100 + 100 + (score - 1000000) / 100;
|
|
|
|
} else if (score >= 975000) {
|
|
|
|
rating = level * 100 + (score - 975000) / 250;
|
|
|
|
} else if (score >= 925000) {
|
|
|
|
rating = level * 100 - 300 + ((score - 925000) * 3) / 500;
|
|
|
|
} else if (score >= 900000) {
|
|
|
|
rating = level * 100 - 500 + ((score - 900000) * 4) / 500;
|
|
|
|
} else if (score >= 800000) {
|
|
|
|
rating =
|
|
|
|
(level * 100 - 500) / 2 +
|
|
|
|
((score - 800000) * ((level - 500) / 2)) / 100000;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...ratingListSong,
|
2024-08-15 03:57:35 +00:00
|
|
|
title: songInfo?.title || "Unknown Title",
|
|
|
|
artist: songInfo?.artist || "Unknown Artist",
|
|
|
|
genre: songInfo?.genre || "Unknown Genre",
|
|
|
|
chartId: songInfo?.chartId || "Unknown chartId",
|
|
|
|
level: songInfo?.level || "Unknown Level",
|
|
|
|
jacketPath: songInfo?.jacketPath || "",
|
2024-08-15 03:10:24 +00:00
|
|
|
rating,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return songsWithTitles;
|
2024-08-15 02:27:17 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching songs with titles:", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|