diff --git a/resources/js/chat-room/idiom-quiz.js b/resources/js/chat-room/idiom-quiz.js index 94d050d..0cbc5db 100644 --- a/resources/js/chat-room/idiom-quiz.js +++ b/resources/js/chat-room/idiom-quiz.js @@ -8,6 +8,17 @@ function csrf() { let currentRoundId = 0; let currentRoomId = 0; +/** + * 查找当前回合是否已经有对应的聊天室消息节点。 + */ +function findIdiomRoundMessageNode(roundId) { + if (roundId <= 0) { + return null; + } + + return document.querySelector(`[data-idiom-round-id="${roundId}"]`); +} + /** * 为指定回合创建统一样式的答题按钮。 */ @@ -127,8 +138,38 @@ function handleIdiomGameStarted(e) { currentRoundId = round_id; currentRoomId = window.chatContext?.roomId || 0; - // 追加一条聊天室消息(由 MessageSent 事件负责渲染,不重复添加) - // 这里只存储当前回合信息 + // 线上如果 MessageSent 补消息没有到达,这里主动补一条公屏消息兜底; + // 本地或正常链路下若消息已存在,则只补挂答题按钮,避免重复渲染。 + const existingMessageNode = findIdiomRoundMessageNode(round_id); + if (existingMessageNode) { + attachIdiomAnswerButton(existingMessageNode, { + from_user: "星海小博士", + content: message || `🧩 猜成语时间!${hint}`, + idiom_game_round_id: round_id, + idiom_reward_gold: reward_gold, + idiom_reward_exp: reward_exp, + }); + console.log(`猜成语开始:${hint},奖励 ${reward_gold}金/${reward_exp}经验`); + return; + } + + const now = new Date(); + const timeStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")} ${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}:${String(now.getSeconds()).padStart(2, "0")}`; + window.appendMessage?.({ + id: `idiom-start-live-${round_id}`, + room_id: currentRoomId || window.chatContext?.roomId || 0, + from_user: "星海小博士", + to_user: "大家", + content: message || `🧩 猜成语时间!${hint}`, + is_secret: false, + font_color: "#7c3aed", + action: "", + idiom_game_round_id: round_id, + idiom_reward_gold: reward_gold, + idiom_reward_exp: reward_exp, + sent_at: timeStr, + }); + console.log(`猜成语开始:${hint},奖励 ${reward_gold}金/${reward_exp}经验`); } diff --git a/resources/js/chat-room/message-renderer.js b/resources/js/chat-room/message-renderer.js index b2a33cf..9f4ad40 100644 --- a/resources/js/chat-room/message-renderer.js +++ b/resources/js/chat-room/message-renderer.js @@ -121,6 +121,23 @@ export function appendMessage(msg, renderBatch = null) { state.trackMaxMsgId(msg.id || 0); + const idiomRoundId = Number.parseInt( + String(msg.idiom_game_round_id || msg.idom_game_round_id || "0"), + 10, + ); + const isIdiomStartMessage = idiomRoundId > 0 + && msg.from_user === "星海小博士" + && !msg.action + && String(msg.content || "").includes("猜成语时间"); + + if (isIdiomStartMessage) { + const existingIdiomNode = document.querySelector(`[data-idiom-round-id="${idiomRoundId}"]`); + if (existingIdiomNode) { + attachIdiomAnswerButton(existingIdiomNode, msg); + return existingIdiomNode; + } + } + const isMe = msg.from_user === window.chatContext?.username; const fontColor = msg.font_color || "#000000"; const blockRuleKey = resolveBlockedSystemSenderKey(msg); @@ -131,6 +148,9 @@ export function appendMessage(msg, renderBatch = null) { if (msg?.from_user) { div.dataset.fromUser = msg.from_user; } + if (idiomRoundId > 0) { + div.dataset.idiomRoundId = String(idiomRoundId); + } if (blockRuleKey) { div.dataset.blockKey = blockRuleKey; }