Files
jinzuanwork/portal/assets/universe/layout.js
T
ywsandClaude Opus 5.5 12e66badc6 内容后台上线:网页里改网站内容(账号密码 + 首次强制改密 + 草稿/预览/发布/回滚 + 画展传图);新世界线自动排位;星球预设 18 种
- backend-site/cms.py(新):账号(scrypt 加盐哈希)、服务端会话(只存哈希,30 天滑动,可撤销)、
  admin/admin 首次登录强制改密(改密前其它接口一律 403)、CSRF / 来源 / https / 登录冷却、
  内容校验、发布写 content/ 与 prices.json / payinfo.json、版本与回滚、传图核验 + 去元数据 + ffmpeg 缩略图
- server.py 把 /api/cms/* 交给 cms.py;admin.py 加 user list/add/passwd/reset/disable/enable/sessions
- portal/admin/(新):内容后台页面;门户顶栏加不带字的淡色铅笔入口
- 门户 / 下单页读 content/site.js、order/content/order.js(代码里的数据降级为兜底);?preview=1 预览草稿
- portal/assets/universe/layout.js(新):新点亮的世界线自动排到中圈不挤的位置、补给线接附近的星、已有的都不动
- planets.js:加 10 种卡通预设(类地 / 海洋 / 丛林 / 荒漠 / 冻原 / 熔岩 / 毒雾 / 荒芜岩球 / 冰巨星 / 带环巨星)
- galaxy.js:名牌 / 档案卡文字一律转义
- tests/内容后台测试.py(75 条);回归 27 + 数据库 38 照旧全过;端到端浏览器 36 条
- 文档:交接文档-2026-10.md(变更 / 接口 / 部署 7.1b / 测试 / 第 10 节)、内容后台设计.md(实现对照)、星系图交接说明、nginx 片段 ④

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 21:25:06 +00:00

129 lines
7.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* =============================================================================
* 金钻宇宙 · 星系图排布(layout.js)—— 纯计算,不依赖 three.js,可以单独测试
* -----------------------------------------------------------------------------
* 决定每条世界线的星球放在哪、补给线从哪连过来。规则(2026-10-05):
*
* 1. 在 LOOKS 里写了 orbit: [离核心多远, 第几条旋臂] 的 —— 就放在那儿,补给线从核心连过去。(现在的 0000–0004)
* 2. 空槽(empty: true)—— 按**编号**放在外圈的固定槽位(从 GALAXY.firstSlot 号起,轮流分到各条旋臂)。
* 只看自己的编号,点亮其中一个,别的空槽不会挪。空槽的淡虚线接到离它最近、又不穿过别的星的那颗已点亮的星
* (没有就接核心)—— 这条淡线会随着新点亮的星「找到更近的落脚点」,空槽本身不动。
* 3. 已点亮、但没写 orbit 的(内容后台里「点亮一条」新加的)—— **自动排位**:
* · 在中圈的旋臂上挑一个位置:跟核心、已有星系(连同它的卫星轨道)都留足距离,尽量让各条旋臂均匀;
* · 补给线连到「比它更靠近核心、又离它最近」的那颗星(没有合适的就连核心),
* 而且这条线不能从别的星系中间穿过;
* · 按编号从小到大一颗一颗排,每一颗只看排在它前面的 —— 所以**新加一条,已有的星和线都不动**。
*
* 用法:import { layoutSystems } from './layout.js';
* const L = layoutSystems(window.JZ_UNIVERSE) → [{ u, look, x, z, kind, parent }]
* kind = 'pinned' | 'slot' | 'auto';parent = 补给线起点的编号,null = 核心
* ============================================================================= */
export const armAngle = (r, arm, G) => arm * Math.PI * 2 / G.arms + r * G.twist;
/** 一个星系「占的地方」半径:星球本身 + 最外面那颗卫星的轨道 */
export function footprint(look) {
const moons = (look.moons || []).map(m => (m.d || 0) + (m.r || 0));
return (look.r || 1) * 2.2 + (moons.length ? Math.max(...moons) : 0);
}
const dist = (a, b) => Math.hypot(a.x - b.x, a.z - b.z);
/** 点到线段的距离(补给线按直线近似;实际画的是稍微拱起来的弧,水平投影还是这条线) */
function segDist(p, a, b) {
const vx = b.x - a.x, vz = b.z - a.z, L2 = vx * vx + vz * vz || 1;
const t = Math.max(0, Math.min(1, ((p.x - a.x) * vx + (p.z - a.z) * vz) / L2));
return Math.hypot(p.x - (a.x + vx * t), p.z - (a.z + vz * t));
}
export function layoutSystems(CFG) {
const G = CFG.GALAXY, R = G.radius, ARMS = G.arms, first = G.firstSlot ?? 5;
const LOOKS = CFG.LOOKS || {};
const lookOf = u => Object.assign({}, CFG.EMPTY_LOOK, LOOKS[u.no] || {});
const num = u => parseInt(u.no, 10) || 0;
const list = [...CFG.UNIVERSES].sort((a, b) => num(a) - num(b));
const CORE = { x: 0, z: 0, fp: 12, no: null };
const out = new Map();
const put = (u, look, r, a, kind, parent) => {
const s = { u, look, x: Math.cos(a) * r, z: Math.sin(a) * r, r, kind, parent, fp: footprint(look), no: u.no };
out.set(u.no, s); return s;
};
// 1) 写死位置的
for (const u of list) {
const o = (LOOKS[u.no] || {}).orbit;
if (!o) continue;
const [r, arm] = o;
put(u, lookOf(u), r, r ? armAngle(r, arm, G) : 0, 'pinned', null);
}
// 2) 空槽:按编号固定在外圈
for (const u of list) {
if (!u.empty || out.has(u.no)) continue;
const e = Math.max(0, num(u) - first);
const r = R * (0.8 + 0.12 * (e % 2));
put(u, lookOf(u), r, armAngle(r, e % ARMS, G) + 0.32 * Math.floor(e / ARMS), 'slot', null);
}
// 3) 自动排位:只避开「核心 + 写死的 + 排在前面的自动星」(空槽在外圈,自动星只用中圈,互不影响)
const placed = [...out.values()].filter(s => s.kind === 'pinned' && s.r > 0);
const armCount = new Array(ARMS).fill(0);
for (const s of placed) {
const a = Math.atan2(s.z, s.x);
let best = 0, bd = Infinity;
for (let k = 0; k < ARMS; k++) { const d = Math.abs(Math.atan2(Math.sin(a - armAngle(s.r, k, G)), Math.cos(a - armAngle(s.r, k, G)))); if (d < bd) { bd = d; best = k; } }
armCount[best]++;
}
const rMin = 16, rMax = R * 0.68, mid = R * 0.5; // 自动星只用中圈:外圈(0.8R 往外)留给空槽,两圈之间留出空隙
// 给一个候选位置挑补给线起点:比它更靠近核心的星(或核心),线不能穿过别的星系
function parentFor(c, others) {
const cands = [CORE, ...others.filter(o => Math.hypot(o.x, o.z) < c.r - 4)];
let best = null;
for (const p of cands) {
const len = dist(p, c);
let clear = Infinity;
for (const o of others) { if (o === p) continue; clear = Math.min(clear, segDist(o, p, c) - o.fp); }
if (clear < 1.5) continue;
// 同样不穿的情况下挑短的;连核心稍微加一点「距离」,让新星更愿意接在附近的星上,连线成网而不是全扎向中心
const score = len + (p === CORE ? 6 : 0);
if (!best || score < best.score) best = { p, score, clear };
}
return best;
}
for (const u of list) {
if (u.empty || out.has(u.no)) continue;
const look = lookOf(u), fp = footprint(look);
let best = null;
for (let arm = 0; arm < ARMS; arm++) {
for (let r = rMin; r <= rMax; r += 1.25) {
for (const off of [-0.2, -0.1, 0, 0.1, 0.2]) {
const a = armAngle(r, arm, G) + off;
const c = { x: Math.cos(a) * r, z: Math.sin(a) * r, r };
let clear = r - CORE.fp - fp;
for (const o of placed) clear = Math.min(clear, dist(o, c) - o.fp - fp);
if (clear < 4) continue;
const par = parentFor(c, placed);
if (!par) continue;
// 分数:够宽敞(封顶,不为了宽敞跑到边上)、靠近中圈、所在旋臂人少、在旋臂正中更好
const score = Math.min(clear, 12) - 0.08 * Math.abs(r - mid) - 2.2 * armCount[arm] - 3 * Math.abs(off) + Math.min(par.clear, 6) * 0.3;
if (!best || score > best.score + 1e-9) best = { score, r, a, arm, parent: par.p };
}
}
}
if (!best) { // 实在挤不下(几十条以后):退回外圈槽位,至少不叠在别人身上
const e = Math.max(0, num(u) - first), r = R * 0.86;
put(u, look, r, armAngle(r, e % ARMS, G) + 0.32 * Math.floor(e / ARMS) + 0.16, 'auto', null);
continue;
}
const s = put(u, look, best.r, best.a, 'auto', best.parent.no);
armCount[best.arm]++;
placed.push(s);
}
// 4) 空槽的淡线:接到最近的、不穿星的已点亮星(或核心)
const litNow = [...out.values()].filter(s => !s.u.empty && s.r > 0);
for (const s of out.values()) {
if (s.kind !== 'slot') continue;
const par = parentFor(s, litNow);
s.parent = par && par.p !== CORE ? par.p.no : null;
}
return list.map(u => out.get(u.no));
}