diff --git a/app/api/kill/confirm/route.ts b/app/api/kill/confirm/route.ts index 6e0e73f..6c842fb 100644 --- a/app/api/kill/confirm/route.ts +++ b/app/api/kill/confirm/route.ts @@ -1,4 +1,6 @@ +import { setPlayerTargets } from "@/lib/party"; import prisma from "@/lib/prisma"; +import _ from "lodash"; import { getServerSession } from "next-auth"; export async function POST() { @@ -7,15 +9,7 @@ export async function POST() { return Response.json(null, { status: 401 }); } - let user = await prisma.user.findUniqueOrThrow({ - where: { - email: session.user.email, - }, - include: { - targetedBy: true, - party: true, - }, - }); + let user = await getUser(session.user.email); if (!user.party) { return Response.json("User is not in a party", { status: 400 }); @@ -35,17 +29,18 @@ export async function POST() { return Response.json("User is not targetted by anyone", { status: 500 }); } - await prisma.user.update({ - where: { - id: user.targetedBy.id, - }, - data: { - targetId: user.targetId, - kills: { - increment: 1, - }, - }, - }); + switch (user.party.mode) { + case "CLASSIC": + setTargetClassic(user); + break; + case "SHUFFLE": + setTargetShuffle(user); + break; + default: + return Response.json("Party does not have a valid mode", { + status: 500, + }); + } const { party } = await prisma.user.update({ where: { @@ -96,3 +91,42 @@ export async function POST() { return Response.json(null); } + +export type User = NonNullable>>; + +async function getUser(email: string) { + return await prisma.user.findUniqueOrThrow({ + where: { + email, + }, + include: { + targetedBy: true, + party: { + include: { + players: true, + }, + }, + }, + }); +} + +async function setTargetClassic(user: User) { + return await prisma.user.update({ + where: { + id: user.targetedBy!.id, + }, + data: { + targetId: user.targetId, + kills: { + increment: 1, + }, + }, + }); +} + +async function setTargetShuffle(user: User) { + const players = user.party!.players.filter( + (p) => p.alive && p.id !== user.targetId, + ); + await setPlayerTargets(players); +} diff --git a/app/api/party/start/route.ts b/app/api/party/start/route.ts index 05235f8..a66489b 100644 --- a/app/api/party/start/route.ts +++ b/app/api/party/start/route.ts @@ -1,6 +1,7 @@ import prisma from "@/lib/prisma"; import { getServerSession } from "next-auth"; import _ from "lodash"; +import { setPlayerTargets } from "@/lib/party"; export async function POST() { const session = await getServerSession(); @@ -51,27 +52,7 @@ export async function POST() { }, }); - const players = _.shuffle(party.players); - - for (let i = 0; i < players.length; i++) { - let targetId: string; - if (i === players.length - 1) { - targetId = players[0].id; - } else { - targetId = players[i + 1].id; - } - - await prisma.user.update({ - where: { - id: players[i].id, - }, - data: { - alive: true, - pending: false, - targetId, - }, - }); - } + await setPlayerTargets(party.players); return Response.json(null); } diff --git a/lib/party.ts b/lib/party.ts index 43f2cc2..29738af 100644 --- a/lib/party.ts +++ b/lib/party.ts @@ -166,3 +166,29 @@ async function updateTarget(user: User) { return Response.json(null); } + +type Players = Party["players"]; + +export async function setPlayerTargets(players: Players) { + players = _.shuffle(players); + + for (let i = 0; i < players.length; i++) { + let targetId: string; + if (i === players.length - 1) { + targetId = players[0].id; + } else { + targetId = players[i + 1].id; + } + + await prisma.user.update({ + where: { + id: players[i].id, + }, + data: { + alive: true, + pending: false, + targetId, + }, + }); + } +}