@myclaw163/clawclaw-cli 0.6.105 → 0.6.107

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.
Files changed (225) hide show
  1. package/README.md +429 -429
  2. package/bin/clawclaw-cli.mjs +3 -3
  3. package/package.json +48 -48
  4. package/personas//345/217/243/346/230/257/345/277/203/351/235/236.md +50 -50
  5. package/personas//345/277/240/345/277/203/350/200/201/344/273/206.md +51 -51
  6. package/personas//346/227/240/344/272/272/350/256/276.md +1 -1
  7. package/personas//350/264/253/345/230/264/346/215/237/345/217/213.md +48 -48
  8. package/personas//350/275/257/350/220/214/350/205/271/351/273/221.md +51 -51
  9. package/scripts/check-skill-command-surface.mjs +118 -118
  10. package/scripts/check_monitor.cjs +435 -435
  11. package/scripts/check_monitor.test.mjs +212 -212
  12. package/scripts/enable_monitor.cjs +677 -677
  13. package/scripts/enable_monitor.test.mjs +201 -201
  14. package/scripts/find-hide-spots.py +157 -157
  15. package/scripts/postinstall.mjs +20 -20
  16. package/scripts/sync-bundled-skill.mjs +254 -254
  17. package/scripts/sync-bundled-skill.test.mjs +152 -152
  18. package/skills/clawclaw/SKILL.md +275 -275
  19. package/skills/clawclaw/references/CHATTERBOX.md +142 -142
  20. package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +224 -224
  21. package/skills/clawclaw/references/COMMANDS.md +165 -165
  22. package/skills/clawclaw/references/GAME-MECHANICS.md +190 -190
  23. package/skills/clawclaw/references/HUB.md +48 -48
  24. package/skills/clawclaw/references/KNOWLEDGE.md +42 -42
  25. package/skills/clawclaw/references/STRATEGIES.md +59 -59
  26. package/skills/clawclaw/references/STREAM.md +75 -75
  27. package/skills/clawclaw/references/TACTICS.md +65 -65
  28. package/src/assets/clawclaw-ascii-map.txt +40 -40
  29. package/src/cli.ts +113 -113
  30. package/src/commands/_schema.ts +124 -124
  31. package/src/commands/account.test.ts +345 -345
  32. package/src/commands/account.ts +340 -340
  33. package/src/commands/competition.ts +42 -42
  34. package/src/commands/data.test.ts +33 -33
  35. package/src/commands/data.ts +22 -22
  36. package/src/commands/do.test.ts +117 -117
  37. package/src/commands/do.ts +195 -191
  38. package/src/commands/events.test.ts +142 -142
  39. package/src/commands/events.ts +268 -268
  40. package/src/commands/game-map.test.ts +28 -28
  41. package/src/commands/game-monitor-status.test.ts +58 -58
  42. package/src/commands/game-start-plan.test.ts +252 -252
  43. package/src/commands/game.ts +1892 -1892
  44. package/src/commands/history-player.test.ts +105 -105
  45. package/src/commands/history.ts +573 -573
  46. package/src/commands/hub.test.ts +113 -113
  47. package/src/commands/hub.ts +244 -244
  48. package/src/commands/knowledge.test.ts +13 -13
  49. package/src/commands/knowledge.ts +176 -176
  50. package/src/commands/load.test.ts +77 -77
  51. package/src/commands/load.ts +24 -24
  52. package/src/commands/meeting-history.test.ts +106 -106
  53. package/src/commands/memory.ts +40 -40
  54. package/src/commands/peek.ts +45 -45
  55. package/src/commands/persona.ts +62 -62
  56. package/src/commands/setup/codex.ts +266 -266
  57. package/src/commands/skill.ts +128 -128
  58. package/src/commands/state.ts +46 -46
  59. package/src/commands/strategy.test.ts +153 -153
  60. package/src/commands/strategy.ts +194 -194
  61. package/src/commands/upgrade.test.ts +82 -82
  62. package/src/commands/upgrade.ts +157 -157
  63. package/src/commands/watch.test.ts +1313 -1313
  64. package/src/commands/watch.ts +824 -824
  65. package/src/lib/account-guidance.test.ts +32 -32
  66. package/src/lib/account-guidance.ts +62 -62
  67. package/src/lib/auth.test.ts +116 -116
  68. package/src/lib/auth.ts +189 -189
  69. package/src/lib/command-meta.ts +37 -37
  70. package/src/lib/competition.ts +79 -79
  71. package/src/lib/custom-tts.test.ts +387 -387
  72. package/src/lib/custom-tts.ts +463 -463
  73. package/src/lib/game-client-tts.test.ts +151 -151
  74. package/src/lib/game-client.test.ts +82 -82
  75. package/src/lib/game-client.ts +472 -467
  76. package/src/lib/game-context.ts +92 -92
  77. package/src/lib/http-keepalive.ts +15 -15
  78. package/src/lib/http-transport.test.ts +61 -61
  79. package/src/lib/http-transport.ts +118 -118
  80. package/src/lib/hub-client.test.ts +57 -57
  81. package/src/lib/hub-client.ts +88 -88
  82. package/src/lib/hub-install.test.ts +157 -157
  83. package/src/lib/hub-install.ts +213 -213
  84. package/src/lib/hub-reminder.ts +69 -69
  85. package/src/lib/hub-unzip.test.ts +69 -69
  86. package/src/lib/hub-unzip.ts +62 -62
  87. package/src/lib/init-command.test.ts +79 -79
  88. package/src/lib/init-command.ts +136 -136
  89. package/src/lib/knowledge-store.test.ts +170 -170
  90. package/src/lib/knowledge-store.ts +369 -369
  91. package/src/lib/load-context.test.ts +62 -62
  92. package/src/lib/load-context.ts +49 -49
  93. package/src/lib/match-state.test.ts +134 -134
  94. package/src/lib/match-state.ts +94 -94
  95. package/src/lib/normalize.ts +42 -42
  96. package/src/lib/persona.test.ts +84 -84
  97. package/src/lib/persona.ts +120 -120
  98. package/src/lib/room.test.ts +74 -74
  99. package/src/lib/room.ts +129 -129
  100. package/src/lib/server-registry.test.ts +54 -54
  101. package/src/lib/server-registry.ts +185 -185
  102. package/src/lib/skill-version.test.ts +48 -48
  103. package/src/lib/skill-version.ts +19 -19
  104. package/src/lib/strategy-export.test.ts +314 -314
  105. package/src/lib/strategy-export.ts +371 -371
  106. package/src/lib/user-data.test.ts +98 -98
  107. package/src/lib/user-data.ts +370 -370
  108. package/src/lib/workspace-argv.test.ts +49 -49
  109. package/src/lib/workspace-argv.ts +44 -44
  110. package/src/perception/player-history-store.test.ts +177 -177
  111. package/src/perception/player-history-store.ts +262 -262
  112. package/src/pipeline/event-format.test.ts +307 -307
  113. package/src/pipeline/event-format.ts +595 -595
  114. package/src/pipeline/event-hints.ts +204 -204
  115. package/src/pipeline/event-store.test.ts +28 -28
  116. package/src/pipeline/event-store.ts +193 -193
  117. package/src/pipeline/pipeline.ts +35 -35
  118. package/src/pipeline/player-projection.test.ts +168 -168
  119. package/src/pipeline/player-projection.ts +370 -370
  120. package/src/runtime/auto-upgrade.test.ts +66 -66
  121. package/src/runtime/auto-upgrade.ts +31 -31
  122. package/src/runtime/event-daemon.test.ts +268 -268
  123. package/src/runtime/event-daemon.ts +531 -531
  124. package/src/runtime/owner-control.ts +150 -150
  125. package/src/runtime/raw-ws-log.test.ts +33 -33
  126. package/src/runtime/raw-ws-log.ts +32 -32
  127. package/src/runtime/room-lobby-runtime.test.ts +95 -95
  128. package/src/runtime/room-lobby-runtime.ts +367 -367
  129. package/src/runtime/runtime-logger.ts +107 -107
  130. package/src/runtime/ws-client.test.ts +169 -169
  131. package/src/runtime/ws-client.ts +280 -280
  132. package/src/sdk/action.ts +155 -155
  133. package/src/sdk/index.ts +112 -112
  134. package/src/sdk/types.ts +163 -163
  135. package/src/strategies/avoid-lone.ts +14 -14
  136. package/src/strategies/avoid-players.knowledge.md +19 -19
  137. package/src/strategies/avoid-players.ts +18 -18
  138. package/src/strategies/corpse-patrol.ts +21 -21
  139. package/src/strategies/crab-sabotage.ts +24 -24
  140. package/src/strategies/custom-module.test.ts +270 -270
  141. package/src/strategies/find-player.ts +19 -19
  142. package/src/strategies/game-utils.test.ts +242 -242
  143. package/src/strategies/game-utils.ts +853 -853
  144. package/src/strategies/goals/anchor-linger.ts +77 -77
  145. package/src/strategies/goals/avoid-lone-top.ts +168 -168
  146. package/src/strategies/goals/avoid-players-top.test.ts +83 -83
  147. package/src/strategies/goals/avoid-players-top.ts +121 -121
  148. package/src/strategies/goals/conversation-goal.ts +51 -51
  149. package/src/strategies/goals/corpse-patrol-top.ts +113 -113
  150. package/src/strategies/goals/crab-octopus-reflexes.ts +101 -101
  151. package/src/strategies/goals/crab-sabotage-top.ts +226 -226
  152. package/src/strategies/goals/emergency-hunt-goal.ts +28 -28
  153. package/src/strategies/goals/find-player-top.ts +93 -93
  154. package/src/strategies/goals/flee-players-goal.ts +53 -53
  155. package/src/strategies/goals/follow-companion-goal.ts +106 -106
  156. package/src/strategies/goals/goal-manager.ts +41 -41
  157. package/src/strategies/goals/goal-root-strategy.ts +49 -49
  158. package/src/strategies/goals/goal.ts +28 -28
  159. package/src/strategies/goals/hide-top.ts +197 -197
  160. package/src/strategies/goals/keep-away-goal.ts +221 -221
  161. package/src/strategies/goals/kill-frenzy-top.ts +80 -80
  162. package/src/strategies/goals/kill-lone-top.ts +160 -160
  163. package/src/strategies/goals/kill-target-goal.ts +59 -59
  164. package/src/strategies/goals/kill-target-top.ts +109 -109
  165. package/src/strategies/goals/leaf-goal.ts +27 -27
  166. package/src/strategies/goals/linger-corpse-goal.ts +35 -35
  167. package/src/strategies/goals/lone-kill-core.ts +82 -82
  168. package/src/strategies/goals/lone-kill-goal.ts +24 -24
  169. package/src/strategies/goals/lone-kill-task-top.test.ts +85 -85
  170. package/src/strategies/goals/lone-kill-task-top.ts +133 -133
  171. package/src/strategies/goals/move-room-goal.ts +60 -60
  172. package/src/strategies/goals/normal-shrimp-top.test.ts +97 -97
  173. package/src/strategies/goals/normal-shrimp-top.ts +248 -248
  174. package/src/strategies/goals/paradise-fish-top.test.ts +126 -126
  175. package/src/strategies/goals/paradise-fish-top.ts +224 -224
  176. package/src/strategies/goals/patrol-top.ts +57 -57
  177. package/src/strategies/goals/report-patrol-top.ts +86 -86
  178. package/src/strategies/goals/safe-task-goal.ts +102 -102
  179. package/src/strategies/goals/social-task-top.ts +161 -161
  180. package/src/strategies/goals/task-kill-report-top.ts +163 -163
  181. package/src/strategies/goals/task-only-top.ts +57 -57
  182. package/src/strategies/goals/task-or-patrol-goal.ts +41 -41
  183. package/src/strategies/goals/task-report-top.ts +57 -57
  184. package/src/strategies/goals/wander-task-goal.ts +33 -33
  185. package/src/strategies/goals/warrior-shrimp-top.test.ts +87 -87
  186. package/src/strategies/goals/warrior-shrimp-top.ts +267 -267
  187. package/src/strategies/greeting.ts +53 -53
  188. package/src/strategies/hide-spots.ts +59 -59
  189. package/src/strategies/hide.ts +26 -26
  190. package/src/strategies/kill-frenzy.ts +15 -15
  191. package/src/strategies/kill-lone.knowledge.md +17 -17
  192. package/src/strategies/kill-lone.ts +16 -16
  193. package/src/strategies/kill-target.ts +21 -21
  194. package/src/strategies/knowledge-contract.ts +85 -85
  195. package/src/strategies/loader.test.ts +678 -678
  196. package/src/strategies/loader.ts +197 -197
  197. package/src/strategies/lone-kill-task.ts +24 -24
  198. package/src/strategies/meeting-gate.test.ts +59 -59
  199. package/src/strategies/meeting-gate.ts +28 -28
  200. package/src/strategies/move-room.ts +18 -18
  201. package/src/strategies/new-events-backfill.ts +98 -98
  202. package/src/strategies/off-route-points.ts +105 -105
  203. package/src/strategies/paradise-fish.knowledge.md +19 -19
  204. package/src/strategies/paradise-fish.ts +24 -24
  205. package/src/strategies/pathfind/distance-field.ts +150 -150
  206. package/src/strategies/pathfind/escape-planner.test.ts +197 -197
  207. package/src/strategies/pathfind/escape-planner.ts +355 -355
  208. package/src/strategies/pathfind/walkable-grid.ts +117 -117
  209. package/src/strategies/patrol.ts +14 -14
  210. package/src/strategies/player-targets.ts +13 -13
  211. package/src/strategies/report-patrol.ts +14 -14
  212. package/src/strategies/shrimp-memory.knowledge.md +19 -19
  213. package/src/strategies/shrimp-memory.ts +24 -24
  214. package/src/strategies/social-task.test.ts +28 -28
  215. package/src/strategies/social-task.ts +52 -52
  216. package/src/strategies/spawn.ts +82 -82
  217. package/src/strategies/speech-module.ts +123 -123
  218. package/src/strategies/strategy-loop.test.ts +15 -15
  219. package/src/strategies/strategy-loop.ts +748 -748
  220. package/src/strategies/task-kill-report.ts +20 -20
  221. package/src/strategies/task-only.ts +14 -14
  222. package/src/strategies/task-report.ts +21 -21
  223. package/src/strategies/types.ts +155 -155
  224. package/src/strategies/warrior-memory.knowledge.md +21 -21
  225. package/src/strategies/warrior-memory.ts +19 -19
@@ -1,573 +1,573 @@
1
- import { Command } from 'commander';
2
- import { EventStore } from '../pipeline/event-store.js';
3
- import {
4
- PlayerHistoryStore,
5
- type FramePlayer,
6
- type LobsterTaskPoint,
7
- type PlayerHistoryData,
8
- type VisibleFrame,
9
- } from '../perception/player-history-store.js';
10
-
11
- type HistoryKind = 'stay' | 'last_appear' | 'rooms_seen';
12
-
13
- const KINDS = new Set<HistoryKind>(['stay', 'last_appear', 'rooms_seen']);
14
- const STOP_MIN_SECS = 2;
15
- const TASK_DONE_RADIUS = 15;
16
- const DIR_MIN_MOVE = 6;
17
-
18
- interface PlayerPoint {
19
- frame: VisibleFrame;
20
- pos: FramePlayer;
21
- }
22
-
23
- function round1(value: number): number {
24
- return Math.round(value * 10) / 10;
25
- }
26
-
27
- function dist(a: { x: number; y: number }, b: { x: number; y: number }): number {
28
- return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
29
- }
30
-
31
- function ageSecs(time: string, now: number): number {
32
- const ms = Date.parse(time);
33
- if (!Number.isFinite(ms)) return 0;
34
- return Math.max(0, round1((now - ms) / 1000));
35
- }
36
-
37
- function durationSecs(start: string, end: string): number {
38
- const startMs = Date.parse(start);
39
- const endMs = Date.parse(end);
40
- if (!Number.isFinite(startMs) || !Number.isFinite(endMs)) return 0;
41
- return Math.max(0, round1((endMs - startMs) / 1000));
42
- }
43
-
44
- function splitSelectors(values: string[]): string[] {
45
- return values
46
- .flatMap((value) => String(value).split(','))
47
- .map((value) => value.trim())
48
- .filter(Boolean);
49
- }
50
-
51
- function knownPlayerNames(data: PlayerHistoryData): string[] {
52
- const names = new Set<string>(Object.keys(data.seats ?? {}));
53
- for (const frame of data.visible_frames ?? []) {
54
- for (const name of Object.keys(frame.players ?? {})) names.add(name);
55
- }
56
- return Array.from(names).sort((a, b) => a.localeCompare(b));
57
- }
58
-
59
- function resolvePlayers(data: PlayerHistoryData, selectors: string[]): { players?: string[]; error?: Record<string, any> } {
60
- const names = knownPlayerNames(data);
61
- if (selectors.length === 0) {
62
- return { error: { code: 'missing_player_id', message: 'Provide a player id/name, list, or all.' } };
63
- }
64
- if (selectors.some((selector) => selector.toLowerCase() === 'all')) return { players: names };
65
-
66
- const resolved: string[] = [];
67
- for (const selector of selectors) {
68
- const seatMatch = selector.match(/^seat:(\d+)$/i);
69
- const seat = seatMatch ? Number(seatMatch[1]) : /^\d+$/.test(selector) ? Number(selector) : null;
70
- let matches: string[] = [];
71
- if (seat != null) {
72
- matches = names.filter((name) => data.seats?.[name] === seat);
73
- } else {
74
- matches = names.filter((name) => name === selector);
75
- if (matches.length === 0) matches = names.filter((name) => name.startsWith(selector));
76
- if (matches.length === 0) matches = names.filter((name) => name.toLowerCase().includes(selector.toLowerCase()));
77
- }
78
- if (matches.length === 0) {
79
- return { error: { code: 'not_found', selector, known_players: names } };
80
- }
81
- if (matches.length > 1) {
82
- return { error: { code: 'ambiguous', selector, candidates: matches } };
83
- }
84
- if (!resolved.includes(matches[0])) resolved.push(matches[0]);
85
- }
86
- return { players: resolved };
87
- }
88
-
89
- function pointsForPlayer(frames: VisibleFrame[], player: string): PlayerPoint[] {
90
- const points: PlayerPoint[] = [];
91
- for (const frame of frames) {
92
- const pos = frame.players?.[player];
93
- if (pos) points.push({ frame, pos });
94
- }
95
- return points;
96
- }
97
-
98
- function continuousRuns(points: PlayerPoint[]): PlayerPoint[][] {
99
- const runs: PlayerPoint[][] = [];
100
- let current: PlayerPoint[] = [];
101
- for (const point of points) {
102
- const prev = current[current.length - 1];
103
- if (!prev || point.frame.tick === prev.frame.tick + 1) {
104
- current.push(point);
105
- } else {
106
- if (current.length > 0) runs.push(current);
107
- current = [point];
108
- }
109
- }
110
- if (current.length > 0) runs.push(current);
111
- return runs;
112
- }
113
-
114
- function centroid(points: PlayerPoint[]): { x: number; y: number } {
115
- const sum = points.reduce((acc, point) => ({ x: acc.x + point.pos.x, y: acc.y + point.pos.y }), { x: 0, y: 0 });
116
- return { x: sum.x / points.length, y: sum.y / points.length };
117
- }
118
-
119
- function mostCommonRoom(points: PlayerPoint[]): string | undefined {
120
- const counts = new Map<string, number>();
121
- for (const point of points) {
122
- if (!point.pos.room) continue;
123
- counts.set(point.pos.room, (counts.get(point.pos.room) ?? 0) + 1);
124
- }
125
- return Array.from(counts.entries()).sort((a, b) => b[1] - a[1])[0]?.[0];
126
- }
127
-
128
- function nearTask(tasks: LobsterTaskPoint[], point: { x: number; y: number }): string | null {
129
- let best: { task: LobsterTaskPoint; distance: number } | null = null;
130
- for (const task of tasks) {
131
- const distance = dist(point, task);
132
- if (distance > TASK_DONE_RADIUS) continue;
133
- if (!best || distance < best.distance) best = { task, distance };
134
- }
135
- return best?.task.name ?? null;
136
- }
137
-
138
- function stayKey(data: PlayerHistoryData, point: PlayerPoint): string {
139
- const task = nearTask(data.lobster_tasks, point.pos);
140
- if (task) return `task:${task}`;
141
- return `pos:${point.pos.x},${point.pos.y}`;
142
- }
143
-
144
- function buildStayResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
145
- const stays: Record<string, any>[] = [];
146
- for (const run of continuousRuns(pointsForPlayer(data.visible_frames, player))) {
147
- let segment: PlayerPoint[] = [];
148
- let segmentKey: string | null = null;
149
- for (const point of run) {
150
- const key = stayKey(data, point);
151
- if (segment.length === 0 || key === segmentKey) {
152
- segment.push(point);
153
- segmentKey = key;
154
- } else {
155
- appendStay(stays, segment, data.lobster_tasks, now);
156
- segment = [point];
157
- segmentKey = key;
158
- }
159
- }
160
- appendStay(stays, segment, data.lobster_tasks, now);
161
- }
162
- stays.sort((a, b) => a.to_ago - b.to_ago);
163
- return {
164
- player,
165
- ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
166
- stays,
167
- };
168
- }
169
-
170
- function appendStay(out: Record<string, any>[], segment: PlayerPoint[], tasks: LobsterTaskPoint[], now: number): void {
171
- if (segment.length < 2) return;
172
- const first = segment[0];
173
- const last = segment[segment.length - 1];
174
- const secs = durationSecs(first.frame.time, last.frame.time);
175
- if (secs < STOP_MIN_SECS) return;
176
- const center = centroid(segment);
177
- const room = mostCommonRoom(segment);
178
- out.push({
179
- from_ago: ageSecs(first.frame.time, now),
180
- to_ago: ageSecs(last.frame.time, now),
181
- secs,
182
- room,
183
- x: round1(center.x),
184
- y: round1(center.y),
185
- pos: `${round1(center.x)},${round1(center.y)}`,
186
- near_task: nearTask(tasks, center),
187
- });
188
- }
189
-
190
- function direction(from: { x: number; y: number }, to: { x: number; y: number }): string {
191
- const dx = to.x - from.x;
192
- const dy = to.y - from.y;
193
- if (Math.sqrt(dx ** 2 + dy ** 2) < DIR_MIN_MOVE) return 'stay';
194
- const horizontal = Math.abs(dx) >= DIR_MIN_MOVE / 2 ? (dx > 0 ? '右' : '左') : '';
195
- const vertical = Math.abs(dy) >= DIR_MIN_MOVE / 2 ? (dy > 0 ? '下' : '上') : '';
196
- return `${horizontal}${vertical}` || 'stay';
197
- }
198
-
199
- function buildLastAppearResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
200
- const runs = continuousRuns(pointsForPlayer(data.visible_frames, player));
201
- const latestFrame = data.visible_frames[data.visible_frames.length - 1];
202
- const lastRun = runs[runs.length - 1] ?? [];
203
- const last = lastRun[lastRun.length - 1];
204
- if (!last) {
205
- return {
206
- player,
207
- ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
208
- visible: false,
209
- ago: null,
210
- };
211
- }
212
-
213
- const previous = lastRun.length >= 2 ? lastRun[lastRun.length - 2] : last;
214
- return {
215
- player,
216
- ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
217
- visible: latestFrame?.players?.[player] != null,
218
- ago: ageSecs(last.frame.time, now),
219
- room: last.pos.room,
220
- x: round1(last.pos.x),
221
- y: round1(last.pos.y),
222
- dir: direction(previous.pos, last.pos),
223
- };
224
- }
225
-
226
- function buildRoomsSeenResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
227
- const rooms: Record<string, any>[] = [];
228
- for (const run of continuousRuns(pointsForPlayer(data.visible_frames, player))) {
229
- let currentRoom = run[0]?.pos.room ?? 'unknown';
230
- let start = run[0];
231
- let previous = run[0];
232
- for (const point of run.slice(1)) {
233
- const room = point.pos.room ?? 'unknown';
234
- if (room !== currentRoom) {
235
- rooms.push(roomSegment(currentRoom, start, previous, now));
236
- currentRoom = room;
237
- start = point;
238
- }
239
- previous = point;
240
- }
241
- if (start && previous) rooms.push(roomSegment(currentRoom, start, previous, now));
242
- }
243
- rooms.sort((a, b) => a.to_ago - b.to_ago);
244
- return {
245
- player,
246
- ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
247
- rooms,
248
- };
249
- }
250
-
251
- function roomSegment(room: string, start: PlayerPoint, end: PlayerPoint, now: number): Record<string, any> {
252
- return {
253
- room,
254
- from_ago: ageSecs(start.frame.time, now),
255
- to_ago: ageSecs(end.frame.time, now),
256
- secs: durationSecs(start.frame.time, end.frame.time),
257
- };
258
- }
259
-
260
- interface MeetingRecord {
261
- round: number;
262
- start?: Record<string, any>;
263
- briefing?: Record<string, any>;
264
- states: Record<string, any>[];
265
- speechEvents: Record<string, any>[];
266
- voteEvents: Record<string, any>[];
267
- resultEvent?: Record<string, any>;
268
- ended?: Record<string, any>;
269
- interrupted?: boolean;
270
- }
271
-
272
- interface DeathInfo {
273
- type: 'found_in_meeting' | 'exiled';
274
- round: number;
275
- }
276
-
277
- function buildMeetingRecords(events: Record<string, any>[]): MeetingRecord[] {
278
- const meetings: MeetingRecord[] = [];
279
- let current: MeetingRecord | null = null;
280
-
281
- const finishCurrent = (interrupted = false) => {
282
- if (!current) return;
283
- current.interrupted = current.interrupted || interrupted;
284
- meetings.push(current);
285
- current = null;
286
- };
287
-
288
- for (const event of events) {
289
- if (event.type === 'meeting_start') {
290
- finishCurrent(true);
291
- current = {
292
- round: meetings.length + 1,
293
- start: event,
294
- states: [],
295
- speechEvents: [],
296
- voteEvents: [],
297
- };
298
- continue;
299
- }
300
- if (!current) continue;
301
-
302
- if (event.type === 'meeting_briefing') current.briefing = event;
303
- if (event.type === 'meeting_state' && (event.round == null || event.round === current.round)) current.states.push(event);
304
- if (event.type === 'speech' || event.type === 'speech_skipped' || event.type === 'speech_turn') current.speechEvents.push(event);
305
- if (event.type === 'vote_cast') current.voteEvents.push(event);
306
- if (event.type === 'exile' || event.type === 'no_exile') current.resultEvent = event;
307
- if (event.type === 'meeting_ended') {
308
- current.ended = event;
309
- finishCurrent(false);
310
- continue;
311
- }
312
- if (event.type === 'game_over') {
313
- finishCurrent(true);
314
- }
315
- }
316
- finishCurrent(false);
317
- return meetings;
318
- }
319
-
320
- function latestMeetingState(record: MeetingRecord): Record<string, any> | undefined {
321
- return record.states[record.states.length - 1];
322
- }
323
-
324
- function meetingVotes(record: MeetingRecord): Record<string, string> {
325
- const resultVotes = record.resultEvent?.votes;
326
- if (resultVotes && typeof resultVotes === 'object' && !Array.isArray(resultVotes)) {
327
- return resultVotes as Record<string, string>;
328
- }
329
- const votes: Record<string, string> = {};
330
- for (const vote of record.voteEvents) {
331
- if (typeof vote.actor_name === 'string' && typeof vote.target === 'string') votes[vote.actor_name] = vote.target;
332
- }
333
- return votes;
334
- }
335
-
336
- function exiledPlayer(record: MeetingRecord): string | null {
337
- if (record.ended?.result === 'exiled' && typeof record.ended.result_target === 'string') return record.ended.result_target;
338
- if (record.resultEvent?.type === 'exile' && typeof record.resultEvent.actor_name === 'string') return record.resultEvent.actor_name;
339
- return null;
340
- }
341
-
342
- function meetingState(record: MeetingRecord): string {
343
- if (record.ended) return 'ended';
344
- const subPhase = latestMeetingState(record)?.sub_phase;
345
- if (typeof subPhase === 'string' && subPhase.length > 0) return subPhase;
346
- if (record.interrupted) return 'interrupted_or_missing_end';
347
- return 'interrupted_or_missing_end';
348
- }
349
-
350
- function meetingAllSeats(record: MeetingRecord): Array<{ name: string; seat: number }> {
351
- const allSeats = record.briefing?.all_seats;
352
- if (!Array.isArray(allSeats)) return [];
353
- return allSeats
354
- .filter((player: any) => typeof player?.name === 'string' && Number.isFinite(player.seat))
355
- .map((player: any) => ({ name: player.name, seat: player.seat }))
356
- .sort((a, b) => a.seat - b.seat);
357
- }
358
-
359
- function latestAlivePlayers(record: MeetingRecord): Set<string> | null {
360
- for (let i = record.states.length - 1; i >= 0; i--) {
361
- const alive = record.states[i].alive_players;
362
- if (Array.isArray(alive)) return new Set(alive.filter((name: any) => typeof name === 'string'));
363
- }
364
- return null;
365
- }
366
-
367
- function collectSpeechOrder(record: MeetingRecord): string[] {
368
- for (const state of record.states) {
369
- if (Array.isArray(state.speech_order)) {
370
- return state.speech_order.filter((name: any) => typeof name === 'string');
371
- }
372
- }
373
- const order: string[] = [];
374
- for (const event of record.speechEvents) {
375
- const speaker = event.actor_name;
376
- if (typeof speaker === 'string' && !order.includes(speaker)) order.push(speaker);
377
- }
378
- return order;
379
- }
380
-
381
- function meetingSpeeches(record: MeetingRecord): Array<{ speaker: string; text: string; skipped: boolean }> {
382
- const order = collectSpeechOrder(record);
383
- const texts = new Map<string, string>();
384
- const skipped = new Set<string>();
385
- const seen = new Set<string>();
386
-
387
- for (const state of record.states) {
388
- const stateSpeeches = state.speeches;
389
- if (stateSpeeches && typeof stateSpeeches === 'object' && !Array.isArray(stateSpeeches)) {
390
- for (const [speaker, text] of Object.entries(stateSpeeches)) {
391
- if (typeof speaker !== 'string') continue;
392
- texts.set(speaker, typeof text === 'string' ? text : '');
393
- seen.add(speaker);
394
- }
395
- }
396
- if (Array.isArray(state.speech_skipped)) {
397
- for (const speaker of state.speech_skipped) {
398
- if (typeof speaker !== 'string') continue;
399
- skipped.add(speaker);
400
- seen.add(speaker);
401
- }
402
- }
403
- }
404
-
405
- for (const event of record.speechEvents) {
406
- const speaker = event.actor_name;
407
- if (typeof speaker !== 'string') continue;
408
- if (event.type === 'speech') {
409
- texts.set(speaker, typeof event.text === 'string' ? event.text : '');
410
- seen.add(speaker);
411
- } else if (event.type === 'speech_skipped') {
412
- skipped.add(speaker);
413
- seen.add(speaker);
414
- }
415
- }
416
-
417
- const outputOrder = [...order, ...Array.from(seen).filter((speaker) => !order.includes(speaker))];
418
- return outputOrder
419
- .filter((speaker) => seen.has(speaker))
420
- .map((speaker) => ({
421
- speaker,
422
- text: texts.get(speaker) ?? '',
423
- skipped: skipped.has(speaker),
424
- }));
425
- }
426
-
427
- function applyMeetingDeaths(record: MeetingRecord, deaths: Map<string, DeathInfo>): void {
428
- const alive = latestAlivePlayers(record);
429
- if (alive) {
430
- for (const player of meetingAllSeats(record)) {
431
- if (!alive.has(player.name) && !deaths.has(player.name)) {
432
- deaths.set(player.name, { type: 'found_in_meeting', round: record.round });
433
- }
434
- }
435
- }
436
- const exiled = exiledPlayer(record);
437
- if (exiled) deaths.set(exiled, { type: 'exiled', round: record.round });
438
- }
439
-
440
- function meetingPlayers(record: MeetingRecord, deaths: Map<string, DeathInfo>): Record<string, any>[] {
441
- return meetingAllSeats(record).map((player) => {
442
- const death = deaths.get(player.name) ?? null;
443
- return {
444
- name: player.name,
445
- seat: player.seat,
446
- status: death ? 'dead' : 'alive',
447
- death,
448
- };
449
- });
450
- }
451
-
452
- function meetingOutput(record: MeetingRecord, deaths: Map<string, DeathInfo>): Record<string, any> {
453
- applyMeetingDeaths(record, deaths);
454
- return {
455
- round: record.round,
456
- tick: record.start?.tick ?? record.briefing?.tick ?? null,
457
- caller: record.start?.actor_name ?? record.briefing?.caller ?? latestMeetingState(record)?.caller ?? null,
458
- corpses_found: null,
459
- state: meetingState(record),
460
- exiled_player: exiledPlayer(record),
461
- players: meetingPlayers(record, deaths),
462
- votes: meetingVotes(record),
463
- speeches: meetingSpeeches(record),
464
- };
465
- }
466
-
467
- function allMeetingOutputs(records: MeetingRecord[]): Record<string, any>[] {
468
- const deaths = new Map<string, DeathInfo>();
469
- return records.map((record) => meetingOutput(record, deaths));
470
- }
471
-
472
- function createMeetingsCommand(): Command {
473
- return new Command('meetings')
474
- .description('Show meeting history')
475
- .argument('[round]', 'Meeting round number, starting from 1')
476
- .action((roundText?: string) => {
477
- const records = buildMeetingRecords(EventStore.forLatestGame().all());
478
- const outputs = allMeetingOutputs(records);
479
- if (roundText != null) {
480
- const round = Number(roundText);
481
- const record = Number.isInteger(round) && round > 0 ? outputs.find((meeting) => meeting.round === round) : null;
482
- if (!record) {
483
- console.log(JSON.stringify({
484
- error: {
485
- code: 'meeting_round_not_found',
486
- round: roundText,
487
- available_rounds: outputs.map((meeting) => meeting.round),
488
- },
489
- }, null, 2));
490
- return;
491
- }
492
- console.log(JSON.stringify(record, null, 2));
493
- return;
494
- }
495
- console.log(JSON.stringify({ meetings: outputs }, null, 2));
496
- });
497
- }
498
-
499
- function createPlayerHistorySubcommand(): Command {
500
- return new Command('player')
501
- .description('Show derived visible-player history')
502
- .argument('<kind>', 'stay | last_appear | rooms_seen')
503
- .argument('[player_id...]', 'Player name/seat/list, or all')
504
- .action(async (kind: string, playerArgs: string[]) => {
505
- if (!KINDS.has(kind as HistoryKind)) {
506
- console.log(JSON.stringify({ error: { code: 'invalid_kind', allowed: Array.from(KINDS) } }, null, 2));
507
- return;
508
- }
509
-
510
- const sessionPath = EventStore.latestSessionPath();
511
- if (!sessionPath) {
512
- console.log(JSON.stringify({
513
- kind,
514
- error: { code: 'no_game_events', message: 'No game events found. Join a game first.' },
515
- }, null, 2));
516
- return;
517
- }
518
- const data = PlayerHistoryStore.forSession(sessionPath).read();
519
- if (!data || !Array.isArray(data.visible_frames) || data.visible_frames.length === 0) {
520
- console.log(JSON.stringify({
521
- kind,
522
- error: {
523
- code: 'no_player_history',
524
- message: 'No player_spotted history yet. Join a game and wait for the game runtime to receive player_spotted events.',
525
- },
526
- }, null, 2));
527
- return;
528
- }
529
-
530
- const selectors = splitSelectors(playerArgs);
531
- const resolved = resolvePlayers(data, selectors);
532
- if (resolved.error) {
533
- console.log(JSON.stringify({ kind, error: resolved.error }, null, 2));
534
- return;
535
- }
536
-
537
- const now = Date.now();
538
- const players = resolved.players ?? [];
539
- const resultPlayers = players.map((player) => {
540
- if (kind === 'stay') return buildStayResult(data, player, now);
541
- if (kind === 'last_appear') return buildLastAppearResult(data, player, now);
542
- return buildRoomsSeenResult(data, player, now);
543
- });
544
-
545
- console.log(JSON.stringify({
546
- kind,
547
- time: new Date(now).toISOString(),
548
- players: resultPlayers,
549
- }, null, 2));
550
- });
551
- }
552
-
553
- export function createHistoryCommand(): Command {
554
- const history = new Command('history')
555
- .description('Show derived player and meeting history');
556
- history.addCommand(createPlayerHistorySubcommand());
557
- history.addCommand(createMeetingsCommand());
558
- return history;
559
- }
560
-
561
- export const playerHistoryCommandInternals = {
562
- direction,
563
- resolvePlayers,
564
- buildStayResult,
565
- buildLastAppearResult,
566
- buildRoomsSeenResult,
567
- };
568
-
569
- export const meetingHistoryCommandInternals = {
570
- buildMeetingRecords,
571
- allMeetingOutputs,
572
- meetingSpeeches,
573
- };
1
+ import { Command } from 'commander';
2
+ import { EventStore } from '../pipeline/event-store.js';
3
+ import {
4
+ PlayerHistoryStore,
5
+ type FramePlayer,
6
+ type LobsterTaskPoint,
7
+ type PlayerHistoryData,
8
+ type VisibleFrame,
9
+ } from '../perception/player-history-store.js';
10
+
11
+ type HistoryKind = 'stay' | 'last_appear' | 'rooms_seen';
12
+
13
+ const KINDS = new Set<HistoryKind>(['stay', 'last_appear', 'rooms_seen']);
14
+ const STOP_MIN_SECS = 2;
15
+ const TASK_DONE_RADIUS = 15;
16
+ const DIR_MIN_MOVE = 6;
17
+
18
+ interface PlayerPoint {
19
+ frame: VisibleFrame;
20
+ pos: FramePlayer;
21
+ }
22
+
23
+ function round1(value: number): number {
24
+ return Math.round(value * 10) / 10;
25
+ }
26
+
27
+ function dist(a: { x: number; y: number }, b: { x: number; y: number }): number {
28
+ return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
29
+ }
30
+
31
+ function ageSecs(time: string, now: number): number {
32
+ const ms = Date.parse(time);
33
+ if (!Number.isFinite(ms)) return 0;
34
+ return Math.max(0, round1((now - ms) / 1000));
35
+ }
36
+
37
+ function durationSecs(start: string, end: string): number {
38
+ const startMs = Date.parse(start);
39
+ const endMs = Date.parse(end);
40
+ if (!Number.isFinite(startMs) || !Number.isFinite(endMs)) return 0;
41
+ return Math.max(0, round1((endMs - startMs) / 1000));
42
+ }
43
+
44
+ function splitSelectors(values: string[]): string[] {
45
+ return values
46
+ .flatMap((value) => String(value).split(','))
47
+ .map((value) => value.trim())
48
+ .filter(Boolean);
49
+ }
50
+
51
+ function knownPlayerNames(data: PlayerHistoryData): string[] {
52
+ const names = new Set<string>(Object.keys(data.seats ?? {}));
53
+ for (const frame of data.visible_frames ?? []) {
54
+ for (const name of Object.keys(frame.players ?? {})) names.add(name);
55
+ }
56
+ return Array.from(names).sort((a, b) => a.localeCompare(b));
57
+ }
58
+
59
+ function resolvePlayers(data: PlayerHistoryData, selectors: string[]): { players?: string[]; error?: Record<string, any> } {
60
+ const names = knownPlayerNames(data);
61
+ if (selectors.length === 0) {
62
+ return { error: { code: 'missing_player_id', message: 'Provide a player id/name, list, or all.' } };
63
+ }
64
+ if (selectors.some((selector) => selector.toLowerCase() === 'all')) return { players: names };
65
+
66
+ const resolved: string[] = [];
67
+ for (const selector of selectors) {
68
+ const seatMatch = selector.match(/^seat:(\d+)$/i);
69
+ const seat = seatMatch ? Number(seatMatch[1]) : /^\d+$/.test(selector) ? Number(selector) : null;
70
+ let matches: string[] = [];
71
+ if (seat != null) {
72
+ matches = names.filter((name) => data.seats?.[name] === seat);
73
+ } else {
74
+ matches = names.filter((name) => name === selector);
75
+ if (matches.length === 0) matches = names.filter((name) => name.startsWith(selector));
76
+ if (matches.length === 0) matches = names.filter((name) => name.toLowerCase().includes(selector.toLowerCase()));
77
+ }
78
+ if (matches.length === 0) {
79
+ return { error: { code: 'not_found', selector, known_players: names } };
80
+ }
81
+ if (matches.length > 1) {
82
+ return { error: { code: 'ambiguous', selector, candidates: matches } };
83
+ }
84
+ if (!resolved.includes(matches[0])) resolved.push(matches[0]);
85
+ }
86
+ return { players: resolved };
87
+ }
88
+
89
+ function pointsForPlayer(frames: VisibleFrame[], player: string): PlayerPoint[] {
90
+ const points: PlayerPoint[] = [];
91
+ for (const frame of frames) {
92
+ const pos = frame.players?.[player];
93
+ if (pos) points.push({ frame, pos });
94
+ }
95
+ return points;
96
+ }
97
+
98
+ function continuousRuns(points: PlayerPoint[]): PlayerPoint[][] {
99
+ const runs: PlayerPoint[][] = [];
100
+ let current: PlayerPoint[] = [];
101
+ for (const point of points) {
102
+ const prev = current[current.length - 1];
103
+ if (!prev || point.frame.tick === prev.frame.tick + 1) {
104
+ current.push(point);
105
+ } else {
106
+ if (current.length > 0) runs.push(current);
107
+ current = [point];
108
+ }
109
+ }
110
+ if (current.length > 0) runs.push(current);
111
+ return runs;
112
+ }
113
+
114
+ function centroid(points: PlayerPoint[]): { x: number; y: number } {
115
+ const sum = points.reduce((acc, point) => ({ x: acc.x + point.pos.x, y: acc.y + point.pos.y }), { x: 0, y: 0 });
116
+ return { x: sum.x / points.length, y: sum.y / points.length };
117
+ }
118
+
119
+ function mostCommonRoom(points: PlayerPoint[]): string | undefined {
120
+ const counts = new Map<string, number>();
121
+ for (const point of points) {
122
+ if (!point.pos.room) continue;
123
+ counts.set(point.pos.room, (counts.get(point.pos.room) ?? 0) + 1);
124
+ }
125
+ return Array.from(counts.entries()).sort((a, b) => b[1] - a[1])[0]?.[0];
126
+ }
127
+
128
+ function nearTask(tasks: LobsterTaskPoint[], point: { x: number; y: number }): string | null {
129
+ let best: { task: LobsterTaskPoint; distance: number } | null = null;
130
+ for (const task of tasks) {
131
+ const distance = dist(point, task);
132
+ if (distance > TASK_DONE_RADIUS) continue;
133
+ if (!best || distance < best.distance) best = { task, distance };
134
+ }
135
+ return best?.task.name ?? null;
136
+ }
137
+
138
+ function stayKey(data: PlayerHistoryData, point: PlayerPoint): string {
139
+ const task = nearTask(data.lobster_tasks, point.pos);
140
+ if (task) return `task:${task}`;
141
+ return `pos:${point.pos.x},${point.pos.y}`;
142
+ }
143
+
144
+ function buildStayResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
145
+ const stays: Record<string, any>[] = [];
146
+ for (const run of continuousRuns(pointsForPlayer(data.visible_frames, player))) {
147
+ let segment: PlayerPoint[] = [];
148
+ let segmentKey: string | null = null;
149
+ for (const point of run) {
150
+ const key = stayKey(data, point);
151
+ if (segment.length === 0 || key === segmentKey) {
152
+ segment.push(point);
153
+ segmentKey = key;
154
+ } else {
155
+ appendStay(stays, segment, data.lobster_tasks, now);
156
+ segment = [point];
157
+ segmentKey = key;
158
+ }
159
+ }
160
+ appendStay(stays, segment, data.lobster_tasks, now);
161
+ }
162
+ stays.sort((a, b) => a.to_ago - b.to_ago);
163
+ return {
164
+ player,
165
+ ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
166
+ stays,
167
+ };
168
+ }
169
+
170
+ function appendStay(out: Record<string, any>[], segment: PlayerPoint[], tasks: LobsterTaskPoint[], now: number): void {
171
+ if (segment.length < 2) return;
172
+ const first = segment[0];
173
+ const last = segment[segment.length - 1];
174
+ const secs = durationSecs(first.frame.time, last.frame.time);
175
+ if (secs < STOP_MIN_SECS) return;
176
+ const center = centroid(segment);
177
+ const room = mostCommonRoom(segment);
178
+ out.push({
179
+ from_ago: ageSecs(first.frame.time, now),
180
+ to_ago: ageSecs(last.frame.time, now),
181
+ secs,
182
+ room,
183
+ x: round1(center.x),
184
+ y: round1(center.y),
185
+ pos: `${round1(center.x)},${round1(center.y)}`,
186
+ near_task: nearTask(tasks, center),
187
+ });
188
+ }
189
+
190
+ function direction(from: { x: number; y: number }, to: { x: number; y: number }): string {
191
+ const dx = to.x - from.x;
192
+ const dy = to.y - from.y;
193
+ if (Math.sqrt(dx ** 2 + dy ** 2) < DIR_MIN_MOVE) return 'stay';
194
+ const horizontal = Math.abs(dx) >= DIR_MIN_MOVE / 2 ? (dx > 0 ? '右' : '左') : '';
195
+ const vertical = Math.abs(dy) >= DIR_MIN_MOVE / 2 ? (dy > 0 ? '下' : '上') : '';
196
+ return `${horizontal}${vertical}` || 'stay';
197
+ }
198
+
199
+ function buildLastAppearResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
200
+ const runs = continuousRuns(pointsForPlayer(data.visible_frames, player));
201
+ const latestFrame = data.visible_frames[data.visible_frames.length - 1];
202
+ const lastRun = runs[runs.length - 1] ?? [];
203
+ const last = lastRun[lastRun.length - 1];
204
+ if (!last) {
205
+ return {
206
+ player,
207
+ ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
208
+ visible: false,
209
+ ago: null,
210
+ };
211
+ }
212
+
213
+ const previous = lastRun.length >= 2 ? lastRun[lastRun.length - 2] : last;
214
+ return {
215
+ player,
216
+ ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
217
+ visible: latestFrame?.players?.[player] != null,
218
+ ago: ageSecs(last.frame.time, now),
219
+ room: last.pos.room,
220
+ x: round1(last.pos.x),
221
+ y: round1(last.pos.y),
222
+ dir: direction(previous.pos, last.pos),
223
+ };
224
+ }
225
+
226
+ function buildRoomsSeenResult(data: PlayerHistoryData, player: string, now: number): Record<string, any> {
227
+ const rooms: Record<string, any>[] = [];
228
+ for (const run of continuousRuns(pointsForPlayer(data.visible_frames, player))) {
229
+ let currentRoom = run[0]?.pos.room ?? 'unknown';
230
+ let start = run[0];
231
+ let previous = run[0];
232
+ for (const point of run.slice(1)) {
233
+ const room = point.pos.room ?? 'unknown';
234
+ if (room !== currentRoom) {
235
+ rooms.push(roomSegment(currentRoom, start, previous, now));
236
+ currentRoom = room;
237
+ start = point;
238
+ }
239
+ previous = point;
240
+ }
241
+ if (start && previous) rooms.push(roomSegment(currentRoom, start, previous, now));
242
+ }
243
+ rooms.sort((a, b) => a.to_ago - b.to_ago);
244
+ return {
245
+ player,
246
+ ...(data.seats?.[player] != null ? { seat: data.seats[player] } : {}),
247
+ rooms,
248
+ };
249
+ }
250
+
251
+ function roomSegment(room: string, start: PlayerPoint, end: PlayerPoint, now: number): Record<string, any> {
252
+ return {
253
+ room,
254
+ from_ago: ageSecs(start.frame.time, now),
255
+ to_ago: ageSecs(end.frame.time, now),
256
+ secs: durationSecs(start.frame.time, end.frame.time),
257
+ };
258
+ }
259
+
260
+ interface MeetingRecord {
261
+ round: number;
262
+ start?: Record<string, any>;
263
+ briefing?: Record<string, any>;
264
+ states: Record<string, any>[];
265
+ speechEvents: Record<string, any>[];
266
+ voteEvents: Record<string, any>[];
267
+ resultEvent?: Record<string, any>;
268
+ ended?: Record<string, any>;
269
+ interrupted?: boolean;
270
+ }
271
+
272
+ interface DeathInfo {
273
+ type: 'found_in_meeting' | 'exiled';
274
+ round: number;
275
+ }
276
+
277
+ function buildMeetingRecords(events: Record<string, any>[]): MeetingRecord[] {
278
+ const meetings: MeetingRecord[] = [];
279
+ let current: MeetingRecord | null = null;
280
+
281
+ const finishCurrent = (interrupted = false) => {
282
+ if (!current) return;
283
+ current.interrupted = current.interrupted || interrupted;
284
+ meetings.push(current);
285
+ current = null;
286
+ };
287
+
288
+ for (const event of events) {
289
+ if (event.type === 'meeting_start') {
290
+ finishCurrent(true);
291
+ current = {
292
+ round: meetings.length + 1,
293
+ start: event,
294
+ states: [],
295
+ speechEvents: [],
296
+ voteEvents: [],
297
+ };
298
+ continue;
299
+ }
300
+ if (!current) continue;
301
+
302
+ if (event.type === 'meeting_briefing') current.briefing = event;
303
+ if (event.type === 'meeting_state' && (event.round == null || event.round === current.round)) current.states.push(event);
304
+ if (event.type === 'speech' || event.type === 'speech_skipped' || event.type === 'speech_turn') current.speechEvents.push(event);
305
+ if (event.type === 'vote_cast') current.voteEvents.push(event);
306
+ if (event.type === 'exile' || event.type === 'no_exile') current.resultEvent = event;
307
+ if (event.type === 'meeting_ended') {
308
+ current.ended = event;
309
+ finishCurrent(false);
310
+ continue;
311
+ }
312
+ if (event.type === 'game_over') {
313
+ finishCurrent(true);
314
+ }
315
+ }
316
+ finishCurrent(false);
317
+ return meetings;
318
+ }
319
+
320
+ function latestMeetingState(record: MeetingRecord): Record<string, any> | undefined {
321
+ return record.states[record.states.length - 1];
322
+ }
323
+
324
+ function meetingVotes(record: MeetingRecord): Record<string, string> {
325
+ const resultVotes = record.resultEvent?.votes;
326
+ if (resultVotes && typeof resultVotes === 'object' && !Array.isArray(resultVotes)) {
327
+ return resultVotes as Record<string, string>;
328
+ }
329
+ const votes: Record<string, string> = {};
330
+ for (const vote of record.voteEvents) {
331
+ if (typeof vote.actor_name === 'string' && typeof vote.target === 'string') votes[vote.actor_name] = vote.target;
332
+ }
333
+ return votes;
334
+ }
335
+
336
+ function exiledPlayer(record: MeetingRecord): string | null {
337
+ if (record.ended?.result === 'exiled' && typeof record.ended.result_target === 'string') return record.ended.result_target;
338
+ if (record.resultEvent?.type === 'exile' && typeof record.resultEvent.actor_name === 'string') return record.resultEvent.actor_name;
339
+ return null;
340
+ }
341
+
342
+ function meetingState(record: MeetingRecord): string {
343
+ if (record.ended) return 'ended';
344
+ const subPhase = latestMeetingState(record)?.sub_phase;
345
+ if (typeof subPhase === 'string' && subPhase.length > 0) return subPhase;
346
+ if (record.interrupted) return 'interrupted_or_missing_end';
347
+ return 'interrupted_or_missing_end';
348
+ }
349
+
350
+ function meetingAllSeats(record: MeetingRecord): Array<{ name: string; seat: number }> {
351
+ const allSeats = record.briefing?.all_seats;
352
+ if (!Array.isArray(allSeats)) return [];
353
+ return allSeats
354
+ .filter((player: any) => typeof player?.name === 'string' && Number.isFinite(player.seat))
355
+ .map((player: any) => ({ name: player.name, seat: player.seat }))
356
+ .sort((a, b) => a.seat - b.seat);
357
+ }
358
+
359
+ function latestAlivePlayers(record: MeetingRecord): Set<string> | null {
360
+ for (let i = record.states.length - 1; i >= 0; i--) {
361
+ const alive = record.states[i].alive_players;
362
+ if (Array.isArray(alive)) return new Set(alive.filter((name: any) => typeof name === 'string'));
363
+ }
364
+ return null;
365
+ }
366
+
367
+ function collectSpeechOrder(record: MeetingRecord): string[] {
368
+ for (const state of record.states) {
369
+ if (Array.isArray(state.speech_order)) {
370
+ return state.speech_order.filter((name: any) => typeof name === 'string');
371
+ }
372
+ }
373
+ const order: string[] = [];
374
+ for (const event of record.speechEvents) {
375
+ const speaker = event.actor_name;
376
+ if (typeof speaker === 'string' && !order.includes(speaker)) order.push(speaker);
377
+ }
378
+ return order;
379
+ }
380
+
381
+ function meetingSpeeches(record: MeetingRecord): Array<{ speaker: string; text: string; skipped: boolean }> {
382
+ const order = collectSpeechOrder(record);
383
+ const texts = new Map<string, string>();
384
+ const skipped = new Set<string>();
385
+ const seen = new Set<string>();
386
+
387
+ for (const state of record.states) {
388
+ const stateSpeeches = state.speeches;
389
+ if (stateSpeeches && typeof stateSpeeches === 'object' && !Array.isArray(stateSpeeches)) {
390
+ for (const [speaker, text] of Object.entries(stateSpeeches)) {
391
+ if (typeof speaker !== 'string') continue;
392
+ texts.set(speaker, typeof text === 'string' ? text : '');
393
+ seen.add(speaker);
394
+ }
395
+ }
396
+ if (Array.isArray(state.speech_skipped)) {
397
+ for (const speaker of state.speech_skipped) {
398
+ if (typeof speaker !== 'string') continue;
399
+ skipped.add(speaker);
400
+ seen.add(speaker);
401
+ }
402
+ }
403
+ }
404
+
405
+ for (const event of record.speechEvents) {
406
+ const speaker = event.actor_name;
407
+ if (typeof speaker !== 'string') continue;
408
+ if (event.type === 'speech') {
409
+ texts.set(speaker, typeof event.text === 'string' ? event.text : '');
410
+ seen.add(speaker);
411
+ } else if (event.type === 'speech_skipped') {
412
+ skipped.add(speaker);
413
+ seen.add(speaker);
414
+ }
415
+ }
416
+
417
+ const outputOrder = [...order, ...Array.from(seen).filter((speaker) => !order.includes(speaker))];
418
+ return outputOrder
419
+ .filter((speaker) => seen.has(speaker))
420
+ .map((speaker) => ({
421
+ speaker,
422
+ text: texts.get(speaker) ?? '',
423
+ skipped: skipped.has(speaker),
424
+ }));
425
+ }
426
+
427
+ function applyMeetingDeaths(record: MeetingRecord, deaths: Map<string, DeathInfo>): void {
428
+ const alive = latestAlivePlayers(record);
429
+ if (alive) {
430
+ for (const player of meetingAllSeats(record)) {
431
+ if (!alive.has(player.name) && !deaths.has(player.name)) {
432
+ deaths.set(player.name, { type: 'found_in_meeting', round: record.round });
433
+ }
434
+ }
435
+ }
436
+ const exiled = exiledPlayer(record);
437
+ if (exiled) deaths.set(exiled, { type: 'exiled', round: record.round });
438
+ }
439
+
440
+ function meetingPlayers(record: MeetingRecord, deaths: Map<string, DeathInfo>): Record<string, any>[] {
441
+ return meetingAllSeats(record).map((player) => {
442
+ const death = deaths.get(player.name) ?? null;
443
+ return {
444
+ name: player.name,
445
+ seat: player.seat,
446
+ status: death ? 'dead' : 'alive',
447
+ death,
448
+ };
449
+ });
450
+ }
451
+
452
+ function meetingOutput(record: MeetingRecord, deaths: Map<string, DeathInfo>): Record<string, any> {
453
+ applyMeetingDeaths(record, deaths);
454
+ return {
455
+ round: record.round,
456
+ tick: record.start?.tick ?? record.briefing?.tick ?? null,
457
+ caller: record.start?.actor_name ?? record.briefing?.caller ?? latestMeetingState(record)?.caller ?? null,
458
+ corpses_found: null,
459
+ state: meetingState(record),
460
+ exiled_player: exiledPlayer(record),
461
+ players: meetingPlayers(record, deaths),
462
+ votes: meetingVotes(record),
463
+ speeches: meetingSpeeches(record),
464
+ };
465
+ }
466
+
467
+ function allMeetingOutputs(records: MeetingRecord[]): Record<string, any>[] {
468
+ const deaths = new Map<string, DeathInfo>();
469
+ return records.map((record) => meetingOutput(record, deaths));
470
+ }
471
+
472
+ function createMeetingsCommand(): Command {
473
+ return new Command('meetings')
474
+ .description('Show meeting history')
475
+ .argument('[round]', 'Meeting round number, starting from 1')
476
+ .action((roundText?: string) => {
477
+ const records = buildMeetingRecords(EventStore.forLatestGame().all());
478
+ const outputs = allMeetingOutputs(records);
479
+ if (roundText != null) {
480
+ const round = Number(roundText);
481
+ const record = Number.isInteger(round) && round > 0 ? outputs.find((meeting) => meeting.round === round) : null;
482
+ if (!record) {
483
+ console.log(JSON.stringify({
484
+ error: {
485
+ code: 'meeting_round_not_found',
486
+ round: roundText,
487
+ available_rounds: outputs.map((meeting) => meeting.round),
488
+ },
489
+ }, null, 2));
490
+ return;
491
+ }
492
+ console.log(JSON.stringify(record, null, 2));
493
+ return;
494
+ }
495
+ console.log(JSON.stringify({ meetings: outputs }, null, 2));
496
+ });
497
+ }
498
+
499
+ function createPlayerHistorySubcommand(): Command {
500
+ return new Command('player')
501
+ .description('Show derived visible-player history')
502
+ .argument('<kind>', 'stay | last_appear | rooms_seen')
503
+ .argument('[player_id...]', 'Player name/seat/list, or all')
504
+ .action(async (kind: string, playerArgs: string[]) => {
505
+ if (!KINDS.has(kind as HistoryKind)) {
506
+ console.log(JSON.stringify({ error: { code: 'invalid_kind', allowed: Array.from(KINDS) } }, null, 2));
507
+ return;
508
+ }
509
+
510
+ const sessionPath = EventStore.latestSessionPath();
511
+ if (!sessionPath) {
512
+ console.log(JSON.stringify({
513
+ kind,
514
+ error: { code: 'no_game_events', message: 'No game events found. Join a game first.' },
515
+ }, null, 2));
516
+ return;
517
+ }
518
+ const data = PlayerHistoryStore.forSession(sessionPath).read();
519
+ if (!data || !Array.isArray(data.visible_frames) || data.visible_frames.length === 0) {
520
+ console.log(JSON.stringify({
521
+ kind,
522
+ error: {
523
+ code: 'no_player_history',
524
+ message: 'No player_spotted history yet. Join a game and wait for the game runtime to receive player_spotted events.',
525
+ },
526
+ }, null, 2));
527
+ return;
528
+ }
529
+
530
+ const selectors = splitSelectors(playerArgs);
531
+ const resolved = resolvePlayers(data, selectors);
532
+ if (resolved.error) {
533
+ console.log(JSON.stringify({ kind, error: resolved.error }, null, 2));
534
+ return;
535
+ }
536
+
537
+ const now = Date.now();
538
+ const players = resolved.players ?? [];
539
+ const resultPlayers = players.map((player) => {
540
+ if (kind === 'stay') return buildStayResult(data, player, now);
541
+ if (kind === 'last_appear') return buildLastAppearResult(data, player, now);
542
+ return buildRoomsSeenResult(data, player, now);
543
+ });
544
+
545
+ console.log(JSON.stringify({
546
+ kind,
547
+ time: new Date(now).toISOString(),
548
+ players: resultPlayers,
549
+ }, null, 2));
550
+ });
551
+ }
552
+
553
+ export function createHistoryCommand(): Command {
554
+ const history = new Command('history')
555
+ .description('Show derived player and meeting history');
556
+ history.addCommand(createPlayerHistorySubcommand());
557
+ history.addCommand(createMeetingsCommand());
558
+ return history;
559
+ }
560
+
561
+ export const playerHistoryCommandInternals = {
562
+ direction,
563
+ resolvePlayers,
564
+ buildStayResult,
565
+ buildLastAppearResult,
566
+ buildRoomsSeenResult,
567
+ };
568
+
569
+ export const meetingHistoryCommandInternals = {
570
+ buildMeetingRecords,
571
+ allMeetingOutputs,
572
+ meetingSpeeches,
573
+ };