From 77eb503b3515b25a981a2da1ee7e5c6185e7b3e9 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Mon, 26 Feb 2024 00:07:36 -0500 Subject: [PATCH] handle winning party --- app/api/kill/confirm/route.ts | 37 +++++++++++++++++++++++++++++++++-- app/api/kill/route.ts | 9 ++++++++- app/api/user/[id]/route.ts | 4 +++- lib/party.ts | 21 ++++++++++++++++++-- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/app/api/kill/confirm/route.ts b/app/api/kill/confirm/route.ts index 82e098e..15974d3 100644 --- a/app/api/kill/confirm/route.ts +++ b/app/api/kill/confirm/route.ts @@ -13,9 +13,18 @@ export async function POST() { }, include: { targetedBy: true, + party: true, }, }); + if (!user.party) { + return Response.json("User is not in a party", { status: 400 }); + } + + if (!user.party.started) { + return Response.json("Party has not started", { status: 400 }); + } + if (!user.pending) { return Response.json("User must be pending in order to confirm", { status: 400, @@ -23,7 +32,7 @@ export async function POST() { } if (!user.targetedBy) { - return Response.json(user, { status: 500 }); + return Response.json("User is not targetted by anyone", { status: 500 }); } await prisma.user.update({ @@ -38,7 +47,7 @@ export async function POST() { }, }); - await prisma.user.update({ + const { party } = await prisma.user.update({ where: { id: user.id, }, @@ -50,7 +59,31 @@ export async function POST() { increment: 1, }, }, + include: { + party: { + include: { + players: true, + }, + }, + }, }); + if (!party) { + return Response.json("Party not found", { status: 500 }); + } + + const players = party.players.filter((e) => e.alive); + if (players.length < 2) { + await prisma.party.update({ + where: { + id: party.id, + }, + data: { + started: false, + winnerId: players[0].id, + }, + }); + } + return Response.json(null); } diff --git a/app/api/kill/route.ts b/app/api/kill/route.ts index 16f7e9e..f4569c8 100644 --- a/app/api/kill/route.ts +++ b/app/api/kill/route.ts @@ -26,12 +26,19 @@ export async function POST() { where: { email: session.user.email, }, + include: { + party: true, + }, }); - if (!user.partyId) { + if (!user.party) { return Response.json("User is not in a party", { status: 400 }); } + if (!user.party.started) { + return Response.json("Party has not started", { status: 400 }); + } + if (!user.targetId) { return Response.json("User does not have a target", { status: 400 }); } diff --git a/app/api/user/[id]/route.ts b/app/api/user/[id]/route.ts index ce368a0..12f8ede 100644 --- a/app/api/user/[id]/route.ts +++ b/app/api/user/[id]/route.ts @@ -15,7 +15,9 @@ export async function GET(_: Request, { params }: { params: { id: string } }) { }); if (!user) { - return Response.json(null, { status: 400 }); + return Response.json(`Could not find user with id ${params.id}`, { + status: 400, + }); } return Response.json(user); diff --git a/lib/party.ts b/lib/party.ts index 68b587b..d37fd34 100644 --- a/lib/party.ts +++ b/lib/party.ts @@ -48,9 +48,26 @@ export async function removePlayer(email: string) { return Response.json(party); } + const players = party.players.filter((e) => e.alive); + if (party.started && players.length === 1) { + await prisma.party.update({ + where: { + id: party.id, + }, + data: { + started: false, + winnerId: players[0].id, + }, + }); + } + if (party.started) { - if (!user.targetedBy || !user.targetId) { - return Response.json(null, { status: 500 }); + if (!user.targetId) { + return Response.json("User has no target", { status: 500 }); + } + + if (!user.targetedBy) { + return Response.json("User is not targetted by anyone", { status: 500 }); } await prisma.user.update({