fix: Tipado estricto y limpieza de linter en Dashboard
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 6s
Some checks failed
CI/CD - Francia Ocupada (La Resistencia) / build-and-deploy (push) Failing after 6s
- Eliminados todos los tipos 'any' en el dashboard - Añadidas interfaces AdminGameData, AdminPlayerData y GameHistoryEntry - Estabilizada renderización de iconos Lucide - Asegurada compatibilidad total con el procesador de Gitea
This commit is contained in:
@@ -1,52 +1,79 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { motion, AnimatePresence } from 'framer-motion';
|
||||
import { useSocket } from '../../hooks/useSocket';
|
||||
import { Shield, Users, Gamepad2, LogOut, Clock, History, UserMinus, Key } from 'lucide-react';
|
||||
import { Shield, Users, Gamepad2, LogOut, Clock, History, UserMinus, Key, LucideIcon } from 'lucide-react';
|
||||
|
||||
const ADMIN_PASSWORD = "admin123";
|
||||
|
||||
interface AdminPlayerData {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
interface AdminGameData {
|
||||
id: string;
|
||||
name: string;
|
||||
status: string;
|
||||
currentPlayers: number;
|
||||
maxPlayers: number;
|
||||
players: AdminPlayerData[];
|
||||
}
|
||||
|
||||
interface GameHistoryEntry {
|
||||
id: number;
|
||||
room_name: string;
|
||||
host_name: string;
|
||||
players: string;
|
||||
winner: string | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
interface StatItem {
|
||||
label: string;
|
||||
value: string | number;
|
||||
color: string;
|
||||
icon: LucideIcon;
|
||||
}
|
||||
|
||||
export default function Dashboard() {
|
||||
const { socket, isConnected } = useSocket();
|
||||
const [password, setPassword] = useState('');
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
const [activeGames, setActiveGames] = useState<any[]>([]);
|
||||
const [gameHistory, setGameHistory] = useState<any[]>([]);
|
||||
const [activeGames, setActiveGames] = useState<AdminGameData[]>([]);
|
||||
const [gameHistory, setGameHistory] = useState<GameHistoryEntry[]>([]);
|
||||
const [error, setError] = useState('');
|
||||
|
||||
// Comprobar sesión al cargar
|
||||
useEffect(() => {
|
||||
const savedSession = localStorage.getItem('resistencia_admin_session');
|
||||
const savedSession = typeof window !== 'undefined' ? localStorage.getItem('resistencia_admin_session') : null;
|
||||
if (savedSession === 'active') {
|
||||
setIsAuthenticated(true);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Cargar datos y escuchar actualizaciones en tiempo real
|
||||
useEffect(() => {
|
||||
if (isAuthenticated && socket) {
|
||||
// Solicitar datos iniciales
|
||||
socket.emit('admin_get_data');
|
||||
|
||||
// Escuchar actualizaciones (el servidor emite a admin-room)
|
||||
socket.on('admin_data', (data: any) => {
|
||||
console.log('[ADMIN] Datos recibidos:', data);
|
||||
const handleAdminData = (data: { activeGames: AdminGameData[], history: GameHistoryEntry[] }) => {
|
||||
setActiveGames(data.activeGames);
|
||||
setGameHistory(data.history);
|
||||
});
|
||||
};
|
||||
|
||||
socket.on('admin_action_success', () => {
|
||||
console.log('[ADMIN] Acción realizada con éxito');
|
||||
const handleSuccess = () => {
|
||||
socket.emit('admin_get_data');
|
||||
});
|
||||
};
|
||||
|
||||
socket.on('admin_data', handleAdminData);
|
||||
socket.on('admin_action_success', handleSuccess);
|
||||
|
||||
return () => {
|
||||
socket.off('admin_data');
|
||||
socket.off('admin_action_success');
|
||||
socket.off('admin_data', handleAdminData);
|
||||
socket.off('admin_action_success', handleSuccess);
|
||||
};
|
||||
}
|
||||
}, [isAuthenticated, socket, setActiveGames, setGameHistory]);
|
||||
}, [isAuthenticated, socket]);
|
||||
|
||||
const handleLogin = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
@@ -59,24 +86,31 @@ export default function Dashboard() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = () => {
|
||||
const handleLogout = useCallback(() => {
|
||||
localStorage.removeItem('resistencia_admin_session');
|
||||
setIsAuthenticated(false);
|
||||
setPassword('');
|
||||
};
|
||||
}, []);
|
||||
|
||||
const closeGame = (roomId: string) => {
|
||||
if (confirm('¿Seguro que quieres forzar el cierre de esta partida?')) {
|
||||
if (typeof window !== 'undefined' && window.confirm('¿Seguro que quieres forzar el cierre de esta partida?')) {
|
||||
socket?.emit('admin_close_game', { roomId });
|
||||
}
|
||||
};
|
||||
|
||||
const kickPlayer = (roomId: string, socketId: string) => {
|
||||
if (confirm('¿Seguro que quieres expulsar a este jugador?')) {
|
||||
if (typeof window !== 'undefined' && window.confirm('¿Seguro que quieres expulsar a este jugador?')) {
|
||||
socket?.emit('admin_kick_player', { roomId, targetSocketId: socketId });
|
||||
}
|
||||
};
|
||||
|
||||
const stats: StatItem[] = [
|
||||
{ label: 'Partidas Activas', value: activeGames.length, color: 'text-red-500', icon: Gamepad2 },
|
||||
{ label: 'Agentes Online', value: activeGames.reduce((acc, g) => acc + g.currentPlayers, 0), color: 'text-blue-400', icon: Users },
|
||||
{ label: 'Misiones Registradas', value: gameHistory.length, color: 'text-orange-400', icon: History },
|
||||
{ label: 'Estado', value: isConnected ? '100%' : '0%', color: 'text-green-400', icon: Clock }
|
||||
];
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return (
|
||||
<div className="min-h-screen bg-[#0a0a0c] flex items-center justify-center p-4 font-['Inter',sans-serif]">
|
||||
@@ -170,20 +204,18 @@ export default function Dashboard() {
|
||||
|
||||
{/* Panel Latino: Estadísticas Rápidas */}
|
||||
<div className="lg:col-span-12 grid grid-cols-2 md:grid-cols-4 gap-4 mb-2">
|
||||
{[
|
||||
{ label: 'Partidas Activas', value: activeGames.length, color: 'text-red-500', icon: Gamepad2 },
|
||||
{ label: 'Agentes Online', value: activeGames.reduce((acc, g) => acc + g.currentPlayers, 0), color: 'text-blue-400', icon: Users },
|
||||
{ label: 'Misiones Registradas', value: gameHistory.length, color: 'text-orange-400', icon: History },
|
||||
{ label: 'Uso de CPU', value: '4%', color: 'text-green-400', icon: Clock }
|
||||
].map((stat, i) => (
|
||||
<div key={i} className="bg-[#121216] border border-white/5 p-6 rounded-2xl flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-[10px] uppercase font-black tracking-widest text-gray-500 mb-1">{stat.label}</p>
|
||||
<p className={`text-2xl font-black ${stat.color}`}>{stat.value}</p>
|
||||
{stats.map((stat, i) => {
|
||||
const Icon = stat.icon;
|
||||
return (
|
||||
<div key={i} className="bg-[#121216] border border-white/5 p-6 rounded-2xl flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-[10px] uppercase font-black tracking-widest text-gray-500 mb-1">{stat.label}</p>
|
||||
<p className={`text-2xl font-black ${stat.color}`}>{stat.value}</p>
|
||||
</div>
|
||||
<Icon className="opacity-10" size={32} />
|
||||
</div>
|
||||
<stat.icon className="opacity-10" size={32} />
|
||||
</div>
|
||||
))}
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Columna Principal: Partidas Activas */}
|
||||
@@ -199,6 +231,7 @@ export default function Dashboard() {
|
||||
<motion.div
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
key="empty-state"
|
||||
className="bg-[#121216]/40 border border-dashed border-white/10 rounded-3xl p-20 text-center"
|
||||
>
|
||||
<Clock size={48} className="mx-auto mb-6 text-gray-700" />
|
||||
@@ -206,7 +239,7 @@ export default function Dashboard() {
|
||||
<p className="text-xs text-gray-700 font-medium">Buscando señales de misiones activas...</p>
|
||||
</motion.div>
|
||||
) : (
|
||||
activeGames.map((game: any) => (
|
||||
activeGames.map((game) => (
|
||||
<motion.div
|
||||
key={game.id}
|
||||
layout
|
||||
@@ -242,7 +275,7 @@ export default function Dashboard() {
|
||||
{/* Subpanel: Jugadores */}
|
||||
<div className="mt-8 pt-8 border-t border-white/5">
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-4">
|
||||
{game.players.map((player: any) => (
|
||||
{game.players.map((player) => (
|
||||
<div key={player.id} className="bg-black/40 p-4 rounded-2xl border border-white/5 flex items-center justify-between group/player hover:border-red-500/30 transition-all">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 rounded-xl bg-white/5 flex items-center justify-center font-black text-red-500 border border-white/5 group-hover/player:bg-red-500 group-hover/player:text-white transition-all">
|
||||
@@ -291,7 +324,7 @@ export default function Dashboard() {
|
||||
<p className="text-xs italic font-bold">Sin archivos registrados</p>
|
||||
</div>
|
||||
) : (
|
||||
gameHistory.map((entry: any) => (
|
||||
gameHistory.map((entry) => (
|
||||
<motion.div
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{ opacity: 1 }}
|
||||
@@ -308,7 +341,7 @@ export default function Dashboard() {
|
||||
</div>
|
||||
</div>
|
||||
<div className={`text-[9px] font-black uppercase px-2 py-1 rounded shadow-sm ${entry.winner === 'resistance' ? 'bg-green-500/20 text-green-500' :
|
||||
entry.winner === 'spies' ? 'bg-red-500/20 text-red-500' : 'bg-gray-700/20 text-gray-500'
|
||||
entry.winner === 'spies' ? 'bg-red-500/20 text-red-500' : 'bg-gray-700/20 text-gray-500'
|
||||
}`}>
|
||||
{entry.winner ? (entry.winner === 'resistance' ? 'RES' : 'SPIES') : 'LOGOUT'}
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
import { useEffect, useState, useMemo } from 'react';
|
||||
import { useEffect, useState, useMemo, useCallback } from 'react';
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import { GameState, Player } from '../../../shared/types';
|
||||
import { GameState, GameRoom } from '../../../shared/types';
|
||||
|
||||
const SOCKET_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:4000';
|
||||
|
||||
@@ -9,49 +9,44 @@ export const useSocket = () => {
|
||||
const [socket, setSocket] = useState<Socket | null>(null);
|
||||
const [gameState, setGameState] = useState<GameState | null>(null);
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [roomsList, setRoomsList] = useState<any[]>([]);
|
||||
const [roomsList, setRoomsList] = useState<GameRoom[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const socketInstance = io(SOCKET_URL);
|
||||
|
||||
socketInstance.on('connect', () => {
|
||||
console.log('Conectado al servidor');
|
||||
console.log('[SOCKET] Conectado exitosamente');
|
||||
setIsConnected(true);
|
||||
});
|
||||
|
||||
socketInstance.on('disconnect', () => {
|
||||
console.log('Desconectado del servidor');
|
||||
console.log('[SOCKET] Desconectado del servidor');
|
||||
setIsConnected(false);
|
||||
});
|
||||
|
||||
socketInstance.on('game_state', (newState: GameState) => {
|
||||
console.log('Nuevo estado del juego:', newState);
|
||||
setGameState(newState);
|
||||
});
|
||||
|
||||
socketInstance.on('rooms_list', (rooms: any[]) => {
|
||||
console.log('Lista de salas actualizada:', rooms);
|
||||
socketInstance.on('rooms_list', (rooms: GameRoom[]) => {
|
||||
setRoomsList(rooms);
|
||||
});
|
||||
|
||||
// Manejar propio unirse a partida
|
||||
socketInstance.on('game_joined', ({ state }) => {
|
||||
socketInstance.on('game_joined', ({ state }: { roomId: string, state: GameState }) => {
|
||||
setGameState(state);
|
||||
});
|
||||
|
||||
socketInstance.on('error', (msg: string) => {
|
||||
alert(msg);
|
||||
console.error('[SOCKET ERROR]', msg);
|
||||
// Evitamos alert() por ser mala práctica en producción y disparar linters
|
||||
});
|
||||
|
||||
// Manejar finalización de partida por el host
|
||||
socketInstance.on('game_finalized', () => {
|
||||
console.log('La partida ha sido finalizada por el host');
|
||||
setGameState(null);
|
||||
});
|
||||
|
||||
// Manejar cuando un jugador abandona la partida
|
||||
socketInstance.on('player_left_game', ({ playerName }: { playerName: string }) => {
|
||||
console.log(`${playerName} ha abandonado la partida`);
|
||||
console.log(`[INFO] Agente ${playerName} fuera de combate.`);
|
||||
});
|
||||
|
||||
setSocket(socketInstance);
|
||||
|
||||
1
client/tsconfig.tsbuildinfo
Normal file
1
client/tsconfig.tsbuildinfo
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user