From c765df76627924665cb32ddb0c213a83b711c672 Mon Sep 17 00:00:00 2001 From: Polaris Date: Tue, 23 Jul 2024 16:15:27 -0400 Subject: [PATCH] cleanup --- app/(authenticated)/chunithm/page.tsx | 2 +- app/(sharing)/[token]/[id]/actions.ts | 154 +++++++++++++++ app/(sharing)/[token]/[id]/page.tsx | 2 +- components/avatarcustomization/actions.ts | 33 ++++ components/scoreplaylog/page.tsx | 2 +- lib/api.ts | 218 ---------------------- 6 files changed, 190 insertions(+), 221 deletions(-) create mode 100644 app/(sharing)/[token]/[id]/actions.ts create mode 100644 components/avatarcustomization/actions.ts diff --git a/app/(authenticated)/chunithm/page.tsx b/app/(authenticated)/chunithm/page.tsx index c2bea48..c06b564 100644 --- a/app/(authenticated)/chunithm/page.tsx +++ b/app/(authenticated)/chunithm/page.tsx @@ -2,10 +2,10 @@ //https://github.com/vercel/next.js/discussions/63862 import React from "react"; -import { getAllAvatarParts } from "@/lib/api"; import { AvatarCustomization } from "@/components/avatarcustomization/page"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import ChunithmScorePlaylog from "@/components/scoreplaylog/page"; +import { getAllAvatarParts } from "@/components/avatarcustomization/actions"; // the number is the category id for the specific part diff --git a/app/(sharing)/[token]/[id]/actions.ts b/app/(sharing)/[token]/[id]/actions.ts new file mode 100644 index 0000000..5324907 --- /dev/null +++ b/app/(sharing)/[token]/[id]/actions.ts @@ -0,0 +1,154 @@ + +"use server"; + + +import { artemis, daphnis } from "@/lib/prisma"; +import type * as Prisma from "@prisma/client"; + + type ChuniScorePlaylog = Prisma.PrismaClient; + type ChuniStaticMusic = Prisma.PrismaClient; + +type LinkSharingToken = { + playlogId: number; + }; + + +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, + }, + }); + + const playCounts = await artemis.chuni_score_playlog.groupBy({ + by: ['musicId'], + _count: { + musicId: true, + }, + where: { + user: userId, + musicId: { + in: chuniScorePlaylogMusicId, + }, + }, + }); + + const playCountMap = playCounts.reduce((map, item) => { + if (item.musicId !== null) { + map[item.musicId] = item._count.musicId; + } + return map; + }, {} as Record); + + 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", + 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; + } + } + + +export async function generatePlaylogId(playlogid: number) { + 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; + } + } \ No newline at end of file diff --git a/app/(sharing)/[token]/[id]/page.tsx b/app/(sharing)/[token]/[id]/page.tsx index ccaa765..a390ccd 100644 --- a/app/(sharing)/[token]/[id]/page.tsx +++ b/app/(sharing)/[token]/[id]/page.tsx @@ -1,11 +1,11 @@ import { getAuth } from "@/auth/queries/getauth"; -import { generatePlaylogId, getSongsWithTitles } from "@/lib/api"; import { shareScore } from "../token"; import { getDifficultyClass, getDifficultyText, getGrade } from "@/lib/helpers"; import { type chuni_score_playlog, chuni_static_music, } from "@/prisma/schemas/artemis/generated/artemis"; +import { generatePlaylogId, getSongsWithTitles } from "./actions"; type chunithm = chuni_score_playlog & chuni_static_music & { playCount: number }; diff --git a/components/avatarcustomization/actions.ts b/components/avatarcustomization/actions.ts new file mode 100644 index 0000000..ff0c6b1 --- /dev/null +++ b/components/avatarcustomization/actions.ts @@ -0,0 +1,33 @@ +"use server"; + +import { getAuth } from "@/auth/queries/getauth"; +import { artemis, daphnis } from "@/lib/prisma"; +import type * as Prisma from "@prisma/client"; + +// type ChuniScorePlaylog = Prisma.PrismaClient; +// type ChuniStaticMusic = Prisma.PrismaClient; + + +export async function getAllAvatarParts(category: 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: { + category: category + }, + select: { + id: true, + name: true, + avatarAccessoryId: true, + category: true, + version: true, + iconPath: true, + texturePath: true, + } + }); + return avatarParts; + } \ No newline at end of file diff --git a/components/scoreplaylog/page.tsx b/components/scoreplaylog/page.tsx index 6be4b7f..412c0fb 100644 --- a/components/scoreplaylog/page.tsx +++ b/components/scoreplaylog/page.tsx @@ -1,8 +1,8 @@ import { DataTable } from "./data-table"; -import { getSongsWithTitles } from "@/lib/api"; import { getAuth } from "@/auth/queries/getauth"; import { z } from "zod"; import { columns } from "./colums"; +import { getSongsWithTitles } from "@/app/(sharing)/[token]/[id]/actions"; const userSchema = z.object({ UserId: z.number(), diff --git a/lib/api.ts b/lib/api.ts index ad35d1d..5c450b1 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -2,155 +2,6 @@ import { getAuth } from "@/auth/queries/getauth"; import { artemis, daphnis } from "@/lib/prisma"; -import type * as Prisma from "@prisma/client"; - -type ChuniScorePlaylog = Prisma.PrismaClient; -type ChuniStaticMusic = Prisma.PrismaClient; - -type LinkSharingToken = { - playlogId: number; -}; - -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, // Add jacketPath to the selection - }, - }); - - const playCounts = await artemis.chuni_score_playlog.groupBy({ - by: ['musicId'], - _count: { - musicId: true, - }, - where: { - user: userId, - musicId: { - in: chuniScorePlaylogMusicId, - }, - }, - }); - - const playCountMap = playCounts.reduce((map, item) => { - if (item.musicId !== null) { - map[item.musicId] = item._count.musicId; - } - return map; - }, {} as Record); - - 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", - 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; - } -} - - - -export async function generatePlaylogId(playlogid: number) { - 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; - } -} export const getUsername = async () => { const { user } = await getAuth(); @@ -180,75 +31,6 @@ export async function getAllAimeCards() { return aimeUser; } - -export async function getAllAvatarParts(category: 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: { - category: category - }, - select: { - id: true, - name: true, - avatarAccessoryId: true, - category: true, - version: true, - iconPath: true, - texturePath: true, - } - }); - return avatarParts; -} - - -export async function GetSpecificAvatarParts(category: 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: { category }, - select: { - name: true, - category: true, - version: true, - iconPath: true, - texturePath: true, - }, - }); - - return avatarParts; -} - -export async function getCurrentPlayerAvatarParts() { - const { user } = await getAuth(); - - if (!user || !user.accessCode) { - throw new Error("User is not authenticated or accessCode is missing"); - } - - const avatarParts = await artemis.chuni_profile_data.findMany({ - select: { - avatarBack: true, - avatarFace: true, - avatarItem: true, - avatarHead: true, - avatarWear: true, - }, - }); - - return avatarParts; -} - - - export async function verifyAimeCodeAgainstArtemis() { const { user } = await getAuth();