added data to sharing page

This commit is contained in:
polaris
2024-06-29 17:49:55 -04:00
parent 3e770e56b7
commit f8786d1ad7
3 changed files with 179 additions and 89 deletions

View File

@ -1,6 +1,7 @@
import { getAuth } from "@/auth/queries/getauth"; import { getAuth } from "@/auth/queries/getauth";
import { generatePlaylogId, getSongsWithTitles } from "@/lib/api"; import { generatePlaylogId, getSongsWithTitles } from "@/lib/api";
import { shareScore } from "../token"; import { shareScore } from "../token";
import { getDifficultyClass, getDifficultyText } from "@/lib/helpers";
export default async function Share({ export default async function Share({
params, params,
@ -13,19 +14,14 @@ export default async function Share({
const tokenResult = await shareScore(token); const tokenResult = await shareScore(token);
if (tokenResult.error) { if (tokenResult.error) {
return <p>{tokenResult.error}</p>; return <p>{tokenResult.error}</p>;
} }
const { user } = await getAuth();
const { user } = await getAuth(); if (!user?.UserId) {
return <p>Error: No user ID found.</p>;
if (!user?.UserId) {
return {
error:"no user id"
} }
}
const userId = user?.UserId; const userId = user?.UserId;
const playlogId = parseInt(id); const playlogId = parseInt(id);
@ -33,17 +29,38 @@ export default async function Share({
const songsData = await getSongsWithTitles(userId); const songsData = await getSongsWithTitles(userId);
const playlogIds = await generatePlaylogId(playlogId); const playlogIds = await generatePlaylogId(playlogId);
const matchSongToPlaylogId = songsData.filter((song) => // Filter songsData to match the playlogIds
playlogIds.includes(song.id) const matchedSongs = songsData.filter((song) => playlogIds.includes(song.id));
);
const songTitles = matchSongToPlaylogId.map((song) => song.title);
return ( return (
<ul> <div className="flex justify-center items-center h-screen">
{songTitles.map((title) => ( {matchedSongs.map((song) => (
<li key={title}>{title}</li> <div
key={song.id}
className="bg-slate-500 h-200 w-400 flex justify-center items-center"
>
<div className="text-white text-lg">
<p>{song.title}</p>
<p>Artist: {song.artist}</p>
<p>Genre: {song.genre}</p>
<p>diff: {song.chartlevel}</p>
<p>Level: {song.level}</p>
<p>Full Combo: {song.isFullCombo ? "Yes" : "No"}</p>
<p>Score: {song.score}</p>
<div>
<span
className={`justify-center rounded-sm text-center text-sm font-bold ${getDifficultyClass(
song.chartId
)} text-white`}
>
{getDifficultyText(song.chartId)}
</span>
</div>
</div>
</div>
))} ))}
</ul> </div>
); );
} }

View File

@ -2,91 +2,111 @@
import { getAuth } from "@/auth/queries/getauth"; import { getAuth } from "@/auth/queries/getauth";
import { artemis, daphnis } from "@/lib/prisma"; import { artemis, daphnis } from "@/lib/prisma";
import type * as Prisma from '@prisma/client'; import type * as Prisma from "@prisma/client";
type ChuniScorePlaylog = Prisma.PrismaClient; type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient; type ChuniStaticMusic = Prisma.PrismaClient;
type LinkSharingToken = { type LinkSharingToken = {
playlogId: number; playlogId: number;
}; };
export async function getSongsWithTitles(userId: number) { export async function getSongsWithTitles(userId: number) {
try { try {
const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({ // Fetch songs played by the user
where: { const songs: ChuniScorePlaylog[] =
user: userId, await artemis.chuni_score_playlog.findMany({
}, where: {
orderBy: { user: userId,
userPlayDate: "desc", },
}, orderBy: {
select: { userPlayDate: "desc",
id: true, },
maxCombo: true, select: {
userPlayDate: true, id: true,
isFullCombo: true, maxCombo: true,
playerRating: true, userPlayDate: true,
isAllJustice: true, isFullCombo: true,
score: true, playerRating: true,
judgeHeaven: true, isAllJustice: true,
judgeGuilty: true, score: true,
judgeJustice: true, judgeHeaven: true,
judgeAttack: true, judgeGuilty: true,
judgeCritical: true, judgeJustice: true,
isClear: true, judgeAttack: true,
skillId: true, judgeCritical: true,
skillEffect: true, isClear: true,
skillLevel: true, skillId: true,
isNewRecord: true, skillEffect: true,
musicId: true, skillLevel: true,
level: true, isNewRecord: true,
rateAir: true, musicId: true,
rateHold: true, level: true,
rateFlick: true, rateAir: true,
rateSlide: true, rateHold: true,
rateTap: true, rateFlick: true,
romVersion: true, rateSlide: true,
eventId: true, rateTap: true,
characterId: true, romVersion: true,
charaIllustId: true, eventId: true,
track: true, characterId: true,
isContinue: true, charaIllustId: true,
isFreeToPlay: true, track: true,
playKind: true, isContinue: true,
playDate: true, isFreeToPlay: true,
orderId: true, playKind: true,
sortNumber: true, playDate: true,
user: true, orderId: true,
placeId: true, sortNumber: true,
ticketId: true, user: true,
}, placeId: true,
}); ticketId: true,
},
});
// Extract unique musicIds from the fetched songs
const musicIds = songs const musicIds = songs
.map((song) => song.musicId) .map((song) => song.musicId)
.filter((id) => id !== null) as number[]; .filter((id) => id !== null) as number[];
const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({ // Fetch static music information for the extrcted musicIds
where: { const staticMusicInfo: ChuniStaticMusic[] =
songId: { await artemis.chuni_static_music.findMany({
in: musicIds, where: {
songId: {
in: musicIds,
},
}, },
}, select: {
select: { songId: true,
songId: true, title: true,
title: true, artist: true,
artist: true, chartId: true,
}, level: true,
}); genre: true,
worldsEndTag: true,
},
});
const songsWithTitles = songs.map((song) => ({ // Map each song with its corresponding static music information
...song, const songsWithTitles = songs.map((song) => {
title: // Find static info corresponding to the song's musicId and level (chartId)
staticMusicInfo.find((title) => title.songId === song.musicId)?.title || const staticInfo = staticMusicInfo.find(
"Unknown Title", (chuniStatiMusic) =>
artist: chuniStatiMusic.songId === song.musicId &&
staticMusicInfo.find((artist) => artist.songId === song.musicId) chuniStatiMusic.chartId === song.level
?.artist || "Unknown Artist", );
}));
// Return mapped song with title, artist, genre, and correct level
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",
};
});
return songsWithTitles; return songsWithTitles;
} catch (error) { } catch (error) {
@ -97,14 +117,14 @@ export async function getSongsWithTitles(userId: number) {
export async function generatePlaylogId(playlogid: number) { export async function generatePlaylogId(playlogid: number) {
try { try {
const tokens = await daphnis.linkSharingToken.findMany({ const tokens = (await daphnis.linkSharingToken.findMany({
where: { where: {
playlogId: playlogid, playlogId: playlogid,
}, },
select: { select: {
playlogId: true, playlogId: true,
}, },
}) as LinkSharingToken[]; })) as LinkSharingToken[];
const playlogIds: number[] = tokens.map((token) => token.playlogId); const playlogIds: number[] = tokens.map((token) => token.playlogId);

53
lib/helpers.ts Normal file
View File

@ -0,0 +1,53 @@
export const getDifficultyClass = (level: number) => {
switch (level) {
case 0:
return "bg-green-500"; // Green for basic
case 1:
return "bg-yellow-500"; // Yellow for advance
case 2:
return "bg-red-500"; // Red for expert
case 3:
return "bg-purple-500"; // Purple for master
case 4:
return "bg-yellow-500"; // Example of gradient background
case 5:
return "bg-black"; // Black for worlds end
default:
return "bg-gray-500"; // Default to gray background for unknown difficulty
}
};
export const getDifficultyText = (chartId: number) => {
switch (chartId) {
case 0:
return "EASY"; // Text for difficulty
case 1:
return "ADVANCE";
case 2:
return "EXPERT";
case 3:
return "MASTER";
case 4:
return "ULTIMA";
case 5:
return "WORLDS END";
}
};
export const getGrade = (score: number) => {
if (score >= 1009000) return "SSS+";
if (score >= 1007500 && score <= 1008999) return "SSS";
if (score >= 1005000 && score <= 1007499) return "SS+";
if (score >= 1000000 && score <= 1004999) return "SS";
if (score >= 990000 && score <= 999999) return "S+";
if (score >= 975000 && score <= 990000) return "S";
if (score >= 950000 && score <= 974999) return "AAA";
if (score >= 925000 && score <= 949999) return "AA";
if (score >= 900000 && score <= 924999) return "A";
if (score >= 800000 && score <= 899999) return "BBB";
if (score >= 700000 && score <= 799999) return "BB";
if (score >= 600000 && score <= 699999) return "B";
if (score >= 500000 && score <= 599999) return "C";
if (score < 500000) return "D";
return "";
};