Skip to content

Commit

Permalink
handle winning party
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 26, 2024
1 parent 542f774 commit 77eb503
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
37 changes: 35 additions & 2 deletions app/api/kill/confirm/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ 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,
});
}

if (!user.targetedBy) {
return Response.json(user, { status: 500 });
return Response.json("User is not targetted by anyone", { status: 500 });
}

await prisma.user.update({
Expand All @@ -38,7 +47,7 @@ export async function POST() {
},
});

await prisma.user.update({
const { party } = await prisma.user.update({
where: {
id: user.id,
},
Expand All @@ -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);
}
9 changes: 8 additions & 1 deletion app/api/kill/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
4 changes: 3 additions & 1 deletion app/api/user/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 77eb503

Please sign in to comment.