'use client';
import { useEffect, useState } from 'react';
import { useGameStore } from '@/lib/store';
import { Lobby } from '@/components/Lobby';
import { WaitingRoom } from '@/components/WaitingRoom';
import { GameBoard } from '@/components/GameBoard';
import { PlayerHand } from '@/components/PlayerHand';
import { GameOver } from '@/components/GameOver';
import { getValidMoves } from '@/lib/gameLogic';
import { motion, AnimatePresence } from 'framer-motion';
export default function Home() {
const {
gameState,
currentPlayerId,
roomId,
error,
selectedTile,
initSocket,
createRoom,
joinRoom,
setPlayerReady,
makeMove,
drawTile,
selectTile,
leaveRoom,
startAIGame,
setError,
} = useGameStore();
const [showRules, setShowRules] = useState(false);
useEffect(() => {
initSocket();
}, [initSocket]);
const handleCreateRoom = (playerName: string) => {
createRoom(playerName);
};
const handleJoinRoom = (roomId: string, playerName: string) => {
joinRoom(roomId, playerName);
};
const handleStartAI = (playerName: string) => {
startAIGame(playerName);
};
const handleTileClick = (tileId: string) => {
const tile = gameState?.players
.find(p => p.id === currentPlayerId)
?.tiles.find(t => t.id === tileId);
if (tile) {
selectTile(selectedTile?.id === tileId ? null : tile);
}
};
const handlePlaceTile = (side: 'left' | 'right') => {
if (!selectedTile || !currentPlayerId || !gameState) return;
// Verificar si el movimiento es válido
const isValid = validMoves.some(m =>
m.tile.id === selectedTile.id &&
(gameState.boardEnds.length === 0 || m.side === side || validMoves.filter(vm => vm.tile.id === selectedTile.id).length > 1)
);
if (!isValid && gameState.boardEnds.length > 0) {
setError(`Cannot place tile ${selectedTile.left}-${selectedTile.right} on the ${side} side. It doesn't match the board end.`);
return;
}
makeMove({
playerId: currentPlayerId,
tile: selectedTile,
side,
});
};
const handlePass = () => {
if (!currentPlayerId || !gameState) return;
const currentPlayer = gameState.players.find(p => p.id === currentPlayerId);
if (!currentPlayer) return;
makeMove({
playerId: currentPlayerId,
tile: currentPlayer.tiles[0],
side: 'left',
pass: true,
});
};
const handlePlayAgain = () => {
leaveRoom();
};
// Show lobby if no game state or game mode is waiting
if (!gameState || gameState.gameMode === 'waiting') {
if (roomId) {
return (
);
}
return (
);
}
const currentPlayer = gameState.players.find(p => p.id === currentPlayerId);
const isMyTurn = gameState.players[gameState.currentPlayerIndex]?.id === currentPlayerId;
const validMoves = currentPlayer ? getValidMoves(currentPlayer, gameState.boardEnds) : [];
const validTileIds = currentPlayer && gameState.boardEnds.length === 0
? currentPlayer.tiles.map(t => t.id)
: Array.from(new Set(validMoves.map(m => m.tile.id)));
const canDraw = gameState.boneyard.length > 0 && validMoves.length === 0;
return (
{/* Header */}
{/* Error notification */}
{error && (
{error}
)}
{/* Rules modal */}
{showRules && (
setShowRules(false)}
role="dialog"
aria-modal="true"
aria-labelledby="rules-title"
>
e.stopPropagation()}
>
How to Play
- • Click on a tile to select it
- • Click "Place Left" or "Place Right" to place it
- • Tiles must match the numbers on the board ends
- • Draw a tile if you can't play
- • First player to use all tiles wins!
- • Game ends when blocked (no one can move)
)}
{/* Main game area */}
{/* Left side - Game board and controls */}
{/* Game info */}
Current Turn
{gameState.players[gameState.currentPlayerIndex]?.name}
Boneyard
{gameState.boneyard.length} tiles
{/* Game board */}
{/* Controls */}
{isMyTurn && currentPlayer && (
{selectedTile && (
Selected:
{selectedTile.left}-{selectedTile.right}
{gameState.boardEnds.length > 0 && (
Board ends: {gameState.boardEnds[0]?.value} (left) | {gameState.boardEnds[1]?.value} (right)
)}
)}
{canDraw && (
)}
{validMoves.length === 0 && !canDraw && (
)}
)}
{/* Current player's hand */}
{currentPlayer && (
)}
{/* Right side - Other players */}
{/* Game over modal */}
{gameState.isGameOver && (
p.id === gameState.winner) || null}
players={gameState.players}
onPlayAgain={handlePlayAgain}
onLeave={leaveRoom}
/>
)}
);
}