// 办公室的布局数学(和绘制分开)。 // // 为什么单独一个模块:像素块边长 u 决定"人和东西有多大",而它完全由 // 工位数、房间分区尺寸算出来。把它放在这里就能脱离浏览器核对—— // node --experimental-strip-types scripts/office-layout-check.mjs(见仓库 README) // 也方便以后调房间比例时不用动绘制代码。 // // 这里只依赖结构类型(不 import @/lib/types),所以纯 Node 也能跑。 export const SCENE_W = 1280; export const SCENE_H = 720; // 房间分区:上面一条是墙,中间排工位,右侧留给机柜和上下走的走道, // 底部一条是茶水间 + 休息区 + 讨论区,三个地方都有人会去。 export const WALL_H = 158; export const AREA = { x: 40, y: 166, w: 1088, h: 450 }; export const LABEL_H = 26; // 工位上方名牌的高度 export const ART_W = 44; // 一张桌子的实际宽度(格) export const ART_H = 26; // 一张桌子的实际高度(格) export const MAX_ART_SCALE = 11; // 像素块边长上限:工位很少时别把人画成巨人 export type Mood = "busy" | "idle" | "warn" | "off"; /** 布局只看这几个字段,Agent/Task 的完整类型在调用方。 */ export interface SceneAgent { id: string; name: string; status: string; active_tasks: number; last_error?: string; } export interface SceneTask { agent_id?: string; agent_name?: string; status: string; } export interface DeskLayout { agent: A; mood: Mood; job?: T; done: number; cellX: number; cellY: number; cellW: number; cellH: number; ax: number; // 工位画原点 ay: number; u: number; homeX: number; // 门前走道上的位置 aisleY: number; } export interface SceneLayout { u: number; cols: number; rows: number; cellW: number; cellH: number; desks: DeskLayout[]; lanes: number[]; } export const clamp = (value: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, value)); export const jobsOf = (jobs: T[], agent: { id: string; name: string }) => jobs.filter((job) => job.agent_id === agent.id || job.agent_name === agent.name); export function moodOf(agent: SceneAgent, mine: SceneTask[]): Mood { if (agent.status === "offline") return "off"; if (agent.status === "degraded" || agent.last_error) return "warn"; if ( agent.active_tasks > 0 || mine.some((job) => job.status === "running" || job.status === "queued") ) return "busy"; return "idle"; } /** 给定工位数,某一种列数下能拿到的像素块边长。 */ function scaleFor(count: number, cols: number): { u: number; rows: number; cellW: number; cellH: number } { const rows = Math.max(1, Math.ceil(count / cols)); const cellW = Math.floor(AREA.w / cols); const cellH = Math.floor(AREA.h / rows); const u = clamp( Math.floor(Math.min((cellW - 8) / ART_W, (cellH - LABEL_H - 16) / ART_H)), 2, MAX_ART_SCALE, ); return { u, rows, cellW, cellH }; } /** * 挑一个让工位尽量大的列数:先比像素块边长,一样大再比"行数少"(更紧凑), * 最后比列数多(更接近方形)。比旧的"固定 sqrt(2n) 再夹到 4 列"更直接: * 人越小越看不清,所以让 u 最大化是第一目标。 */ export function bestColumns(count: number): number { let best = 1; let bestKey: [number, number, number] = [-1, -1, -1]; for (let cols = 1; cols <= 4; cols += 1) { const { u, rows } = scaleFor(count, cols); const key: [number, number, number] = [u, -rows, cols]; if (key[0] > bestKey[0] || (key[0] === bestKey[0] && (key[1] > bestKey[1] || (key[1] === bestKey[1] && key[2] > bestKey[2])))) { best = cols; bestKey = key; } } return best; } /** 只看工位数就能算出的像素块边长(离线核对用)。 */ export function artScaleFor(agentCount: number): number { return scaleFor(Math.max(agentCount, 1), bestColumns(Math.max(agentCount, 1))).u; } /** 工位网格:格子越多格子越小,像素块边长只取整数,人和桌子同比缩放。 */ export function layoutScene( agents: A[], jobs: T[], ): SceneLayout { const count = Math.max(agents.length, 1); const cols = bestColumns(count); const { u, rows, cellW, cellH } = scaleFor(count, cols); const ayOf = (row: number) => AREA.y + row * cellH + LABEL_H + 4; const lanes = Array.from({ length: rows }, (_, row) => ayOf(row) + ART_H * u + 8); const desks = agents.map((agent, index) => { const col = index % cols; const row = Math.floor(index / cols); const cellX = AREA.x + col * cellW; const mine = jobsOf(jobs, agent); return { agent, mood: moodOf(agent, mine), job: mine.find((job) => job.status === "running") ?? mine.find((job) => job.status === "queued"), done: mine.filter((job) => job.status === "completed").length, cellX, cellY: AREA.y + row * cellH, cellW, cellH, ax: cellX + Math.round((cellW - ART_W * u) / 2), ay: ayOf(row), u, homeX: cellX + Math.round(cellW / 2), aisleY: lanes[row], } satisfies DeskLayout; }); return { u, cols, rows, cellW, cellH, desks, lanes }; }