feat: v1.2.0 房间详情/聊天/踢人 + 速率限制 + WebSocket增强

Server:
- API速率限制中间件 (120 req/min per IP, X-RateLimit headers)
- 房间聊天API: POST /rooms/:id/chat
- 认证中间件放行公开GET路由和房间join
- WebSocket: 房间订阅/取消订阅 (subscribe/unsubscribe)
- WebSocket: 房间聊天广播 (chat -> broadcastToRoom)
- WebSocket: 房间事件通知 (roomCreated/Deleted/playerJoined/Left)

Client:
- 房间详情弹窗: 点击房间卡片打开
  - 房间信息网格 (房间号/房主/版本/人数)
  - 在线玩家列表 (5秒自动刷新)
  - 踢出玩家 (确认对话框)
  - 房间聊天 (实时发送/显示)
  - 加入房间 / 删除房间按钮
- 连接状态指示器动画 (online/offline/connecting)
- 房间卡片hover效果
- 版本更新到 v1.2.0
- ApiClient: 新增 getRoomDetail/kickPlayer/sendChat
- Preload: 新增对应IPC方法
- Main: 新增 rooms:detail/kick/chat handlers
This commit is contained in:
FunMC
2026-02-23 08:21:09 +08:00
parent 7fdc570391
commit 80fe5e6e6e
8 changed files with 415 additions and 5 deletions

View File

@@ -326,6 +326,122 @@ async function showRecentServers() {
});
}
// ===== Room Detail Modal =====
let currentModalRoomId = null;
let modalRefreshTimer = null;
function openRoomModal(roomId) {
currentModalRoomId = roomId;
$('#modal-overlay').classList.remove('hidden');
refreshModalRoom();
modalRefreshTimer = setInterval(refreshModalRoom, 5000);
}
function closeRoomModal() {
currentModalRoomId = null;
$('#modal-overlay').classList.add('hidden');
if (modalRefreshTimer) { clearInterval(modalRefreshTimer); modalRefreshTimer = null; }
}
$('#modal-close').addEventListener('click', closeRoomModal);
$('#modal-overlay').addEventListener('click', (e) => {
if (e.target === $('#modal-overlay')) closeRoomModal();
});
async function refreshModalRoom() {
if (!currentModalRoomId || !isConnected) return;
const result = await window.funmc.getRoomDetail(currentModalRoomId);
if (!result || !result.success) return;
const room = result.data.room;
const players = result.data.players || [];
$('#modal-room-name').textContent = `${room.name} ${room.password ? '🔒' : ''}`;
$('#modal-room-info').innerHTML = [
{ l: '房间号', v: room.id },
{ l: '房主', v: room.hostName },
{ l: '版本', v: `${room.gameEdition === 'java' ? 'Java' : '基岩'} ${room.gameVersion}` },
{ l: '玩家', v: `${room.currentPlayers}/${room.maxPlayers}` },
].map(i => `<div class="modal-info-item"><div class="label">${i.l}</div><div class="value">${i.v}</div></div>`).join('');
$('#modal-player-count').textContent = `(${players.length})`;
if (players.length === 0) {
$('#modal-player-list').innerHTML = '<div class="player-empty">暂无玩家在线</div>';
} else {
$('#modal-player-list').innerHTML = players.map(p => `
<div class="player-item">
<div>
<span class="player-name">👤 ${escapeHtml(p.name)}</span>
<span class="player-time">${formatTimeSince(p.joinedAt)}</span>
</div>
<button class="btn-kick" onclick="kickPlayer('${currentModalRoomId}','${p.id}')">踢出</button>
</div>
`).join('');
}
}
async function kickPlayer(roomId, playerId) {
if (!confirm('确定要踢出该玩家吗?')) return;
const result = await window.funmc.kickPlayer(roomId, playerId);
if (result && result.success) {
refreshModalRoom();
} else {
alert('踢出失败: ' + (result?.error || '未知错误'));
}
}
$('#modal-join-btn').addEventListener('click', () => {
if (currentModalRoomId) {
closeRoomModal();
quickJoin(currentModalRoomId);
}
});
$('#modal-delete-btn').addEventListener('click', async () => {
if (!currentModalRoomId) return;
if (!confirm('确定要删除此房间吗?此操作不可撤销。')) return;
const result = await window.funmc.deleteRoom(currentModalRoomId);
if (result && result.success) {
closeRoomModal();
loadRooms();
} else {
alert('删除失败: ' + (result?.error || '未知错误'));
}
});
// Chat in modal
$('#modal-chat-send').addEventListener('click', sendChatMessage);
$('#modal-chat-input').addEventListener('keydown', (e) => {
if (e.key === 'Enter') sendChatMessage();
});
async function sendChatMessage() {
const input = $('#modal-chat-input');
const msg = input.value.trim();
if (!msg || !currentModalRoomId) return;
input.value = '';
const sender = appSettings.playerName || 'Player';
appendChatMessage(sender, msg);
await window.funmc.sendChat(currentModalRoomId, sender, msg);
}
function appendChatMessage(sender, text, time) {
const container = $('#modal-chat-messages');
const t = time || new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
const div = document.createElement('div');
div.className = 'chat-msg';
div.innerHTML = `<span class="chat-sender">${escapeHtml(sender)}</span> <span class="chat-text">${escapeHtml(text)}</span><span class="chat-time">${t}</span>`;
container.appendChild(div);
container.scrollTop = container.scrollHeight;
}
// Make room cards open modal
document.addEventListener('click', (e) => {
const card = e.target.closest('.room-card');
if (card && !e.target.classList.contains('room-btn-join')) {
openRoomModal(card.dataset.id);
}
});
// ===== Utilities =====
function showMsg(selector, text, type) {
const el = $(selector);
@@ -347,3 +463,11 @@ function formatUptime(seconds) {
if (m > 0) return `${m}${s}`;
return `${s}`;
}
function formatTimeSince(dateStr) {
const ms = Date.now() - new Date(dateStr).getTime();
const m = Math.floor(ms / 60000);
if (m < 1) return '刚刚';
if (m < 60) return `${m}分钟前`;
return `${Math.floor(m / 60)}小时前`;
}