From 7edec855145bce0462f9e6a809cf376fc1a7fed5 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Mon, 26 Feb 2024 02:55:48 -0500 Subject: [PATCH] add player component --- components/AdminBadge.tsx | 3 -- components/Party.tsx | 69 ++++++++++++++++++++------------------- components/Player.tsx | 46 ++++++++++++++++++++++++++ lib/user.ts | 1 + 4 files changed, 83 insertions(+), 36 deletions(-) delete mode 100644 components/AdminBadge.tsx create mode 100644 components/Player.tsx diff --git a/components/AdminBadge.tsx b/components/AdminBadge.tsx deleted file mode 100644 index 75b2b35..0000000 --- a/components/AdminBadge.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export default function AdminBadge() { - return admin; -} diff --git a/components/Party.tsx b/components/Party.tsx index 97fd419..6a0b8b1 100644 --- a/components/Party.tsx +++ b/components/Party.tsx @@ -6,29 +6,23 @@ import CreateParty from "./CreateParty"; import JoinParty from "./JoinParty"; import StartGame from "./StartGame"; import { Party } from "@prisma/client"; -import AdminBadge from "./AdminBadge"; import useSWR, { Fetcher } from "swr"; import Spinner from "./Spinner"; import StopGame from "./StopGame"; import Alert from "./Alert"; -import PromotePlayer from "./PromotePlayer"; import KillTarget from "./KillTarget"; import PartyCard from "./PartyCard"; -import KickPlayer from "./KickPlayer"; import { User } from "@/lib/user"; import ConfirmKill from "./ConfirmKill"; import DenyKill from "./DenyKill"; -import { useContext } from "react"; -import { ErrorContext } from "./App"; +import Player from "./Player"; export default function Party() { - const { setError } = useContext(ErrorContext); - const fetcher: Fetcher = (url) => fetch(url).then((res) => res.json()); const { data, error, isLoading } = useSWR("/api/user", fetcher, { - refreshInterval: 500, + refreshInterval: 1000, }); if (isLoading) { @@ -54,10 +48,32 @@ export default function Party() { ); } + const isAdmin = party.adminId === user.id; + if (party.started) { - if (!user.target) { - setError("User has no target"); - return; + if (!user.target || !user.alive) { + const players = party.players.map((player, i) => ( + + )); + + return ( + +

+ You have died, waiting for round to finish... +

+
    {players}
+
+ {isAdmin && } + +
+
+ ); } return ( @@ -79,28 +95,15 @@ export default function Party() { ); } - const isAdmin = party.adminId === user.id; - - const players = party.players.map((player) => { - const isUser = user.id === player.id; - - return ( -
  • -
    -

    {player.name}

    -
    - {party.adminId === player.id && } - {isAdmin && !isUser && ( - <> - - - - )} -
    -
    -
  • - ); - }); + const players = party.players.map((player, i) => ( + + )); return ( diff --git a/components/Player.tsx b/components/Player.tsx new file mode 100644 index 0000000..59d8df3 --- /dev/null +++ b/components/Player.tsx @@ -0,0 +1,46 @@ +import { User } from "@prisma/client"; +import PromotePlayer from "./PromotePlayer"; +import KickPlayer from "./KickPlayer"; +import { Party } from "@/lib/user"; + +export function AdminBadge() { + return admin; +} + +export function AliveBadge() { + return alive; +} + +export function DeadBadge() { + return dead; +} + +export default function Player({ + player, + isAdmin, + userId, + party, +}: { + player: User; + isAdmin: boolean; + userId: string; + party: Party; +}) { + return ( +
  • +
    +

    {player.name}

    +
    + {party.adminId === player.id && } + {party.started && (player.alive ? : )} + {isAdmin && userId !== player.id && ( + <> + + + + )} +
    +
    +
  • + ); +} diff --git a/lib/user.ts b/lib/user.ts index 6be1e2b..b0c939a 100644 --- a/lib/user.ts +++ b/lib/user.ts @@ -1,6 +1,7 @@ import prisma from "./prisma"; export type User = Awaited>; +export type Party = NonNullable; export async function getUser(email: string) { return await prisma.user.findUniqueOrThrow({