/** * Action — an immutable, typed game command sent to the server each tick. * * Active static factories (`move`, `kill`, `speech`, …) return frozen `Action` * instances. Retired compatibility helpers throw migration errors locally. * Attach spectator-visible reasoning with `.withThinking(content)`, * which returns a new `Action` without mutating the original. * * @example * ```ts * return Action.move('kitchen').withThinking('Heading to the kitchen for my task.'); * return Action.kill(7).withThinking('Alone with target — safe window.'); * ``` */ export class Action { readonly type: string; readonly payload: Record; /** * Spectator-visible reasoning string sent as `thinking_content` to the server. * Visible to observers only — not exposed to other in-game players. */ readonly thinkingContent: string | null; private constructor(type: string, payload: Record, thinkingContent: string | null = null) { this.type = type; this.payload = payload; this.thinkingContent = thinkingContent; Object.freeze(this); Object.freeze(this.payload); } /** * Attach (or replace) the spectator-visible thinking content. * Returns a **new** immutable `Action` — the original is unchanged. * * @example Action.kill(7).withThinking('Safe kill — only one witness.') */ withThinking(content: string): Action { return new Action(this.type, { ...this.payload }, content); } /** * Move toward a room (by name) or a coordinate target. * * @param target Room id string, or a `{ x, y }` coordinate pair. * @param stopOnPlayer If true, movement stops when encountering another alive player. */ static move(target: { x: number; y: number } | string, stopOnPlayer = false): Action { if (typeof target === 'string') { const payload: Record = { target }; if (stopOnPlayer) payload.stop_on_player = true; return new Action('move', payload, `前往 ${target}`); } const payload: Record = { target_x: target.x, target_y: target.y }; if (stopOnPlayer) payload.stop_on_player = true; return new Action('move', payload); } /** * Kill the player at the given seat number. * Only succeeds when cooldown is ready, a kill use remains, and the target is in range. * Seats are 1-based; the server resolves the target by `target_seat`. */ static kill(seat: number): Action { return new Action('kill', { target_seat: seat }, `击杀 ${seat}号`); } /** * Broadcast a speech during the wandering phase. * * @param text Words spoken (audible to nearby players). * @param thinkingContent Optional spectator-visible reasoning. * @param audioUrl Legacy owner/manual-command-only uploaded audio URL. * Behavior-originated actions must omit this parameter; * the runtime rejects Behavior actions that contain `audio_url`. */ static speech(text: string, thinkingContent?: string, audioUrl?: string): Action { const payload: Record = { text }; if (audioUrl) payload.audio_url = audioUrl; return new Action('speech', payload, thinkingContent ?? null); // speech: no default thinking (the speech content itself is the message) } /** * Cast a vote during the meeting vote sub-phase. * * @param seat Seat number (1-based) of the player to vote out. `0` means skip. */ static vote(seat: number): Action { return new Action('vote', { target_seat: seat }, seat === 0 ? '投票 跳过' : `投票 ${seat}号`); } /** * Execute (or move toward) a task. * * @param taskName Optional task name. Omit to let the server resolve which * task to execute at the current location. */ static doTask(taskName?: string): Action { return new Action('task', taskName ? { task_name: taskName } : {}, taskName ? `执行任务 ${taskName}` : '执行任务'); } /** * Report a corpse to trigger an emergency meeting. * * @param corpseName Name of the dead player (written to thinking log). * @param room Room where the corpse was found. */ static report(corpseName?: string, room?: string): Action { const thinking = corpseName ? `发现尸体(${corpseName}${room ? ` 在 ${room}` : ''}),报告召开会议` : '发现尸体,报告召开会议'; return new Action('report', {}, thinking); } /** * Pure thinking action — sends only `thinking_content` with no in-game effect. * Useful for pushing reasoning text to spectators without altering game state. */ static think(): Action { return new Action('think', {}, '正在思考...'); } /** * @deprecated Sabotage emergencies now start automatically when a Crab * completes one of its assigned sabotage tasks. Submit the existing task * action instead. */ static triggerAlarm(): never { throw new Error('trigger_alarm_removed_use_task'); } /** * Deserialize an `Action` from a plain JSON object. * Accepts both `{ action: "move", … }` and `{ type: "move", … }` formats. * * @throws {Error} If the object contains no valid action type string. */ static fromJSON(json: Record): Action { // Support both { action: "move", ... } and { type: "move", ... } formats const type = json.action ?? json.type; if (!type || typeof type !== 'string') { throw new Error(`Action.fromJSON: missing or invalid action type in ${JSON.stringify(json)}`); } const { action: _a, type: _t, thinking_content, ...payload } = json; return new Action(type, payload, thinking_content ?? null); } /** * Serialize this `Action` to a plain object for transmission to the server. * The `thinking_content` key is omitted when `thinkingContent` is null. */ toJSON(): Record { const obj: Record = { action: this.type, ...this.payload }; if (this.thinkingContent) { obj.thinking_content = this.thinkingContent; } return obj; } }