import { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom' import { useRoomStore, Room } from '../stores/roomStore' import { useAuthStore } from '../stores/authStore' function RoomCard({ room, onJoin }: { room: Room; onJoin: (room: Room) => void }) { const statusColors: Record = { open: 'text-accent-green', in_game: 'text-accent-orange', closed: 'text-text-muted', } const statusLabels: Record = { open: '开放', in_game: '游戏中', closed: '已关闭', } return (
onJoin(room)}>

{room.name}

{room.has_password && ( )}

房主: {room.owner_username}

{statusLabels[room.status] ?? room.status}
{room.current_players}/{room.max_players}
{room.game_version}
) } export default function DashboardPage() { const { rooms, loading, fetchRooms, joinRoom, createRoom } = useRoomStore() const { user } = useAuthStore() const navigate = useNavigate() const [showCreate, setShowCreate] = useState(false) const [createName, setCreateName] = useState('') const [createVersion, setCreateVersion] = useState('1.20') const [createMax, setCreateMax] = useState('10') const [createPw, setCreatePw] = useState('') const [createPublic, setCreatePublic] = useState(true) const [createError, setCreateError] = useState('') const [creating, setCreating] = useState(false) useEffect(() => { fetchRooms() const id = setInterval(fetchRooms, 10000) return () => clearInterval(id) }, []) const handleJoin = async (room: Room) => { try { let password: string | undefined if (room.has_password) { password = window.prompt(`房间 "${room.name}" 需要密码:`) ?? undefined if (password === undefined) return } await joinRoom(room.id, password) navigate(`/room/${room.id}`) } catch (e: any) { alert(`加入失败: ${e}`) } } const handleCreate = async (e: React.FormEvent) => { e.preventDefault() setCreateError('') setCreating(true) try { const roomId = await createRoom({ name: createName, maxPlayers: parseInt(createMax) || 10, isPublic: createPublic, password: createPw || undefined, gameVersion: createVersion, }) await joinRoom(roomId) navigate(`/room/${roomId}`) } catch (e: any) { setCreateError(String(e)) setCreating(false) } } return (
{/* Header */}

游戏大厅

找到一个房间加入,或创建你自己的

{/* Room grid */}
{loading && rooms.length === 0 ? (
加载中...
) : rooms.length === 0 ? (

暂无公开房间

创建第一个房间开始游戏

) : (
{rooms.map((room) => ( ))}
)}
{/* Create room modal */} {showCreate && (

创建房间

setCreateName(e.target.value)} required />
setCreateVersion(e.target.value)} />
setCreateMax(e.target.value)} />
setCreatePw(e.target.value)} />
{createError && (
{createError}
)}
)}
) }