Skip to content

Commit

Permalink
add kick player
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 26, 2024
1 parent abd42ca commit fc35c10
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 16 deletions.
13 changes: 11 additions & 2 deletions app/api/party/join/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth";

type PartyJoinBody = {
code?: string;
};

export async function POST(req: Request) {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

const data = await req.json();
const code = data.code;
const body: PartyJoinBody = await req.json();
if (!body.code) {
return Response.json("No party code provided", {
status: 400,
});
}
const code = body.code;

let party = await prisma.party.findUnique({
where: {
Expand Down
57 changes: 57 additions & 0 deletions app/api/party/kick/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { getServerSession } from "next-auth";
import prisma from "@/lib/prisma";
import { removePlayer } from "../leave/route";

type PartyKickBody = {
playerId?: string;
};

export async function POST(req: Request) {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json("User is not authenticated", { status: 401 });
}

const body: PartyKickBody = await req.json();

if (!body.playerId) {
return Response.json("Must provide playerId in request body", {
status: 400,
});
}

const user = await prisma.user.findUniqueOrThrow({
where: {
email: session.user.email,
},
include: {
party: {
include: {
players: true,
},
},
targetedBy: true,
},
});

if (!user.party) {
return Response.json("You are not part of a party", {
status: 400,
});
}

if (user.party.adminId !== user.id) {
return Response.json("You must be the party admin to kick a player", {
status: 403,
});
}

const player = user.party.players.find((e) => e.id === body.playerId);
if (!player) {
return Response.json("Player is not in your party", {
status: 400,
});
}

return removePlayer(player.email);
}
18 changes: 11 additions & 7 deletions app/api/party/leave/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import prisma from "@/lib/prisma";
import _ from "lodash";
import { getServerSession } from "next-auth";

export async function POST() {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

export async function removePlayer(email: string) {
const user = await prisma.user.findUniqueOrThrow({
where: {
email: session.user.email,
email,
},
include: {
party: {
Expand Down Expand Up @@ -97,3 +92,12 @@ export async function POST() {

return Response.json(party);
}

export async function POST() {
const session = await getServerSession();
if (!session?.user?.email) {
return Response.json(null, { status: 401 });
}

return removePlayer(session.user.email);
}
14 changes: 9 additions & 5 deletions components/RemovePlayer.tsx → components/KickPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"use client";

import { useContext } from "react";
import { useSWRConfig } from "swr";
import { ErrorContext } from "./App";

export default function RemovePlayer({ playerId }: { playerId: string }) {
export default function KickPlayer({ playerId }: { playerId: string }) {
const { setError } = useContext(ErrorContext);
const { mutate } = useSWRConfig();

async function removePlayer() {
const res = await fetch("/api/party/remove", {
async function onClick() {
const res = await fetch("/api/party/kick", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand All @@ -15,7 +18,8 @@ export default function RemovePlayer({ playerId }: { playerId: string }) {
});

if (!res.ok) {
throw new Error(await res.json());
setError(await res.json());
return;
}

mutate("/api/user");
Expand All @@ -24,7 +28,7 @@ export default function RemovePlayer({ playerId }: { playerId: string }) {
return (
<button
className="btn btn-square btn-xs btn-error"
onClick={() => removePlayer}
onClick={() => onClick()}
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
4 changes: 2 additions & 2 deletions components/Party.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import Spinner from "./Spinner";
import StopGame from "./StopGame";
import Alert from "./Alert";
import PromotePlayer from "./PromotePlayer";
import RemovePlayer from "./RemovePlayer";
import KillTarget from "./KillTarget";
import PartyCard from "./PartyCard";
import KickPlayer from "./KickPlayer";

export default function Party() {
const fetcher: Fetcher<User, string> = (url) =>
Expand Down Expand Up @@ -81,7 +81,7 @@ export default function Party() {
{isAdmin && !isUser && (
<>
<PromotePlayer playerId={player.id} />
<RemovePlayer playerId={player.id} />
<KickPlayer playerId={player.id} />
</>
)}
</div>
Expand Down

0 comments on commit fc35c10

Please sign in to comment.