From 223dfa72f5f130899d4f9fe8e36b731b50fa40d2 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Sun, 18 Feb 2024 02:06:58 -0500 Subject: [PATCH] added start and stop game --- components/Party.tsx | 30 ++++++++++++++++------------ components/PartyStarted.tsx | 38 ++++++++++++++++++++++++++++++++++++ components/PromotePlayer.tsx | 0 components/RemovePlayer.tsx | 33 +++++++++++++++++++++++++++++++ components/StartGame.tsx | 4 +++- components/StopGame.tsx | 31 +++++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 components/PartyStarted.tsx create mode 100644 components/PromotePlayer.tsx create mode 100644 components/RemovePlayer.tsx create mode 100644 components/StopGame.tsx diff --git a/components/Party.tsx b/components/Party.tsx index 2414ac4..766c7b4 100644 --- a/components/Party.tsx +++ b/components/Party.tsx @@ -7,6 +7,9 @@ import JoinParty from "./JoinParty"; import StartGame from "./StartGame"; import { Party } from "@prisma/client"; import AdminBadge from "./AdminBadge"; +import PartyStarted from "./PartyStarted"; + +export type User = Awaited>; async function getUser() { const session = await getServerSession(); @@ -24,6 +27,7 @@ async function getUser() { players: true, }, }, + target: true, }, }); @@ -46,6 +50,10 @@ export default async function Party() { ); } + if (party.started) { + return ; + } + const players = party.players.map((player) => (
  • {player.name} @@ -54,17 +62,15 @@ export default async function Party() { )); return ( - <> -
    -

    - Code: {party.code.toUpperCase()} -

    -

    Mode: {_.startCase(_.toLower(party.mode))}

    -

    Players:

    -
      {players}
    - {party.adminId === user.id && } - -
    - +
    +

    + Code: {party.code.toUpperCase()} +

    +

    Mode: {_.startCase(_.toLower(party.mode))}

    +

    Players:

    +
      {players}
    + {party.adminId === user.id && } + +
    ); } diff --git a/components/PartyStarted.tsx b/components/PartyStarted.tsx new file mode 100644 index 0000000..2ce9c4d --- /dev/null +++ b/components/PartyStarted.tsx @@ -0,0 +1,38 @@ +import _ from "lodash"; +import LeaveParty from "./LeaveParty"; +import { User } from "./Party"; +import StartGame from "./StartGame"; +import StopGame from "./StopGame"; + +export default function PartyStarted({ user }: { user: User }) { + if (!user) { + throw new Error("User does not exist"); + } + + if (!user.party) { + throw new Error("User is not party of a party"); + } + + if (!user.party.started) { + throw new Error("Party has not started"); + } + + if (!user.target) { + throw new Error("User does not have a target"); + } + + const party = user.party; + + return ( +
    +

    + Code: {party.code.toUpperCase()} +

    +

    Mode: {_.startCase(_.toLower(party.mode))}

    +

    Target:

    +
      {user.target.name}
    + {party.adminId === user.id && } + +
    + ); +} diff --git a/components/PromotePlayer.tsx b/components/PromotePlayer.tsx new file mode 100644 index 0000000..e69de29 diff --git a/components/RemovePlayer.tsx b/components/RemovePlayer.tsx new file mode 100644 index 0000000..c7ecb8e --- /dev/null +++ b/components/RemovePlayer.tsx @@ -0,0 +1,33 @@ +"use client"; + +import { useRouter } from "next/navigation"; + +export default function RemovePlayer({ playerId }: { playerId: string }) { + const router = useRouter(); + + async function removePlayer(playerId: string) { + const res = await fetch("/api/party/remove", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ playerId }), + }); + + if (!res.ok) { + throw new Error(await res.json()); + } + + router.refresh(); + } + + return ( + + ); +} diff --git a/components/StartGame.tsx b/components/StartGame.tsx index 1310e19..863293f 100644 --- a/components/StartGame.tsx +++ b/components/StartGame.tsx @@ -2,7 +2,7 @@ import { useRouter } from "next/navigation"; -export default function StartParty() { +export default function StartGame() { const router = useRouter(); async function startGame() { @@ -14,6 +14,8 @@ export default function StartParty() { throw new Error(await res.json()); } + console.log(await res.json()); + router.refresh(); } diff --git a/components/StopGame.tsx b/components/StopGame.tsx new file mode 100644 index 0000000..d90157e --- /dev/null +++ b/components/StopGame.tsx @@ -0,0 +1,31 @@ +"use client"; + +import { useRouter } from "next/navigation"; + +export default function StopGame() { + const router = useRouter(); + + async function stopGame() { + const res = await fetch("/api/party/stop", { + method: "POST", + }); + + if (!res.ok) { + throw new Error(await res.json()); + } + + console.log(await res.json()); + + router.refresh(); + } + + return ( + + ); +}