Skip to content

Commit 24b647a

Browse files
author
Alicization Town Bot
committed
feat: 在 CLI 输出中附加 Agent 短 ID,修复同名 Agent 无法区分的问题
- formatters.js: 新增 shortId() 工具函数,在 formatLook、formatChat、 formatPerceptions 的输出中为每个 Agent 附加 #xxxx 短标识 - world-engine.js: addChat 增加 playerId 参数,chat entry 携带发送者 ID; drainChat 改用 playerId 过滤自己的消息,修复同名 Agent 消息误过滤 bug
1 parent 736f0ef commit 24b647a

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

server/src/engine/world-engine.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ function sanitize(player) {
142142
};
143143
}
144144

145-
function addChat(name, message, x, y) {
146-
const entry = { id: ++nextChatCursor, time: Date.now(), name, message, x, y };
145+
function addChat(playerId, name, message, x, y) {
146+
const entry = { id: ++nextChatCursor, playerId, time: Date.now(), name, message, x, y };
147147
chatHistory.push(entry);
148148
if (chatHistory.length > MAX_CHAT_MESSAGES) chatHistory.shift();
149149
events.emit('chat', entry);
@@ -153,7 +153,7 @@ function drainChat(playerId) {
153153
const player = players[playerId];
154154
if (!player) return [];
155155
const cursor = player.lastChatCursor || 0;
156-
const newMessages = chatHistory.filter((m) => m.id > cursor && m.name !== player.name);
156+
const newMessages = chatHistory.filter((m) => m.id > cursor && m.playerId !== playerId);
157157
if (newMessages.length > 0) {
158158
player.lastChatCursor = newMessages[newMessages.length - 1].id;
159159
}
@@ -447,7 +447,7 @@ function chat(playerId, text) {
447447
touchAction(playerId);
448448
player.message = text;
449449
player.lastSpeakAt = Date.now();
450-
addChat(player.name, text, player.x, player.y);
450+
addChat(playerId, player.name, text, player.x, player.y);
451451
addActivity(playerId, { type: 'chat', text: `说: "${text.substring(0, 30)}${text.length > 30 ? '...' : ''}"` });
452452
emitPerception('chat', playerId, player.name, player.x, player.y, { text });
453453
broadcast();

shared/town-client/formatters.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
function shortId(id) {
2+
if (!id) return '';
3+
const s = String(id);
4+
return ` #${s.slice(-4)}`;
5+
}
6+
17
function stringifyResult(value) {
28
return JSON.stringify(value, null, 2);
39
}
@@ -53,7 +59,7 @@ function formatLook(result) {
5359

5460
info += '👥 【附近的人】\n';
5561
nearby.forEach((person) => {
56-
info += `- ${person.name} 距离你 ${person.distance} 步 (位于 ${person.zone})`;
62+
info += `- ${person.name}${shortId(person.id)} 距离你 ${person.distance} 步 (位于 ${person.zone})`;
5763
if (person.relativeDirection) info += `,在你的${person.relativeDirection}`;
5864
if (person.message) info += `,他正在说: "${person.message}"`;
5965
else if (person.lastSpeakAt) info += `,最近说过话`;
@@ -85,7 +91,7 @@ function formatChat(messages, selfText) {
8591
for (const msg of messages) {
8692
const t = new Date(msg.time);
8793
const ts = `${String(t.getHours()).padStart(2, '0')}:${String(t.getMinutes()).padStart(2, '0')}`;
88-
info += `[${ts}] ${msg.name}: ${msg.message}\n`;
94+
info += `[${ts}] ${msg.name}${shortId(msg.playerId)}: ${msg.message}\n`;
8995
}
9096
return info.trimEnd();
9197
}
@@ -105,16 +111,17 @@ function formatPerceptions(perceptions) {
105111
for (const event of perceptions) {
106112
const icon = typeLabels[event.type] || '•';
107113
const attentionBar = event.attention >= 0.7 ? '⚡' : event.attention >= 0.4 ? '●' : '○';
114+
const tag = shortId(event.fromId);
108115
if (event.type === 'chat') {
109-
info += `${attentionBar} ${icon} ${event.from} 说: "${event.text}" (距离 ${event.distance} 步)\n`;
116+
info += `${attentionBar} ${icon} ${event.from}${tag} 说: "${event.text}" (距离 ${event.distance} 步)\n`;
110117
} else if (event.type === 'interact') {
111-
info += `${attentionBar} ${icon} ${event.from}${event.zone}进行了: ${event.action} (距离 ${event.distance} 步)\n`;
118+
info += `${attentionBar} ${icon} ${event.from}${tag}${event.zone}进行了: ${event.action} (距离 ${event.distance} 步)\n`;
112119
} else if (event.type === 'move') {
113-
info += `${attentionBar} ${icon} ${event.from} 移动到了${event.zone} (距离 ${event.distance} 步)\n`;
120+
info += `${attentionBar} ${icon} ${event.from}${tag} 移动到了${event.zone} (距离 ${event.distance} 步)\n`;
114121
} else if (event.type === 'join') {
115-
info += `${attentionBar} ${icon} ${event.from} 加入了小镇\n`;
122+
info += `${attentionBar} ${icon} ${event.from}${tag} 加入了小镇\n`;
116123
} else if (event.type === 'leave') {
117-
info += `${attentionBar} ${icon} ${event.from} 离开了小镇\n`;
124+
info += `${attentionBar} ${icon} ${event.from}${tag} 离开了小镇\n`;
118125
}
119126
}
120127
return info.trimEnd();

0 commit comments

Comments
 (0)