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

120 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-07-26 19:15:13 +00:00
"use server";
import { getAuth } from "@/auth/queries/getauth";
2024-08-02 01:42:14 +00:00
import { supportedVersionNumber } from "@/lib/helpers";
import { artemis } from "@/lib/prisma";
2024-07-26 19:15:13 +00:00
export async function getCurrentSystemVoice() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
2024-07-28 05:16:27 +00:00
const currentSystemVoice = await artemis.chuni_profile_data.findMany({
2024-07-26 19:15:13 +00:00
where: {
user: user.UserId,
2024-08-02 01:42:14 +00:00
version: supportedVersionNumber,
2024-07-26 19:15:13 +00:00
},
select: {
voiceId: true,
},
});
2024-07-28 05:16:27 +00:00
return currentSystemVoice;
2024-07-26 19:15:13 +00:00
}
2024-08-02 01:42:14 +00:00
export async function updatePlayerSystemVoiceId(voiceId: number) {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
if (voiceId === undefined) {
throw new Error("nameplateId is required");
}
const checkIfSystemVoiceIsUnlocked = await artemis.chuni_item_item.findMany({
where: {
itemKind: 10,
user: user.UserId,
},
select: {
itemId: true,
},
});
const unlockedSystemVoices = checkIfSystemVoiceIsUnlocked.map(
(item) => item.itemId,
);
2024-08-02 01:42:14 +00:00
try {
const updatePlayerNameplate = await artemis.chuni_profile_data.update({
where: {
user_version: {
user: user.UserId,
version: supportedVersionNumber,
},
},
data: {
voiceId,
},
});
console.log(updatePlayerNameplate);
return updatePlayerNameplate;
} catch (error) {
console.error("Error updating nameplate:", error);
throw error;
}
}
2024-07-26 19:15:13 +00:00
export async function getSystemVoices() {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
const checkIfSystemVoiceIsUnlocked = await artemis.chuni_item_item.findMany({
where: {
itemKind: 9,
user: user.UserId,
},
select: {
itemId: true,
},
});
const unlockedSystemVoices = checkIfSystemVoiceIsUnlocked.map(
(item) => item.itemId,
);
2024-07-28 05:16:27 +00:00
const AllSystemVoices =
await artemis.cozynet_chuni_static_systemvoice.findMany({
select: {
id: true,
str: true,
sortName: true,
category: true,
imagePath: true,
rareType: true,
netOpenName: true,
},
});
const currentlyUnlockedSystemVoices = Array.from(
new Map(
AllSystemVoices.filter((matchingsystemVoiceIds) =>
unlockedSystemVoices.includes(matchingsystemVoiceIds.id),
2024-08-19 23:14:47 +00:00
).map((unlockedSystemVoiceIds) => [
unlockedSystemVoiceIds.id,
unlockedSystemVoiceIds,
]),
).values(),
);
return currentlyUnlockedSystemVoices;
2024-07-26 19:15:13 +00:00
}