Compare commits
13 Commits
fc0466ae89
...
trunk
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
269abd237e | ||
|
|
52636017a5 | ||
|
|
9ca34540e8 | ||
|
|
6e312d5d2e | ||
|
|
11245bf4d8 | ||
|
|
62c3b7b557 | ||
|
|
01a05d2aa1 | ||
|
|
c6c1c63dcd | ||
|
|
87de2f57b2 | ||
|
|
b4b0c853f1 | ||
|
|
c6e316a1f8 | ||
|
|
d3eb6f1b46 | ||
|
|
8cbf281ef8 |
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { createContext, useContext, useEffect, useState } from 'react';
|
import { createContext, useContext, useEffect, useState } from 'react';
|
||||||
import useSocket from '@/hooks/useSocket';
|
import useSocket from '@/hooks/useSocket';
|
||||||
|
import { reduceTilts } from '@/tools';
|
||||||
|
|
||||||
import { GAME_START, LOCAL_DEFAULTS } from '@/constants';
|
import { GAME_START, LOCAL_DEFAULTS } from '@/constants';
|
||||||
import type { Dispatch, ReactNode, SetStateAction } from 'react';
|
import type { Dispatch, ReactNode, SetStateAction } from 'react';
|
||||||
@@ -15,7 +16,7 @@ export interface AppContext {
|
|||||||
noGame: boolean;
|
noGame: boolean;
|
||||||
selectCardIndex: number;
|
selectCardIndex: number;
|
||||||
settings: Settings;
|
settings: Settings;
|
||||||
tilt: Tilt[];
|
tilts: Tilt[];
|
||||||
emitFlip: (cardIndex: number) => void;
|
emitFlip: (cardIndex: number) => void;
|
||||||
emitSettings: (gameData: GameUpdate) => void;
|
emitSettings: (gameData: GameUpdate) => void;
|
||||||
emitRedraw: (cardIndex: number) => void;
|
emitRedraw: (cardIndex: number) => void;
|
||||||
@@ -23,7 +24,7 @@ export interface AppContext {
|
|||||||
setGameID: (gameID: string) => void;
|
setGameID: (gameID: string) => void;
|
||||||
setLocalSettings: Dispatch<SetStateAction<LocalSettings>>;
|
setLocalSettings: Dispatch<SetStateAction<LocalSettings>>;
|
||||||
setSelectCardIndex: (cardIndex: number) => void;
|
setSelectCardIndex: (cardIndex: number) => void;
|
||||||
setTilt: (tilt: Tilt[]) => void;
|
setLocalTilt: (tilt: Tilt[]) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function AppProvider({ children }: { children: ReactNode }) {
|
export function AppProvider({ children }: { children: ReactNode }) {
|
||||||
@@ -32,7 +33,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
const [gameID, setGameID] = useState('');
|
const [gameID, setGameID] = useState('');
|
||||||
const [noGame, setNoGame] = useState(false);
|
const [noGame, setNoGame] = useState(false);
|
||||||
const [selectCardIndex, setSelectCardIndex] = useState(-1);
|
const [selectCardIndex, setSelectCardIndex] = useState(-1);
|
||||||
const [tilt, setTilt] = useState<Tilt[]>([]);
|
const [localTilt, setLocalTilt] = useState<Tilt[]>([]);
|
||||||
|
|
||||||
const { emitFlip, emitRedraw, emitSelect, emitSettings, emitTilt } = useSocket({
|
const { emitFlip, emitRedraw, emitSelect, emitSettings, emitTilt } = useSocket({
|
||||||
gameID,
|
gameID,
|
||||||
@@ -42,17 +43,17 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (localSettings.remoteTilt) {
|
if (localSettings.remoteTilt) {
|
||||||
const cardIndex = tilt.findIndex((tilt) => !!tilt);
|
const cardIndex = localTilt.findIndex((tilt) => !!tilt);
|
||||||
|
|
||||||
if (tilt[cardIndex]) {
|
if (localTilt[cardIndex]) {
|
||||||
emitTilt(cardIndex, tilt[cardIndex]);
|
emitTilt(cardIndex, localTilt[cardIndex]);
|
||||||
} else {
|
} else {
|
||||||
// cardIndex does not matter
|
// cardIndex does not matter
|
||||||
// all tilts for this user will be cleared
|
// all tilts for this user will be cleared
|
||||||
emitTilt(0, { rotateX: 0, rotateY: 0 });
|
emitTilt(0, { percentX: -1, percentY: -1, rotateX: 0, rotateY: 0 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [tilt, localSettings]);
|
}, [localTilt, localSettings]);
|
||||||
|
|
||||||
const handleSelect = (cardID: string) => {
|
const handleSelect = (cardID: string) => {
|
||||||
setSelectCardIndex(-1);
|
setSelectCardIndex(-1);
|
||||||
@@ -62,14 +63,15 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
|
|
||||||
const { dmID } = gameData;
|
const { dmID } = gameData;
|
||||||
const isDM = !!dmID;
|
const isDM = !!dmID;
|
||||||
|
const settings = { ...gameData.settings, ...localSettings };
|
||||||
|
|
||||||
const appInterface = {
|
const appInterface = {
|
||||||
gameData,
|
gameData,
|
||||||
isDM,
|
isDM,
|
||||||
noGame,
|
noGame,
|
||||||
selectCardIndex,
|
selectCardIndex,
|
||||||
settings: { ...gameData.settings, ...localSettings },
|
settings,
|
||||||
tilt,
|
tilts: reduceTilts(gameData, localTilt, settings),
|
||||||
emitFlip,
|
emitFlip,
|
||||||
emitSettings,
|
emitSettings,
|
||||||
emitRedraw,
|
emitRedraw,
|
||||||
@@ -77,7 +79,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
setGameID,
|
setGameID,
|
||||||
setLocalSettings,
|
setLocalSettings,
|
||||||
setSelectCardIndex,
|
setSelectCardIndex,
|
||||||
setTilt,
|
setLocalTilt,
|
||||||
};
|
};
|
||||||
|
|
||||||
return <AppContext.Provider value={appInterface}>{children}</AppContext.Provider>;
|
return <AppContext.Provider value={appInterface}>{children}</AppContext.Provider>;
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ export default function GamePage() {
|
|||||||
return noGame ? (
|
return noGame ? (
|
||||||
<NotFound />
|
<NotFound />
|
||||||
) : (
|
) : (
|
||||||
<main className="min-h-screen flex flex-col items-center justify-center gap-4 bg-[url('/img/table3.png')] bg-cover bg-center">
|
<main className="h-dvh flex flex-col items-center justify-center gap-4 bg-[url('/img/table3.png')] bg-cover bg-center">
|
||||||
<SpectatorLink />
|
<SpectatorLink />
|
||||||
<Settings />
|
<Settings />
|
||||||
<TarokkaGrid />
|
<TarokkaGrid />
|
||||||
|
|||||||
@@ -1,26 +1,14 @@
|
|||||||
import type { Metadata } from 'next';
|
import type { Metadata } from 'next';
|
||||||
import { Pirata_One, Eagle_Lake, Cinzel_Decorative } from 'next/font/google';
|
import { Eagle_Lake } from 'next/font/google';
|
||||||
import { AppProvider } from '@/app/AppContext';
|
import { AppProvider } from '@/app/AppContext';
|
||||||
import './globals.css';
|
import './globals.css';
|
||||||
|
|
||||||
const pirataOne = Pirata_One({
|
|
||||||
variable: '--font-pirata',
|
|
||||||
subsets: ['latin'],
|
|
||||||
weight: '400',
|
|
||||||
});
|
|
||||||
|
|
||||||
const eagleLake = Eagle_Lake({
|
const eagleLake = Eagle_Lake({
|
||||||
variable: '--font-eagle-lake',
|
variable: '--font-eagle-lake',
|
||||||
subsets: ['latin'],
|
subsets: ['latin'],
|
||||||
weight: '400',
|
weight: '400',
|
||||||
});
|
});
|
||||||
|
|
||||||
const cinzel = Cinzel_Decorative({
|
|
||||||
variable: '--font-cinzel',
|
|
||||||
subsets: ['latin'],
|
|
||||||
weight: '400',
|
|
||||||
});
|
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: 'Tarokka',
|
title: 'Tarokka',
|
||||||
description: 'Fortune telling for D&D’s Curse of Strahd',
|
description: 'Fortune telling for D&D’s Curse of Strahd',
|
||||||
@@ -37,11 +25,8 @@ export default function RootLayout({
|
|||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}>) {
|
}>) {
|
||||||
return (
|
return (
|
||||||
<html
|
<html lang="en" className={`${eagleLake.variable} antialiased overscroll-none`}>
|
||||||
lang="en"
|
<body className={`${eagleLake.className} antialiased h-dvh`}>
|
||||||
className={`${pirataOne.variable} ${eagleLake.variable} ${cinzel.variable} antialiased`}
|
|
||||||
>
|
|
||||||
<body className={`${eagleLake.className} antialiased`}>
|
|
||||||
<AppProvider>{children}</AppProvider>
|
<AppProvider>{children}</AppProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export default function Home() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="min-h-screen flex justify-center items-center text-yellow-400 bg-[url('/img/table3.png')] bg-cover bg-center">
|
<main className="flex justify-center items-center h-dvh text-yellow-400 bg-[url('/img/table3.png')] bg-cover bg-center">
|
||||||
<div className="flex flex-col items-center gap-8 text-center">
|
<div className="flex flex-col items-center gap-8 text-center">
|
||||||
<h1 className="text-5xl font-bold text-center text-primary">Tarokka</h1>
|
<h1 className="text-5xl font-bold text-center text-primary">Tarokka</h1>
|
||||||
<p className="text-l text-center w-[350px] m-auto">
|
<p className="text-l text-center w-[350px] m-auto">
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import TiltCard from '@/components/TiltCard';
|
|||||||
import ToolTip from '@/components/ToolTip';
|
import ToolTip from '@/components/ToolTip';
|
||||||
import StackTheDeck from '@/components/StackTheDeck';
|
import StackTheDeck from '@/components/StackTheDeck';
|
||||||
import Sheen from '@/components/Sheen';
|
import Sheen from '@/components/Sheen';
|
||||||
import getCardInfo from '@/tools/getCardInfo';
|
import { getCardInfo, getURL } from '@/tools';
|
||||||
import getURL from '@/tools/getURL';
|
|
||||||
|
|
||||||
import tarokkaCards from '@/constants/tarokkaCards';
|
import tarokkaCards from '@/constants/tarokkaCards';
|
||||||
import { layout } from '@/constants/tarokka';
|
import { layout } from '@/constants/tarokka';
|
||||||
@@ -55,7 +54,7 @@ export default function Card({ card, cardIndex }: CardProps) {
|
|||||||
return (
|
return (
|
||||||
<ToolTip content={tooltip || getTooltip()}>
|
<ToolTip content={tooltip || getTooltip()}>
|
||||||
<TiltCard
|
<TiltCard
|
||||||
className={`h-[21vh] w-[15vh] relative perspective transition-transform duration-200 z-0 hover:z-10 hover:scale-150 ${isDM ? 'cursor-pointer' : ''} `}
|
className={`h-[21vh] w-[15vh] max-w-[30vw] relative perspective transition-transform duration-200 z-0 hover:z-10 hover:scale-150 ${isDM ? 'cursor-pointer' : ''} `}
|
||||||
cardIndex={cardIndex}
|
cardIndex={cardIndex}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { CircleX } from 'lucide-react';
|
import { CircleX } from 'lucide-react';
|
||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
import TarokkaDeck from '@/lib/TarokkaDeck';
|
import TarokkaDeck from '@/lib/TarokkaDeck';
|
||||||
import getURL from '@/tools/getURL';
|
import { getURL } from '@/tools';
|
||||||
|
|
||||||
import { Deck } from '@/types';
|
import { Deck } from '@/types';
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { CircleX, ScrollText } from 'lucide-react';
|
|||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
import CopyButton from '@/components/CopyButton';
|
import CopyButton from '@/components/CopyButton';
|
||||||
import Scrim from '@/components/Scrim';
|
import Scrim from '@/components/Scrim';
|
||||||
import getCardInfo from '@/tools/getCardInfo';
|
import { getCardInfo } from '@/tools';
|
||||||
import { cardMap, layout } from '@/constants/tarokka';
|
import { cardMap, layout } from '@/constants/tarokka';
|
||||||
|
|
||||||
export default function Notes() {
|
export default function Notes() {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ export default function CardStyle({ className }: { className?: string }) {
|
|||||||
flex justify-center
|
flex justify-center
|
||||||
cursor-pointer
|
cursor-pointer
|
||||||
w-full px-3 py-2
|
w-full px-3 py-2
|
||||||
text-xs font-medium
|
text-xs font-medium capitalize
|
||||||
border border-yellow-500
|
border border-yellow-500
|
||||||
transition hover:text-yellow-300 hover:drop-shadow-[0_0_3px_#ffd700]
|
transition hover:text-yellow-300 hover:drop-shadow-[0_0_3px_#ffd700]
|
||||||
${settings.cardStyle === option ? 'bg-slate-700 text-yellow-300 font-extrabold' : 'bg-slate-800 hover:bg-slate-700'}
|
${settings.cardStyle === option ? 'bg-slate-700 text-yellow-300 font-extrabold' : 'bg-slate-800 hover:bg-slate-700'}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { CircleX, Settings as Gear } from 'lucide-react';
|
import { CircleX, Settings as Gear } from 'lucide-react';
|
||||||
import { Cinzel_Decorative } from 'next/font/google';
|
|
||||||
|
|
||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
import Scrim from '@/components/Scrim';
|
import Scrim from '@/components/Scrim';
|
||||||
@@ -12,18 +11,12 @@ import ExternalLinks from './ExternalLinks';
|
|||||||
import GameLinks from './GameLinks';
|
import GameLinks from './GameLinks';
|
||||||
import Permissions from './Permissions';
|
import Permissions from './Permissions';
|
||||||
|
|
||||||
const cinzel = Cinzel_Decorative({
|
|
||||||
variable: '--font-cinzel',
|
|
||||||
subsets: ['latin'],
|
|
||||||
weight: '400',
|
|
||||||
});
|
|
||||||
|
|
||||||
export default function Settings() {
|
export default function Settings() {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const { isDM } = useAppContext();
|
const { isDM } = useAppContext();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`fixed top-4 right-4 z-25 ${cinzel.className}`}>
|
<div className={`fixed top-4 right-4 z-25`}>
|
||||||
<Scrim
|
<Scrim
|
||||||
clickAction={() => setOpen((prev) => !prev)}
|
clickAction={() => setOpen((prev) => !prev)}
|
||||||
className={`transition-all duration-250 ${open ? 'pointer-events-auto opacity-100' : 'pointer-events-none opacity-0'}`}
|
className={`transition-all duration-250 ${open ? 'pointer-events-auto opacity-100' : 'pointer-events-none opacity-0'}`}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
|
import { validTilt } from '@/tools';
|
||||||
|
|
||||||
const tiltSheen = (sheen: HTMLDivElement, tiltX: number, tiltY: number) => {
|
const tiltSheen = (sheen: HTMLDivElement, x: number, y: number) => {
|
||||||
const rect = sheen.getBoundingClientRect();
|
const rect = sheen.getBoundingClientRect();
|
||||||
const centerX = rect.width / 2;
|
const sheenX = rect.width - x * rect.width;
|
||||||
const centerY = rect.height / 2;
|
const sheenY = rect.height - y * rect.height;
|
||||||
const sheenX = centerX + (tiltY / -20) * centerX;
|
|
||||||
const sheenY = centerY + (tiltX / 20) * centerY;
|
|
||||||
|
|
||||||
sheen.style.opacity = '1';
|
sheen.style.opacity = '1';
|
||||||
sheen.style.backgroundImage = `
|
sheen.style.backgroundImage = `
|
||||||
@@ -22,49 +21,21 @@ const tiltSheen = (sheen: HTMLDivElement, tiltX: number, tiltY: number) => {
|
|||||||
export default function Sheen({ cardIndex, className }: { cardIndex: number; className?: string }) {
|
export default function Sheen({ cardIndex, className }: { cardIndex: number; className?: string }) {
|
||||||
const sheenRef = useRef<HTMLDivElement>(null);
|
const sheenRef = useRef<HTMLDivElement>(null);
|
||||||
const [untilt, setUntilt] = useState(false);
|
const [untilt, setUntilt] = useState(false);
|
||||||
const {
|
const { tilts } = useAppContext();
|
||||||
gameData,
|
|
||||||
settings: { tilt, remoteTilt },
|
|
||||||
tilt: localTilts,
|
|
||||||
} = useAppContext();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const sheen = sheenRef.current;
|
const sheen = sheenRef.current;
|
||||||
if (!sheen) return;
|
if (!sheen) return;
|
||||||
|
|
||||||
if (tilt) {
|
const tilt = tilts[cardIndex];
|
||||||
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
|
||||||
const rotateY = localTilts[cardIndex]?.rotateY || 0;
|
|
||||||
|
|
||||||
const tilts = remoteTilt
|
if (validTilt(tilt)) {
|
||||||
? [...gameData.tilts[cardIndex], { rotateX, rotateY }]
|
|
||||||
: [{ rotateX, rotateY }];
|
|
||||||
|
|
||||||
const { totalX, totalY, count } = tilts
|
|
||||||
.filter(({ rotateX, rotateY }) => !!rotateX && !!rotateY)
|
|
||||||
.reduce(
|
|
||||||
({ totalX, totalY, count }, { rotateX, rotateY }) => ({
|
|
||||||
totalX: totalX + rotateX,
|
|
||||||
totalY: totalY + rotateY,
|
|
||||||
count: ++count,
|
|
||||||
}),
|
|
||||||
{ totalX: 0, totalY: 0, count: 0 },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (count && (totalX || totalY)) {
|
|
||||||
setUntilt(false);
|
setUntilt(false);
|
||||||
|
tiltSheen(sheen, tilt.percentX, tilt.percentY);
|
||||||
const x = totalX / count;
|
|
||||||
const y = totalY / count;
|
|
||||||
|
|
||||||
tiltSheen(sheen, x, y);
|
|
||||||
} else {
|
} else {
|
||||||
setUntilt(true);
|
setUntilt(true);
|
||||||
}
|
}
|
||||||
} else {
|
}, [tilts]);
|
||||||
setUntilt(true);
|
|
||||||
}
|
|
||||||
}, [tilt, localTilts, gameData]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const sheen = sheenRef.current;
|
const sheen = sheenRef.current;
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ export default function TarokkaGrid() {
|
|||||||
const arrangeCards = (_cell: unknown, index: number) => cards[cardMap[index]];
|
const arrangeCards = (_cell: unknown, index: number) => cards[cardMap[index]];
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="grid grid-cols-3 grid-rows-3 gap-8 w-fit mx-auto">
|
<div className="grid grid-cols-3 grid-rows-3 gap-2 sm:gap-4 md:gap-8 w-fit mx-auto">
|
||||||
{Array.from({ length: 9 })
|
{Array.from({ length: 9 })
|
||||||
.map(arrangeCards)
|
.map(arrangeCards)
|
||||||
.map((card, index) => (
|
.map((card, index) => (
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { useAppContext } from '@/app/AppContext';
|
import { useAppContext } from '@/app/AppContext';
|
||||||
import throttle from '@/tools/throttle';
|
import { throttle, validTilt } from '@/tools';
|
||||||
|
|
||||||
import { thirtyFPS } from '@/constants/time';
|
import { thirtyFPS } from '@/constants/time';
|
||||||
import type { Tilt } from '@/types';
|
import type { Tilt } from '@/types';
|
||||||
@@ -18,50 +18,21 @@ export default function TiltCard({
|
|||||||
}) {
|
}) {
|
||||||
const cardRef = useRef<HTMLDivElement>(null);
|
const cardRef = useRef<HTMLDivElement>(null);
|
||||||
const [untilt, setUntilt] = useState(false);
|
const [untilt, setUntilt] = useState(false);
|
||||||
const {
|
const { settings, tilts, setLocalTilt } = useAppContext();
|
||||||
gameData,
|
|
||||||
settings: { tilt, remoteTilt },
|
|
||||||
setTilt,
|
|
||||||
tilt: localTilts,
|
|
||||||
} = useAppContext();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
if (!card) return;
|
if (!card) return;
|
||||||
|
|
||||||
if (tilt) {
|
const tilt = tilts[cardIndex];
|
||||||
const rotateX = localTilts[cardIndex]?.rotateX || 0;
|
|
||||||
const rotateY = localTilts[cardIndex]?.rotateY || 0;
|
|
||||||
|
|
||||||
const tilts = remoteTilt
|
if (validTilt(tilt)) {
|
||||||
? [...gameData.tilts[cardIndex], { rotateX, rotateY }]
|
|
||||||
: [{ rotateX, rotateY }];
|
|
||||||
|
|
||||||
const { totalX, totalY, count } = tilts
|
|
||||||
.filter(({ rotateX, rotateY }) => !!rotateX && !!rotateY)
|
|
||||||
.reduce(
|
|
||||||
({ totalX, totalY, count }, { rotateX, rotateY }) => ({
|
|
||||||
totalX: totalX + rotateX,
|
|
||||||
totalY: totalY + rotateY,
|
|
||||||
count: ++count,
|
|
||||||
}),
|
|
||||||
{ totalX: 0, totalY: 0, count: 0 },
|
|
||||||
);
|
|
||||||
|
|
||||||
if (count && (totalX || totalY)) {
|
|
||||||
setUntilt(false);
|
setUntilt(false);
|
||||||
|
card.style.transform = `rotateX(${tilt.rotateX}deg) rotateY(${tilt.rotateY}deg)`;
|
||||||
const x = totalX / count;
|
|
||||||
const y = totalY / count;
|
|
||||||
|
|
||||||
card.style.transform = `rotateX(${x}deg) rotateY(${y}deg)`;
|
|
||||||
} else {
|
} else {
|
||||||
setUntilt(true);
|
setUntilt(true);
|
||||||
}
|
}
|
||||||
} else if (card.style.transform !== ZERO_ROTATION) {
|
}, [tilts]);
|
||||||
setUntilt(true);
|
|
||||||
}
|
|
||||||
}, [tilt, localTilts, gameData]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
@@ -70,33 +41,64 @@ export default function TiltCard({
|
|||||||
card.style.transform = ZERO_ROTATION;
|
card.style.transform = ZERO_ROTATION;
|
||||||
}, [untilt]);
|
}, [untilt]);
|
||||||
|
|
||||||
const handleMouseMove = throttle((e: React.MouseEvent) => {
|
const handleTilt = (x: number, y: number) => {
|
||||||
const card = cardRef.current;
|
const card = cardRef.current;
|
||||||
if (!card) return;
|
if (!card) return;
|
||||||
|
|
||||||
const rect = card.getBoundingClientRect();
|
const rect = card.getBoundingClientRect();
|
||||||
const x = e.clientX - rect.left;
|
x -= rect.left;
|
||||||
const y = e.clientY - rect.top;
|
y -= rect.top;
|
||||||
|
|
||||||
const centerX = rect.width / 2;
|
const centerX = rect.width / 2;
|
||||||
const centerY = rect.height / 2;
|
const centerY = rect.height / 2;
|
||||||
|
|
||||||
const rotateX = ((y - centerY) / centerY) * -20;
|
const rotateX = ((y - centerY) / centerY) * -20;
|
||||||
const rotateY = ((x - centerX) / centerX) * 20;
|
const rotateY = ((x - centerX) / centerX) * 20;
|
||||||
|
const percentX = x / rect.width;
|
||||||
|
const percentY = y / rect.height;
|
||||||
|
|
||||||
const newTilt: Tilt[] = [];
|
const newTilt: Tilt[] = [];
|
||||||
newTilt[cardIndex] = { rotateX, rotateY };
|
newTilt[cardIndex] = {
|
||||||
|
percentX,
|
||||||
|
percentY,
|
||||||
|
rotateX,
|
||||||
|
rotateY,
|
||||||
|
};
|
||||||
|
|
||||||
setTilt(newTilt);
|
setLocalTilt(newTilt);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMouseMove = throttle((e: React.MouseEvent) => {
|
||||||
|
handleTilt(e.clientX, e.clientY);
|
||||||
|
}, thirtyFPS);
|
||||||
|
|
||||||
|
const handleTouchMove = throttle((e: React.TouchEvent) => {
|
||||||
|
const card = cardRef.current;
|
||||||
|
const touch = e.touches[0];
|
||||||
|
|
||||||
|
if (card && touch) {
|
||||||
|
const rect = card.getBoundingClientRect();
|
||||||
|
const x = touch.clientX;
|
||||||
|
const y = touch.clientY;
|
||||||
|
|
||||||
|
if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
|
||||||
|
handleTilt(x, y);
|
||||||
|
} else {
|
||||||
|
setLocalTilt([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}, thirtyFPS);
|
}, thirtyFPS);
|
||||||
|
|
||||||
const handleMouseLeave = () => {
|
const handleMouseLeave = () => {
|
||||||
setTilt([]);
|
setLocalTilt([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`group ${className}`}
|
className={`group ${className}`}
|
||||||
onMouseMove={tilt ? handleMouseMove : undefined}
|
onMouseMove={settings.tilt ? handleMouseMove : undefined}
|
||||||
|
onTouchMove={settings.tilt ? handleTouchMove : undefined}
|
||||||
|
onTouchEnd={handleMouseLeave}
|
||||||
onMouseLeave={handleMouseLeave}
|
onMouseLeave={handleMouseLeave}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import Deck from '@/lib/TarokkaDeck';
|
import Deck from '@/lib/TarokkaDeck';
|
||||||
import generateID from '@/tools/simpleID';
|
import { generateID, parseMilliseconds } from '@/tools';
|
||||||
import parseMilliseconds from '@/tools/parseMilliseconds';
|
|
||||||
|
|
||||||
import { HOUR, DAY, SETTINGS } from '@/constants';
|
import { HOUR, DAY, SETTINGS } from '@/constants';
|
||||||
import { GameState, GameUpdate, Settings, Tilt } from '@/types';
|
import { GameState, GameUpdate, Settings, Tilt } from '@/types';
|
||||||
@@ -157,7 +156,7 @@ export default class GameStore {
|
|||||||
return this.gameUpdate(game);
|
return this.gameUpdate(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
tilt(playerID: string, cardIndex: number, { rotateX, rotateY }: Tilt) {
|
tilt(playerID: string, cardIndex: number, tilt: Tilt) {
|
||||||
const game = this.getGameByPlayerID(playerID);
|
const game = this.getGameByPlayerID(playerID);
|
||||||
const cardTilts = game.tilts[cardIndex];
|
const cardTilts = game.tilts[cardIndex];
|
||||||
|
|
||||||
@@ -165,8 +164,8 @@ export default class GameStore {
|
|||||||
|
|
||||||
this._clearTilts(game, playerID);
|
this._clearTilts(game, playerID);
|
||||||
|
|
||||||
if (rotateX && rotateY) {
|
if (tilt.rotateX && tilt.rotateY) {
|
||||||
game.tilts[cardIndex] = [...game.tilts[cardIndex], { playerID, rotateX, rotateY }];
|
game.tilts[cardIndex] = [...game.tilts[cardIndex], { ...tilt, playerID }];
|
||||||
game.lastUpdated = Date.now();
|
game.lastUpdated = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import getRandomItems from '@/tools/getRandomItems';
|
import { getRandomItems } from '@/tools';
|
||||||
import cards from '@/constants/standardCards';
|
import cards from '@/constants/standardCards';
|
||||||
import type { StandardCard } from '@/types';
|
import type { StandardCard } from '@/types';
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import getRandomItems from '@/tools/getRandomItems';
|
import { getRandomItems } from '@/tools';
|
||||||
import cards from '@/constants/tarokkaCards';
|
import cards from '@/constants/tarokkaCards';
|
||||||
import type { TarokkaCard, TarokkaGameCard } from '@/types';
|
import type { TarokkaCard, TarokkaGameCard } from '@/types';
|
||||||
|
|
||||||
|
|||||||
2048
package-lock.json
generated
2048
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "tarokka",
|
"name": "tarokka",
|
||||||
"version": "1.1.0",
|
"version": "1.1.2",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "nodemon",
|
"dev": "nodemon",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import type { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
export function middleware(request: NextRequest) {
|
export function proxy(request: NextRequest) {
|
||||||
const url = request.nextUrl;
|
const url = request.nextUrl;
|
||||||
const slug = url.pathname.slice(1);
|
const slug = url.pathname.slice(1);
|
||||||
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.5 MiB After Width: | Height: | Size: 5.9 MiB |
@@ -3,7 +3,7 @@ import { createServer } from 'http';
|
|||||||
import { Server as SocketIOServer, type Socket } from 'socket.io';
|
import { Server as SocketIOServer, type Socket } from 'socket.io';
|
||||||
|
|
||||||
import GameStore from '@/lib/GameStore';
|
import GameStore from '@/lib/GameStore';
|
||||||
import omit from '@/tools/omit';
|
import { omit } from '@/tools';
|
||||||
|
|
||||||
import { thirtyFPS } from '@/constants/time';
|
import { thirtyFPS } from '@/constants/time';
|
||||||
import type { ClientUpdate, GameUpdate, Tilt } from '@/types';
|
import type { ClientUpdate, GameUpdate, Tilt } from '@/types';
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { isHighCard, isLowCard } from '@/tools/cardTypes';
|
import { isHighCard, isLowCard } from '@/tools';
|
||||||
import { Layout, Settings, TarokkaGameCard } from '@/types';
|
import { Layout, Settings, TarokkaGameCard } from '@/types';
|
||||||
|
|
||||||
export default function getTooltip(
|
export const getCardInfo = (
|
||||||
card: TarokkaGameCard,
|
card: TarokkaGameCard,
|
||||||
position: Layout,
|
position: Layout,
|
||||||
dm: boolean,
|
dm: boolean,
|
||||||
settings: Settings,
|
settings: Settings,
|
||||||
) {
|
) => {
|
||||||
const { card: cardName, description, flipped } = card;
|
const { card: cardName, description, flipped } = card;
|
||||||
|
|
||||||
let text: string[] = [];
|
let text: string[] = [];
|
||||||
@@ -39,4 +39,4 @@ export default function getTooltip(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export default function getRandomItems<T>(items: T[], count: number): T[] {
|
export const getRandomItems = <T>(items: T[], count: number): T[] => {
|
||||||
const shuffled = [...items];
|
const shuffled = [...items];
|
||||||
|
|
||||||
// Fisher-Yates shuffle
|
// Fisher-Yates shuffle
|
||||||
@@ -8,4 +8,4 @@ export default function getRandomItems<T>(items: T[], count: number): T[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
|
return count > shuffled.length ? shuffled : shuffled.slice(0, count);
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { cardStyles, standardMap } from '@/constants/tarokka';
|
import { cardStyles, standardMap } from '@/constants/tarokka';
|
||||||
import { Settings, TarokkaCard, TarokkaGameCard } from '@/types';
|
import { Settings, TarokkaCard, TarokkaGameCard } from '@/types';
|
||||||
|
|
||||||
export default function getURL(card: TarokkaCard | TarokkaGameCard, settings: Settings) {
|
export const getURL = (card: TarokkaCard | TarokkaGameCard, settings: Settings) => {
|
||||||
const styleConfig = cardStyles[settings.cardStyle];
|
const styleConfig = cardStyles[settings.cardStyle];
|
||||||
const fileBase = settings.cardStyle === 'standard' ? standardMap[card.id] : card.id;
|
const fileBase = settings.cardStyle === 'standard' ? standardMap[card.id] : card.id;
|
||||||
return `${styleConfig.baseURL}${fileBase}${card.extension || styleConfig.extension}`;
|
return `${styleConfig.baseURL}${fileBase}${card.extension || styleConfig.extension}`;
|
||||||
}
|
};
|
||||||
|
|||||||
11
tools/index.ts
Normal file
11
tools/index.ts
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
export * from '@/tools/cardTypes';
|
||||||
|
export * from '@/tools/getCardInfo';
|
||||||
|
export * from '@/tools/getRandomItems';
|
||||||
|
export * from '@/tools/getURL';
|
||||||
|
export * from '@/tools/log';
|
||||||
|
export * from '@/tools/omit';
|
||||||
|
export * from '@/tools/parseMilliseconds';
|
||||||
|
export * from '@/tools/reduceTilts';
|
||||||
|
export * from '@/tools/simpleID';
|
||||||
|
export * from '@/tools/throttle';
|
||||||
|
export * from '@/tools/validTilt';
|
||||||
19
tools/log.ts
Normal file
19
tools/log.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* A logging utility designed to be inserted into functional chains.
|
||||||
|
* Logs all parameters with an optional prefix, then returns the first argument unchanged.
|
||||||
|
*
|
||||||
|
* @param {string} [prefix=''] - A label or message to prepend to the logged output.
|
||||||
|
* @returns {(value: any, ...rest: any[]) => any} - A function that logs its arguments and returns the first one.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const result = [1, 2, 3]
|
||||||
|
* .map((n) => n * 2)
|
||||||
|
* .map(log('doubled:'))
|
||||||
|
* .filter((n) => n > 2);
|
||||||
|
*/
|
||||||
|
export const log =
|
||||||
|
(prefix: string = ''): ((value: any, ...rest: any[]) => any) =>
|
||||||
|
(...args: any[]) => {
|
||||||
|
console.log(prefix, ...args);
|
||||||
|
return args[0];
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
export default function omit<T extends Record<string, any>>(
|
export const omit = <T extends Record<string, any>>(
|
||||||
obj: T,
|
obj: T,
|
||||||
propToRemove: keyof T,
|
propToRemove: keyof T,
|
||||||
): Omit<T, typeof propToRemove> {
|
): Omit<T, typeof propToRemove> => {
|
||||||
const { [propToRemove]: _, ...rest } = obj;
|
const { [propToRemove]: _, ...rest } = obj;
|
||||||
return rest;
|
return rest;
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ export interface ParsedMilliseconds {
|
|||||||
seconds: number;
|
seconds: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function parseMilliseconds(timestamp: number): ParsedMilliseconds {
|
export const parseMilliseconds = (timestamp: number): ParsedMilliseconds => {
|
||||||
const days = Math.floor(timestamp / DAY);
|
const days = Math.floor(timestamp / DAY);
|
||||||
timestamp %= DAY;
|
timestamp %= DAY;
|
||||||
|
|
||||||
@@ -21,4 +21,4 @@ export default function parseMilliseconds(timestamp: number): ParsedMilliseconds
|
|||||||
timestamp %= SECOND;
|
timestamp %= SECOND;
|
||||||
|
|
||||||
return { days, hours, minutes, seconds };
|
return { days, hours, minutes, seconds };
|
||||||
}
|
};
|
||||||
|
|||||||
36
tools/reduceTilts.ts
Normal file
36
tools/reduceTilts.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { validTilt } from '@/tools';
|
||||||
|
import { GameUpdate, Settings, Tilt } from '@/types';
|
||||||
|
|
||||||
|
const combineTilts = (tilts: Tilt[]) =>
|
||||||
|
tilts.reduce(
|
||||||
|
({ pX, pY, rX, rY, count }, { percentX, percentY, rotateX, rotateY }) => ({
|
||||||
|
pX: pX + percentX,
|
||||||
|
pY: pY + percentY,
|
||||||
|
rX: rX + rotateX,
|
||||||
|
rY: rY + rotateY,
|
||||||
|
count: count + 1,
|
||||||
|
}),
|
||||||
|
{ pX: 0, pY: 0, rX: 0, rY: 0, count: 0 },
|
||||||
|
);
|
||||||
|
|
||||||
|
export function reduceTilts(
|
||||||
|
gameData: GameUpdate,
|
||||||
|
localTilt: Tilt[],
|
||||||
|
{ tilt, remoteTilt }: Settings,
|
||||||
|
): Tilt[] {
|
||||||
|
const remoteTilts = gameData.tilts;
|
||||||
|
|
||||||
|
if (!tilt) return [];
|
||||||
|
if (!remoteTilt) return localTilt;
|
||||||
|
|
||||||
|
return Array.from({ length: 5 }, (_, i) => (localTilt[i] ? [localTilt[i]] : []))
|
||||||
|
.map((cardTilts, cardIndex) => [...remoteTilts[cardIndex], ...cardTilts])
|
||||||
|
.map((cardTilts) => cardTilts.filter(validTilt))
|
||||||
|
.map(combineTilts)
|
||||||
|
.map(({ pX, pY, rX, rY, count }) => ({
|
||||||
|
percentX: count ? pX / count : -1,
|
||||||
|
percentY: count ? pY / count : -1,
|
||||||
|
rotateX: count ? rX / count : 0,
|
||||||
|
rotateY: count ? rY / count : 0,
|
||||||
|
}));
|
||||||
|
}
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
import getRandomItems from '@/tools/getRandomItems';
|
import { getRandomItems } from '@/tools';
|
||||||
|
|
||||||
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
|
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||||
|
|
||||||
const generateID = (length: number = 6) => {
|
export const generateID = (length: number = 6) => {
|
||||||
return getRandomItems(alphabet.split(''), length).join('');
|
return getRandomItems(alphabet.split(''), length).join('');
|
||||||
};
|
};
|
||||||
|
|
||||||
export default generateID;
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export default function throttle(func: Function, threshold: number) {
|
export function throttle(func: Function, threshold: number) {
|
||||||
let lastCall = 0;
|
let lastCall = 0;
|
||||||
|
|
||||||
return (...args: any[]) => {
|
return (...args: any[]) => {
|
||||||
|
|||||||
9
tools/validTilt.ts
Normal file
9
tools/validTilt.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { Tilt } from '@/types';
|
||||||
|
|
||||||
|
export const validTilt = (tilt: Tilt) => {
|
||||||
|
if (!tilt) return false;
|
||||||
|
|
||||||
|
const { percentX, percentY, rotateX, rotateY } = tilt;
|
||||||
|
|
||||||
|
return percentX >= 0 && percentY >= 0 && !!rotateX && !!rotateY;
|
||||||
|
};
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2020",
|
"target": "ES2020",
|
||||||
"lib": ["dom", "dom.iterable", "esnext"],
|
"lib": [
|
||||||
|
"dom",
|
||||||
|
"dom.iterable",
|
||||||
|
"esnext"
|
||||||
|
],
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": false,
|
"strict": false,
|
||||||
@@ -12,7 +16,7 @@
|
|||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"jsx": "preserve",
|
"jsx": "react-jsx",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"plugins": [
|
"plugins": [
|
||||||
{
|
{
|
||||||
@@ -20,10 +24,20 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"paths": {
|
"paths": {
|
||||||
"@/*": ["./*"]
|
"@/*": [
|
||||||
|
"./*"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"strictNullChecks": true
|
"strictNullChecks": true
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
|
"include": [
|
||||||
"exclude": ["node_modules"]
|
"next-env.d.ts",
|
||||||
|
"**/*.ts",
|
||||||
|
"**/*.tsx",
|
||||||
|
".next/types/**/*.ts",
|
||||||
|
".next/dev/types/**/*.ts"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,8 @@ export interface Layout {
|
|||||||
|
|
||||||
export interface Tilt {
|
export interface Tilt {
|
||||||
playerID?: string;
|
playerID?: string;
|
||||||
|
percentX: number;
|
||||||
|
percentY: number;
|
||||||
rotateX: number;
|
rotateX: number;
|
||||||
rotateY: number;
|
rotateY: number;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user