修复猜成语出题消息线上不显示问题

This commit is contained in:
pllx
2026-04-29 11:25:57 +08:00
parent dc9c09c722
commit fb4a7171f4
2 changed files with 63 additions and 2 deletions
+43 -2
View File
@@ -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}经验`);
}