75 lines
3.0 KiB
TypeScript
75 lines
3.0 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getServerSession } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
import { campaigns, bingoItems, marks, users } from "@/lib/db/schema";
|
|
import { eq, inArray, desc } from "drizzle-orm";
|
|
|
|
export async function GET(req: NextRequest, { params }: { params: Promise<{ campaignId: string }> }) {
|
|
const session = await getServerSession();
|
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
|
|
try {
|
|
const { campaignId } = await params;
|
|
const campaign = db.select().from(campaigns).where(eq(campaigns.id, campaignId)).get();
|
|
if (!campaign) return NextResponse.json({ error: "Campaign not found" }, { status: 404 });
|
|
|
|
const items = db.select().from(bingoItems).where(eq(bingoItems.campaignId, campaignId)).orderBy(bingoItems.gridIndex).all();
|
|
const allMarks = db.select().from(marks).where(eq(marks.campaignId, campaignId)).orderBy(desc(marks.createdAt)).all();
|
|
|
|
const userIds = [...new Set(allMarks.map(m => m.userId))];
|
|
const userMap: Record<string, string> = {};
|
|
if (userIds.length > 0) {
|
|
const markedUsers = db.select({ id: users.id, nickname: users.nickname })
|
|
.from(users).where(inArray(users.id, userIds)).all();
|
|
markedUsers.forEach(u => { userMap[u.id] = u.nickname; });
|
|
}
|
|
|
|
const itemMap: Record<string, typeof items[0]> = {};
|
|
items.forEach(it => { itemMap[it.id] = it; });
|
|
|
|
const markedItemIds = allMarks.map(m => m.itemId);
|
|
const markCountMap: Record<string, number> = {};
|
|
const markUsersMap: Record<string, string[]> = {};
|
|
for (const m of allMarks) {
|
|
markCountMap[m.itemId] = (markCountMap[m.itemId] || 0) + 1;
|
|
if (!markUsersMap[m.itemId]) markUsersMap[m.itemId] = [];
|
|
if (!markUsersMap[m.itemId].includes(userMap[m.userId] || "???")) {
|
|
markUsersMap[m.itemId].push(userMap[m.userId] || "???");
|
|
}
|
|
}
|
|
|
|
const activityLog = allMarks.slice(0, 20).map(m => {
|
|
const nickname = userMap[m.userId] || "???";
|
|
const item = itemMap[m.itemId];
|
|
const emoji = item?.emoji || "💀";
|
|
const text = item?.text || "unknown";
|
|
const time = new Date(m.createdAt).toLocaleTimeString();
|
|
return `${time} ${nickname} marked ${emoji} "${text}"`;
|
|
});
|
|
|
|
const totalCells = campaign.gridSize * campaign.gridSize;
|
|
const grid: Array<{
|
|
index: number;
|
|
item: typeof items[0] | null;
|
|
marked: boolean;
|
|
markedBy: string[];
|
|
markCount: number;
|
|
}> = [];
|
|
|
|
for (let i = 0; i < totalCells; i++) {
|
|
const item = items.find(it => it.gridIndex === i) || null;
|
|
grid.push({
|
|
index: i,
|
|
item,
|
|
marked: markedItemIds.includes(item?.id || ""),
|
|
markedBy: markUsersMap[item?.id || ""] || [],
|
|
markCount: markCountMap[item?.id || ""] || 0,
|
|
});
|
|
}
|
|
|
|
return NextResponse.json({ campaign, grid, activityLog });
|
|
} catch {
|
|
return NextResponse.json({ error: "Failed to load game state" }, { status: 500 });
|
|
}
|
|
}
|