useSocket #2
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { useParams } from 'next/navigation';
|
import { useParams } from 'next/navigation';
|
||||||
import { socket } from '@/socket';
|
import useSocket from '@/hooks/useSocket';
|
||||||
import { Eye } from 'lucide-react';
|
import { Eye } from 'lucide-react';
|
||||||
|
|
||||||
import Card from '@/components/Card';
|
import Card from '@/components/Card';
|
||||||
@@ -39,65 +39,18 @@ export default function GamePage() {
|
|||||||
const isDM = !!dmID;
|
const isDM = !!dmID;
|
||||||
const selectDeck: Deck | null = selectCard >= 0 ? cards[selectCard].deck : null;
|
const selectDeck: Deck | null = selectCard >= 0 ? cards[selectCard].deck : null;
|
||||||
|
|
||||||
|
const socket = useSocket({ gameID, setGameData, setNoGame });
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (gameIDParam) {
|
if (gameIDParam) {
|
||||||
setGameID(Array.isArray(gameIDParam) ? gameIDParam[0] : gameIDParam);
|
setGameID(Array.isArray(gameIDParam) ? gameIDParam[0] : gameIDParam);
|
||||||
}
|
}
|
||||||
}, [gameIDParam]);
|
}, [gameIDParam]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (gameID) {
|
|
||||||
socket.emit('join', gameID);
|
|
||||||
|
|
||||||
socket.on('init', (data: GameUpdate) => {
|
|
||||||
setGameData(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('game-update', (data: GameUpdate) => {
|
|
||||||
setGameData(data);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('join-error', (error) => {
|
|
||||||
console.error('Error:', error);
|
|
||||||
setNoGame(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('flip-error', (error) => {
|
|
||||||
console.error('Error:', error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
socket.removeAllListeners();
|
|
||||||
};
|
|
||||||
}, [gameID]);
|
|
||||||
|
|
||||||
const flipCard = (cardIndex: number) => {
|
|
||||||
socket.emit('flip-card', {
|
|
||||||
gameID,
|
|
||||||
cardIndex,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const redraw = (cardIndex: number) => {
|
|
||||||
socket.emit('redraw', {
|
|
||||||
gameID,
|
|
||||||
cardIndex,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const select = (cardIndex: number, cardID: string) => {
|
const select = (cardIndex: number, cardID: string) => {
|
||||||
setSelectCard(-1);
|
setSelectCard(-1);
|
||||||
|
|
||||||
socket.emit('select', {
|
socket.select(cardIndex, cardID);
|
||||||
gameID,
|
|
||||||
cardIndex,
|
|
||||||
cardID,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSettings = (gameData: GameUpdate) => {
|
|
||||||
socket.emit('settings', { gameID, gameData });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// map our five Tarokka cards to their proper locations in a 3x3 grid
|
// map our five Tarokka cards to their proper locations in a 3x3 grid
|
||||||
@@ -119,7 +72,7 @@ export default function GamePage() {
|
|||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isDM && <Settings gameData={gameData} changeAction={handleSettings} />}
|
{isDM && <Settings gameData={gameData} changeAction={socket.handleSettings} />}
|
||||||
<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-8 w-fit mx-auto">
|
||||||
{Array.from({ length: 9 })
|
{Array.from({ length: 9 })
|
||||||
.map(arrangeCards)
|
.map(arrangeCards)
|
||||||
@@ -131,8 +84,8 @@ export default function GamePage() {
|
|||||||
card={card}
|
card={card}
|
||||||
position={layout[cardMap[index]]}
|
position={layout[cardMap[index]]}
|
||||||
settings={settings}
|
settings={settings}
|
||||||
flipAction={() => flipCard(cardMap[index])}
|
flipAction={() => socket.flipCard(cardMap[index])}
|
||||||
redrawAction={() => redraw(cardMap[index])}
|
redrawAction={() => socket.redraw(cardMap[index])}
|
||||||
selectAction={() => setSelectCard(cardMap[index])}
|
selectAction={() => setSelectCard(cardMap[index])}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
75
hooks/useSocket.ts
Normal file
75
hooks/useSocket.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
import { socket } from '@/socket';
|
||||||
|
|
||||||
|
import type { GameUpdate } from '@/types';
|
||||||
|
|
||||||
|
interface UseSocketProps {
|
||||||
|
gameID: string;
|
||||||
|
setGameData: (gameUpdate: GameUpdate) => void;
|
||||||
|
setNoGame: (noGame: boolean) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function useSocket({ gameID, setGameData, setNoGame }: UseSocketProps) {
|
||||||
|
useEffect(() => {
|
||||||
|
if (gameID) {
|
||||||
|
socket.emit('join', gameID);
|
||||||
|
|
||||||
|
socket.on('init', (data: GameUpdate) => {
|
||||||
|
setGameData(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('game-update', (data: GameUpdate) => {
|
||||||
|
setGameData(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('join-error', (error) => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
setNoGame(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on('flip-error', (error) => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
socket.removeAllListeners();
|
||||||
|
};
|
||||||
|
}, [gameID]);
|
||||||
|
|
||||||
|
const flipCard = (cardIndex: number) => {
|
||||||
|
socket.emit('flip-card', {
|
||||||
|
gameID,
|
||||||
|
cardIndex,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const redraw = (cardIndex: number) => {
|
||||||
|
socket.emit('redraw', {
|
||||||
|
gameID,
|
||||||
|
cardIndex,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const select = (cardIndex: number, cardID: string) => {
|
||||||
|
socket.emit('select', {
|
||||||
|
gameID,
|
||||||
|
cardIndex,
|
||||||
|
cardID,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSettings = (gameData: GameUpdate) => {
|
||||||
|
socket.emit('settings', {
|
||||||
|
gameID,
|
||||||
|
gameData,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
flipCard,
|
||||||
|
redraw,
|
||||||
|
select,
|
||||||
|
handleSettings,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user