teletilt #3
@@ -25,10 +25,10 @@ export interface AppContext {
|
|||||||
noGame: boolean;
|
noGame: boolean;
|
||||||
tilt: Tilt[];
|
tilt: Tilt[];
|
||||||
selectCardIndex: number;
|
selectCardIndex: number;
|
||||||
flipCard: (cardIndex: number) => void;
|
emitFlip: (cardIndex: number) => void;
|
||||||
handleSettings: (gameData: GameUpdate) => void;
|
emitSettings: (gameData: GameUpdate) => void;
|
||||||
redraw: (cardIndex: number) => void;
|
emitRedraw: (cardIndex: number) => void;
|
||||||
select: (cardID: string) => void;
|
emitSelect: (cardID: string) => void;
|
||||||
setGameID: (gameID: string) => void;
|
setGameID: (gameID: string) => void;
|
||||||
setSelectCardIndex: (cardIndex: number) => void;
|
setSelectCardIndex: (cardIndex: number) => void;
|
||||||
setTilt: (tilt: Tilt[]) => void;
|
setTilt: (tilt: Tilt[]) => void;
|
||||||
@@ -41,7 +41,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
const [selectCardIndex, setSelectCardIndex] = useState(-1);
|
const [selectCardIndex, setSelectCardIndex] = useState(-1);
|
||||||
const [tilt, setTilt] = useState<Tilt[]>([]);
|
const [tilt, setTilt] = useState<Tilt[]>([]);
|
||||||
|
|
||||||
const { flipCard, redraw, select, handleSettings, emitTilt } = useSocket({
|
const { emitFlip, emitRedraw, emitSelect, emitSettings, emitTilt } = useSocket({
|
||||||
gameID,
|
gameID,
|
||||||
setGameData,
|
setGameData,
|
||||||
setNoGame,
|
setNoGame,
|
||||||
@@ -58,7 +58,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
const handleSelect = (cardID: string) => {
|
const handleSelect = (cardID: string) => {
|
||||||
setSelectCardIndex(-1);
|
setSelectCardIndex(-1);
|
||||||
|
|
||||||
select(selectCardIndex, cardID);
|
emitSelect(selectCardIndex, cardID);
|
||||||
};
|
};
|
||||||
|
|
||||||
const appInterface = {
|
const appInterface = {
|
||||||
@@ -66,10 +66,10 @@ export function AppProvider({ children }: { children: ReactNode }) {
|
|||||||
noGame,
|
noGame,
|
||||||
tilt,
|
tilt,
|
||||||
selectCardIndex,
|
selectCardIndex,
|
||||||
flipCard,
|
emitFlip,
|
||||||
handleSettings,
|
emitSettings,
|
||||||
redraw,
|
emitRedraw,
|
||||||
select: handleSelect,
|
emitSelect: handleSelect,
|
||||||
setGameID,
|
setGameID,
|
||||||
setSelectCardIndex,
|
setSelectCardIndex,
|
||||||
setTilt,
|
setTilt,
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type CardProps = {
|
|||||||
|
|
||||||
export default function Card({ card, cardIndex }: CardProps) {
|
export default function Card({ card, cardIndex }: CardProps) {
|
||||||
const [tooltip, setTooltip] = useState<React.ReactNode>(null);
|
const [tooltip, setTooltip] = useState<React.ReactNode>(null);
|
||||||
const { flipCard, gameData, redraw, setSelectCardIndex } = useAppContext();
|
const { emitFlip, gameData, emitRedraw, setSelectCardIndex } = useAppContext();
|
||||||
|
|
||||||
const { dmID, settings } = gameData;
|
const { dmID, settings } = gameData;
|
||||||
const isDM = !!dmID;
|
const isDM = !!dmID;
|
||||||
@@ -32,7 +32,7 @@ export default function Card({ card, cardIndex }: CardProps) {
|
|||||||
|
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
if (isDM) {
|
if (isDM) {
|
||||||
flipCard(cardIndex);
|
emitFlip(cardIndex);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ export default function Card({ card, cardIndex }: CardProps) {
|
|||||||
/>
|
/>
|
||||||
{isDM && !flipped && (
|
{isDM && !flipped && (
|
||||||
<StackTheDeck
|
<StackTheDeck
|
||||||
onRedraw={() => redraw(cardIndex)}
|
onRedraw={() => emitRedraw(cardIndex)}
|
||||||
onSelect={() => setSelectCardIndex(cardIndex)}
|
onSelect={() => setSelectCardIndex(cardIndex)}
|
||||||
onHover={setTooltip}
|
onHover={setTooltip}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type CardSelectProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default function CardSelect({ className = '' }: CardSelectProps) {
|
export default function CardSelect({ className = '' }: CardSelectProps) {
|
||||||
const { gameData, select, selectCardIndex, setSelectCardIndex } = useAppContext();
|
const { gameData, emitSelect, selectCardIndex, setSelectCardIndex } = useAppContext();
|
||||||
const { cards: hand, settings } = gameData;
|
const { cards: hand, settings } = gameData;
|
||||||
|
|
||||||
const handIDs = hand.map(({ id }) => id);
|
const handIDs = hand.map(({ id }) => id);
|
||||||
@@ -53,7 +53,7 @@ export default function CardSelect({ className = '' }: CardSelectProps) {
|
|||||||
<div
|
<div
|
||||||
key={card.id}
|
key={card.id}
|
||||||
className={`relative h-[21vh] w-[15vh] perspective transition-transform duration-200 hover:scale-150 z-0 hover:z-10`}
|
className={`relative h-[21vh] w-[15vh] perspective transition-transform duration-200 hover:scale-150 z-0 hover:z-10`}
|
||||||
onClick={() => select(card.id)}
|
onClick={() => emitSelect(card.id)}
|
||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={getURL(card, settings)}
|
src={getURL(card, settings)}
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ const cardStyleOptions: CardStyle[] = ['standard', 'color', 'grayscale'];
|
|||||||
|
|
||||||
export default function Settings() {
|
export default function Settings() {
|
||||||
const [open, setOpen] = useState(false);
|
const [open, setOpen] = useState(false);
|
||||||
const { gameData, handleSettings } = useAppContext();
|
const { gameData, emitSettings } = useAppContext();
|
||||||
|
|
||||||
const { dmID } = gameData;
|
const { dmID } = gameData;
|
||||||
const isDM = !!dmID;
|
const isDM = !!dmID;
|
||||||
|
|
||||||
const togglePermission = (key: string) => {
|
const togglePermission = (key: string) => {
|
||||||
handleSettings({
|
emitSettings({
|
||||||
...gameData,
|
...gameData,
|
||||||
settings: {
|
settings: {
|
||||||
...gameData.settings,
|
...gameData.settings,
|
||||||
@@ -38,7 +38,7 @@ export default function Settings() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const tuneRadio = (cardStyle: CardStyle) => {
|
const tuneRadio = (cardStyle: CardStyle) => {
|
||||||
handleSettings({
|
emitSettings({
|
||||||
...gameData,
|
...gameData,
|
||||||
settings: {
|
settings: {
|
||||||
...gameData.settings,
|
...gameData.settings,
|
||||||
|
|||||||
@@ -2,3 +2,5 @@ export const SECOND = 1000;
|
|||||||
export const MINUTE = 60 * SECOND;
|
export const MINUTE = 60 * SECOND;
|
||||||
export const HOUR = 60 * MINUTE;
|
export const HOUR = 60 * MINUTE;
|
||||||
export const DAY = 24 * HOUR;
|
export const DAY = 24 * HOUR;
|
||||||
|
|
||||||
|
export const thirtyFPS = SECOND / 30;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { useEffect } from 'react';
|
|||||||
import { socket } from '@/socket';
|
import { socket } from '@/socket';
|
||||||
import throttle from '@/tools/throttle';
|
import throttle from '@/tools/throttle';
|
||||||
|
|
||||||
|
import { thirtyFPS } from '@/constants/time';
|
||||||
import type { GameUpdate, Tilt } from '@/types';
|
import type { GameUpdate, Tilt } from '@/types';
|
||||||
|
|
||||||
interface UseSocketProps {
|
interface UseSocketProps {
|
||||||
@@ -38,28 +39,28 @@ export default function useSocket({ gameID, setGameData, setNoGame }: UseSocketP
|
|||||||
};
|
};
|
||||||
}, [gameID]);
|
}, [gameID]);
|
||||||
|
|
||||||
const flipCard = (cardIndex: number) => {
|
const emitFlip = (cardIndex: number) => {
|
||||||
socket.emit('flip-card', {
|
socket.emit('flip-card', {
|
||||||
gameID,
|
gameID,
|
||||||
cardIndex,
|
cardIndex,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSettings = (gameData: GameUpdate) => {
|
const emitSettings = (gameData: GameUpdate) => {
|
||||||
socket.emit('settings', {
|
socket.emit('settings', {
|
||||||
gameID,
|
gameID,
|
||||||
gameData,
|
gameData,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const redraw = (cardIndex: number) => {
|
const emitRedraw = (cardIndex: number) => {
|
||||||
socket.emit('redraw', {
|
socket.emit('redraw', {
|
||||||
gameID,
|
gameID,
|
||||||
cardIndex,
|
cardIndex,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const select = (cardIndex: number, cardID: string) => {
|
const emitSelect = (cardIndex: number, cardID: string) => {
|
||||||
socket.emit('select', {
|
socket.emit('select', {
|
||||||
gameID,
|
gameID,
|
||||||
cardIndex,
|
cardIndex,
|
||||||
@@ -72,13 +73,13 @@ export default function useSocket({ gameID, setGameData, setNoGame }: UseSocketP
|
|||||||
cardIndex,
|
cardIndex,
|
||||||
tilt,
|
tilt,
|
||||||
});
|
});
|
||||||
}, 33.3);
|
}, thirtyFPS);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
flipCard,
|
emitFlip,
|
||||||
handleSettings,
|
emitSettings,
|
||||||
redraw,
|
emitRedraw,
|
||||||
select,
|
emitSelect,
|
||||||
emitTilt,
|
emitTilt,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
25
server.ts
25
server.ts
@@ -4,6 +4,8 @@ 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/omit';
|
||||||
|
|
||||||
|
import { thirtyFPS } from '@/constants/time';
|
||||||
import type { ClientUpdate, GameUpdate, Tilt } from '@/types';
|
import type { ClientUpdate, GameUpdate, Tilt } from '@/types';
|
||||||
|
|
||||||
const dev = process.env.NODE_ENV !== 'production';
|
const dev = process.env.NODE_ENV !== 'production';
|
||||||
@@ -15,6 +17,8 @@ const handler = app.getRequestHandler();
|
|||||||
|
|
||||||
const gameStore = new GameStore();
|
const gameStore = new GameStore();
|
||||||
|
|
||||||
|
const timedReleases = {};
|
||||||
|
|
||||||
app.prepare().then(() => {
|
app.prepare().then(() => {
|
||||||
const httpServer = createServer(handler);
|
const httpServer = createServer(handler);
|
||||||
|
|
||||||
@@ -25,6 +29,25 @@ app.prepare().then(() => {
|
|||||||
io.to(gameUpdate.spectatorID).emit(event, omit(gameUpdate, 'dmID'));
|
io.to(gameUpdate.spectatorID).emit(event, omit(gameUpdate, 'dmID'));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const timedRelease = (event: string, gameUpdate: GameUpdate, threshold: number) => {
|
||||||
|
const now = Date.now();
|
||||||
|
const lastEvent = timedReleases[event];
|
||||||
|
|
||||||
|
if (lastEvent?.embargo >= now) {
|
||||||
|
clearTimeout(lastEvent.to);
|
||||||
|
const embargo = lastEvent.embargo - now;
|
||||||
|
|
||||||
|
const to = setTimeout(() => {
|
||||||
|
broadcast(event, gameUpdate);
|
||||||
|
}, embargo);
|
||||||
|
|
||||||
|
timedReleases[event] = { embargo, to };
|
||||||
|
} else {
|
||||||
|
broadcast(event, gameUpdate);
|
||||||
|
timedReleases[event] = { embargo: now + threshold };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
io.on('connection', (socket: Socket) => {
|
io.on('connection', (socket: Socket) => {
|
||||||
//console.log(Date.now(), `Client connected: ${socket.id}`);
|
//console.log(Date.now(), `Client connected: ${socket.id}`);
|
||||||
|
|
||||||
@@ -122,7 +145,7 @@ app.prepare().then(() => {
|
|||||||
socket.on('tilt', ({ cardIndex, tilt }: { cardIndex: number; tilt: Tilt }) => {
|
socket.on('tilt', ({ cardIndex, tilt }: { cardIndex: number; tilt: Tilt }) => {
|
||||||
try {
|
try {
|
||||||
const gameState = gameStore.tilt(socket.id, cardIndex, tilt);
|
const gameState = gameStore.tilt(socket.id, cardIndex, tilt);
|
||||||
broadcast('game-update', gameState);
|
timedRelease('game-update', gameState, thirtyFPS);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const error = e instanceof Error ? e.message : e;
|
const error = e instanceof Error ? e.message : e;
|
||||||
console.error(Date.now(), 'Error[tilt]', error);
|
console.error(Date.now(), 'Error[tilt]', error);
|
||||||
|
|||||||
12
tools/throttle.ts
Normal file
12
tools/throttle.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
export default function throttle(func: Function, threshold: number) {
|
||||||
|
let lastCall = 0;
|
||||||
|
|
||||||
|
return (...args: any[]) => {
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
if (now - lastCall >= threshold) {
|
||||||
|
lastCall = now;
|
||||||
|
func(...args);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user