added update functions

This commit is contained in:
Polaris
2024-08-01 21:42:14 -04:00
parent aabbef26bf
commit d3acd9377c
12 changed files with 391 additions and 132 deletions

View File

@ -1,11 +1,8 @@
"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;
import { supportedVersionNumber } from "@/lib/helpers";
import { artemis } from "@/lib/prisma";
export async function getCurrentAvatarParts() {
const { user } = await getAuth();
@ -17,6 +14,7 @@ export async function getCurrentAvatarParts() {
const CurrentAvatarParts = await artemis.chuni_profile_data.findMany({
where: {
user: user.UserId,
version: supportedVersionNumber, // TODO: eventually change this so the user can set their own version
},
select: {
avatarSkin: true,
@ -30,7 +28,46 @@ export async function getCurrentAvatarParts() {
});
return CurrentAvatarParts;
}
export async function updateAvatarParts(
avatarHead?: number,
avatarFace?: number,
avatarBack?: number,
avatarWear?: number,
avatarItem?: number,
) {
const { user } = await getAuth();
if (!user || !user.accessCode) {
throw new Error("User is not authenticated or accessCode is missing");
}
try {
// only including the values that aren't undefined i.e 0
const AvatarPartData: any = {};
if (avatarHead !== undefined) AvatarPartData.avatarHead = avatarHead;
if (avatarFace !== undefined) AvatarPartData.avatarFace = avatarFace;
if (avatarBack !== undefined) AvatarPartData.avatarBack = avatarBack;
if (avatarWear !== undefined) AvatarPartData.avatarWear = avatarWear;
if (avatarItem !== undefined) AvatarPartData.avatarItem = avatarItem;
const UpdateAvatarHead = await artemis.chuni_profile_data.update({
where: {
user_version: {
user: user.UserId,
version: supportedVersionNumber,
},
},
data: AvatarPartData,
});
console.log(UpdateAvatarHead);
return UpdateAvatarHead;
} catch (error) {
console.error("Error updating avatar parts:", error);
throw error;
}
}
export async function getAllAvatarParts(category: number) {
const { user } = await getAuth();

View File

@ -37,6 +37,7 @@ const getAvatarTextureSrc = (id: number | undefined) => {
return `avatarAccessory/CHU_UI_Avatar_Tex_0${id}.png`;
};
import { updateAvatarParts } from "./actions";
type chunithm_avatar = chuni_static_avatar;
type AvatarSelectionProps = {
@ -65,21 +66,11 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
avatarWearSelectionData,
}) => {
const FormSchema = z.object({
AvatarHeadAccessory: z.number({
required_error: "Please select an Avatar Head Item.",
}),
AvatarFaceAccessory: z.number({
required_error: "Please select an Avatar Face Item.",
}),
AvatarItemAccessory: z.number({
required_error: "Please select an Avatar Item.",
}),
AvatarBackAccessory: z.number({
required_error: "Please select an Avatar Back Item.",
}),
AvatarWearAccessory: z.number({
required_error: "Please select an Avatar Wear Item.",
}),
AvatarHeadAccessory: z.number().optional(),
AvatarFaceAccessory: z.number().optional(),
AvatarItemAccessory: z.number().optional(),
AvatarBackAccessory: z.number().optional(),
AvatarWearAccessory: z.number().optional(),
});
const form = useForm<z.infer<typeof FormSchema>>({
@ -87,37 +78,35 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
});
const [avatarFaceId, setAvatarFaceId] = useState<number | undefined>(
undefined
undefined,
);
const [avatarSkinId, setAvatarSkinId] = useState<number | undefined>(
undefined
undefined,
);
const [avatarHeadId, setAvatarHeadId] = useState<number | undefined>(
undefined
undefined,
);
const [avatarWearId, setAvatarWearId] = useState<number | undefined>(
undefined
undefined,
);
const [avatarBackId, setAvatarBackId] = useState<number | undefined>(
undefined
undefined,
);
const [avatarItemId, setAvatarItemId] = useState<number | undefined>(
undefined
undefined,
);
useEffect(() => {
const fetchAvatarParts = async () => {
try {
const data = await getCurrentAvatarParts();
if (data.length > 0) {
setAvatarFaceId(data[0].avatarFace!);
setAvatarSkinId(data[0].avatarSkin!);
setAvatarHeadId(data[0].avatarHead!);
setAvatarWearId(data[0].avatarWear!);
setAvatarBackId(data[0].avatarBack!);
setAvatarItemId(data[0].avatarItem!);
}
setAvatarFaceId(data[0].avatarFace!);
setAvatarSkinId(data[0].avatarSkin!);
setAvatarHeadId(data[0].avatarHead!);
setAvatarWearId(data[0].avatarWear!);
setAvatarBackId(data[0].avatarBack!);
setAvatarItemId(data[0].avatarItem!);
} catch (error) {
console.error("Error fetching avatar parts:", error);
}
@ -125,18 +114,59 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
fetchAvatarParts();
}, []);
function onSubmit(data: z.infer<typeof FormSchema>) {
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(data, null, 2)}</code>
</pre>
),
});
// Existing state
const unchangedHeadId = avatarHeadId;
const unchangedFaceId = avatarFaceId;
const unchangedBackId = avatarBackId;
const unchangedWearId = avatarWearId;
const unchangedItemId = avatarItemId;
// either change to the new body part id or fallback to the unchanged if nothing has changed
const newHeadId = data.AvatarHeadAccessory ?? unchangedHeadId;
const newFaceId = data.AvatarFaceAccessory ?? unchangedFaceId;
const newBackId = data.AvatarBackAccessory ?? unchangedBackId;
const newWearId = data.AvatarWearAccessory ?? unchangedWearId;
const newItemId = data.AvatarItemAccessory ?? unchangedItemId;
updateAvatarParts(newHeadId, newFaceId, newBackId, newWearId, newItemId)
.then(() => {
setAvatarHeadId(newHeadId);
setAvatarFaceId(newFaceId);
setAvatarBackId(newBackId);
setAvatarWearId(newWearId);
setAvatarItemId(newItemId);
toast({
title: "Avatar updated successfully!",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">
{JSON.stringify(data, null, 2)}
</code>
</pre>
),
});
resetFormValues();
})
.catch((error) => {
toast({
title: "Error updating avatar",
description: error.message,
variant: "destructive",
});
});
}
function resetFormValues() {
form.reset({
AvatarHeadAccessory: undefined,
AvatarFaceAccessory: undefined,
AvatarItemAccessory: undefined,
AvatarBackAccessory: undefined,
AvatarWearAccessory: undefined,
});
}
const getTexture = (id: number | undefined, defaultSrc: string) => {
return id ? getAvatarTextureSrc(id) : defaultSrc;
};
@ -145,49 +175,49 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
AvatarHeadAccessory: {
src: getTexture(
form.watch("AvatarHeadAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarHeadId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarHeadId}.png`,
),
className: "avatar_head",
},
AvatarFaceAccessory: {
src: getTexture(
form.watch("AvatarFaceAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarFaceId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarFaceId}.png`,
),
className: "avatar_face",
},
AvatarItemAccessoryR: {
src: getTexture(
form.watch("AvatarItemAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`,
),
className: "avatar_item_r ",
},
AvatarItemAccessoryL: {
src: getTexture(
form.watch("AvatarItemAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarItemId}.png`,
),
className: "avatar_item_l ",
},
AvatarBackAccessory: {
src: getTexture(
form.watch("AvatarBackAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarBackId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarBackId}.png`,
),
className: "avatar_back",
},
AvatarWearAccessory: {
src: getTexture(
form.watch("AvatarWearAccessory"),
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarWearId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarWearId}.png`,
),
className: "avatar_wear",
},
avatarSkinAccessory: {
src: getTexture(
avatarSkinId,
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarSkinId}.png`
`avatarAccessory/CHU_UI_Avatar_Tex_0${avatarSkinId}.png`,
),
className: "avatar_skin",
},
@ -218,7 +248,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
return (
<main className="flex flex-col items-center space-y-6">
{/* Avatar Customization Section */}
<div className="w-full flex justify-center">
<div className="flex w-full justify-center">
<div className="avatar_base">
{Object.entries(AvatarTextures).map(([key, { className, src }]) => (
<div className={className} key={key}>
@ -244,12 +274,13 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground"
!field.value && "text-muted-foreground",
)}
>
{field.value
? avatarHeadSelectionData.avatarParts.find(
(part) => part.avatarAccessoryId === field.value
(part) =>
part.avatarAccessoryId === field.value,
)?.name
: "Select Avatar Head Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
@ -268,7 +299,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
onSelect={() => {
form.setValue(
"AvatarHeadAccessory",
part.avatarAccessoryId!
part.avatarAccessoryId!,
);
}}
>
@ -277,7 +308,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
: "opacity-0"
: "opacity-0",
)}
/>
{part.name}
@ -308,12 +339,13 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground"
!field.value && "text-muted-foreground",
)}
>
{field.value
? avatarFaceSelectionData.avatarParts.find(
(part) => part.avatarAccessoryId === field.value
(part) =>
part.avatarAccessoryId === field.value,
)?.name
: "Select Avatar Face Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
@ -332,7 +364,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
onSelect={() => {
form.setValue(
"AvatarFaceAccessory",
part.avatarAccessoryId!
part.avatarAccessoryId!,
);
}}
>
@ -341,7 +373,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
: "opacity-0"
: "opacity-0",
)}
/>
{part.name}
@ -372,12 +404,13 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground"
!field.value && "text-muted-foreground",
)}
>
{field.value
? avatarItemSelectionData.avatarParts.find(
(part) => part.avatarAccessoryId === field.value
(part) =>
part.avatarAccessoryId === field.value,
)?.name
: "Select Avatar Face Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
@ -396,7 +429,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
onSelect={() => {
form.setValue(
"AvatarItemAccessory",
part.avatarAccessoryId!
part.avatarAccessoryId!,
);
}}
>
@ -405,7 +438,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
: "opacity-0"
: "opacity-0",
)}
/>
{part.name}
@ -436,12 +469,13 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground"
!field.value && "text-muted-foreground",
)}
>
{field.value
? avatarBackSelectionData.avatarParts.find(
(part) => part.avatarAccessoryId === field.value
(part) =>
part.avatarAccessoryId === field.value,
)?.name
: "Select Avatar Back Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
@ -460,7 +494,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
onSelect={() => {
form.setValue(
"AvatarBackAccessory",
part.avatarAccessoryId!
part.avatarAccessoryId!,
);
}}
>
@ -469,7 +503,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
: "opacity-0"
: "opacity-0",
)}
/>
{part.name}
@ -500,12 +534,13 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
role="combobox"
className={cn(
"w-[300px] justify-between",
!field.value && "text-muted-foreground"
!field.value && "text-muted-foreground",
)}
>
{field.value
? avatarWearSelectionData.avatarParts.find(
(part) => part.avatarAccessoryId === field.value
(part) =>
part.avatarAccessoryId === field.value,
)?.name
: "Select Avatar Clothing Item"}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
@ -524,7 +559,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
onSelect={() => {
form.setValue(
"AvatarWearAccessory",
part.avatarAccessoryId!
part.avatarAccessoryId!,
);
}}
>
@ -533,7 +568,7 @@ export const AvatarCustomization: FC<AvatarSelectionProps> = ({
"mr-2 h-4 w-4",
part.avatarAccessoryId === field.value
? "opacity-100"
: "opacity-0"
: "opacity-0",
)}
/>
{part.name}