From 14dc1139fbbaf84f59dcbcc1ebd961e9a103d136 Mon Sep 17 00:00:00 2001 From: Gavin McDonald Date: Wed, 16 Apr 2025 15:20:06 -0400 Subject: [PATCH] adjustable settings --- app/[gameID]/page.tsx | 25 ++++++++++++------- components/Card.tsx | 20 ++++++--------- components/CopyButton.tsx | 23 ++++++++++-------- components/Settings.tsx | 51 +++++++++++++++++++++++++-------------- components/Switch.tsx | 27 +++++++++++++++++++++ components/ToolTip.tsx | 2 +- lib/GameStore.ts | 20 ++++++++++++--- server.ts | 23 +++++++++++++++--- tools/getCardInfo.ts | 21 +++++++++++----- types/index.ts | 9 +++++++ 10 files changed, 158 insertions(+), 63 deletions(-) create mode 100644 components/Switch.tsx diff --git a/app/[gameID]/page.tsx b/app/[gameID]/page.tsx index d25ca59..4ab833e 100644 --- a/app/[gameID]/page.tsx +++ b/app/[gameID]/page.tsx @@ -4,9 +4,9 @@ import { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import { socket } from '@/socket'; +import Settings from '@/components/Settings'; import Card from '@/components/Card'; import NotFound from '@/components/NotFound'; -import CopyButton from '@/components/CopyButton'; import { cardMap, layout } from '@/constants/tarokka'; import type { GameUpdate, ClientUpdate } from '@/types'; @@ -16,12 +16,19 @@ export default function GamePage() { const [gameID, setGameID] = useState(''); const [noGame, setNoGame] = useState(false); - const [{ dmID, spectatorID, cards }, setGameData] = useState({ + const [gameData, setGameData] = useState({ dmID: '', spectatorID: '', cards: [], + settings: { + positionBack: false, + positionFront: false, + prophecy: false, + notes: false, + }, }); + const { dmID, cards, settings } = gameData; const isDM = !!dmID; useEffect(() => { @@ -39,7 +46,7 @@ export default function GamePage() { setGameData(data); }); - socket.on('card-flipped', (data: GameUpdate) => { + socket.on('game-update', (data: GameUpdate) => { console.log('>>>', data); setGameData(data); }); @@ -68,6 +75,10 @@ export default function GamePage() { socket.emit('flip-card', flip); }; + const handleSettings = (gameData: GameUpdate) => { + socket.emit('settings', { gameID, gameData }); + }; + // map our five Tarokka cards to their proper locations in a 3x3 grid // common deck cards: left, top, and right // high deck cards: bottom and center @@ -77,12 +88,7 @@ export default function GamePage() { ) : cards ? (
-
- {dmID && } - {spectatorID && ( - - )} -
+ {isDM && }
{Array.from({ length: 9 }) .map(arrangeCards) @@ -93,6 +99,7 @@ export default function GamePage() { dm={isDM} card={card} position={layout[cardMap[index]]} + settings={settings} flipAction={() => flipCard(cardMap[index])} /> )} diff --git a/components/Card.tsx b/components/Card.tsx index cff57c2..2e4017b 100644 --- a/components/Card.tsx +++ b/components/Card.tsx @@ -1,11 +1,10 @@ 'use client'; -import { useRef, useState } from 'react'; import ToolTip from '@/components/ToolTip'; import tarokkaCards from '@/constants/tarokkaCards'; import getCardInfo from '@/tools/getCardInfo'; -import { Layout, TarokkaGameCard } from '@/types'; +import { Layout, Settings, TarokkaGameCard } from '@/types'; const cardBack = tarokkaCards.find((card) => card.back)!; @@ -13,11 +12,12 @@ type CardProps = { dm: boolean; card: TarokkaGameCard; position: Layout; + settings: Settings; flipAction: () => void; }; -export default function Card({ dm, card, position, flipAction }: CardProps) { - const { aria, card: cardName, description, flipped, url } = card; +export default function Card({ dm, card, position, settings, flipAction }: CardProps) { + const { aria, flipped, url } = card; const handleClick = () => { if (dm) { @@ -26,9 +26,9 @@ export default function Card({ dm, card, position, flipAction }: CardProps) { }; const getTooltip = () => { - const text = getCardInfo(card, position, dm); + const text = getCardInfo(card, position, dm, settings); - return ( + return text.length ? ( <> {text.map((t, i) => (
@@ -37,7 +37,7 @@ export default function Card({ dm, card, position, flipAction }: CardProps) {
))} - ); + ) : null; }; return ( @@ -50,11 +50,7 @@ export default function Card({ dm, card, position, flipAction }: CardProps) { className={`transition-transform duration-500 transform-style-preserve-3d ${flipped ? 'rotate-y-180' : ''}`} >
- Card Back + Card Back
{aria} diff --git a/components/CopyButton.tsx b/components/CopyButton.tsx index f119bda..2f055de 100644 --- a/components/CopyButton.tsx +++ b/components/CopyButton.tsx @@ -3,6 +3,8 @@ import { useState } from 'react'; import { Copy as CopyIcon } from 'lucide-react'; +import ToolTip from '@/components/ToolTip'; + type CopyButtonProps = { title: string; copy: string; @@ -22,15 +24,16 @@ export default function CopyButton({ title, copy }: CopyButtonProps) { }; return ( - + + + ); } diff --git a/components/Settings.tsx b/components/Settings.tsx index 480edaf..d91d584 100644 --- a/components/Settings.tsx +++ b/components/Settings.tsx @@ -1,41 +1,56 @@ 'use client'; import { useState } from 'react'; -import { Settings as Gear } from 'lucide-react'; +import { Settings as Gear, X } from 'lucide-react'; -import { Settings } from '@/types'; +import CopyButton from '@/components/CopyButton'; +import Switch from '@/components/Switch'; +import { GameUpdate } from '@/types'; type PermissionTogglePanelProps = { - settings: Settings; - changeAction: (updatedSettings: Settings) => void; + gameData: GameUpdate; + changeAction: (updatedSettings: GameUpdate) => void; }; export default function PermissionTogglePanel({ - settings, + gameData, changeAction, }: PermissionTogglePanelProps) { const [open, setOpen] = useState(false); const togglePermission = (key: string) => { - changeAction({ ...settings, [key]: !settings[key] }); + changeAction({ + ...gameData, + settings: { + ...gameData.settings, + [key]: !gameData.settings[key], + }, + }); }; return (
- + {!open && ( + + )} {open && ( -
- {Object.entries(settings).map(([key, value]) => ( - +
+ + + + {Object.entries(gameData.settings).map(([key, value]) => ( + togglePermission(key)} /> ))}
)} diff --git a/components/Switch.tsx b/components/Switch.tsx new file mode 100644 index 0000000..d1ad149 --- /dev/null +++ b/components/Switch.tsx @@ -0,0 +1,27 @@ +export interface SwitchProps { + label: string; + value: boolean; + toggleAction: (event: React.ChangeEvent) => void; +} + +export default function Switch({ label, value, toggleAction }: SwitchProps) { + return ( +