63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { usePrisma } from "../../utils/prisma";
|
|
import { getProfile } from "../../db/profile";
|
|
|
|
const MEMORY_EMOJIS = ["🛡️", "💪", "🌟", "🧠", "🌊", "🎯", "🌱", "🔑"];
|
|
|
|
function shuffle<T>(arr: T[]): T[] {
|
|
const a = [...arr];
|
|
for (let i = a.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
const tmp = a[i]!;
|
|
a[i] = a[j]!;
|
|
a[j] = tmp;
|
|
}
|
|
return a;
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const user = await requireUser(event);
|
|
const db = usePrisma();
|
|
|
|
const profile = await getProfile(user.id);
|
|
const name = profile?.nickname || profile?.username || "Anonym";
|
|
|
|
const pairs = shuffle([...MEMORY_EMOJIS, ...MEMORY_EMOJIS]);
|
|
const memoryState = {
|
|
cards: pairs.map((emoji: string, id: number) => ({
|
|
id,
|
|
emoji,
|
|
matchedBy: null,
|
|
})),
|
|
flipped: [] as number[],
|
|
scores: { X: 0, O: 0 },
|
|
mismatchRevealed: false,
|
|
};
|
|
|
|
const challenge = await db.gameChallenge.create({
|
|
data: {
|
|
challengerId: user.id,
|
|
challengerName: name,
|
|
gameType: "memory",
|
|
memoryState,
|
|
},
|
|
});
|
|
|
|
const post = await db.communityPost.create({
|
|
data: {
|
|
userId: user.id,
|
|
category: "challenge",
|
|
content: `${name} sucht einen Gegner für Memory! Wer nimmt die Challenge an?`,
|
|
isAnonymous: false,
|
|
isModerated: false,
|
|
challengeId: challenge.id,
|
|
},
|
|
});
|
|
|
|
await db.gameChallenge.update({
|
|
where: { id: challenge.id },
|
|
data: { postId: post.id },
|
|
});
|
|
|
|
return { challengeId: challenge.id };
|
|
});
|