@myclaw163/clawclaw-cli 0.6.96 → 0.6.101
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/clawclaw-cli.mjs +3 -3
- package/package.json +1 -1
- package/skills/clawclaw/SKILL.md +26 -10
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +1 -1
- package/skills/clawclaw/references/COMMANDS.md +17 -5
- package/skills/clawclaw/references/STREAM.md +15 -12
- package/src/commands/config.ts +30 -30
- package/src/commands/do.test.ts +27 -1
- package/src/commands/do.ts +61 -0
- package/src/commands/events.test.ts +21 -3
- package/src/commands/events.ts +8 -2
- package/src/commands/game-start-plan.test.ts +158 -0
- package/src/commands/game.ts +670 -64
- package/src/commands/history-player.test.ts +3 -0
- package/src/commands/setup/hermes.test.ts +96 -96
- package/src/commands/setup/hermes.ts +76 -76
- package/src/commands/setup/index.ts +13 -13
- package/src/commands/setup/openclaw.test.ts +124 -114
- package/src/commands/setup/openclaw.ts +162 -147
- package/src/commands/watch.test.ts +272 -0
- package/src/commands/watch.ts +185 -25
- package/src/lib/game-client.ts +34 -0
- package/src/lib/host-config-patcher.test.ts +130 -130
- package/src/lib/host-config-patcher.ts +151 -151
- package/src/lib/room.test.ts +74 -0
- package/src/lib/room.ts +129 -0
- package/src/lib/server-registry.test.ts +54 -0
- package/src/lib/server-registry.ts +35 -2
- package/src/perception/player-history-store.test.ts +92 -2
- package/src/perception/player-history-store.ts +68 -0
- package/src/pipeline/event-format.test.ts +61 -1
- package/src/pipeline/event-format.ts +88 -0
- package/src/pipeline/event-hints.ts +10 -1
- package/src/runtime/event-daemon.test.ts +43 -0
- package/src/runtime/event-daemon.ts +8 -4
- package/src/runtime/room-lobby-runtime.test.ts +95 -0
- package/src/runtime/room-lobby-runtime.ts +367 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, mkdtempSync, readFileSync } from 'fs';
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from 'fs';
|
|
2
2
|
import { tmpdir } from 'os';
|
|
3
3
|
import { join } from 'path';
|
|
4
4
|
import { describe, expect, it } from 'vitest';
|
|
@@ -58,6 +58,93 @@ describe('PlayerHistoryStore', () => {
|
|
|
58
58
|
expect(store.read()!.visible_frames.some((frame) => frame.tick === 9)).toBe(false);
|
|
59
59
|
});
|
|
60
60
|
|
|
61
|
+
it('records self position from state snapshots without touching visible frames', () => {
|
|
62
|
+
const dir = mkdtempSync(join(tmpdir(), 'player-history-'));
|
|
63
|
+
const store = new PlayerHistoryStore(join(dir, 'session.player-history.json'));
|
|
64
|
+
store.reset(Date.parse('2026-05-21T00:00:00.000Z'));
|
|
65
|
+
|
|
66
|
+
expect(store.recordStateSnapshot({
|
|
67
|
+
tick: 21,
|
|
68
|
+
phase: 'wandering',
|
|
69
|
+
you: {
|
|
70
|
+
name: 'me',
|
|
71
|
+
x: 10.5,
|
|
72
|
+
y: 20.25,
|
|
73
|
+
room: 'H1',
|
|
74
|
+
currently_moving: true,
|
|
75
|
+
remaining_secs: 1.2,
|
|
76
|
+
},
|
|
77
|
+
}, Date.parse('2026-05-21T00:00:03.000Z'))).toBe(true);
|
|
78
|
+
|
|
79
|
+
const data = store.read()!;
|
|
80
|
+
expect(data.visible_frames).toEqual([]);
|
|
81
|
+
expect(data.self_name).toBe('me');
|
|
82
|
+
expect(data.last_self_tick).toBe(21);
|
|
83
|
+
expect(data.tick_interval_ms).toBe(100);
|
|
84
|
+
expect(data.self_frames).toEqual([
|
|
85
|
+
{
|
|
86
|
+
tick: 21,
|
|
87
|
+
time: '2026-05-21T00:00:03.000Z',
|
|
88
|
+
game_time_secs: 2.1,
|
|
89
|
+
x: 10.5,
|
|
90
|
+
y: 20.25,
|
|
91
|
+
room: 'H1',
|
|
92
|
+
phase: 'wandering',
|
|
93
|
+
currently_moving: true,
|
|
94
|
+
remaining_secs: 1.2,
|
|
95
|
+
source: 'state',
|
|
96
|
+
},
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('dedupes same-tick self snapshots and ignores older self ticks', () => {
|
|
101
|
+
const dir = mkdtempSync(join(tmpdir(), 'player-history-'));
|
|
102
|
+
const store = new PlayerHistoryStore(join(dir, 'session.player-history.json'));
|
|
103
|
+
store.reset();
|
|
104
|
+
|
|
105
|
+
store.recordStateSnapshot({ tick: 5, you: { name: 'me', x: 1, y: 2, room: 'A' } });
|
|
106
|
+
store.recordStateSnapshot({ tick: 5, you: { name: 'me', x: 3, y: 4, room: 'B' } });
|
|
107
|
+
store.recordStateSnapshot({ tick: 6, you: { name: 'me', x: 5, y: 6, room: 'C' } });
|
|
108
|
+
store.recordStateSnapshot({ tick: 5, you: { name: 'me', x: 9, y: 10, room: 'B2' } });
|
|
109
|
+
|
|
110
|
+
expect(store.recordStateSnapshot({ tick: 4, you: { name: 'me', x: 7, y: 8, room: 'D' } })).toBe(false);
|
|
111
|
+
const data = store.read()!;
|
|
112
|
+
expect(data.self_frames.map((frame) => [frame.tick, frame.x, frame.y, frame.room])).toEqual([
|
|
113
|
+
[5, 9, 10, 'B2'],
|
|
114
|
+
[6, 5, 6, 'C'],
|
|
115
|
+
]);
|
|
116
|
+
expect(data.last_self_tick).toBe(6);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('ignores invalid self state snapshots', () => {
|
|
120
|
+
const dir = mkdtempSync(join(tmpdir(), 'player-history-'));
|
|
121
|
+
const store = new PlayerHistoryStore(join(dir, 'session.player-history.json'));
|
|
122
|
+
store.reset();
|
|
123
|
+
|
|
124
|
+
expect(store.recordStateSnapshot({ tick: 1, you: { name: 'me', x: 1 } })).toBe(false);
|
|
125
|
+
expect(store.recordStateSnapshot({ tick: 1.5, you: { name: 'me', x: 1, y: 2 } })).toBe(false);
|
|
126
|
+
expect(store.recordStateSnapshot({ tick: 2, you: null })).toBe(false);
|
|
127
|
+
expect(store.read()!.self_frames).toEqual([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('loads old sidecars without self history fields', () => {
|
|
131
|
+
const dir = mkdtempSync(join(tmpdir(), 'player-history-'));
|
|
132
|
+
const path = join(dir, 'session.player-history.json');
|
|
133
|
+
writeFileSync(path, JSON.stringify({
|
|
134
|
+
time: '2026-05-21T00:00:00.000Z',
|
|
135
|
+
visible_frames: [],
|
|
136
|
+
seats: {},
|
|
137
|
+
lobster_tasks: [],
|
|
138
|
+
last_raw_frame: null,
|
|
139
|
+
last_history_tick: null,
|
|
140
|
+
}));
|
|
141
|
+
|
|
142
|
+
const data = new PlayerHistoryStore(path).read()!;
|
|
143
|
+
expect(data.self_frames).toEqual([]);
|
|
144
|
+
expect(data.last_self_tick).toBeNull();
|
|
145
|
+
expect(data.tick_interval_ms).toBe(100);
|
|
146
|
+
});
|
|
147
|
+
|
|
61
148
|
it('caches seats and lobster task locations from map data', () => {
|
|
62
149
|
const dir = mkdtempSync(join(tmpdir(), 'player-history-'));
|
|
63
150
|
const store = new PlayerHistoryStore(join(dir, 'session.player-history.json'));
|
|
@@ -80,7 +167,10 @@ describe('PlayerHistoryStore', () => {
|
|
|
80
167
|
const store = new PlayerHistoryStore(path);
|
|
81
168
|
store.reset();
|
|
82
169
|
expect(existsSync(path)).toBe(true);
|
|
83
|
-
expect(JSON.parse(readFileSync(path, 'utf8'))
|
|
170
|
+
expect(JSON.parse(readFileSync(path, 'utf8'))).toMatchObject({
|
|
171
|
+
visible_frames: [],
|
|
172
|
+
self_frames: [],
|
|
173
|
+
});
|
|
84
174
|
store.delete();
|
|
85
175
|
expect(existsSync(path)).toBe(false);
|
|
86
176
|
});
|
|
@@ -20,13 +20,30 @@ export interface LobsterTaskPoint {
|
|
|
20
20
|
y: number;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export interface SelfFrame {
|
|
24
|
+
tick: number;
|
|
25
|
+
time: string;
|
|
26
|
+
game_time_secs: number;
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
room?: string | null;
|
|
30
|
+
phase?: string;
|
|
31
|
+
currently_moving?: boolean;
|
|
32
|
+
remaining_secs?: number;
|
|
33
|
+
source: 'state';
|
|
34
|
+
}
|
|
35
|
+
|
|
23
36
|
export interface PlayerHistoryData {
|
|
24
37
|
time: string;
|
|
25
38
|
visible_frames: VisibleFrame[];
|
|
39
|
+
self_frames: SelfFrame[];
|
|
26
40
|
seats: Record<string, number>;
|
|
27
41
|
lobster_tasks: LobsterTaskPoint[];
|
|
42
|
+
self_name?: string;
|
|
28
43
|
last_raw_frame: null;
|
|
29
44
|
last_history_tick: number | null;
|
|
45
|
+
last_self_tick: number | null;
|
|
46
|
+
tick_interval_ms?: number;
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
const TICK_MS = 100;
|
|
@@ -39,6 +56,10 @@ function isFiniteNumber(value: unknown): value is number {
|
|
|
39
56
|
return typeof value === 'number' && Number.isFinite(value);
|
|
40
57
|
}
|
|
41
58
|
|
|
59
|
+
function gameTimeSecs(tick: number): number {
|
|
60
|
+
return Math.round(tick * TICK_MS) / 1000;
|
|
61
|
+
}
|
|
62
|
+
|
|
42
63
|
function frameTimeFromPrevious(data: PlayerHistoryData, tick: number, nowMs: number): string {
|
|
43
64
|
const lastTick = data.last_history_tick;
|
|
44
65
|
if (lastTick == null || tick <= lastTick) return nowIso(nowMs);
|
|
@@ -52,10 +73,13 @@ function createEmptyData(nowMs = Date.now()): PlayerHistoryData {
|
|
|
52
73
|
return {
|
|
53
74
|
time: nowIso(nowMs),
|
|
54
75
|
visible_frames: [],
|
|
76
|
+
self_frames: [],
|
|
55
77
|
seats: {},
|
|
56
78
|
lobster_tasks: [],
|
|
57
79
|
last_raw_frame: null,
|
|
58
80
|
last_history_tick: null,
|
|
81
|
+
last_self_tick: null,
|
|
82
|
+
tick_interval_ms: TICK_MS,
|
|
59
83
|
};
|
|
60
84
|
}
|
|
61
85
|
|
|
@@ -63,10 +87,14 @@ function normalizeData(raw: Partial<PlayerHistoryData> | null, nowMs = Date.now(
|
|
|
63
87
|
return {
|
|
64
88
|
time: typeof raw?.time === 'string' ? raw.time : nowIso(nowMs),
|
|
65
89
|
visible_frames: Array.isArray(raw?.visible_frames) ? raw.visible_frames : [],
|
|
90
|
+
self_frames: Array.isArray(raw?.self_frames) ? raw.self_frames : [],
|
|
66
91
|
seats: raw?.seats && typeof raw.seats === 'object' ? raw.seats : {},
|
|
67
92
|
lobster_tasks: Array.isArray(raw?.lobster_tasks) ? raw.lobster_tasks : [],
|
|
93
|
+
self_name: typeof raw?.self_name === 'string' ? raw.self_name : undefined,
|
|
68
94
|
last_raw_frame: null,
|
|
69
95
|
last_history_tick: isFiniteNumber(raw?.last_history_tick) ? raw.last_history_tick : null,
|
|
96
|
+
last_self_tick: isFiniteNumber(raw?.last_self_tick) ? raw.last_self_tick : null,
|
|
97
|
+
tick_interval_ms: isFiniteNumber(raw?.tick_interval_ms) ? raw.tick_interval_ms : TICK_MS,
|
|
70
98
|
};
|
|
71
99
|
}
|
|
72
100
|
|
|
@@ -141,6 +169,46 @@ export class PlayerHistoryStore {
|
|
|
141
169
|
this.write(data);
|
|
142
170
|
}
|
|
143
171
|
|
|
172
|
+
recordStateSnapshot(state: Record<string, any>, nowMs = Date.now()): boolean {
|
|
173
|
+
if (!state || typeof state !== 'object') return false;
|
|
174
|
+
const tick = state.tick;
|
|
175
|
+
const you = state.you;
|
|
176
|
+
if (!isFiniteNumber(tick) || !Number.isInteger(tick)) return false;
|
|
177
|
+
if (!you || typeof you !== 'object') return false;
|
|
178
|
+
if (!isFiniteNumber(you.x) || !isFiniteNumber(you.y)) return false;
|
|
179
|
+
|
|
180
|
+
const data = this.read() ?? createEmptyData(nowMs);
|
|
181
|
+
const lastTick = data.last_self_tick;
|
|
182
|
+
const existing = data.self_frames.findIndex((candidate) => candidate.tick === tick);
|
|
183
|
+
if (lastTick != null && tick < lastTick && existing < 0) return false;
|
|
184
|
+
|
|
185
|
+
const frame: SelfFrame = {
|
|
186
|
+
tick,
|
|
187
|
+
time: nowIso(nowMs),
|
|
188
|
+
game_time_secs: gameTimeSecs(tick),
|
|
189
|
+
x: you.x,
|
|
190
|
+
y: you.y,
|
|
191
|
+
room: typeof you.room === 'string' ? you.room : null,
|
|
192
|
+
source: 'state',
|
|
193
|
+
};
|
|
194
|
+
if (typeof state.phase === 'string') frame.phase = state.phase;
|
|
195
|
+
if (typeof you.currently_moving === 'boolean') frame.currently_moving = you.currently_moving;
|
|
196
|
+
if (isFiniteNumber(you.remaining_secs)) frame.remaining_secs = you.remaining_secs;
|
|
197
|
+
if (typeof you.name === 'string' && you.name.length > 0) data.self_name = you.name;
|
|
198
|
+
|
|
199
|
+
if (existing >= 0) {
|
|
200
|
+
data.self_frames[existing] = frame;
|
|
201
|
+
} else {
|
|
202
|
+
data.self_frames.push(frame);
|
|
203
|
+
data.self_frames.sort((a, b) => a.tick - b.tick);
|
|
204
|
+
}
|
|
205
|
+
data.last_self_tick = lastTick == null ? tick : Math.max(lastTick, tick);
|
|
206
|
+
data.tick_interval_ms = TICK_MS;
|
|
207
|
+
data.time = nowIso(nowMs);
|
|
208
|
+
this.write(data);
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
|
|
144
212
|
recordPlayerSpotted(event: Record<string, any>, nowMs = Date.now()): boolean {
|
|
145
213
|
if (event.type !== 'player_spotted') return false;
|
|
146
214
|
const tick = event.tick;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from 'vitest';
|
|
2
|
-
import { compactEventFields, formatEventMessage } from './event-format.js';
|
|
2
|
+
import { compactEventFields, compactStateForEvents, formatEventMessage, shortStateText } from './event-format.js';
|
|
3
3
|
import { EVENT_HINT_FORMATTERS } from './event-hints.js';
|
|
4
4
|
|
|
5
5
|
describe('event-format', () => {
|
|
@@ -203,6 +203,66 @@ describe('event-format', () => {
|
|
|
203
203
|
});
|
|
204
204
|
});
|
|
205
205
|
|
|
206
|
+
it('formats room ready notices as user confirmation prompts', () => {
|
|
207
|
+
const notice = formatEventMessage({
|
|
208
|
+
type: 'room_ready_to_start',
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(notice).toContain('房间已满,可以开始对局。请先询问用户是否现在开始对局。');
|
|
212
|
+
expect(notice).not.toContain('ccl game start-room');
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('formats room game finished as a room return notice', () => {
|
|
216
|
+
const notice = formatEventMessage({
|
|
217
|
+
type: 'room.game_finished',
|
|
218
|
+
game_id: 'game-1',
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
expect(notice).toContain('上一轮房间对局已结束');
|
|
222
|
+
expect(notice).toContain('所有人已回到房间');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('uses custom heartbeat messages when provided', () => {
|
|
226
|
+
expect(formatEventMessage({
|
|
227
|
+
type: 'heartbeat',
|
|
228
|
+
message: '房间 123456 当前 6/10 人,正在等待更多玩家加入。',
|
|
229
|
+
})).toContain('正在等待更多玩家加入');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('compacts room waiting state without game player projection', () => {
|
|
233
|
+
const state = compactStateForEvents({
|
|
234
|
+
phase: 'room_waiting',
|
|
235
|
+
you: { name: 'me', seat: 1, is_owner: true },
|
|
236
|
+
room: {
|
|
237
|
+
room_code: '123456',
|
|
238
|
+
room_type: 'casual',
|
|
239
|
+
status: 'waiting',
|
|
240
|
+
member_count: 10,
|
|
241
|
+
cooldown_seconds: 44,
|
|
242
|
+
is_owner: true,
|
|
243
|
+
ready_to_start: true,
|
|
244
|
+
members: [{ seat: 1, name: 'me', is_owner: true }],
|
|
245
|
+
},
|
|
246
|
+
}, [
|
|
247
|
+
{ type: 'room_ready_to_start', room_code: '123456' },
|
|
248
|
+
]);
|
|
249
|
+
|
|
250
|
+
expect(state).toMatchObject({
|
|
251
|
+
phase: 'room_waiting',
|
|
252
|
+
you: { name: 'me', seat: 1, is_owner: true },
|
|
253
|
+
room: {
|
|
254
|
+
room_code: '123456',
|
|
255
|
+
status: 'waiting',
|
|
256
|
+
member_count: 10,
|
|
257
|
+
ready_to_start: true,
|
|
258
|
+
},
|
|
259
|
+
recent_events: [{ type: 'room_ready_to_start', room_code: '123456' }],
|
|
260
|
+
});
|
|
261
|
+
expect(state).not.toHaveProperty('players');
|
|
262
|
+
expect(state?.room).not.toHaveProperty('cooldown_seconds');
|
|
263
|
+
expect(shortStateText(state)).toContain('ready=confirm-start');
|
|
264
|
+
});
|
|
265
|
+
|
|
206
266
|
it('formats corpse_spotted monitor notices as my discovery with body owner', () => {
|
|
207
267
|
const notice = formatEventMessage({
|
|
208
268
|
type: 'corpse_spotted',
|
|
@@ -24,6 +24,12 @@ function cleanObject<T extends Record<string, any>>(obj: T): T {
|
|
|
24
24
|
return obj;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
function isRoomGameEvent(event: Record<string, any>, ctx: EventFormatContext): boolean {
|
|
28
|
+
return event.game_mode === 'room'
|
|
29
|
+
|| ctx.summary?.game?.mode === 'room'
|
|
30
|
+
|| ctx.summary?.game_mode === 'room';
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
function text(value: unknown, fallback = '?'): string {
|
|
28
34
|
if (typeof value === 'string' && value.length > 0) return value;
|
|
29
35
|
if (typeof value === 'number' && Number.isFinite(value)) return String(value);
|
|
@@ -261,6 +267,7 @@ function shortBody(event: Record<string, any>, ctx: EventFormatContext): string
|
|
|
261
267
|
case 'wandering_speech':
|
|
262
268
|
return `${text(event.actor_name, '你')}在${firstString(event, ['room'])}说:${truncate(event.text ?? event.message, maxTextLength)}`;
|
|
263
269
|
case 'game_over':
|
|
270
|
+
if (isRoomGameEvent(event, ctx)) return '本轮房间对局结束,将回到房间等待。';
|
|
264
271
|
return `游戏结束:${firstString(event, ['winner_name'], '未知玩家')}获胜(${firstString(event, ['winner'])}),原因 ${firstString(event, ['reason'])}。`;
|
|
265
272
|
case 'user_left_game':
|
|
266
273
|
return 'The user left the game from the web page. This match has ended; stop playing and do not restart unless the user asks.';
|
|
@@ -268,6 +275,42 @@ function shortBody(event: Record<string, any>, ctx: EventFormatContext): string
|
|
|
268
275
|
return `仍在匹配队列,已等待${text(event.waited_secs, '?')}秒。`;
|
|
269
276
|
case 'match_timeout':
|
|
270
277
|
return `匹配超时,累计等待${text(event.waited_secs, '?')}秒。询问用户是否重试。`;
|
|
278
|
+
case 'room.snapshot': {
|
|
279
|
+
const count = Array.isArray(event.members) ? event.members.length : event.member_count;
|
|
280
|
+
const owner = event.is_owner ? '你是房主' : '等待房主';
|
|
281
|
+
const cooldown = numberValue(event.cooldown_seconds);
|
|
282
|
+
const cooldownText = cooldown && cooldown > 0 ? `,冷却${cooldown}秒` : '';
|
|
283
|
+
return `房间${firstString(event, ['room_code'])}:${firstString(event, ['status'])},${text(count, '?')}/10,${owner}${cooldownText}。`;
|
|
284
|
+
}
|
|
285
|
+
case 'room.member_joined':
|
|
286
|
+
return `${playerLabel(event.name ?? event.agent_id, firstNumber(event, ['seat']), ctx)}加入房间。`;
|
|
287
|
+
case 'room.member_left':
|
|
288
|
+
return `${playerLabel(event.name ?? event.agent_id, firstNumber(event, ['seat']), ctx)}离开房间。`;
|
|
289
|
+
case 'room.owner_changed':
|
|
290
|
+
return `房主变更为${playerLabel(event.name ?? event.agent_id, firstNumber(event, ['seat']), ctx)}。`;
|
|
291
|
+
case 'room.full':
|
|
292
|
+
return '房间已满,可以开始对局。请先询问用户是否现在开始对局。';
|
|
293
|
+
case 'room_ready_to_start':
|
|
294
|
+
return '房间已满,可以开始对局。请先询问用户是否现在开始对局。';
|
|
295
|
+
case 'room.chat':
|
|
296
|
+
return `${playerLabel(event.name ?? event.agent_id, firstNumber(event, ['seat']), ctx)}房间弹幕:${truncate(event.content ?? event.text ?? event.message, maxTextLength)}`;
|
|
297
|
+
case 'room.game_starting':
|
|
298
|
+
return `房间对局开始倒计时:${text(event.countdown, '?')}秒。`;
|
|
299
|
+
case 'room.game_started':
|
|
300
|
+
return '房间对局已启动,正在切换到 GameServer。';
|
|
301
|
+
case 'room.game_start_failed':
|
|
302
|
+
return `房间开局失败:${firstString(event, ['reason'], 'unknown')}。`;
|
|
303
|
+
case 'room.game_finished':
|
|
304
|
+
return '上一轮房间对局已结束,所有人已回到房间。';
|
|
305
|
+
case 'room.cooldown':
|
|
306
|
+
return `房间冷却中:${text(event.seconds_remaining ?? event.cooldown_seconds, '?')}秒。`;
|
|
307
|
+
case 'room.self_left':
|
|
308
|
+
case 'room_self_left':
|
|
309
|
+
return 'The user left the room from the web page, or the current CLI quit command has left the room. Stop the room flow and do not create or join another room unless the user asks.';
|
|
310
|
+
case 'room_returning_to_lobby':
|
|
311
|
+
return '房间对局已结束,正在重连 Lobby 房间长流。';
|
|
312
|
+
case 'room_ping_timeout':
|
|
313
|
+
return '房间 Lobby 连接心跳超时,正在重连。';
|
|
271
314
|
case 'robot_speak_rule':
|
|
272
315
|
return `发言提示:${truncate(event.message ?? event.text, maxTextLength)}`;
|
|
273
316
|
case 'strategy_alert':
|
|
@@ -275,6 +318,7 @@ function shortBody(event: Record<string, any>, ctx: EventFormatContext): string
|
|
|
275
318
|
case 'game_start':
|
|
276
319
|
return '事件流已连接,等待身份分配。';
|
|
277
320
|
case 'heartbeat':
|
|
321
|
+
if (typeof event.message === 'string' && event.message.length > 0) return truncate(event.message, maxTextLength);
|
|
278
322
|
return '事件流正常,暂无新事件。';
|
|
279
323
|
case 'snapshot':
|
|
280
324
|
return '当前状态快照。';
|
|
@@ -443,6 +487,40 @@ export function compactStateForEvents(
|
|
|
443
487
|
events: Array<Record<string, any>> = [],
|
|
444
488
|
): Record<string, any> | null {
|
|
445
489
|
if (!summary || typeof summary !== 'object') return null;
|
|
490
|
+
if (typeof summary.phase === 'string' && summary.phase.startsWith('room_')) {
|
|
491
|
+
const room = summary.room ?? {};
|
|
492
|
+
const you = summary.you ?? {};
|
|
493
|
+
return cleanObject({
|
|
494
|
+
phase: summary.phase,
|
|
495
|
+
you: cleanObject({
|
|
496
|
+
name: you.name,
|
|
497
|
+
seat: you.seat,
|
|
498
|
+
is_owner: you.is_owner,
|
|
499
|
+
}),
|
|
500
|
+
room: cleanObject({
|
|
501
|
+
room_code: room.room_code,
|
|
502
|
+
room_type: room.room_type,
|
|
503
|
+
status: room.status,
|
|
504
|
+
member_count: room.member_count,
|
|
505
|
+
deadline: room.deadline,
|
|
506
|
+
is_owner: room.is_owner,
|
|
507
|
+
ready_to_start: room.ready_to_start,
|
|
508
|
+
members: room.members,
|
|
509
|
+
game_id: room.game_id,
|
|
510
|
+
game_server_name: room.game_server_name,
|
|
511
|
+
}),
|
|
512
|
+
recent_events: events.slice(-10).map((event) => cleanObject({
|
|
513
|
+
type: event.type,
|
|
514
|
+
room_code: event.room_code,
|
|
515
|
+
name: event.name,
|
|
516
|
+
seat: event.seat,
|
|
517
|
+
content: event.content,
|
|
518
|
+
reason: event.reason,
|
|
519
|
+
seconds_remaining: event.seconds_remaining,
|
|
520
|
+
})),
|
|
521
|
+
automation: summary.automation,
|
|
522
|
+
});
|
|
523
|
+
}
|
|
446
524
|
const you = summary.you ?? {};
|
|
447
525
|
const meeting = summary.meeting ?? null;
|
|
448
526
|
const alivePlayers = Array.isArray(meeting?.alive_players) ? meeting.alive_players : undefined;
|
|
@@ -486,6 +564,16 @@ export function compactStateForEvents(
|
|
|
486
564
|
export function shortStateText(summary: any | null | undefined): string {
|
|
487
565
|
if (!summary || typeof summary !== 'object') return 'state unavailable';
|
|
488
566
|
const phase = text(summary.phase, 'unknown');
|
|
567
|
+
if (phase.startsWith('room_')) {
|
|
568
|
+
const room = summary.room ?? {};
|
|
569
|
+
const parts = [phase];
|
|
570
|
+
if (room.room_code) parts.push(`room=${room.room_code}`);
|
|
571
|
+
if (room.status) parts.push(`status=${room.status}`);
|
|
572
|
+
if (room.member_count !== undefined) parts.push(`members=${room.member_count}/10`);
|
|
573
|
+
if (room.is_owner !== undefined) parts.push(room.is_owner ? 'owner=yes' : 'owner=no');
|
|
574
|
+
if (room.ready_to_start) parts.push('ready=confirm-start');
|
|
575
|
+
return parts.join('; ');
|
|
576
|
+
}
|
|
489
577
|
const sub = typeof summary.meeting?.sub_phase === 'string' ? `/${summary.meeting.sub_phase}` : '';
|
|
490
578
|
const parts = [`${phase}${sub}`];
|
|
491
579
|
const you = summary.you ?? {};
|
|
@@ -31,6 +31,12 @@ function firstString(event: Record<string, any>, keys: string[], fallback = '?')
|
|
|
31
31
|
return fallback;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
function isRoomGameEvent(event: Record<string, any>, ctx: EventHintContext): boolean {
|
|
35
|
+
return event.game_mode === 'room'
|
|
36
|
+
|| ctx.summary?.game?.mode === 'room'
|
|
37
|
+
|| ctx.summary?.game_mode === 'room';
|
|
38
|
+
}
|
|
39
|
+
|
|
34
40
|
function firstNumber(event: Record<string, any>, keys: string[]): number | undefined {
|
|
35
41
|
for (const key of keys) {
|
|
36
42
|
const value = numberValue(event[key]);
|
|
@@ -179,7 +185,10 @@ export const EVENT_HINT_FORMATTERS: Record<string, EventHintFormatter> = {
|
|
|
179
185
|
if (result === 'tie') return 'Meeting ended — tie vote, no exile. Deadlock shields the guilty and wastes the innocent\'s leverage.';
|
|
180
186
|
return 'Meeting ended — majority skipped, no exile. The table refused to pull the trigger.';
|
|
181
187
|
},
|
|
182
|
-
game_over: (event) => {
|
|
188
|
+
game_over: (event, ctx) => {
|
|
189
|
+
if (isRoomGameEvent(event, ctx)) {
|
|
190
|
+
return '本轮房间对局结束,将回到房间等待。';
|
|
191
|
+
}
|
|
183
192
|
if (event.reason === 'game_timeout') {
|
|
184
193
|
return 'Game over — match time limit reached. The round ends with no faction victory. Review this game with your user, discuss key moments and decisions, then ask if they want to play again.';
|
|
185
194
|
}
|
|
@@ -88,6 +88,29 @@ describe('EventRuntime map seat cache', () => {
|
|
|
88
88
|
});
|
|
89
89
|
});
|
|
90
90
|
|
|
91
|
+
describe('EventRuntime self history', () => {
|
|
92
|
+
it('records self position from _state snapshots without appending a history event', () => {
|
|
93
|
+
const { runtime, appended } = makeRuntimeHarness();
|
|
94
|
+
const recordStateSnapshot = vi.fn();
|
|
95
|
+
(runtime as any).playerHistory = { recordStateSnapshot };
|
|
96
|
+
|
|
97
|
+
(runtime as any).handleEvent({
|
|
98
|
+
type: '_state',
|
|
99
|
+
phase: 'wandering',
|
|
100
|
+
tick: 42,
|
|
101
|
+
you: { name: 'me', x: 12, y: 34, room: 'H1' },
|
|
102
|
+
new_events: [],
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
expect(recordStateSnapshot).toHaveBeenCalledWith(expect.objectContaining({
|
|
106
|
+
phase: 'wandering',
|
|
107
|
+
tick: 42,
|
|
108
|
+
you: { name: 'me', x: 12, y: 34, room: 'H1' },
|
|
109
|
+
}));
|
|
110
|
+
expect(appended.some((event) => event.type === '_state')).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
91
114
|
describe('EventRuntime game over detection', () => {
|
|
92
115
|
it('stops when a state snapshot phase is game_over even without a game_over event', () => {
|
|
93
116
|
const { runtime, appended, stops } = makeRuntimeHarness();
|
|
@@ -139,6 +162,26 @@ describe('EventRuntime game over detection', () => {
|
|
|
139
162
|
]));
|
|
140
163
|
expect(stops).toEqual(['game_over']);
|
|
141
164
|
});
|
|
165
|
+
|
|
166
|
+
it('adds room game_mode to synthetic game_over events', () => {
|
|
167
|
+
const { runtime, appended } = makeRuntimeHarness({
|
|
168
|
+
getGameMode: () => 'room',
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
(runtime as any).handleEvent({
|
|
172
|
+
type: '_state',
|
|
173
|
+
phase: 'game_over',
|
|
174
|
+
tick: 12,
|
|
175
|
+
winner: 'crab',
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
expect(appended).toEqual(expect.arrayContaining([
|
|
179
|
+
expect.objectContaining({
|
|
180
|
+
type: 'game_over',
|
|
181
|
+
game_mode: 'room',
|
|
182
|
+
}),
|
|
183
|
+
]));
|
|
184
|
+
});
|
|
142
185
|
});
|
|
143
186
|
|
|
144
187
|
describe('EventRuntime user-left-game detection', () => {
|
|
@@ -10,6 +10,8 @@ import { PlayerHistoryStore } from '../perception/player-history-store.js';
|
|
|
10
10
|
import type { WsCloseInfo } from './ws-client.js';
|
|
11
11
|
import type { QueueMode } from '../lib/competition.js';
|
|
12
12
|
|
|
13
|
+
type RuntimeGameMode = QueueMode | 'room';
|
|
14
|
+
|
|
13
15
|
const log = new RuntimeLogger();
|
|
14
16
|
|
|
15
17
|
let feedSerial = 0;
|
|
@@ -135,14 +137,14 @@ export interface EventRuntimeOptions {
|
|
|
135
137
|
authStore?: AuthStore;
|
|
136
138
|
onStop?: (stop: EventRuntimeStop) => void;
|
|
137
139
|
getAutomation?: () => Record<string, any> | undefined;
|
|
138
|
-
getGameMode?: () =>
|
|
140
|
+
getGameMode?: () => RuntimeGameMode | undefined;
|
|
139
141
|
}
|
|
140
142
|
|
|
141
143
|
export class EventRuntime {
|
|
142
144
|
private readonly store: AuthStore;
|
|
143
145
|
private readonly onStop?: (stop: EventRuntimeStop) => void;
|
|
144
146
|
private readonly getAutomation?: () => Record<string, any> | undefined;
|
|
145
|
-
private readonly getGameMode?: () =>
|
|
147
|
+
private readonly getGameMode?: () => RuntimeGameMode | undefined;
|
|
146
148
|
private events: EventStore | null = null;
|
|
147
149
|
private playerHistory: PlayerHistoryStore | null = null;
|
|
148
150
|
private client: GameClient | null = null;
|
|
@@ -304,7 +306,7 @@ export class EventRuntime {
|
|
|
304
306
|
this.currentMeeting,
|
|
305
307
|
this.recentEvents,
|
|
306
308
|
this.currentYou,
|
|
307
|
-
this.currentGame,
|
|
309
|
+
cleanObject({ ...this.currentGame, mode: this.getGameMode?.() }),
|
|
308
310
|
this.getAutomation?.(),
|
|
309
311
|
);
|
|
310
312
|
if (!shouldWriteFeedFile()) return;
|
|
@@ -323,6 +325,7 @@ export class EventRuntime {
|
|
|
323
325
|
synthetic: true,
|
|
324
326
|
source: '_state',
|
|
325
327
|
tick: state.tick,
|
|
328
|
+
game_mode: this.getGameMode?.(),
|
|
326
329
|
winner: state.winner,
|
|
327
330
|
winning_faction: state.winning_faction,
|
|
328
331
|
result: state.result,
|
|
@@ -372,6 +375,7 @@ export class EventRuntime {
|
|
|
372
375
|
if (this.currentYou.seat === undefined && mySeat !== undefined) {
|
|
373
376
|
this.currentYou = { ...this.currentYou, seat: mySeat };
|
|
374
377
|
}
|
|
378
|
+
this.playerHistory?.recordStateSnapshot(s);
|
|
375
379
|
this.currentGame = cleanObject({
|
|
376
380
|
game_id: s.game_id,
|
|
377
381
|
alive_count: s.alive_count,
|
|
@@ -465,7 +469,7 @@ export class EventRuntime {
|
|
|
465
469
|
private handleEvent(data: Record<string, any>): void {
|
|
466
470
|
if (this.stopped) return;
|
|
467
471
|
try {
|
|
468
|
-
if (data.type === 'game_started' && data.game_mode == null) {
|
|
472
|
+
if ((data.type === 'game_started' || data.type === 'game_over') && data.game_mode == null) {
|
|
469
473
|
const gameMode = this.getGameMode?.();
|
|
470
474
|
if (gameMode) data = { ...data, game_mode: gameMode };
|
|
471
475
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { RoomLobbyRuntime, gameStartedFromRoomSnapshot } from './room-lobby-runtime.js';
|
|
3
|
+
|
|
4
|
+
describe('gameStartedFromRoomSnapshot', () => {
|
|
5
|
+
it('returns null for non-playing room snapshots', () => {
|
|
6
|
+
expect(gameStartedFromRoomSnapshot({
|
|
7
|
+
status: 'waiting',
|
|
8
|
+
room_code: '123456',
|
|
9
|
+
game_server_name: 'room-gs',
|
|
10
|
+
})).toBeNull();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('returns null when a playing snapshot has no game server route', () => {
|
|
14
|
+
expect(gameStartedFromRoomSnapshot({
|
|
15
|
+
status: 'playing',
|
|
16
|
+
room_code: '123456',
|
|
17
|
+
game_id: 'game-1',
|
|
18
|
+
})).toBeNull();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('converts a playing snapshot with a game server name into a game-start event', () => {
|
|
22
|
+
expect(gameStartedFromRoomSnapshot({
|
|
23
|
+
status: 'playing',
|
|
24
|
+
room_code: '123456',
|
|
25
|
+
game_id: 'game-1',
|
|
26
|
+
game_server_name: 'room-gs',
|
|
27
|
+
})).toEqual({
|
|
28
|
+
game_id: 'game-1',
|
|
29
|
+
game_server_name: 'room-gs',
|
|
30
|
+
ws_url: undefined,
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('RoomLobbyRuntime room.game_finished', () => {
|
|
36
|
+
it('returns the local room snapshot to waiting without ending the room stream', () => {
|
|
37
|
+
const runtime: any = new RoomLobbyRuntime();
|
|
38
|
+
runtime.latestSnapshot = {
|
|
39
|
+
room_code: '123456',
|
|
40
|
+
status: 'playing',
|
|
41
|
+
game_id: 'game-1',
|
|
42
|
+
game_server_name: 'room-gs',
|
|
43
|
+
ws_url: 'ws://game',
|
|
44
|
+
members: [{ seat: 1, name: 'me', is_owner: true }],
|
|
45
|
+
};
|
|
46
|
+
runtime.gameStartedEmitted = true;
|
|
47
|
+
runtime.events = { append: () => {} };
|
|
48
|
+
runtime.writeFeed = () => {};
|
|
49
|
+
|
|
50
|
+
runtime.handleEvent('room.game_finished', { game_id: 'game-1' });
|
|
51
|
+
|
|
52
|
+
expect(runtime.currentRoomSnapshot()).toMatchObject({
|
|
53
|
+
room_code: '123456',
|
|
54
|
+
status: 'waiting',
|
|
55
|
+
members: [{ seat: 1, name: 'me', is_owner: true }],
|
|
56
|
+
});
|
|
57
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('game_id');
|
|
58
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('game_server_name');
|
|
59
|
+
expect(runtime.currentRoomSnapshot()).not.toHaveProperty('ws_url');
|
|
60
|
+
expect(runtime.stopped).toBe(false);
|
|
61
|
+
expect(runtime.gameStartedEmitted).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('RoomLobbyRuntime room.snapshot logging', () => {
|
|
66
|
+
it('omits volatile cooldown seconds from room.snapshot event logs only', () => {
|
|
67
|
+
const runtime: any = new RoomLobbyRuntime();
|
|
68
|
+
const appended: any[] = [];
|
|
69
|
+
runtime.events = { append: (event: any) => appended.push(event) };
|
|
70
|
+
runtime.writeFeed = () => {};
|
|
71
|
+
|
|
72
|
+
runtime.handleEvent('room.snapshot', {
|
|
73
|
+
room_code: '123456',
|
|
74
|
+
status: 'waiting',
|
|
75
|
+
cooldown_seconds: 44,
|
|
76
|
+
seconds_remaining: 44,
|
|
77
|
+
cooldown_until: '2026-07-07T03:00:45.000Z',
|
|
78
|
+
members: [],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const snapshotEvent = appended.find((event) => event.type === 'room.snapshot');
|
|
82
|
+
expect(snapshotEvent).toMatchObject({
|
|
83
|
+
type: 'room.snapshot',
|
|
84
|
+
room_code: '123456',
|
|
85
|
+
status: 'waiting',
|
|
86
|
+
cooldown_until: '2026-07-07T03:00:45.000Z',
|
|
87
|
+
});
|
|
88
|
+
expect(snapshotEvent).not.toHaveProperty('cooldown_seconds');
|
|
89
|
+
expect(snapshotEvent).not.toHaveProperty('seconds_remaining');
|
|
90
|
+
expect(runtime.currentRoomSnapshot()).toMatchObject({
|
|
91
|
+
cooldown_seconds: 44,
|
|
92
|
+
seconds_remaining: 44,
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|