@myclaw163/clawclaw-cli 0.6.70 → 0.6.74
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/README.md +377 -427
- package/package.json +48 -48
- package/personas//347/220/206/346/231/272/346/270/251/345/222/214.md +23 -23
- package/personas//350/200/201/350/260/213/346/267/261/347/256/227.md +22 -22
- package/personas//350/257/232/346/201/263/347/233/264/347/216/207.md +22 -22
- package/personas//350/275/273/346/235/276/346/264/273/346/263/274.md +22 -22
- package/personas//351/207/216/346/200/247/345/217/233/351/200/206.md +23 -23
- package/scripts/check-skill-command-surface.mjs +116 -0
- package/scripts/find-hide-spots.py +157 -157
- package/scripts/postinstall.mjs +20 -20
- package/scripts/sync-bundled-skill.mjs +244 -244
- package/scripts/sync-bundled-skill.test.mjs +152 -152
- package/skills/clawclaw/SKILL.md +246 -244
- package/skills/clawclaw/references/CHATTERBOX.md +141 -142
- package/skills/clawclaw/references/COMMANDS.md +155 -148
- package/skills/clawclaw/references/GAME-MECHANICS.md +188 -188
- package/skills/clawclaw/references/HUB.md +48 -48
- package/skills/clawclaw/references/KNOWLEDGE.md +42 -45
- package/skills/clawclaw/references/STRATEGIES.md +59 -59
- package/skills/clawclaw/references/STREAM.md +93 -91
- package/skills/clawclaw/references/TACTICS.md +65 -65
- package/src/assets/clawclaw-ascii-map.txt +40 -40
- package/src/cli.ts +110 -110
- package/src/commands/_schema.ts +124 -109
- package/src/commands/account.ts +209 -209
- package/src/commands/config.ts +30 -30
- package/src/commands/do.test.ts +84 -73
- package/src/commands/do.ts +130 -126
- package/src/commands/events.test.ts +71 -71
- package/src/commands/events.ts +221 -155
- package/src/commands/game-map.test.ts +28 -28
- package/src/commands/game-start-plan.test.ts +84 -84
- package/src/commands/game.ts +1113 -1042
- package/src/commands/history-player.test.ts +102 -102
- package/src/commands/history.ts +573 -573
- package/src/commands/hub.test.ts +96 -96
- package/src/commands/hub.ts +234 -234
- package/src/commands/knowledge.test.ts +13 -13
- package/src/commands/knowledge.ts +139 -139
- package/src/commands/load.test.ts +51 -51
- package/src/commands/load.ts +13 -13
- package/src/commands/meeting-history.test.ts +106 -106
- package/src/commands/memory.ts +40 -40
- package/src/commands/peek.ts +45 -45
- package/src/commands/persona.ts +57 -57
- package/src/commands/setup/codex.ts +266 -266
- 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 +114 -114
- package/src/commands/setup/openclaw.ts +147 -147
- package/src/commands/skill.ts +128 -128
- package/src/commands/state.ts +46 -46
- package/src/commands/strategy.test.ts +145 -145
- package/src/commands/strategy.ts +181 -181
- package/src/commands/tts.ts +128 -128
- package/src/commands/upgrade.test.ts +82 -82
- package/src/commands/upgrade.ts +148 -148
- package/src/commands/watch.test.ts +999 -977
- package/src/commands/watch.ts +660 -658
- package/src/lib/auth.test.ts +74 -74
- package/src/lib/auth.ts +186 -186
- package/src/lib/command-meta.ts +37 -37
- package/src/lib/game-client.ts +403 -391
- package/src/lib/game-context.ts +92 -0
- package/src/lib/host-config-patcher.test.ts +130 -130
- package/src/lib/host-config-patcher.ts +151 -151
- package/src/lib/http-keepalive.ts +15 -15
- package/src/lib/http-transport.test.ts +42 -42
- package/src/lib/http-transport.ts +113 -113
- package/src/lib/hub-client.test.ts +56 -56
- package/src/lib/hub-client.ts +88 -88
- package/src/lib/hub-install.test.ts +98 -98
- package/src/lib/hub-install.ts +121 -121
- package/src/lib/hub-reminder.ts +56 -56
- package/src/lib/hub-unzip.test.ts +69 -69
- package/src/lib/hub-unzip.ts +62 -62
- package/src/lib/init-command.test.ts +75 -75
- package/src/lib/init-command.ts +120 -120
- package/src/lib/knowledge-store.test.ts +170 -170
- package/src/lib/knowledge-store.ts +369 -369
- package/src/lib/load-context.test.ts +52 -52
- package/src/lib/load-context.ts +52 -52
- package/src/lib/match-state.test.ts +134 -134
- package/src/lib/match-state.ts +94 -94
- package/src/lib/netease-tts.ts +83 -83
- package/src/lib/normalize.ts +42 -42
- package/src/lib/persona.test.ts +41 -41
- package/src/lib/persona.ts +72 -72
- package/src/lib/server-registry.ts +152 -152
- package/src/lib/skill-version.test.ts +48 -48
- package/src/lib/skill-version.ts +19 -19
- package/src/lib/strategy-export.test.ts +232 -232
- package/src/lib/strategy-export.ts +242 -242
- package/src/lib/tts-keys.ts +7 -7
- package/src/lib/tts-speech.test.ts +63 -63
- package/src/lib/tts-speech.ts +76 -76
- package/src/lib/workspace-argv.test.ts +49 -49
- package/src/lib/workspace-argv.ts +44 -44
- package/src/perception/player-history-store.test.ts +87 -87
- package/src/perception/player-history-store.ts +194 -194
- package/src/pipeline/event-format.test.ts +243 -215
- package/src/pipeline/event-format.ts +501 -485
- package/src/pipeline/event-hints.ts +195 -190
- package/src/pipeline/event-store.test.ts +28 -28
- package/src/pipeline/event-store.ts +193 -193
- package/src/pipeline/pipeline.ts +35 -35
- package/src/pipeline/player-projection.test.ts +119 -0
- package/src/pipeline/player-projection.ts +380 -0
- package/src/runtime/auto-upgrade.test.ts +66 -66
- package/src/runtime/auto-upgrade.ts +31 -31
- package/src/runtime/event-daemon.test.ts +209 -141
- package/src/runtime/event-daemon.ts +519 -457
- package/src/runtime/owner-control.ts +150 -150
- package/src/runtime/raw-ws-log.test.ts +33 -33
- package/src/runtime/raw-ws-log.ts +32 -32
- package/src/runtime/runtime-logger.ts +107 -107
- package/src/runtime/ws-client.test.ts +125 -104
- package/src/runtime/ws-client.ts +287 -272
- package/src/sdk/action.ts +166 -166
- package/src/sdk/index.ts +110 -110
- package/src/sdk/types.ts +161 -161
- package/src/strategies/avoid-lone.ts +12 -12
- package/src/strategies/avoid-players.knowledge.md +19 -19
- package/src/strategies/avoid-players.ts +16 -16
- package/src/strategies/corpse-patrol.ts +23 -23
- package/src/strategies/crab-sabotage.ts +22 -22
- package/src/strategies/custom-module.test.ts +270 -270
- package/src/strategies/find-player.ts +17 -17
- package/src/strategies/game-utils.test.ts +242 -242
- package/src/strategies/game-utils.ts +846 -846
- package/src/strategies/goals/anchor-linger.ts +77 -77
- package/src/strategies/goals/avoid-lone-top.ts +168 -168
- package/src/strategies/goals/avoid-players-top.test.ts +83 -83
- package/src/strategies/goals/avoid-players-top.ts +121 -121
- package/src/strategies/goals/conversation-goal.ts +51 -51
- package/src/strategies/goals/corpse-patrol-top.ts +113 -113
- package/src/strategies/goals/crab-octopus-reflexes.ts +101 -101
- package/src/strategies/goals/crab-sabotage-top.ts +197 -197
- package/src/strategies/goals/emergency-hunt-goal.ts +28 -28
- package/src/strategies/goals/find-player-top.ts +93 -93
- package/src/strategies/goals/flee-players-goal.ts +53 -53
- package/src/strategies/goals/follow-companion-goal.ts +106 -106
- package/src/strategies/goals/goal-manager.ts +41 -41
- package/src/strategies/goals/goal-root-strategy.ts +49 -49
- package/src/strategies/goals/goal.ts +28 -28
- package/src/strategies/goals/hide-top.ts +197 -197
- package/src/strategies/goals/keep-away-goal.ts +221 -221
- package/src/strategies/goals/kill-frenzy-top.ts +80 -80
- package/src/strategies/goals/kill-lone-top.ts +160 -160
- package/src/strategies/goals/kill-target-goal.ts +59 -59
- package/src/strategies/goals/kill-target-top.ts +109 -109
- package/src/strategies/goals/leaf-goal.ts +27 -27
- package/src/strategies/goals/linger-corpse-goal.ts +35 -35
- package/src/strategies/goals/lone-kill-core.ts +82 -82
- package/src/strategies/goals/lone-kill-goal.ts +24 -24
- package/src/strategies/goals/lone-kill-task-top.test.ts +85 -85
- package/src/strategies/goals/lone-kill-task-top.ts +133 -133
- package/src/strategies/goals/move-room-goal.ts +60 -60
- package/src/strategies/goals/normal-shrimp-top.test.ts +80 -80
- package/src/strategies/goals/normal-shrimp-top.ts +242 -242
- package/src/strategies/goals/paradise-fish-top.test.ts +126 -126
- package/src/strategies/goals/paradise-fish-top.ts +224 -224
- package/src/strategies/goals/patrol-top.ts +57 -57
- package/src/strategies/goals/report-patrol-top.ts +80 -80
- package/src/strategies/goals/safe-task-goal.ts +102 -102
- package/src/strategies/goals/social-task-top.ts +161 -161
- package/src/strategies/goals/task-kill-report-top.ts +163 -163
- package/src/strategies/goals/task-only-top.ts +57 -57
- package/src/strategies/goals/task-or-patrol-goal.ts +41 -41
- package/src/strategies/goals/task-report-top.ts +57 -57
- package/src/strategies/goals/wander-task-goal.ts +33 -33
- package/src/strategies/goals/warrior-shrimp-top.test.ts +87 -87
- package/src/strategies/goals/warrior-shrimp-top.ts +267 -267
- package/src/strategies/greeting.ts +53 -53
- package/src/strategies/hide-spots.ts +59 -59
- package/src/strategies/hide.ts +24 -24
- package/src/strategies/kill-frenzy.ts +13 -13
- package/src/strategies/kill-lone.knowledge.md +17 -17
- package/src/strategies/kill-lone.ts +14 -14
- package/src/strategies/kill-target.ts +19 -19
- package/src/strategies/loader.test.ts +678 -678
- package/src/strategies/loader.ts +179 -179
- package/src/strategies/lone-kill-task.ts +22 -22
- package/src/strategies/meeting-gate.test.ts +59 -59
- package/src/strategies/meeting-gate.ts +23 -23
- package/src/strategies/move-room.ts +16 -16
- package/src/strategies/new-events-backfill.ts +98 -98
- package/src/strategies/off-route-points.ts +105 -105
- package/src/strategies/paradise-fish.knowledge.md +19 -19
- package/src/strategies/paradise-fish.ts +26 -26
- package/src/strategies/pathfind/distance-field.ts +150 -150
- package/src/strategies/pathfind/escape-planner.test.ts +197 -197
- package/src/strategies/pathfind/escape-planner.ts +355 -355
- package/src/strategies/pathfind/walkable-grid.ts +117 -117
- package/src/strategies/patrol.ts +12 -12
- package/src/strategies/player-targets.ts +13 -13
- package/src/strategies/report-patrol.ts +12 -12
- package/src/strategies/shrimp-memory.knowledge.md +19 -19
- package/src/strategies/shrimp-memory.ts +26 -26
- package/src/strategies/social-task.test.ts +28 -28
- package/src/strategies/social-task.ts +50 -50
- package/src/strategies/spawn.ts +82 -82
- package/src/strategies/speech-module.ts +123 -123
- package/src/strategies/strategy-loop.test.ts +15 -0
- package/src/strategies/strategy-loop.ts +776 -771
- package/src/strategies/task-kill-report.ts +18 -18
- package/src/strategies/task-only.ts +12 -12
- package/src/strategies/task-report.ts +23 -23
- package/src/strategies/types.ts +109 -109
- package/src/strategies/warrior-memory.knowledge.md +21 -21
- package/src/strategies/warrior-memory.ts +17 -17
package/src/commands/game.ts
CHANGED
|
@@ -1,1042 +1,1113 @@
|
|
|
1
|
-
import { Command } from 'commander';
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
|
|
3
|
-
import { join } from 'path';
|
|
4
|
-
import { spawnSync, type ChildProcess } from 'child_process';
|
|
5
|
-
import { AuthStore } from '../lib/auth.js';
|
|
6
|
-
import { ApiError, GameClient } from '../lib/game-client.js';
|
|
7
|
-
import { getProfileStateDir } from '../lib/init-command.js';
|
|
8
|
-
import { EventStore } from '../pipeline/event-store.js';
|
|
9
|
-
import { spawnStrategyLoop } from '../strategies/spawn.js';
|
|
10
|
-
import { stopStrategyIfRunning } from '../strategies/strategy-loop.js';
|
|
11
|
-
import { setMeta } from '../lib/command-meta.js';
|
|
12
|
-
import { runStreaming, buildErrorLine, summarizeFeed, nextStepFor } from './watch.js';
|
|
13
|
-
import { hubReminder, readCachedGamesPlayed } from '../lib/hub-reminder.js';
|
|
14
|
-
import { EventRuntime } from '../runtime/event-daemon.js';
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
function
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
type
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
return
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function
|
|
231
|
-
return
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function
|
|
235
|
-
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
)
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
if (
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
}
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
.
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
.
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
.
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
const
|
|
945
|
-
const
|
|
946
|
-
|
|
947
|
-
const
|
|
948
|
-
const
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { spawnSync, type ChildProcess } from 'child_process';
|
|
5
|
+
import { AuthStore } from '../lib/auth.js';
|
|
6
|
+
import { ApiError, GameClient } from '../lib/game-client.js';
|
|
7
|
+
import { getProfileStateDir } from '../lib/init-command.js';
|
|
8
|
+
import { EventStore } from '../pipeline/event-store.js';
|
|
9
|
+
import { spawnStrategyLoop } from '../strategies/spawn.js';
|
|
10
|
+
import { stopStrategyIfRunning } from '../strategies/strategy-loop.js';
|
|
11
|
+
import { setMeta } from '../lib/command-meta.js';
|
|
12
|
+
import { runStreaming, buildErrorLine, summarizeFeed, nextStepFor } from './watch.js';
|
|
13
|
+
import { hubReminder, readCachedGamesPlayed } from '../lib/hub-reminder.js';
|
|
14
|
+
import { EventRuntime } from '../runtime/event-daemon.js';
|
|
15
|
+
import {
|
|
16
|
+
briefGameMap,
|
|
17
|
+
summarizeGameMap,
|
|
18
|
+
} from '../lib/game-context.js';
|
|
19
|
+
import {
|
|
20
|
+
gameStartRuntimePath,
|
|
21
|
+
readGameStartRuntime,
|
|
22
|
+
sendOwnerControlRequest,
|
|
23
|
+
startOwnerControlServer,
|
|
24
|
+
type OwnerControlInfo,
|
|
25
|
+
type OwnerControlServer,
|
|
26
|
+
} from '../runtime/owner-control.js';
|
|
27
|
+
import {
|
|
28
|
+
startMatch,
|
|
29
|
+
endMatch,
|
|
30
|
+
readMatchState,
|
|
31
|
+
shouldEmitWaiting,
|
|
32
|
+
markWaitingEmitted,
|
|
33
|
+
getWaitedSecs,
|
|
34
|
+
hasMatchTimedOut,
|
|
35
|
+
} from '../lib/match-state.js';
|
|
36
|
+
|
|
37
|
+
export { summarizeGameMap } from '../lib/game-context.js';
|
|
38
|
+
|
|
39
|
+
function sleep(ms: number): Promise<void> {
|
|
40
|
+
return new Promise(r => setTimeout(r, ms));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function positiveNumber(value: string, fallback: number): number {
|
|
44
|
+
const parsed = Number(value);
|
|
45
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function queueStatus(result: any): string | undefined {
|
|
49
|
+
return (result?.data ?? result)?.status;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const DEFAULT_QUEUE_WAIT_TIMEOUT_SECS = 30;
|
|
53
|
+
|
|
54
|
+
function clearCachedGameServerUrl(authStore: AuthStore, profileName?: string): void {
|
|
55
|
+
try {
|
|
56
|
+
authStore.updateGameServerUrl(undefined, profileName);
|
|
57
|
+
} catch {
|
|
58
|
+
// Cache cleanup is best-effort; queue status will discover the current server.
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function isLeaveGameSuccess(result: any): boolean {
|
|
63
|
+
const data = result?.data ?? result;
|
|
64
|
+
return data?.ok === true || data?.message === 'left_game';
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isPidAlive(pid: number): boolean {
|
|
68
|
+
if (!Number.isFinite(pid) || pid <= 0 || pid === process.pid) return false;
|
|
69
|
+
try { process.kill(pid, 0); return true; } catch { return false; }
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getRunningGameStartPid(stateDir: string): number | null {
|
|
73
|
+
const info = readGameStartRuntime(stateDir);
|
|
74
|
+
if (!info) return null;
|
|
75
|
+
const pid = Number(info?.owner_pid ?? info?.pid);
|
|
76
|
+
if (isPidAlive(pid)) return pid;
|
|
77
|
+
try { unlinkSync(gameStartRuntimePath(stateDir)); } catch {}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function writeGameStartRuntime(
|
|
82
|
+
stateDir: string,
|
|
83
|
+
mode: GameStartPlanKind,
|
|
84
|
+
phase?: string,
|
|
85
|
+
controlOverride?: { control: OwnerControlInfo; token: string },
|
|
86
|
+
): void {
|
|
87
|
+
mkdirSync(stateDir, { recursive: true });
|
|
88
|
+
let startedAt = new Date().toISOString();
|
|
89
|
+
let existingControl: OwnerControlInfo | undefined;
|
|
90
|
+
let existingToken: string | undefined;
|
|
91
|
+
try {
|
|
92
|
+
const existing = JSON.parse(readFileSync(gameStartRuntimePath(stateDir), 'utf8'));
|
|
93
|
+
const existingPid = Number(existing?.owner_pid ?? existing?.pid);
|
|
94
|
+
if (existingPid === process.pid && typeof existing?.started_at === 'string') {
|
|
95
|
+
startedAt = existing.started_at;
|
|
96
|
+
if (existing?.control?.path) existingControl = existing.control;
|
|
97
|
+
if (typeof existing?.control_token === 'string') existingToken = existing.control_token;
|
|
98
|
+
}
|
|
99
|
+
} catch {}
|
|
100
|
+
const control = controlOverride?.control ?? existingControl;
|
|
101
|
+
const controlToken = controlOverride?.token ?? existingToken;
|
|
102
|
+
writeFileSync(gameStartRuntimePath(stateDir), JSON.stringify({
|
|
103
|
+
schema: 3,
|
|
104
|
+
owner_pid: process.pid,
|
|
105
|
+
pid: process.pid,
|
|
106
|
+
started_at: startedAt,
|
|
107
|
+
heartbeat_at: new Date().toISOString(),
|
|
108
|
+
mode,
|
|
109
|
+
...(control ? { control } : {}),
|
|
110
|
+
...(controlToken ? { control_token: controlToken } : {}),
|
|
111
|
+
...(phase ? { phase } : {}),
|
|
112
|
+
}, null, 2));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function startGameStartHeartbeat(stateDir: string, mode: GameStartPlanKind): ReturnType<typeof setInterval> {
|
|
116
|
+
return setInterval(() => {
|
|
117
|
+
try {
|
|
118
|
+
writeGameStartRuntime(stateDir, mode);
|
|
119
|
+
} catch {}
|
|
120
|
+
}, 5000);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function cleanupGameStartRuntime(stateDir: string, opts: { removeFeed?: boolean; controlPath?: string } = {}): void {
|
|
124
|
+
const runtimePath = gameStartRuntimePath(stateDir);
|
|
125
|
+
try {
|
|
126
|
+
const info = JSON.parse(readFileSync(runtimePath, 'utf8'));
|
|
127
|
+
const pid = Number(info?.owner_pid ?? info?.pid);
|
|
128
|
+
if (pid !== process.pid) return;
|
|
129
|
+
} catch {}
|
|
130
|
+
try { unlinkSync(runtimePath); } catch {}
|
|
131
|
+
if (opts.removeFeed) {
|
|
132
|
+
try { unlinkSync(join(stateDir, 'feed.json')); } catch {}
|
|
133
|
+
}
|
|
134
|
+
if (opts.controlPath && process.platform !== 'win32') {
|
|
135
|
+
try { unlinkSync(opts.controlPath); } catch {}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function terminateProcessTree(pid: number, signal: NodeJS.Signals = 'SIGTERM'): boolean {
|
|
140
|
+
if (!Number.isFinite(pid) || pid <= 0 || pid === process.pid) return false;
|
|
141
|
+
if (process.platform === 'win32') {
|
|
142
|
+
const result = spawnSync('taskkill', ['/PID', String(pid), '/T', '/F'], { stdio: 'ignore' });
|
|
143
|
+
return result.status === 0;
|
|
144
|
+
}
|
|
145
|
+
const pkillSignal = signal === 'SIGKILL' ? '-KILL' : '-TERM';
|
|
146
|
+
try { spawnSync('pkill', [pkillSignal, '-P', String(pid)], { stdio: 'ignore' }); } catch {}
|
|
147
|
+
try {
|
|
148
|
+
process.kill(pid, signal);
|
|
149
|
+
return true;
|
|
150
|
+
} catch {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function waitPidExit(pid: number, timeoutMs = 5000): Promise<boolean> {
|
|
156
|
+
const deadline = Date.now() + timeoutMs;
|
|
157
|
+
while (Date.now() < deadline) {
|
|
158
|
+
if (!isPidAlive(pid)) return true;
|
|
159
|
+
await sleep(200);
|
|
160
|
+
}
|
|
161
|
+
return !isPidAlive(pid);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function stopOwnerIfRunning(stateDir: string, timeoutMs = 5000): Promise<{ pid: number | null; stopped: boolean }> {
|
|
165
|
+
return stopOwnerWithCommand(stateDir, 'stop', timeoutMs);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async function stopOwnerWithCommand(
|
|
169
|
+
stateDir: string,
|
|
170
|
+
command: 'stop' | 'quit' | 'leave',
|
|
171
|
+
timeoutMs = 5000,
|
|
172
|
+
): Promise<{ pid: number | null; stopped: boolean }> {
|
|
173
|
+
const pid = getRunningGameStartPid(stateDir);
|
|
174
|
+
if (!pid) {
|
|
175
|
+
try { unlinkSync(gameStartRuntimePath(stateDir)); } catch {}
|
|
176
|
+
try { unlinkSync(join(stateDir, 'feed.json')); } catch {}
|
|
177
|
+
return { pid: null, stopped: false };
|
|
178
|
+
}
|
|
179
|
+
try {
|
|
180
|
+
const response = await sendOwnerControlRequest(stateDir, command);
|
|
181
|
+
if (response?.ok) {
|
|
182
|
+
const exited = await waitPidExit(pid, timeoutMs);
|
|
183
|
+
if (exited) return { pid, stopped: true };
|
|
184
|
+
}
|
|
185
|
+
} catch {}
|
|
186
|
+
terminateProcessTree(pid, 'SIGTERM');
|
|
187
|
+
const exited = await waitPidExit(pid, timeoutMs);
|
|
188
|
+
if (!exited) {
|
|
189
|
+
terminateProcessTree(pid, 'SIGKILL');
|
|
190
|
+
await waitPidExit(pid, 2000);
|
|
191
|
+
}
|
|
192
|
+
try { unlinkSync(gameStartRuntimePath(stateDir)); } catch {}
|
|
193
|
+
try { unlinkSync(join(stateDir, 'feed.json')); } catch {}
|
|
194
|
+
return { pid, stopped: true };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
type QueueStatus = 'allocated' | 'queued' | 'not_in_queue' | string | undefined;
|
|
198
|
+
|
|
199
|
+
export type GameStartPlan =
|
|
200
|
+
| { kind: 'already_running'; pid: number }
|
|
201
|
+
| { kind: 'resume_queue' }
|
|
202
|
+
| { kind: 'resume_allocated' }
|
|
203
|
+
| { kind: 'fresh_start' };
|
|
204
|
+
|
|
205
|
+
type GameStartPlanKind = GameStartPlan['kind'];
|
|
206
|
+
|
|
207
|
+
export function planGameStartAction(input: {
|
|
208
|
+
gameStartPid?: number | null;
|
|
209
|
+
hasMatchState: boolean;
|
|
210
|
+
queueStatus?: QueueStatus;
|
|
211
|
+
}): GameStartPlan {
|
|
212
|
+
if (input.gameStartPid) return { kind: 'already_running', pid: input.gameStartPid };
|
|
213
|
+
|
|
214
|
+
if (input.queueStatus === 'allocated') return { kind: 'resume_allocated' };
|
|
215
|
+
if (input.queueStatus === 'queued' || input.queueStatus === 'already_in_queue') return { kind: 'resume_queue' };
|
|
216
|
+
if (input.hasMatchState && input.queueStatus !== 'not_in_queue') return { kind: 'resume_queue' };
|
|
217
|
+
return { kind: 'fresh_start' };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function isAlreadyInGameError(err: unknown): boolean {
|
|
221
|
+
return err instanceof ApiError && err.body.includes('ALREADY_IN_GAME');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function apiErrorBody(err: unknown): string {
|
|
225
|
+
if (err instanceof ApiError) return err.body;
|
|
226
|
+
if (err instanceof Error) return err.message;
|
|
227
|
+
return String(err);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function isStillAliveError(err: unknown): boolean {
|
|
231
|
+
return /STILL_ALIVE/i.test(apiErrorBody(err));
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function serializeError(err: unknown): Record<string, any> {
|
|
235
|
+
if (err instanceof ApiError) {
|
|
236
|
+
return { status: err.status, body: err.body };
|
|
237
|
+
}
|
|
238
|
+
return { message: err instanceof Error ? err.message : String(err) };
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
type GamePresence = 'in_game' | 'matching' | 'none' | 'unknown';
|
|
242
|
+
|
|
243
|
+
interface GamePresenceInfo {
|
|
244
|
+
presence: GamePresence;
|
|
245
|
+
game_state?: any;
|
|
246
|
+
queue_status?: any;
|
|
247
|
+
owner_pid: number | null;
|
|
248
|
+
errors?: Array<{ source: string; error: Record<string, any> }>;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function isMatchingQueueStatus(status: QueueStatus): boolean {
|
|
252
|
+
return status === 'queued'
|
|
253
|
+
|| status === 'already_in_queue'
|
|
254
|
+
|| status === 'allocating'
|
|
255
|
+
|| status === 'allocated';
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
async function detectGamePresence(client: GameClient, stateDir: string): Promise<GamePresenceInfo> {
|
|
259
|
+
const errors: Array<{ source: string; error: Record<string, any> }> = [];
|
|
260
|
+
const ownerPid = getRunningGameStartPid(stateDir);
|
|
261
|
+
let gameState: any | null = null;
|
|
262
|
+
try {
|
|
263
|
+
await client.discoverGameServer();
|
|
264
|
+
gameState = await client.getGameState();
|
|
265
|
+
} catch (err) {
|
|
266
|
+
errors.push({ source: 'game_current', error: serializeError(err) });
|
|
267
|
+
}
|
|
268
|
+
if (gameState) {
|
|
269
|
+
return {
|
|
270
|
+
presence: 'in_game',
|
|
271
|
+
game_state: gameState,
|
|
272
|
+
owner_pid: ownerPid,
|
|
273
|
+
...(errors.length ? { errors } : {}),
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
let queue: any;
|
|
278
|
+
try {
|
|
279
|
+
queue = await client.getQueueStatus('clawclaw');
|
|
280
|
+
} catch (err) {
|
|
281
|
+
errors.push({ source: 'queue_status', error: serializeError(err) });
|
|
282
|
+
}
|
|
283
|
+
const status = queueStatus(queue);
|
|
284
|
+
if (isMatchingQueueStatus(status)) {
|
|
285
|
+
return {
|
|
286
|
+
presence: 'matching',
|
|
287
|
+
queue_status: queue,
|
|
288
|
+
owner_pid: ownerPid,
|
|
289
|
+
...(errors.length ? { errors } : {}),
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
if (status === 'not_in_queue') {
|
|
293
|
+
return {
|
|
294
|
+
presence: 'none',
|
|
295
|
+
queue_status: queue,
|
|
296
|
+
owner_pid: ownerPid,
|
|
297
|
+
...(errors.length ? { errors } : {}),
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
presence: errors.length >= 2 ? 'unknown' : 'none',
|
|
302
|
+
...(queue ? { queue_status: queue } : {}),
|
|
303
|
+
owner_pid: ownerPid,
|
|
304
|
+
...(errors.length ? { errors } : {}),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function ensureEventSession(source: string): EventStore {
|
|
309
|
+
const existing = EventStore.latestSessionPath();
|
|
310
|
+
if (existing) {
|
|
311
|
+
const events = EventStore.forActiveAccount();
|
|
312
|
+
events.append({ type: 'session_resumed', source });
|
|
313
|
+
return events;
|
|
314
|
+
}
|
|
315
|
+
const events = EventStore.createSessionForActiveAccount();
|
|
316
|
+
events.append({ type: 'session_started', source });
|
|
317
|
+
return events;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function nonEmptyString(value: unknown): string | undefined {
|
|
321
|
+
return typeof value === 'string' && value.length > 0 ? value : undefined;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function unwrapData(value: any): any {
|
|
325
|
+
return value?.data ?? value;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function cleanObject<T extends Record<string, any>>(obj: T): T {
|
|
329
|
+
for (const key of Object.keys(obj)) {
|
|
330
|
+
if (obj[key] === undefined) delete obj[key];
|
|
331
|
+
}
|
|
332
|
+
return obj;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
export interface GameStrategyIdentity {
|
|
336
|
+
gameId?: string;
|
|
337
|
+
role?: string;
|
|
338
|
+
alive: boolean | null;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function gameStrategyIdentity(stateData: any, roleData: any): GameStrategyIdentity {
|
|
342
|
+
const state = unwrapData(stateData);
|
|
343
|
+
const role = unwrapData(roleData);
|
|
344
|
+
const you = state?.you ?? {};
|
|
345
|
+
const aliveRaw = you?.is_alive ?? you?.alive;
|
|
346
|
+
return {
|
|
347
|
+
gameId: nonEmptyString(state?.game_id) ?? nonEmptyString(state?.game?.id) ?? nonEmptyString(state?.game?.game_id),
|
|
348
|
+
role: nonEmptyString(role?.role) ?? nonEmptyString(you?.role),
|
|
349
|
+
alive: typeof aliveRaw === 'boolean' ? aliveRaw : null,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function commandError(command: string, err: unknown): { command: string; error: string } {
|
|
354
|
+
const message = err instanceof ApiError
|
|
355
|
+
? `${err.status}: ${err.body}`
|
|
356
|
+
: err instanceof Error
|
|
357
|
+
? err.message
|
|
358
|
+
: String(err);
|
|
359
|
+
return { command, error: message };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async function fetchInitialGameContext(client: GameClient): Promise<{
|
|
363
|
+
state: any | null;
|
|
364
|
+
role: any | null;
|
|
365
|
+
map: any | null;
|
|
366
|
+
tasks: any[] | null;
|
|
367
|
+
errors?: Array<{ command: string; error: string }>;
|
|
368
|
+
}> {
|
|
369
|
+
await sleep(500);
|
|
370
|
+
const [stateResult, roleResult, mapResult] = await Promise.allSettled([
|
|
371
|
+
client.getGameState(),
|
|
372
|
+
client.getRoleInfo(),
|
|
373
|
+
client.getMap(),
|
|
374
|
+
]);
|
|
375
|
+
|
|
376
|
+
const errors: Array<{ command: string; error: string }> = [];
|
|
377
|
+
if (stateResult.status === 'rejected') errors.push(commandError('state', stateResult.reason));
|
|
378
|
+
if (roleResult.status === 'rejected') errors.push(commandError('game role', roleResult.reason));
|
|
379
|
+
if (mapResult.status === 'rejected') errors.push(commandError('game map', mapResult.reason));
|
|
380
|
+
|
|
381
|
+
const mapData = mapResult.status === 'fulfilled' ? mapResult.value : null;
|
|
382
|
+
const context = {
|
|
383
|
+
state: stateResult.status === 'fulfilled' ? stateResult.value : null,
|
|
384
|
+
role: roleResult.status === 'fulfilled' ? roleResult.value : null,
|
|
385
|
+
map: briefGameMap(mapData),
|
|
386
|
+
tasks: mapData?.your_tasks ?? null,
|
|
387
|
+
...(errors.length ? { errors } : {}),
|
|
388
|
+
};
|
|
389
|
+
return context;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const ROLE_DEFAULT_STRATEGY: Record<string, string> = {
|
|
393
|
+
'shrimp_generic': 'task-report',
|
|
394
|
+
'shrimp_warrior': 'task-report',
|
|
395
|
+
'shrimp_pistol': 'task-report',
|
|
396
|
+
'crab_generic': 'crab-sabotage',
|
|
397
|
+
'neutral_paradise_fish': 'corpse-patrol',
|
|
398
|
+
'neutral_octopus': 'lone-kill-task',
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
function autoStartStrategy(roleData: any, stateData?: any, gameId?: string): { strategy: string; pid: number | undefined; child: ChildProcess } | null {
|
|
402
|
+
const roleId: string | undefined = roleData?.data?.role ?? roleData?.role;
|
|
403
|
+
if (!roleId) return null;
|
|
404
|
+
const strategyId = ROLE_DEFAULT_STRATEGY[roleId];
|
|
405
|
+
if (!strategyId) return null;
|
|
406
|
+
const child = spawnStrategyLoop(strategyId, [roleId], {
|
|
407
|
+
source: 'auto_start',
|
|
408
|
+
gameId: gameId ?? gameStrategyIdentity(stateData, roleData).gameId,
|
|
409
|
+
role: roleId,
|
|
410
|
+
detached: false,
|
|
411
|
+
writeRuntimeFiles: false,
|
|
412
|
+
});
|
|
413
|
+
return { strategy: strategyId, pid: child.pid, child };
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async function runGameQuit(invokedAs = 'game quit'): Promise<void> {
|
|
417
|
+
const authStore = new AuthStore();
|
|
418
|
+
const profile = authStore.getActive();
|
|
419
|
+
if (!profile) throw new Error('Not logged in.');
|
|
420
|
+
|
|
421
|
+
const stateDir = getProfileStateDir(profile);
|
|
422
|
+
const client = GameClient.fromAuth();
|
|
423
|
+
const presence = await detectGamePresence(client, stateDir);
|
|
424
|
+
let left: 'game' | 'queue' | 'local_runtime' | 'none' = presence.owner_pid ? 'local_runtime' : 'none';
|
|
425
|
+
let gameResult: any;
|
|
426
|
+
let queueResult: any;
|
|
427
|
+
let leaveError: Record<string, any> | undefined;
|
|
428
|
+
|
|
429
|
+
if (presence.presence === 'in_game') {
|
|
430
|
+
try {
|
|
431
|
+
gameResult = await client.leaveGame();
|
|
432
|
+
if (isLeaveGameSuccess(gameResult)) clearCachedGameServerUrl(authStore, profile.agentName);
|
|
433
|
+
left = 'game';
|
|
434
|
+
} catch (err) {
|
|
435
|
+
leaveError = {
|
|
436
|
+
...serializeError(err),
|
|
437
|
+
still_alive: isStillAliveError(err),
|
|
438
|
+
};
|
|
439
|
+
left = 'local_runtime';
|
|
440
|
+
}
|
|
441
|
+
} else if (presence.presence === 'matching') {
|
|
442
|
+
try {
|
|
443
|
+
queueResult = await client.leaveQueue('clawclaw');
|
|
444
|
+
left = 'queue';
|
|
445
|
+
} catch (err) {
|
|
446
|
+
leaveError = serializeError(err);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
endMatch(stateDir);
|
|
451
|
+
const owner = await stopOwnerWithCommand(stateDir, 'quit');
|
|
452
|
+
stopStrategyIfRunning();
|
|
453
|
+
|
|
454
|
+
const reminder = hubReminder(readCachedGamesPlayed());
|
|
455
|
+
const out: Record<string, any> = cleanObject({
|
|
456
|
+
ok: true,
|
|
457
|
+
command: invokedAs,
|
|
458
|
+
left,
|
|
459
|
+
presence: presence.presence,
|
|
460
|
+
game_result: gameResult,
|
|
461
|
+
queue_result: queueResult,
|
|
462
|
+
leave_error: leaveError,
|
|
463
|
+
detection_errors: presence.errors,
|
|
464
|
+
queue_status: presence.queue_status,
|
|
465
|
+
owner_pid: owner.pid ?? presence.owner_pid ?? undefined,
|
|
466
|
+
owner_stopped: owner.stopped,
|
|
467
|
+
hub_reminder: reminder,
|
|
468
|
+
next_step: reminder ? `Stopped local runtime. ${reminder}` : undefined,
|
|
469
|
+
});
|
|
470
|
+
if (presence.presence === 'in_game' && leaveError?.still_alive) {
|
|
471
|
+
out.message = 'The server rejected leaving the active game because you are still alive; local runtime was stopped only.';
|
|
472
|
+
}
|
|
473
|
+
console.log(JSON.stringify(out, null, 2));
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async function runGameStart(opts: { force?: boolean; channelUrl?: string }): Promise<void> {
|
|
477
|
+
if (process.env.CLAWCLAW_REQUIRE_STREAM_TOOL === '1' && process.env.CLAWCLAW_STREAMED !== '1') {
|
|
478
|
+
process.stderr.write(
|
|
479
|
+
'In OpenClaw, start the game via the clawclaw_game_start tool - do not exec `ccl game start` directly.\n' +
|
|
480
|
+
'When run raw, the event NDJSON only fills an unconsumed buffer, so you never receive speech_your_turn and stay silent.\n',
|
|
481
|
+
);
|
|
482
|
+
process.exit(2);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
const authStore = new AuthStore();
|
|
486
|
+
const profile = authStore.getActive();
|
|
487
|
+
if (!profile) throw new Error('Not logged in.');
|
|
488
|
+
|
|
489
|
+
const stateDir = getProfileStateDir(profile);
|
|
490
|
+
const feedPath = join(stateDir, 'feed.json');
|
|
491
|
+
clearCachedGameServerUrl(authStore, profile.agentName);
|
|
492
|
+
const client = GameClient.fromAuth();
|
|
493
|
+
let eventRuntime: EventRuntime | undefined;
|
|
494
|
+
let streamAbortController: AbortController | null = null;
|
|
495
|
+
let heartbeatTimer: ReturnType<typeof setInterval> | null = null;
|
|
496
|
+
let ownerControl: OwnerControlServer | null = null;
|
|
497
|
+
let ownerFeed: any = {
|
|
498
|
+
ts: new Date().toISOString(),
|
|
499
|
+
phase: 'matching',
|
|
500
|
+
terminal: false,
|
|
501
|
+
you: { name: profile.agentName },
|
|
502
|
+
game: {},
|
|
503
|
+
urgent: {},
|
|
504
|
+
meeting: null,
|
|
505
|
+
recent_events: [],
|
|
506
|
+
};
|
|
507
|
+
let strategyChild: ChildProcess | null = null;
|
|
508
|
+
let currentStrategy: string | null = null;
|
|
509
|
+
const currentAutomationSummary = (): Record<string, any> | undefined => {
|
|
510
|
+
if (!currentStrategy) return undefined;
|
|
511
|
+
const pid = strategyChild?.pid;
|
|
512
|
+
return { strategy: currentStrategy, running: !!(pid && isPidAlive(pid)) };
|
|
513
|
+
};
|
|
514
|
+
const currentSummary = (): any | null => {
|
|
515
|
+
const runtimeFeed = eventRuntime?.snapshot();
|
|
516
|
+
const feed = runtimeFeed && runtimeFeed.phase !== 'lobby' ? runtimeFeed : ownerFeed;
|
|
517
|
+
const automation = currentAutomationSummary();
|
|
518
|
+
return summarizeFeed(automation ? { ...feed, automation } : feed);
|
|
519
|
+
};
|
|
520
|
+
let manualExitEmitted = false;
|
|
521
|
+
let ownerExitRequested = false;
|
|
522
|
+
const stopOwnedStrategy = (): void => {
|
|
523
|
+
const child = strategyChild;
|
|
524
|
+
strategyChild = null;
|
|
525
|
+
currentStrategy = null;
|
|
526
|
+
if (child?.pid && isPidAlive(child.pid)) {
|
|
527
|
+
try { child.kill('SIGTERM'); } catch {}
|
|
528
|
+
setTimeout(() => {
|
|
529
|
+
if (child.pid && isPidAlive(child.pid)) {
|
|
530
|
+
try { child.kill('SIGKILL'); } catch {}
|
|
531
|
+
}
|
|
532
|
+
}, 1000).unref();
|
|
533
|
+
}
|
|
534
|
+
eventRuntime?.refreshFeed();
|
|
535
|
+
};
|
|
536
|
+
const onOwnerSignal = (): void => {
|
|
537
|
+
streamAbortController?.abort();
|
|
538
|
+
eventRuntime?.stop('SIGTERM');
|
|
539
|
+
stopOwnedStrategy();
|
|
540
|
+
cleanupGameStartRuntime(stateDir, { removeFeed: true, controlPath: ownerControl?.control.path });
|
|
541
|
+
process.exit(130);
|
|
542
|
+
};
|
|
543
|
+
process.on('SIGINT', onOwnerSignal);
|
|
544
|
+
process.on('SIGTERM', onOwnerSignal);
|
|
545
|
+
const emit = (obj: Record<string, any>): void => {
|
|
546
|
+
const line = JSON.stringify(obj) + '\n';
|
|
547
|
+
process.stdout.write(line);
|
|
548
|
+
if (opts.channelUrl) {
|
|
549
|
+
fetch(opts.channelUrl, { method: 'POST', body: line }).catch(() => {});
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
const emitLifecycle = (
|
|
553
|
+
reason: string,
|
|
554
|
+
payload: Record<string, any>,
|
|
555
|
+
nextStepOverride?: string,
|
|
556
|
+
): void => {
|
|
557
|
+
const event = { ...payload, type: reason };
|
|
558
|
+
emit({
|
|
559
|
+
exit_reason: [reason],
|
|
560
|
+
next_step: nextStepOverride ?? nextStepFor(reason),
|
|
561
|
+
events: [event],
|
|
562
|
+
summary: currentSummary(),
|
|
563
|
+
});
|
|
564
|
+
};
|
|
565
|
+
const emitOwnerExit = (kind: 'stop' | 'quit' | 'leave', command: string): void => {
|
|
566
|
+
if (manualExitEmitted) return;
|
|
567
|
+
manualExitEmitted = true;
|
|
568
|
+
const eventType = kind === 'leave' ? 'stop' : kind;
|
|
569
|
+
emit({
|
|
570
|
+
exit_reason: [eventType],
|
|
571
|
+
next_step: `Received ${command}. The current ccl game start process is exiting now.`,
|
|
572
|
+
events: [{ type: eventType, command }],
|
|
573
|
+
summary: { phase: 'stopped' },
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
const requestOwnerExit = (kind: 'stop' | 'quit' | 'leave', command: string): void => {
|
|
577
|
+
ownerExitRequested = true;
|
|
578
|
+
emitOwnerExit(kind, command);
|
|
579
|
+
streamAbortController?.abort();
|
|
580
|
+
eventRuntime?.stop('manual');
|
|
581
|
+
stopOwnedStrategy();
|
|
582
|
+
};
|
|
583
|
+
const emitMatchEvent = (evt: Record<string, any>): void => {
|
|
584
|
+
try {
|
|
585
|
+
const store = EventStore.forActiveAccount();
|
|
586
|
+
store.append({ ts: new Date().toISOString(), ...evt });
|
|
587
|
+
} catch {}
|
|
588
|
+
};
|
|
589
|
+
const ensureEventRuntime = async (): Promise<void> => {
|
|
590
|
+
if (eventRuntime) return;
|
|
591
|
+
eventRuntime = new EventRuntime({
|
|
592
|
+
authStore,
|
|
593
|
+
getAutomation: currentAutomationSummary,
|
|
594
|
+
onStop: (stop) => {
|
|
595
|
+
if (stop.reason === 'game_over' || stop.reason === 'user_left_game') return;
|
|
596
|
+
streamAbortController?.abort();
|
|
597
|
+
},
|
|
598
|
+
});
|
|
599
|
+
await eventRuntime.start();
|
|
600
|
+
};
|
|
601
|
+
const streamGame = async (): Promise<void> => {
|
|
602
|
+
const sessionPath = EventStore.latestSessionPath();
|
|
603
|
+
const ctrl = new AbortController();
|
|
604
|
+
streamAbortController = ctrl;
|
|
605
|
+
const onSignal = (): void => {
|
|
606
|
+
ctrl.abort();
|
|
607
|
+
eventRuntime?.stop('SIGTERM');
|
|
608
|
+
};
|
|
609
|
+
process.on('SIGINT', onSignal);
|
|
610
|
+
process.on('SIGTERM', onSignal);
|
|
611
|
+
try {
|
|
612
|
+
await runStreaming({
|
|
613
|
+
feedPath,
|
|
614
|
+
sessionPath,
|
|
615
|
+
getSessionPath: () => EventStore.latestSessionPath(),
|
|
616
|
+
stdout: (s) => { process.stdout.write(s); if (opts.channelUrl) { fetch(opts.channelUrl, { method: 'POST', body: s }).catch(() => {}); } },
|
|
617
|
+
signal: ctrl.signal,
|
|
618
|
+
skipFeedWait: true,
|
|
619
|
+
readSummary: currentSummary,
|
|
620
|
+
skipBacklogTypes: ['match_waiting', 'match_timeout'],
|
|
621
|
+
emitGameStart: true,
|
|
622
|
+
hubReminder: hubReminder(readCachedGamesPlayed()),
|
|
623
|
+
});
|
|
624
|
+
} catch (err: any) {
|
|
625
|
+
process.stdout.write(buildErrorLine(err));
|
|
626
|
+
process.exitCode = 1;
|
|
627
|
+
} finally {
|
|
628
|
+
process.off('SIGINT', onSignal);
|
|
629
|
+
process.off('SIGTERM', onSignal);
|
|
630
|
+
streamAbortController = null;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
const handleAllocated = async (queue: any, preserveStrategy: boolean): Promise<void> => {
|
|
634
|
+
const finalState = readMatchState(stateDir);
|
|
635
|
+
const waitedSecs = finalState ? getWaitedSecs(finalState) : 0;
|
|
636
|
+
endMatch(stateDir);
|
|
637
|
+
const context = await fetchInitialGameContext(client);
|
|
638
|
+
const identity = gameStrategyIdentity(context.state, context.role);
|
|
639
|
+
// Supplement game_id from queue response when /game/current doesn't carry it yet.
|
|
640
|
+
const queueGameId = nonEmptyString(queue?.game_id) ?? nonEmptyString(queue?.data?.game_id);
|
|
641
|
+
if (!identity.gameId && queueGameId) {
|
|
642
|
+
identity.gameId = queueGameId;
|
|
643
|
+
}
|
|
644
|
+
const role = unwrapData(context.role);
|
|
645
|
+
ownerFeed = {
|
|
646
|
+
...ownerFeed,
|
|
647
|
+
ts: new Date().toISOString(),
|
|
648
|
+
phase: 'allocated',
|
|
649
|
+
you: {
|
|
650
|
+
...ownerFeed.you,
|
|
651
|
+
role: nonEmptyString(role?.role) ?? identity.role,
|
|
652
|
+
role_display: nonEmptyString(role?.role_display_name) ?? nonEmptyString(role?.role_display),
|
|
653
|
+
faction: nonEmptyString(role?.faction),
|
|
654
|
+
alive: identity.alive,
|
|
655
|
+
},
|
|
656
|
+
game: {
|
|
657
|
+
...ownerFeed.game,
|
|
658
|
+
game_id: identity.gameId,
|
|
659
|
+
},
|
|
660
|
+
};
|
|
661
|
+
let strategyInfo: { strategy: string; pid: number | undefined } | null = null;
|
|
662
|
+
if (identity.alive !== false) {
|
|
663
|
+
stopOwnedStrategy();
|
|
664
|
+
const started = autoStartStrategy(context.role, context.state, identity.gameId);
|
|
665
|
+
if (started) {
|
|
666
|
+
strategyChild = started.child;
|
|
667
|
+
currentStrategy = started.strategy;
|
|
668
|
+
strategyInfo = { strategy: started.strategy, pid: started.pid };
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const allocationPayload = {
|
|
672
|
+
queue,
|
|
673
|
+
waited_secs: waitedSecs,
|
|
674
|
+
game_id: identity.gameId || undefined,
|
|
675
|
+
...context,
|
|
676
|
+
...(strategyInfo ? { default_strategy: strategyInfo } : {}),
|
|
677
|
+
};
|
|
678
|
+
ownerFeed = { ...ownerFeed, allocation: allocationPayload };
|
|
679
|
+
await ensureEventRuntime();
|
|
680
|
+
await streamGame();
|
|
681
|
+
};
|
|
682
|
+
const pollQueue = async (preserveStrategy: boolean): Promise<void> => {
|
|
683
|
+
const intervalMs = 2000;
|
|
684
|
+
const QUEUE_POLL_HEARTBEAT_MS = 60_000;
|
|
685
|
+
const MAX_CONSECUTIVE_FAILURES = 3;
|
|
686
|
+
let lastHeartbeat = Date.now();
|
|
687
|
+
let consecutiveFailures = 0;
|
|
688
|
+
|
|
689
|
+
while (!ownerExitRequested) {
|
|
690
|
+
let queue: any;
|
|
691
|
+
try {
|
|
692
|
+
queue = await client.getQueueStatus('clawclaw');
|
|
693
|
+
consecutiveFailures = 0;
|
|
694
|
+
} catch (err: any) {
|
|
695
|
+
consecutiveFailures++;
|
|
696
|
+
const msg = err?.message ?? String(err);
|
|
697
|
+
if (consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
|
|
698
|
+
emitLifecycle('error', {
|
|
699
|
+
error: msg,
|
|
700
|
+
consecutive_failures: consecutiveFailures,
|
|
701
|
+
message: `Queue status polling failed ${consecutiveFailures} times consecutively.`,
|
|
702
|
+
}, 'Queue polling has failed repeatedly. Check network, then launch a fresh `ccl game start` to retry.');
|
|
703
|
+
return;
|
|
704
|
+
}
|
|
705
|
+
emitLifecycle('poll_error', {
|
|
706
|
+
error: msg,
|
|
707
|
+
consecutive_failures: consecutiveFailures,
|
|
708
|
+
message: `Queue poll failed (${consecutiveFailures}/${MAX_CONSECUTIVE_FAILURES}). Retrying...`,
|
|
709
|
+
}, 'Temporary polling failure. The stream will keep retrying - stay attached.');
|
|
710
|
+
await sleep(intervalMs);
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
if (queueStatus(queue) === 'allocated') {
|
|
715
|
+
await handleAllocated(queue, preserveStrategy);
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
if (queueStatus(queue) === 'not_in_queue') {
|
|
719
|
+
endMatch(stateDir);
|
|
720
|
+
emitLifecycle('not_in_queue', { queue, message: 'Left or dropped from queue.' },
|
|
721
|
+
'No longer in matchmaking queue. The stream will exit; launch a fresh `ccl game start` to retry if the user wants to continue.');
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const cur = readMatchState(stateDir);
|
|
726
|
+
if (cur && shouldEmitWaiting(cur)) {
|
|
727
|
+
const waitedSecs = getWaitedSecs(cur);
|
|
728
|
+
emitMatchEvent({ type: 'match_waiting', waited_secs: waitedSecs });
|
|
729
|
+
emitLifecycle('match_waiting', { waited_secs: waitedSecs });
|
|
730
|
+
markWaitingEmitted(stateDir);
|
|
731
|
+
lastHeartbeat = Date.now();
|
|
732
|
+
}
|
|
733
|
+
if (cur && hasMatchTimedOut(cur)) {
|
|
734
|
+
const waitedSecs = getWaitedSecs(cur);
|
|
735
|
+
emitMatchEvent({ type: 'match_timeout', waited_secs: waitedSecs });
|
|
736
|
+
emitLifecycle('match_timeout', {
|
|
737
|
+
waited_secs: waitedSecs,
|
|
738
|
+
queue,
|
|
739
|
+
message: `No match after ${waitedSecs}s.`,
|
|
740
|
+
});
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
if (Date.now() - lastHeartbeat >= QUEUE_POLL_HEARTBEAT_MS) {
|
|
745
|
+
lastHeartbeat = Date.now();
|
|
746
|
+
const waitedSecs = cur ? getWaitedSecs(cur) : 0;
|
|
747
|
+
emitLifecycle('heartbeat', { waited_secs: waitedSecs },
|
|
748
|
+
'Stream is alive; still waiting for match allocation. Keep chatting with the user.');
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
await sleep(intervalMs);
|
|
752
|
+
}
|
|
753
|
+
};
|
|
754
|
+
const resumeQueue = async (source: string): Promise<void> => {
|
|
755
|
+
ensureEventSession(source);
|
|
756
|
+
if (!readMatchState(stateDir)) startMatch(stateDir);
|
|
757
|
+
await ensureEventRuntime();
|
|
758
|
+
await pollQueue(true);
|
|
759
|
+
};
|
|
760
|
+
const recoverAlreadyInGame = async (): Promise<boolean> => {
|
|
761
|
+
let queue: any;
|
|
762
|
+
try {
|
|
763
|
+
queue = await client.getQueueStatus('clawclaw');
|
|
764
|
+
} catch {
|
|
765
|
+
return false;
|
|
766
|
+
}
|
|
767
|
+
const status = queueStatus(queue);
|
|
768
|
+
if (status === 'allocated') {
|
|
769
|
+
ensureEventSession('game_start_already_in_game');
|
|
770
|
+
await ensureEventRuntime();
|
|
771
|
+
await handleAllocated(queue, true);
|
|
772
|
+
return true;
|
|
773
|
+
}
|
|
774
|
+
if (status === 'queued' || status === 'already_in_queue') {
|
|
775
|
+
await resumeQueue('game_start_already_in_game_queue');
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
return false;
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const runningGameStartPid = getRunningGameStartPid(stateDir);
|
|
782
|
+
if (runningGameStartPid) {
|
|
783
|
+
if (opts.force) {
|
|
784
|
+
terminateProcessTree(runningGameStartPid, 'SIGKILL');
|
|
785
|
+
try { unlinkSync(gameStartRuntimePath(stateDir)); } catch {}
|
|
786
|
+
try { unlinkSync(feedPath); } catch {}
|
|
787
|
+
} else {
|
|
788
|
+
emitLifecycle('already_running', { pid: runningGameStartPid },
|
|
789
|
+
`A ccl game start stream is already running (pid ${runningGameStartPid}). To replace it, re-run with --force or stop it manually: taskkill /F /PID ${runningGameStartPid}`);
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
let initialQueue: any;
|
|
794
|
+
try {
|
|
795
|
+
initialQueue = await client.getQueueStatus('clawclaw');
|
|
796
|
+
} catch {}
|
|
797
|
+
const plan = planGameStartAction({
|
|
798
|
+
gameStartPid: null,
|
|
799
|
+
hasMatchState: readMatchState(stateDir) !== null,
|
|
800
|
+
queueStatus: queueStatus(initialQueue),
|
|
801
|
+
});
|
|
802
|
+
|
|
803
|
+
ownerFeed = {
|
|
804
|
+
...ownerFeed,
|
|
805
|
+
ts: new Date().toISOString(),
|
|
806
|
+
phase: plan.kind === 'resume_allocated' ? 'allocated' : 'matching',
|
|
807
|
+
};
|
|
808
|
+
ownerControl = await startOwnerControlServer(stateDir, async (request) => {
|
|
809
|
+
if (request.type === 'snapshot') {
|
|
810
|
+
return { ok: true, type: 'snapshot', summary: currentSummary() };
|
|
811
|
+
}
|
|
812
|
+
if (request.type === 'stop') {
|
|
813
|
+
requestOwnerExit('stop', 'ccl game stop');
|
|
814
|
+
return { ok: true, type: 'stop' };
|
|
815
|
+
}
|
|
816
|
+
if (request.type === 'quit') {
|
|
817
|
+
requestOwnerExit('quit', 'ccl game quit');
|
|
818
|
+
return { ok: true, type: 'quit' };
|
|
819
|
+
}
|
|
820
|
+
if (request.type === 'leave') {
|
|
821
|
+
requestOwnerExit('leave', 'ccl game leave');
|
|
822
|
+
return { ok: true, type: 'leave' };
|
|
823
|
+
}
|
|
824
|
+
if (request.type === 'stop_strategy') {
|
|
825
|
+
stopOwnedStrategy();
|
|
826
|
+
return { ok: true, type: 'stop_strategy' };
|
|
827
|
+
}
|
|
828
|
+
if (request.type === 'switch_strategy') {
|
|
829
|
+
if (!request.strategy) return { ok: false, error: 'missing_strategy' };
|
|
830
|
+
stopOwnedStrategy();
|
|
831
|
+
const child = spawnStrategyLoop(request.strategy, request.args, {
|
|
832
|
+
source: 'manual',
|
|
833
|
+
detached: false,
|
|
834
|
+
writeRuntimeFiles: false,
|
|
835
|
+
});
|
|
836
|
+
strategyChild = child;
|
|
837
|
+
currentStrategy = request.strategy;
|
|
838
|
+
eventRuntime?.refreshFeed();
|
|
839
|
+
return { ok: true, type: 'switch_strategy', strategy: request.strategy, pid: child.pid };
|
|
840
|
+
}
|
|
841
|
+
return { ok: false, error: 'unsupported_owner_control_request' };
|
|
842
|
+
});
|
|
843
|
+
writeGameStartRuntime(stateDir, plan.kind, undefined, {
|
|
844
|
+
control: ownerControl.control,
|
|
845
|
+
token: ownerControl.token,
|
|
846
|
+
});
|
|
847
|
+
heartbeatTimer = startGameStartHeartbeat(stateDir, plan.kind);
|
|
848
|
+
try {
|
|
849
|
+
if (plan.kind === 'resume_queue') {
|
|
850
|
+
await resumeQueue('game_start_resume_queue');
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
if (plan.kind === 'resume_allocated') {
|
|
855
|
+
ensureEventSession('game_start_resume_allocated');
|
|
856
|
+
await ensureEventRuntime();
|
|
857
|
+
await handleAllocated(initialQueue, true);
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
let joinResult: any;
|
|
862
|
+
try {
|
|
863
|
+
joinResult = await client.joinQueue('clawclaw');
|
|
864
|
+
} catch (err) {
|
|
865
|
+
if (isAlreadyInGameError(err) && await recoverAlreadyInGame()) return;
|
|
866
|
+
throw err;
|
|
867
|
+
}
|
|
868
|
+
const joinedStatus = queueStatus(joinResult);
|
|
869
|
+
if (joinedStatus === 'already_in_queue') {
|
|
870
|
+
await resumeQueue('game_start_already_in_queue');
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
if (joinedStatus && joinedStatus !== 'queued') {
|
|
874
|
+
emitLifecycle('error', { queue: joinResult, message: `Unexpected queue status: ${joinedStatus}.` },
|
|
875
|
+
'Queue join did not enter the clawclaw queue. Check queue status before launching another `ccl game start`.');
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
stopOwnedStrategy();
|
|
880
|
+
|
|
881
|
+
const events = EventStore.createSessionForActiveAccount();
|
|
882
|
+
events.append({ type: 'session_started', source: 'game_start' });
|
|
883
|
+
emitLifecycle('joined', joinResult,
|
|
884
|
+
'Match join request acknowledged. Spectate URL is in `events[0].url`; share it with the user. Game runtime is attached.');
|
|
885
|
+
startMatch(stateDir);
|
|
886
|
+
await ensureEventRuntime();
|
|
887
|
+
await pollQueue(false);
|
|
888
|
+
} finally {
|
|
889
|
+
if (heartbeatTimer) clearInterval(heartbeatTimer);
|
|
890
|
+
process.off('SIGINT', onOwnerSignal);
|
|
891
|
+
process.off('SIGTERM', onOwnerSignal);
|
|
892
|
+
eventRuntime?.stop('manual');
|
|
893
|
+
stopOwnedStrategy();
|
|
894
|
+
if (ownerControl) await ownerControl.close();
|
|
895
|
+
cleanupGameStartRuntime(stateDir, { removeFeed: true, controlPath: ownerControl?.control.path });
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
export function createGameCommand(): Command {
|
|
900
|
+
const game = new Command('game');
|
|
901
|
+
game.description('Game matchmaking & session');
|
|
902
|
+
|
|
903
|
+
game
|
|
904
|
+
.command('join', { hidden: true })
|
|
905
|
+
.alias('j')
|
|
906
|
+
.description('Join matchmaking queue')
|
|
907
|
+
.action(async () => {
|
|
908
|
+
const authStore = new AuthStore();
|
|
909
|
+
const profile = authStore.getActive();
|
|
910
|
+
if (!profile) throw new Error('Not logged in.');
|
|
911
|
+
stopStrategyIfRunning();
|
|
912
|
+
|
|
913
|
+
const client = GameClient.fromAuth();
|
|
914
|
+
const result = await client.joinQueue('clawclaw');
|
|
915
|
+
const events = EventStore.createSessionForActiveAccount();
|
|
916
|
+
events.append({ type: 'session_started', source: 'game_join' });
|
|
917
|
+
console.log(JSON.stringify(result, null, 2));
|
|
918
|
+
const stateDir = getProfileStateDir(profile);
|
|
919
|
+
// Reset match-state: a fresh queue session starts here so `ccl game queue`
|
|
920
|
+
// can compute `waited_secs` from a known anchor and emit the
|
|
921
|
+
// match_waiting / match_timeout synthetic events.
|
|
922
|
+
startMatch(stateDir);
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
game
|
|
926
|
+
.command('start')
|
|
927
|
+
.alias('s')
|
|
928
|
+
.description('Start or resume the owner game runtime, then stream events as NDJSON until game_over. Pass --force to replace an orphaned game-start runtime.')
|
|
929
|
+
.option('--force', 'force restart: kill any lingering game-start stream process and start fresh')
|
|
930
|
+
.option('--channel-url <url>', 'forward each NDJSON line via HTTP POST to this URL')
|
|
931
|
+
.action(runGameStart);
|
|
932
|
+
|
|
933
|
+
game
|
|
934
|
+
.command('queue', { hidden: true })
|
|
935
|
+
.alias('q')
|
|
936
|
+
.description('Wait for matchmaking allocation. Always run as a background bash task (run_in_background: true) — never block on it. Returns when allocated, queue is missing, or timeout (default 30s).')
|
|
937
|
+
.option('-w, --wait', '(no-op, kept for backwards compatibility — wait mode is now the default)')
|
|
938
|
+
.option('--interval <secs>', 'Polling interval', '2')
|
|
939
|
+
.option('--timeout <secs>', 'Max seconds to wait, 0 means forever', String(DEFAULT_QUEUE_WAIT_TIMEOUT_SECS))
|
|
940
|
+
.action(async (opts) => {
|
|
941
|
+
const authStore = new AuthStore();
|
|
942
|
+
const profile = authStore.getActive();
|
|
943
|
+
if (!profile) throw new Error('Not logged in.');
|
|
944
|
+
const stateDir = getProfileStateDir(profile);
|
|
945
|
+
const client = GameClient.fromAuth();
|
|
946
|
+
|
|
947
|
+
const intervalMs = positiveNumber(opts.interval, 2) * 1000;
|
|
948
|
+
const timeoutSecs = Number(opts.timeout);
|
|
949
|
+
const deadline = Number.isFinite(timeoutSecs) && timeoutSecs > 0
|
|
950
|
+
? Date.now() + timeoutSecs * 1000
|
|
951
|
+
: 0;
|
|
952
|
+
|
|
953
|
+
// Synthetic matchmaking events feed the game start stream so the agent can
|
|
954
|
+
// observe match progress through the same channel as in-game events.
|
|
955
|
+
// Concurrency note: the agent typically keeps a 30s `ccl game queue`
|
|
956
|
+
// background chain running, so two queue invocations may observe the
|
|
957
|
+
// same `allocated` server state. That is
|
|
958
|
+
// intentionally fine — stream dedups via `eventKey` (type+tick+actor),
|
|
959
|
+
// so consumers only see one notification.
|
|
960
|
+
const emitMatchEvent = (evt: Record<string, any>): void => {
|
|
961
|
+
try {
|
|
962
|
+
const store = EventStore.forActiveAccount();
|
|
963
|
+
store.append({ ts: new Date().toISOString(), ...evt });
|
|
964
|
+
} catch {
|
|
965
|
+
// No active session (user ran queue without join) — skip silently;
|
|
966
|
+
// queue's own JSON return still informs the caller.
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
|
|
970
|
+
// On entry: emit match_waiting if state exists and heartbeat is due.
|
|
971
|
+
const initialState = readMatchState(stateDir);
|
|
972
|
+
if (initialState && shouldEmitWaiting(initialState)) {
|
|
973
|
+
const waitedSecs = getWaitedSecs(initialState);
|
|
974
|
+
emitMatchEvent({ type: 'match_waiting', waited_secs: waitedSecs });
|
|
975
|
+
markWaitingEmitted(stateDir);
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
while (true) {
|
|
979
|
+
const queue = await client.getQueueStatus('clawclaw');
|
|
980
|
+
if (queueStatus(queue) === 'allocated') {
|
|
981
|
+
const finalState = readMatchState(stateDir);
|
|
982
|
+
const waitedSecs = finalState ? getWaitedSecs(finalState) : 0;
|
|
983
|
+
endMatch(stateDir);
|
|
984
|
+
const context = await fetchInitialGameContext(client);
|
|
985
|
+
console.log(JSON.stringify({
|
|
986
|
+
status: 'active_game',
|
|
987
|
+
queue,
|
|
988
|
+
waited_secs: waitedSecs,
|
|
989
|
+
...context,
|
|
990
|
+
next_step: 'Game allocated. Launch `ccl game start` to attach the owner stream if it is not already running, then narrate the role / map / opening plan to the user.',
|
|
991
|
+
}, null, 2));
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
if (queueStatus(queue) === 'not_in_queue') {
|
|
995
|
+
// Queue session is gone (user manually left or server expired it):
|
|
996
|
+
// clean the match-state so next `game join` starts fresh.
|
|
997
|
+
endMatch(stateDir);
|
|
998
|
+
console.log(JSON.stringify({
|
|
999
|
+
status: 'not_in_queue',
|
|
1000
|
+
queue,
|
|
1001
|
+
message: 'You are not in the matchmaking queue. Did you forget to run `ccl game join` first?',
|
|
1002
|
+
next_step: 'Run `ccl game join` to re-enter the queue, then launch the next `ccl game queue` background task.',
|
|
1003
|
+
}, null, 2));
|
|
1004
|
+
return;
|
|
1005
|
+
}
|
|
1006
|
+
if (deadline > 0 && Date.now() >= deadline) {
|
|
1007
|
+
const cur = readMatchState(stateDir);
|
|
1008
|
+
const waitedSecs = cur ? getWaitedSecs(cur) : timeoutSecs;
|
|
1009
|
+
// Total-wait timeout (>=10 min by default) gets its own synthetic
|
|
1010
|
+
// event so the agent can prompt the user to keep waiting / leave /
|
|
1011
|
+
// retry later. Single 30s queue-attempt timeouts do NOT emit anything
|
|
1012
|
+
// — they are an internal polling boundary, not a user-visible event.
|
|
1013
|
+
if (cur && hasMatchTimedOut(cur)) {
|
|
1014
|
+
emitMatchEvent({ type: 'match_timeout', waited_secs: waitedSecs });
|
|
1015
|
+
}
|
|
1016
|
+
console.log(JSON.stringify({
|
|
1017
|
+
status: 'timeout',
|
|
1018
|
+
waited_secs: waitedSecs,
|
|
1019
|
+
queue,
|
|
1020
|
+
message: `No match after ${timeoutSecs} seconds (total wait ${waitedSecs}s).`,
|
|
1021
|
+
next_step: 'ZERO-GAP PROTOCOL: (1) Launch the next `ccl game queue` background task IMMEDIATELY. (2) While it runs, chat with the user — share strategy, persona, banter — never go silent.',
|
|
1022
|
+
}, null, 2));
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
await sleep(intervalMs);
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
game
|
|
1030
|
+
.command('leave', { hidden: true })
|
|
1031
|
+
.alias('l')
|
|
1032
|
+
.description('Deprecated alias for quit')
|
|
1033
|
+
.action(async () => runGameQuit('game leave'));
|
|
1034
|
+
|
|
1035
|
+
|
|
1036
|
+
game
|
|
1037
|
+
.command('stop', { hidden: true })
|
|
1038
|
+
.alias('x')
|
|
1039
|
+
.description('Stop local game runtime')
|
|
1040
|
+
.action(async () => {
|
|
1041
|
+
const authStore = new AuthStore();
|
|
1042
|
+
const profile = authStore.getActive();
|
|
1043
|
+
if (!profile) throw new Error('Not logged in.');
|
|
1044
|
+
const stateDir = getProfileStateDir(profile);
|
|
1045
|
+
const owner = await stopOwnerIfRunning(stateDir);
|
|
1046
|
+
stopStrategyIfRunning();
|
|
1047
|
+
if (!owner.stopped) {
|
|
1048
|
+
console.log(JSON.stringify({ message: 'No game runtime running.' }, null, 2));
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
console.log(JSON.stringify({
|
|
1052
|
+
message: 'Game runtime stopped.',
|
|
1053
|
+
...(owner.pid ? { owner_pid: owner.pid } : {}),
|
|
1054
|
+
}, null, 2));
|
|
1055
|
+
});
|
|
1056
|
+
|
|
1057
|
+
game
|
|
1058
|
+
.command('quit')
|
|
1059
|
+
.description('Leave active game and stop local runtime')
|
|
1060
|
+
.action(async () => runGameQuit('game quit'));
|
|
1061
|
+
|
|
1062
|
+
game
|
|
1063
|
+
.command('map', { hidden: true })
|
|
1064
|
+
.alias('m')
|
|
1065
|
+
.description('Show game map')
|
|
1066
|
+
.option('--ascii', 'Include packaged ASCII topology map')
|
|
1067
|
+
.action(async (opts) => {
|
|
1068
|
+
const client = GameClient.fromAuth();
|
|
1069
|
+
await client.discoverGameServer();
|
|
1070
|
+
const result = await client.getMap();
|
|
1071
|
+
console.log(JSON.stringify(summarizeGameMap(result, { ascii: opts.ascii === true }), null, 2));
|
|
1072
|
+
});
|
|
1073
|
+
|
|
1074
|
+
game
|
|
1075
|
+
.command('tasks', { hidden: true })
|
|
1076
|
+
.alias('t')
|
|
1077
|
+
.description('Show my tasks')
|
|
1078
|
+
.action(async () => {
|
|
1079
|
+
const client = GameClient.fromAuth();
|
|
1080
|
+
await client.discoverGameServer();
|
|
1081
|
+
const result = await client.getMap();
|
|
1082
|
+
console.log(JSON.stringify(result?.your_tasks ?? [], null, 2));
|
|
1083
|
+
});
|
|
1084
|
+
|
|
1085
|
+
game
|
|
1086
|
+
.command('role', { hidden: true })
|
|
1087
|
+
.alias('r')
|
|
1088
|
+
.description('Show my role info')
|
|
1089
|
+
.action(async () => {
|
|
1090
|
+
const client = GameClient.fromAuth();
|
|
1091
|
+
const result = await client.getRoleInfo();
|
|
1092
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1093
|
+
});
|
|
1094
|
+
|
|
1095
|
+
game
|
|
1096
|
+
.command('watch')
|
|
1097
|
+
.alias('w')
|
|
1098
|
+
.description('Get spectating URL')
|
|
1099
|
+
.action(() => {
|
|
1100
|
+
const authStore = new AuthStore();
|
|
1101
|
+
const profile = authStore.getActive();
|
|
1102
|
+
if (!profile) throw new Error('Not logged in.');
|
|
1103
|
+
const origin = new URL(profile.serverUrl).origin.replace(/^http:\/\//, 'https://');
|
|
1104
|
+
const url = `${origin}/lobby?token=${encodeURIComponent(profile.apiKey)}`;
|
|
1105
|
+
console.log(JSON.stringify({ url, message: 'Open in browser to spectate.' }, null, 2));
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
// ── Adapter metadata: queue blocks up to --timeout secs (default 30); cap L2 spawn at 60s ──
|
|
1109
|
+
setMeta(game.commands.find((c) => c.name() === 'queue')!, { longRunning: true, timeoutMs: 60_000 });
|
|
1110
|
+
setMeta(game.commands.find((c) => c.name() === 'start')!, { longRunning: true, timeoutMs: 1_800_000 });
|
|
1111
|
+
|
|
1112
|
+
return game;
|
|
1113
|
+
}
|