Compare commits

...

7 Commits

Author SHA1 Message Date
Gavin McDonald
b4b0c853f1 simplify tools imports 2025-07-04 14:18:38 -04:00
Gavin McDonald
c6e316a1f8 simplify tilt calculations 2025-07-04 13:59:57 -04:00
Gavin McDonald
d3eb6f1b46 a bit less computation 2025-07-03 21:05:04 -04:00
Gavin McDonald
8cbf281ef8 version bump 2025-07-03 17:03:54 -04:00
Gavin McDonald
fc0466ae89 flippable sheen 2025-07-03 16:38:53 -04:00
Gavin McDonald
22f949aaf8 gussy up Notes 2025-07-03 16:15:08 -04:00
Gavin McDonald
f0aee17ea0 fix default settings values 2025-07-03 15:54:34 -04:00
24 changed files with 222 additions and 120 deletions

View File

@@ -2,6 +2,7 @@
import { createContext, useContext, useEffect, useState } from 'react';
import useSocket from '@/hooks/useSocket';
import { reduceTilts } from '@/tools';
import { GAME_START, LOCAL_DEFAULTS } from '@/constants';
import type { Dispatch, ReactNode, SetStateAction } from 'react';
@@ -15,7 +16,7 @@ export interface AppContext {
noGame: boolean;
selectCardIndex: number;
settings: Settings;
tilt: Tilt[];
tilts: Tilt[];
emitFlip: (cardIndex: number) => void;
emitSettings: (gameData: GameUpdate) => void;
emitRedraw: (cardIndex: number) => void;
@@ -23,7 +24,7 @@ export interface AppContext {
setGameID: (gameID: string) => void;
setLocalSettings: Dispatch<SetStateAction<LocalSettings>>;
setSelectCardIndex: (cardIndex: number) => void;
setTilt: (tilt: Tilt[]) => void;
setLocalTilt: (tilt: Tilt[]) => void;
}
export function AppProvider({ children }: { children: ReactNode }) {
@@ -32,7 +33,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
const [gameID, setGameID] = useState('');
const [noGame, setNoGame] = useState(false);
const [selectCardIndex, setSelectCardIndex] = useState(-1);
const [tilt, setTilt] = useState<Tilt[]>([]);
const [localTilt, setLocalTilt] = useState<Tilt[]>([]);
const { emitFlip, emitRedraw, emitSelect, emitSettings, emitTilt } = useSocket({
gameID,
@@ -42,17 +43,17 @@ export function AppProvider({ children }: { children: ReactNode }) {
useEffect(() => {
if (localSettings.remoteTilt) {
const cardIndex = tilt.findIndex((tilt) => !!tilt);
const cardIndex = localTilt.findIndex((tilt) => !!tilt);
if (tilt[cardIndex]) {
emitTilt(cardIndex, tilt[cardIndex]);
if (localTilt[cardIndex]) {
emitTilt(cardIndex, localTilt[cardIndex]);
} else {
// cardIndex does not matter
// 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) => {
setSelectCardIndex(-1);
@@ -69,7 +70,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
noGame,
selectCardIndex,
settings: { ...gameData.settings, ...localSettings },
tilt,
tilts: reduceTilts(gameData, localTilt),
emitFlip,
emitSettings,
emitRedraw,
@@ -77,7 +78,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
setGameID,
setLocalSettings,
setSelectCardIndex,
setTilt,
setLocalTilt,
};
return <AppContext.Provider value={appInterface}>{children}</AppContext.Provider>;

View File

@@ -5,8 +5,8 @@ import { useAppContext } from '@/app/AppContext';
import TiltCard from '@/components/TiltCard';
import ToolTip from '@/components/ToolTip';
import StackTheDeck from '@/components/StackTheDeck';
import getCardInfo from '@/tools/getCardInfo';
import getURL from '@/tools/getURL';
import Sheen from '@/components/Sheen';
import { getCardInfo, getURL } from '@/tools';
import tarokkaCards from '@/constants/tarokkaCards';
import { layout } from '@/constants/tarokka';
@@ -84,6 +84,7 @@ export default function Card({ card, cardIndex }: CardProps) {
onHover={setTooltip}
/>
)}
<Sheen cardIndex={cardIndex} />
</div>
<div className="absolute inset-0 backface-hidden rotate-y-180">
<img
@@ -91,6 +92,7 @@ export default function Card({ card, cardIndex }: CardProps) {
alt={aria}
className="rounded-lg border border-yellow-500/25 hover:drop-shadow-[0_0_3px_#ffd700/50]"
/>
<Sheen cardIndex={cardIndex} />
</div>
</div>
</TiltCard>

View File

@@ -3,7 +3,7 @@
import { CircleX } from 'lucide-react';
import { useAppContext } from '@/app/AppContext';
import TarokkaDeck from '@/lib/TarokkaDeck';
import getURL from '@/tools/getURL';
import { getURL } from '@/tools';
import { Deck } from '@/types';

View File

@@ -1,12 +1,12 @@
'use client';
import { useMemo, useState } from 'react';
import { ScrollText } from 'lucide-react';
import { CircleX, ScrollText } from 'lucide-react';
import { useAppContext } from '@/app/AppContext';
import CopyButton from '@/components/CopyButton';
import Scrim from '@/components/Scrim';
import getCardInfo from '@/tools/getCardInfo';
import { getCardInfo } from '@/tools';
import { cardMap, layout } from '@/constants/tarokka';
export default function Notes() {
@@ -51,13 +51,24 @@ export default function Notes() {
className={`transition-all duration-250 ${showNotes ? 'pointer-events-auto opacity-100' : 'pointer-events-none opacity-0'}`}
>
<div
className={`fixed bottom-4 right-4 transition-all duration-250 bg-slate-800 border border-yellow-400 rounded-lg space-y-2 ${showNotes ? 'sm:w-[33vw] sm:h-[67vh] w-[80vw] h-[80vh]' : 'w-0 h-0'}`}
className={`
fixed bottom-4 right-4
transition-all duration-250
bg-slate-800
border border-yellow-400 rounded-lg
${showNotes ? 'sm:w-[50vw] sm:h-[67vh] w-[80vw] h-[80vh]' : 'w-0 h-0'}
`}
>
<CopyButton
copy={notes.map((note) => note!.join('\n')).join('\n\n')}
className="text-yellow-400 hover:drop-shadow-[0_0_1px_#ffd700] absolute top-2 right-2 p-2 transition-all duration-250 bg-black/20 hover:bg-black/40 rounded-full cursor-pointer"
className={`
absolute top-2 right-2
cursor-pointer p-2
transition-all duration-250
text-yellow-400 hover:text-yellow-300 hover:drop-shadow-[0_0_3px_#ffd700]
`}
/>
<div className="text-yellow-400 h-full overflow-scroll p-6 transition-all delay-200 duration-50 ${showNotes ? 'opacity-100' : 'opacity-0'}">
<div className="text-yellow-400 h-full overflow-scroll p-8 transition-all delay-200 duration-50 ${showNotes ? 'opacity-100' : 'opacity-0'}">
{notes.map((note, index) => (
<div key={index}>
<div className="flex flex-col gap-2">
@@ -70,6 +81,17 @@ export default function Notes() {
))}
</div>
</div>
<button
className={`
fixed bottom-4 right-4
cursor-pointer p-2
transition-all duration-250
text-yellow-400 hover:text-yellow-300 hover:drop-shadow-[0_0_3px_#ffd700]
`}
onClick={() => setOpen((prev) => !prev)}
>
<CircleX className="w-5 h-5" />
</button>
</Scrim>
</div>
);

59
components/Sheen.tsx Normal file
View File

@@ -0,0 +1,59 @@
import { useEffect, useRef, useState } from 'react';
import { useAppContext } from '@/app/AppContext';
import { validTilt } from '@/tools';
const tiltSheen = (sheen: HTMLDivElement, x: number, y: number) => {
const rect = sheen.getBoundingClientRect();
const sheenX = rect.width - x * rect.width;
const sheenY = rect.height - y * rect.height;
sheen.style.opacity = '1';
sheen.style.backgroundImage = `
radial-gradient(
circle at
${sheenX}px ${sheenY}px,
#ffffff44,
#0000000f
)
`;
};
export default function Sheen({ cardIndex, className }: { cardIndex: number; className?: string }) {
const sheenRef = useRef<HTMLDivElement>(null);
const [untilt, setUntilt] = useState(false);
const { tilts } = useAppContext();
useEffect(() => {
const sheen = sheenRef.current;
if (!sheen) return;
const tilt = tilts[cardIndex];
if (validTilt(tilt)) {
setUntilt(false);
tiltSheen(sheen, tilt.percentX, tilt.percentY);
} else {
setUntilt(true);
}
}, [tilts]);
useEffect(() => {
const sheen = sheenRef.current;
if (!sheen || !untilt) return;
sheen.style.opacity = '0';
}, [untilt]);
return (
<div
ref={sheenRef}
className={`
absolute inset-0
rounded-lg pointer-events-none
transition-opacity duration-500
bg-gradient-to-tr from-transparent via-white/20 to-transparent mix-blend-screen opacity-0
${className}
`}
/>
);
}

View File

@@ -1,30 +1,12 @@
import { useEffect, useRef, useState } from 'react';
import { useAppContext } from '@/app/AppContext';
import throttle from '@/tools/throttle';
import { throttle, validTilt } from '@/tools';
import { thirtyFPS } from '@/constants/time';
import type { Tilt } from '@/types';
const ZERO_ROTATION = 'rotateX(0deg) rotateY(0deg)';
const tiltSheen = (sheen: HTMLDivElement, tiltX: number, tiltY: number) => {
const rect = sheen.getBoundingClientRect();
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const sheenX = centerX + (tiltY / -20) * centerX;
const sheenY = centerY + (tiltX / 20) * centerY;
sheen.style.opacity = '1';
sheen.style.backgroundImage = `
radial-gradient(
circle at
${sheenX}px ${sheenY}px,
#ffffff44,
#0000000f
)
`;
};
export default function TiltCard({
children,
cardIndex,
@@ -35,62 +17,28 @@ export default function TiltCard({
className?: string;
}) {
const cardRef = useRef<HTMLDivElement>(null);
const sheenRef = useRef<HTMLDivElement>(null);
const [untilt, setUntilt] = useState(false);
const {
gameData,
settings: { tilt, remoteTilt },
setTilt,
tilt: localTilts,
} = useAppContext();
const { settings, tilts, setLocalTilt } = useAppContext();
useEffect(() => {
const card = cardRef.current;
const sheen = sheenRef.current;
if (!card || !sheen) return;
if (!card) return;
if (tilt) {
const rotateX = localTilts[cardIndex]?.rotateX || 0;
const rotateY = localTilts[cardIndex]?.rotateY || 0;
const tilt = tilts[cardIndex];
const tilts = remoteTilt
? [...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)) {
if (validTilt(tilt)) {
setUntilt(false);
const x = totalX / count;
const y = totalY / count;
card.style.transform = `rotateX(${x}deg) rotateY(${y}deg)`;
tiltSheen(sheen, x, y);
card.style.transform = `rotateX(${tilt.rotateX}deg) rotateY(${tilt.rotateY}deg)`;
} else {
setUntilt(true);
}
} else if (card.style.transform !== ZERO_ROTATION) {
setUntilt(true);
}
}, [tilt, localTilts, gameData]);
}, [tilts]);
useEffect(() => {
const card = cardRef.current;
const sheen = sheenRef.current;
if (!card || !sheen || !untilt) return;
if (!card || !untilt) return;
card.style.transform = ZERO_ROTATION;
sheen.style.opacity = '0';
}, [untilt]);
const handleMouseMove = throttle((e: React.MouseEvent) => {
@@ -105,21 +53,28 @@ export default function TiltCard({
const rotateX = ((y - centerY) / centerY) * -20;
const rotateY = ((x - centerX) / centerX) * 20;
const percentX = x / rect.width;
const percentY = y / rect.height;
const newTilt: Tilt[] = [];
newTilt[cardIndex] = { rotateX, rotateY };
newTilt[cardIndex] = {
percentX,
percentY,
rotateX,
rotateY,
};
setTilt(newTilt);
setLocalTilt(newTilt);
}, thirtyFPS);
const handleMouseLeave = () => {
setTilt([]);
setLocalTilt([]);
};
return (
<div
className={`group ${className}`}
onMouseMove={tilt ? handleMouseMove : undefined}
onMouseMove={settings.tilt ? handleMouseMove : undefined}
onMouseLeave={handleMouseLeave}
>
<div
@@ -128,10 +83,6 @@ export default function TiltCard({
className={`h-full w-full transition-transform ${untilt ? 'duration-500' : 'duration-0'}`}
>
{children}
<div
ref={sheenRef}
className="pointer-events-none absolute inset-0 rounded-lg bg-gradient-to-tr from-transparent via-white/20 to-transparent mix-blend-screen opacity-0 transition-opacity duration-500"
/>
</div>
</div>
);

View File

@@ -7,12 +7,12 @@ import type { GameUpdate, LocalSettings, Settings } from '@/types';
export const SETTINGS: Settings = {
cardStyle: 'color',
notes: false,
positionBack: false,
positionFront: false,
prophecy: false,
notes: true,
positionBack: true,
positionFront: true,
prophecy: true,
tilt: true,
remoteTilt: false,
remoteTilt: true,
};
export const GAME_START: GameUpdate = {

View File

@@ -1,6 +1,5 @@
import Deck from '@/lib/TarokkaDeck';
import generateID from '@/tools/simpleID';
import parseMilliseconds from '@/tools/parseMilliseconds';
import { generateID, parseMilliseconds } from '@/tools';
import { HOUR, DAY, SETTINGS } from '@/constants';
import { GameState, GameUpdate, Settings, Tilt } from '@/types';
@@ -157,7 +156,7 @@ export default class GameStore {
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 cardTilts = game.tilts[cardIndex];
@@ -165,8 +164,8 @@ export default class GameStore {
this._clearTilts(game, playerID);
if (rotateX && rotateY) {
game.tilts[cardIndex] = [...game.tilts[cardIndex], { playerID, rotateX, rotateY }];
if (tilt.rotateX && tilt.rotateY) {
game.tilts[cardIndex] = [...game.tilts[cardIndex], { ...tilt, playerID }];
game.lastUpdated = Date.now();
}

View File

@@ -1,4 +1,4 @@
import getRandomItems from '@/tools/getRandomItems';
import { getRandomItems } from '@/tools';
import cards from '@/constants/standardCards';
import type { StandardCard } from '@/types';

View File

@@ -1,4 +1,4 @@
import getRandomItems from '@/tools/getRandomItems';
import { getRandomItems } from '@/tools';
import cards from '@/constants/tarokkaCards';
import type { TarokkaCard, TarokkaGameCard } from '@/types';

View File

@@ -1,6 +1,6 @@
{
"name": "tarokka",
"version": "1.1.0",
"version": "1.1.1",
"private": true,
"scripts": {
"dev": "nodemon",

View File

@@ -3,7 +3,7 @@ import { createServer } from 'http';
import { Server as SocketIOServer, type Socket } from 'socket.io';
import GameStore from '@/lib/GameStore';
import omit from '@/tools/omit';
import { omit } from '@/tools';
import { thirtyFPS } from '@/constants/time';
import type { ClientUpdate, GameUpdate, Tilt } from '@/types';

View File

@@ -1,12 +1,12 @@
import { isHighCard, isLowCard } from '@/tools/cardTypes';
import { isHighCard, isLowCard } from '@/tools';
import { Layout, Settings, TarokkaGameCard } from '@/types';
export default function getTooltip(
export const getCardInfo = (
card: TarokkaGameCard,
position: Layout,
dm: boolean,
settings: Settings,
) {
) => {
const { card: cardName, description, flipped } = card;
let text: string[] = [];
@@ -39,4 +39,4 @@ export default function getTooltip(
}
return text;
}
};

View File

@@ -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];
// 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);
}
};

View File

@@ -1,8 +1,8 @@
import { cardStyles, standardMap } from '@/constants/tarokka';
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 fileBase = settings.cardStyle === 'standard' ? standardMap[card.id] : card.id;
return `${styleConfig.baseURL}${fileBase}${card.extension || styleConfig.extension}`;
}
};

11
tools/index.ts Normal file
View 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.tsx Normal file
View 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];
};

View File

@@ -1,7 +1,7 @@
export default function omit<T extends Record<string, any>>(
export const omit = <T extends Record<string, any>>(
obj: T,
propToRemove: keyof T,
): Omit<T, typeof propToRemove> {
): Omit<T, typeof propToRemove> => {
const { [propToRemove]: _, ...rest } = obj;
return rest;
}
};

View File

@@ -7,7 +7,7 @@ export interface ParsedMilliseconds {
seconds: number;
}
export default function parseMilliseconds(timestamp: number): ParsedMilliseconds {
export const parseMilliseconds = (timestamp: number): ParsedMilliseconds => {
const days = Math.floor(timestamp / DAY);
timestamp %= DAY;
@@ -21,4 +21,4 @@ export default function parseMilliseconds(timestamp: number): ParsedMilliseconds
timestamp %= SECOND;
return { days, hours, minutes, seconds };
}
};

34
tools/reduceTilts.ts Normal file
View File

@@ -0,0 +1,34 @@
import { log, validTilt } from '@/tools';
import { GameUpdate, 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[] {
const remoteTilts = gameData.tilts;
const tiltEnabled = gameData.settings.tilt;
const remoteTiltEnabled = gameData.settings.remoteTilt;
if (!tiltEnabled) return [];
if (!remoteTiltEnabled) return Array.from({ length: 5 }, (_, i) => localTilt[i]);
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,
}));
}

View File

@@ -1,9 +1,7 @@
import getRandomItems from '@/tools/getRandomItems';
import { getRandomItems } from '@/tools';
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
const generateID = (length: number = 6) => {
export const generateID = (length: number = 6) => {
return getRandomItems(alphabet.split(''), length).join('');
};
export default generateID;

View File

@@ -1,4 +1,4 @@
export default function throttle(func: Function, threshold: number) {
export function throttle(func: Function, threshold: number) {
let lastCall = 0;
return (...args: any[]) => {

4
tools/validTilt.ts Normal file
View File

@@ -0,0 +1,4 @@
import { Tilt } from '@/types';
export const validTilt = ({ percentX, percentY, rotateX, rotateY }: Tilt) =>
percentX >= 0 && percentY >= 0 && !!rotateX && !!rotateY;

View File

@@ -115,6 +115,8 @@ export interface Layout {
export interface Tilt {
playerID?: string;
percentX: number;
percentY: number;
rotateX: number;
rotateY: number;
}