daphnis/components/(customization)/trophycustomization/actions.ts

102 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-07-23 20:15:27 +00:00
"use server";
import { getAuth } from "@/auth/queries/getauth";
import { getSupportedVersionNumber } from "@/lib/api";
2024-08-02 01:42:14 +00:00
import { artemis } from "@/lib/prisma";
2024-07-23 20:15:27 +00:00
2024-07-26 17:19:37 +00:00
export async function getCurrentTrophies() {
const { user } = await getAuth();
const supportedVersionNumber = await getSupportedVersionNumber();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
2024-07-28 05:16:27 +00:00
const CurrentTrophy = await artemis.chuni_profile_data.findMany({
where: {
user: user.UserId,
2024-08-02 01:42:14 +00:00
version: supportedVersionNumber,
},
select: {
2024-07-26 17:19:37 +00:00
trophyId: true,
},
});
2024-07-28 05:16:27 +00:00
return CurrentTrophy;
}
2024-07-23 20:15:27 +00:00
2024-08-02 01:42:14 +00:00
export async function updatePlayerTrophy(trophyId: number) {
const { user } = await getAuth();
const supportedVersionNumber = await getSupportedVersionNumber();
2024-08-02 01:42:14 +00:00
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
if (trophyId === undefined) {
throw new Error("nameplateId is required");
}
try {
const updatePlayerNameplate = await artemis.chuni_profile_data.update({
where: {
user_version: {
user: user.UserId,
version: supportedVersionNumber,
},
},
data: {
trophyId,
},
});
console.log(updatePlayerNameplate);
return updatePlayerNameplate;
} catch (error) {
console.error("Error updating nameplate:", error);
throw error;
}
}
2024-07-26 17:19:37 +00:00
export async function getTrophies() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const checkIfTrophyIsUnlocked = await artemis.chuni_item_item.findMany({
where: {
itemKind: 3,
user: user.UserId,
},
select: {
itemId: true,
},
});
const unlockedTrophies = checkIfTrophyIsUnlocked.map((item) => item.itemId);
const AllTrophies = await artemis.cozynet_chuni_static_trophies.findMany({
select: {
category: true,
netOpenName: true,
id: true,
str: true,
imagePath: true,
rareType: true,
sortName: true,
},
});
const currentlyUnlockedTrophy = Array.from(
new Map(
AllTrophies.filter((matchingTrophyId) =>
unlockedTrophies.includes(matchingTrophyId.id),
2024-08-19 23:14:47 +00:00
).map((unlockedTrophyIds) => [unlockedTrophyIds.id, unlockedTrophyIds]),
).values(),
);
return currentlyUnlockedTrophy;
}