Skip to content

Commit

Permalink
add shuffle route #22
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 28, 2024
1 parent 23cb949 commit 285ada8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 41 deletions.
74 changes: 54 additions & 20 deletions app/api/kill/confirm/route.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 });
Expand All @@ -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: {
Expand Down Expand Up @@ -96,3 +91,42 @@ export async function POST() {

return Response.json(null);
}

export type User = NonNullable<Awaited<ReturnType<typeof getUser>>>;

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);
}
23 changes: 2 additions & 21 deletions app/api/party/start/route.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
}
26 changes: 26 additions & 0 deletions lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
}
}

0 comments on commit 285ada8

Please sign in to comment.