From 6cac206fe5dd549251e378da21d5669400193ace Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Thu, 8 Oct 2026 20:30:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E7=81=AB=E7=AE=AD?= =?UTF-8?q?=E5=8F=91=E5=B0=84=E6=A8=A1=E6=8B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/rocket.py | 7 +- backend/app/main.py | 3 + backend/app/services/rocket_preset_service.py | 169 +++++++++++++++--- frontend/src/components/admin/DataTable.tsx | 3 + .../features/rocket-simulator/RocketModel.tsx | 77 ++++++-- .../rocket-simulator/rocket-simulator.css | 8 - .../features/rocket-simulator/sceneMath.ts | 93 +++++++--- .../src/features/rocket-simulator/types.ts | 9 +- .../rocket-simulator/useRocketSimulation.ts | 8 +- frontend/src/pages/RocketSimulator.tsx | 62 +++++-- frontend/src/pages/admin/Rockets.tsx | 29 ++- 11 files changed, 376 insertions(+), 92 deletions(-) diff --git a/backend/app/api/rocket.py b/backend/app/api/rocket.py index 71fbd36..837bdf9 100644 --- a/backend/app/api/rocket.py +++ b/backend/app/api/rocket.py @@ -24,16 +24,20 @@ class RocketStage(BaseModel): specific_impulse_s: float = Field(gt=0) engine_count: int = Field(ge=0, description="本级发动机数量;一级的这里是芯级自己的台数,不含助推器") length_ratio: float = Field(gt=0, le=1) + recoverable: bool | None = Field( + default=None, + description="本级设计为可回收;一级的栅格翼据此绘制(脱离前收拢、分离后展开),二级暂不表现回收", + ) class RocketAppearance(BaseModel): """纯外观特征(可选):只登记无法从参数推导的特征件。 构型(助推器数量、各自的发动机数量与直径)一律在 boosters 里登记, + 栅格翼长在可回收的那一级上,因此由 stage_1 / boosters 的 recoverable 决定, 这里不重复存放,否则同一件事会有两处数值。 """ - grid_fins: bool | None = Field(default=None, description="一级顶部栅格翼(回收构型)") fairing: bool | None = Field(default=None, description="是否有载荷整流罩;false 表示二级自身带头锥与襟翼(星舰)") @@ -47,6 +51,7 @@ class RocketBoosters(BaseModel): """ count: int = Field(ge=1, le=8, description="助推器数量") + recoverable: bool | None = Field(default=None, description="助推器是否可回收,据此在助推器顶部画栅格翼") engine_count: int = Field(ge=1, le=33, description="每台助推器的发动机数量") diameter_ratio: float | None = Field( default=None, gt=0, le=1, description="助推器直径 / 芯级直径,留空按 0.45 绘制" diff --git a/backend/app/main.py b/backend/app/main.py index 5618b73..2062f20 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -86,6 +86,7 @@ async def lifespan(app: FastAPI): ensure_high_frequency_rockets, ensure_rocket_appearance, ensure_booster_groups, + ensure_recoverable_flags, ) from app.services.rocket_event_service import ensure_launch_event_job from app.services.menu_service import ensure_admin_menus @@ -97,6 +98,8 @@ async def lifespan(app: FastAPI): await ensure_rocket_appearance(db) # 助推器口径统一:发动机数量各归各位(要在外观预置之后,旧的发动机数藏在 appearance 里) boosted = await ensure_booster_groups(db) + # 可回收标记(栅格翼开关)挪到一级与助推器身上,并清掉旧的外观 grid_fins + await ensure_recoverable_flags(db) await ensure_launch_event_job(db) # 补插新增后台页面所需的菜单(本次:火箭发射事件) new_menus = await ensure_admin_menus(db) diff --git a/backend/app/services/rocket_preset_service.py b/backend/app/services/rocket_preset_service.py index 6ce13ce..ec564b5 100644 --- a/backend/app/services/rocket_preset_service.py +++ b/backend/app/services/rocket_preset_service.py @@ -298,17 +298,27 @@ HIGH_FREQUENCY_ROCKETS: list[dict[str, Any]] = [ # 外观特征预置:按 code 登记,与型号参数定义分开,因此用户手工登记的型号 # (联盟 2、阿丽亚娜 6)也能在这里补齐特征件。仅在库里缺这个 key 时写入, -# 管理端填过的值不会被动。助推器构型不在这里登记,见 ROCKET_BOOSTERS。 +# 管理端填过的值不会被动。助推器构型不在这里登记,见 ROCKET_BOOSTERS; +# 栅格翼也不在这里登记,它属于回收构型,见 ROCKET_RECOVERY。 ROCKET_APPEARANCES: dict[str, dict[str, Any]] = { - "starship": {"grid_fins": True, "fairing": False}, - "electron": {"grid_fins": False}, - "falcon-heavy": {"grid_fins": True}, - "long-march-2f": {"grid_fins": False}, - "long-march-7": {"grid_fins": False}, - "long-march-6a": {"grid_fins": False}, - "long-march-8": {"grid_fins": False}, - "soyuz-2": {"grid_fins": False}, - "ariane-6": {"grid_fins": False}, + "starship": {"fairing": False}, +} + +# 回收构型预置:可回收的那一级才画栅格翼(脱离前收拢、分离后展开), +# 因此这个开关登记在一级和助推器自己的 recoverable 里,而不是外观里。 +# 两者分开登记,芯级回收、助推器一次性的型号(长征八号)才表达得出来。 +ROCKET_RECOVERY: dict[str, dict[str, bool]] = { + "starship": {"stage_1": True, "boosters": False}, + "falcon-9": {"stage_1": True, "boosters": False}, + "falcon-heavy": {"stage_1": True, "boosters": True}, + "long-march-8": {"stage_1": True, "boosters": False}, + "electron": {"stage_1": False, "boosters": False}, + "long-march-5": {"stage_1": False, "boosters": False}, + "long-march-7": {"stage_1": False, "boosters": False}, + "long-march-2f": {"stage_1": False, "boosters": False}, + "long-march-6a": {"stage_1": False, "boosters": False}, + "soyuz-2": {"stage_1": False, "boosters": False}, + "ariane-6": {"stage_1": False, "boosters": False}, } # 助推器组预置:数量、每台的发动机数、直径比与推进参数都登记在助推器自己身上。 @@ -354,19 +364,64 @@ ROCKET_BOOSTERS: dict[str, dict[str, Any]] = { } # 只登记构型、不登记推进参数的助推器:这两个型号的公开推力口径互相矛盾 -# (阿丽亚娜 6 的一级整体就是 P120C 组合,长征六号甲的芯级推力各家说法不一), -# 强行拆分会算出撑不起起飞的芯级推重比,因此推进参数留空 —— +# (阿丽亚娜 6 的 P120C 各家把峰值 4.46 MN 与平均 2.8~3.5 MN 混着写,长征六号甲的 +# 芯级推力各家说法不一),强行拆分会算出撑不起起飞的芯级推重比,因此推进参数留空 —— # 物理仍按一级整段计算,boosters 只决定「画几个助推器、每个几台机、多粗」。 ROCKET_BOOSTER_GEOMETRY: dict[str, dict[str, Any]] = { - # 阿丽亚娜 6:一级 = 4 枚 P120C,没有液体芯级,因此 stage_1.engine_count 记 0 + # 阿丽亚娜 6:4 枚 P120C 捆在 EPC 芯级两侧(3.05 / 5.43 ≈ 0.56),各 1 台固体发动机 "ariane-6": {"count": 4, "engine_count": 1, "diameter_ratio": 0.56}, # 长征六号甲:芯一级 2 台 YF-100 + 4 台固体助推器 "long-march-6a": {"count": 4, "engine_count": 2, "diameter_ratio": 0.75}, } -# 阿丽亚娜 6 的一级本身就是助推器组合,改写口径后芯级没有发动机 +# 阿丽亚娜 6 的旧口径把一级当成 4 枚 P120C 的组合,芯级记 0 台(真实台数见下方勘误表) LEGACY_STAGE1_CORE_ENGINES = {"ariane-6": 0} +# 芯级发动机台数勘误:{code: {from 旧台数, to 正确台数, name 改后的级名}}。 +# 阿丽亚娜 6 的芯级是 EPC,1 台 Vulcain 2.1 从 T-6s 点火、起飞时与 4 台 P120C 一起工作, +# 早期按「一级 = 4 枚 P120C、没有液体芯级」记成 0 台,画出来芯级底部是空的、 +# 起飞机数也少一台。这里只补台数与级名(纯显示口径),一级的推进参数仍是 4 枚 P120C 的 +# 合计,不动推力,因此入轨时序不变。命中旧值才改,管理端自己填过台数就不会被动。 +CORE_ENGINE_CORRECTIONS: dict[str, dict[str, Any]] = { + "ariane-6": {"from": 0, "to": 1, "name": "4 枚 P120C + 芯级 Vulcain(起飞段)"}, +} + +# 旧口径(boosters 还没登记时)的 stage_1.engine_count 是「芯级 + 全部助推器」的总台数。 +# boosters 丢失的行(旧数据没登记过、或被管理端保存清空)只能靠这份台数判断该行 +# 还是不是旧口径:命中旧总数才把助推器的推进参数从一级里减掉、并把台数换成芯级台数, +# 没命中说明它已经是芯级口径,原样保留。数值取自改口径前的预置与线上旧记录。 +LEGACY_STAGE1_TOTAL_ENGINES: dict[str, tuple[int, int]] = { + "falcon-heavy": (27, 9), + "long-march-5": (10, 2), + "long-march-2f": (8, 4), + "long-march-7": (10, 2), + "long-march-6a": (10, 2), + "long-march-8": (6, 2), + "soyuz-2": (5, 1), + "ariane-6": (4, 0), +} + + +def deduct_boosters_from_stage1(stage_1: dict[str, Any], boosters: dict[str, Any]) -> bool: + """把助推器那一份推进参数从一级里减掉,使一级只剩芯级。 + + 只应当对「一级 = 芯级 + 助推器合计」的旧口径行调用。逐项只在一级数值确实大于 + 助推器一组时才相减:数值本来就按芯级登记的行(管理端手工填的)减下去会被压成 0, + 不如保留原值。 + """ + changed = False + for stage_key, booster_key in ( + ("max_thrust_n", "thrust_n"), + ("dry_mass_kg", "dry_mass_kg"), + ("fuel_mass_kg", "fuel_mass_kg"), + ): + booster_value = float(boosters.get(booster_key) or 0) + current = float(stage_1.get(stage_key) or 0) + if booster_value and current > booster_value: + stage_1[stage_key] = current - booster_value + changed = True + return changed + # 预置里的 stage_1.engine_count 已是芯级口径,改口径时优先取它: # 「一级总数减去助推器」要靠旧数据里的每台机数,而早期预置的长征七号把每台助推器 # 记成 2 台 YF-100(真实是 1 台),减法会凭空多出 4 台芯级发动机。 @@ -425,7 +480,8 @@ async def ensure_booster_groups(db: AsyncSession) -> list[str]: """把助推器登记统一成「助推器自己带发动机」的口径,并按预置补齐缺失的助推器组。 一次遍历处理三种行,处理过的行在下次启动时一律不再改动: - 1. boosters 里已有 engine_count → 已是新口径(含管理端自己填的),只清理旧的 appearance key; + 1. boosters 里已有 engine_count → 已是新口径(含管理端自己填的),只清理旧的 appearance + key,外加把缺的直径比按预置补上(只补绘制参数,一级数值一律不动); 2. boosters 里没有 engine_count → 旧口径:机数藏在 appearance 里、一级是含助推器的合计。 这里把芯级发动机数写回 stage_1、机数与直径写进 boosters,并把一级的 推力 / 干重 / 推进剂减去助推器的量 —— 减法只发生在这条分支,且只会发生一次; @@ -466,12 +522,7 @@ async def ensure_booster_groups(db: AsyncSession) -> list[str]: if core_engines is None: core_engines = max(0, int(stage_1.get("engine_count") or 0) - count * per_booster) stage_1["engine_count"] = core_engines - for stage_key, booster_key in ( - ("max_thrust_n", "thrust_n"), - ("dry_mass_kg", "dry_mass_kg"), - ("fuel_mass_kg", "fuel_mass_kg"), - ): - stage_1[stage_key] = max(0.0, float(stage_1.get(stage_key) or 0) - float(boosters.get(booster_key) or 0)) + deduct_boosters_from_stage1(stage_1, boosters) if rocket.code in LEGACY_STAGE1_NAMES: stage_1["name"] = LEGACY_STAGE1_NAMES[rocket.code] rocket.stage_1 = stage_1 @@ -495,9 +546,16 @@ async def ensure_booster_groups(db: AsyncSession) -> list[str]: # 一级的机数口径同样要从「含助推器」改成「只有芯级」,否则芯级和助推器会把 # 同一批发动机画两遍;只在真的要登记助推器组时改写,取不到预置就退到旧 # appearance 里的 core_engine_count + legacy_total, legacy_core = LEGACY_STAGE1_TOTAL_ENGINES.get(rocket.code, (None, None)) + # 一级仍是旧总数时,助推器那一份推进参数还混在一级里,先减出来 + if preset and legacy_total is not None and int(stage_1.get("engine_count") or 0) == legacy_total: + deduct_boosters_from_stage1(stage_1, dict(preset)) + logger.info("火箭 %s 的一级改按芯级口径:减去助推器后 %s N / %s kg 干重 / %s kg 推进剂", + rocket.code, stage_1.get("max_thrust_n"), + stage_1.get("dry_mass_kg"), stage_1.get("fuel_mass_kg")) core_engines = LEGACY_STAGE1_CORE_ENGINES.get(rocket.code) if core_engines is None and preset: - core_engines = PRESET_STAGE1_CORE_ENGINES.get(rocket.code) + core_engines = PRESET_STAGE1_CORE_ENGINES.get(rocket.code, legacy_core) if core_engines is None and preset and legacy.get("core_engine_count"): core_engines = int(legacy["core_engine_count"]) renamed = rocket.code in LEGACY_STAGE1_NAMES and stage_1.get("name") != LEGACY_STAGE1_NAMES[rocket.code] @@ -509,6 +567,27 @@ async def ensure_booster_groups(db: AsyncSession) -> list[str]: changed = True if changed: rocket.stage_1 = stage_1 + elif boosters.get("diameter_ratio") is None: + # 已是新口径的行也可能缺直径比:这个 key 之前入库的、以及管理端把这一项留空的。 + # 前端只能退回 0.45,于是猎鹰重型这种三根等粗的一级会画成「芯级粗、两侧细」。 + # 这里只补绘制用的直径比,不参与推进参数,因此重复执行不会再减一遍助推器。 + preset = ROCKET_BOOSTERS.get(rocket.code) or ROCKET_BOOSTER_GEOMETRY.get(rocket.code) or {} + ratio = preset.get("diameter_ratio") or legacy.get("booster_diameter_ratio") + if ratio is not None: + boosters["diameter_ratio"] = round(float(ratio), 2) + rocket.boosters = boosters + changed = True + logger.info("火箭 %s 的助推器补上直径比 %s", rocket.code, boosters["diameter_ratio"]) + + correction = CORE_ENGINE_CORRECTIONS.get(rocket.code) + if correction and stage_1.get("engine_count") == correction["from"]: + stage_1["engine_count"] = correction["to"] + if stage_1.get("name"): + stage_1["name"] = correction["name"] + rocket.stage_1 = stage_1 + changed = True + logger.info("火箭 %s 的芯级发动机台数勘误 %s → %s", + rocket.code, correction["from"], correction["to"]) if changed or any(value is not None for value in legacy.values()): rocket.appearance = appearance @@ -519,6 +598,52 @@ async def ensure_booster_groups(db: AsyncSession) -> list[str]: logger.info("火箭助推器口径统一:%s", ", ".join(updated)) return updated + +async def ensure_recoverable_flags(db: AsyncSession) -> list[str]: + """把「是否可回收」登记到一级与助推器自己身上,并清发作废的 appearance.grid_fins。 + + 栅格翼是回收构型的特征件,长在可回收的那一级上,芯级与助推器各自独立, + 因此开关从外观参数挪到 stage_1 / boosters 的 recoverable(旧口径只有一根 + 总开关,芯级回收而助推器不回收的长征八号就表达不了)。 + 预置登记过的型号按预置写入,没登记过的沿用它在 appearance.grid_fins 里的旧值, + 管理端填过的 recoverable 一律不动;处理完的行下次启动不再改动。 + """ + result = await db.execute(select(RocketConfig)) + + updated: list[str] = [] + for rocket in result.scalars().all(): + appearance = dict(rocket.appearance or {}) + legacy_fins = appearance.pop("grid_fins", None) + stage_1 = dict(rocket.stage_1 or {}) + boosters = dict(rocket.boosters) if rocket.boosters else None + preset = ROCKET_RECOVERY.get(rocket.code) or {} + # 只要还留着旧的 grid_fins 就得回写 appearance,把这个 key 清掉 + changed = legacy_fins is not None + + if "recoverable" not in stage_1: + if "stage_1" in preset: + stage_1["recoverable"] = preset["stage_1"] + changed = True + elif legacy_fins is not None: + stage_1["recoverable"] = bool(legacy_fins) + changed = True + if boosters is not None and "recoverable" not in boosters and "boosters" in preset: + boosters["recoverable"] = preset["boosters"] + changed = True + + if not changed: + continue + rocket.stage_1 = stage_1 + if boosters is not None: + rocket.boosters = boosters + rocket.appearance = appearance + updated.append(rocket.code) + + if updated: + await db.flush() + logger.info("火箭可回收标记回填:%s", ", ".join(updated)) + return updated + async def ensure_high_frequency_rockets(db: AsyncSession) -> list[str]: """补齐缺失的高频型号,返回新入库的 code 列表。""" existing = set((await db.execute(select(RocketConfig.code))).scalars().all()) diff --git a/frontend/src/components/admin/DataTable.tsx b/frontend/src/components/admin/DataTable.tsx index 28bbd3a..6966351 100644 --- a/frontend/src/components/admin/DataTable.tsx +++ b/frontend/src/components/admin/DataTable.tsx @@ -97,6 +97,9 @@ export function DataTable({ dataIndex: statusField as string, key: 'status', width: 100, + // 和右侧固定的「操作」列钉在一起:列多的表(火箭管理)横向要滚动, + // 不固定的话启用/停用开关会被滚出可视区,看起来像这个列表没有状态列 + fixed: 'right', render: (value: boolean, record: T) => ( onStatusChange(record, checked)} size="small" /> diff --git a/frontend/src/features/rocket-simulator/RocketModel.tsx b/frontend/src/features/rocket-simulator/RocketModel.tsx index 55c0f95..57b8b03 100644 --- a/frontend/src/features/rocket-simulator/RocketModel.tsx +++ b/frontend/src/features/rocket-simulator/RocketModel.tsx @@ -270,7 +270,7 @@ function EnginePlume({ /** * 一组发动机的尾焰:喷管画几台就喷几团,机位与 EngineCluster 的喷管同源 * (都取自 engineLayout),整组共用一个闪烁相位和一盏照明灯。 - * 芯级没有发动机时整组不画(阿丽亚娜 6 的一级就是助推器组合)。 + * 一组没有发动机(台数记 0)时整组不画。 */ function EngineBankPlume({ radius, @@ -326,7 +326,8 @@ function FirstStageBody({ engineCount, label, gridFins = false, -}: StageProps & { engineCount: number; label: string; gridFins?: boolean }) { + gridFinsDeployed = false, +}: StageProps & { engineCount: number; label: string; gridFins?: boolean; gridFinsDeployed?: boolean }) { const accent = shade(color, -0.55); return ( @@ -347,7 +348,7 @@ function FirstStageBody({ - {gridFins && } + {gridFins && } (index / 4) * Math.PI * 2 + Math.PI / 4) + .filter((azimuth) => blockAngle === null || Math.cos(azimuth - blockAngle) <= 0); + const hinges = useRef<(THREE.Group | null)[]>([]); + const fold = useRef(deployed ? 0 : -Math.PI / 2); + + useFrame((_, delta) => { + // 铰链角:0 = 向外展开,-90° = 向下翻折、格栅平贴在箭体上 + fold.current = THREE.MathUtils.damp(fold.current, deployed ? 0 : -Math.PI / 2, 5, delta); + hinges.current.forEach((hinge) => { + if (hinge) hinge.rotation.z = fold.current; + }); + }); + return ( - {[0, 1, 2, 3].map((index) => { - const angle = (index / 4) * Math.PI * 2 + Math.PI / 4; - return ( - + {azimuths.map((azimuth, index) => ( + + {/* 铰链:离箭皮一点点,收拢时格栅正好贴在箭体外侧而不共面 */} + { hinges.current[index] = node; }} position={[radius * 0.05, 0, 0]}> {/* 支臂 */} @@ -405,8 +440,8 @@ function GridFins({ radius, y, color }: { radius: number; y: number; color: stri ))} - ); - })} + + ))} ); } @@ -427,6 +462,7 @@ function StrapOnBooster({ active, running, gridFins = false, + gridFinsDeployed = false, }: { radius: number; height: number; @@ -436,6 +472,7 @@ function StrapOnBooster({ active: boolean; running: boolean; gridFins?: boolean; + gridFinsDeployed?: boolean; }) { const bodyRadius = radius * group.radius; const offset = radius * group.offset; @@ -462,8 +499,16 @@ function StrapOnBooster({ - {/* 栅格翼:并联构型下它长在助推器(回收的那一级)顶部 */} - {gridFins && } + {/* 栅格翼:助推器登记为可回收时长在它自己顶部,芯级那组看一级的 recoverable */} + {gridFins && ( + + )} {/* 助推器发动机舱:只画本机自己的喷管,不借用一级总数 */} @@ -958,7 +1003,8 @@ export function RocketModel({ rocket, state }: RocketModelProps) { color={rocket.color} engineCount={geo.coreEngines} label={firstStageLabel} - gridFins={geo.gridFins && boosterGroups.length === 0} + gridFins={geo.coreRecoverable} + gridFinsDeployed={age !== null} /> {/* 捆绑助推器:先于芯级分离,分离后向四周散开 */} {boosterGroups.length > 0 && ( @@ -973,7 +1019,8 @@ export function RocketModel({ rocket, state }: RocketModelProps) { throttle={state.throttle} active={engines.stage1 && boosterAge === null} running={state.isRunning} - gridFins={geo.gridFins} + gridFins={geo.boosterRecoverable} + gridFinsDeployed={boosterAge !== null} /> ))} diff --git a/frontend/src/features/rocket-simulator/rocket-simulator.css b/frontend/src/features/rocket-simulator/rocket-simulator.css index 3d9c492..36cec01 100644 --- a/frontend/src/features/rocket-simulator/rocket-simulator.css +++ b/frontend/src/features/rocket-simulator/rocket-simulator.css @@ -1082,14 +1082,6 @@ box-shadow: 0 0 6px rgba(245, 196, 81, 0.75); } -.sx-engine-caption { - color: #858d8a; - font-size: 8px; - font-weight: 750; - letter-spacing: 0.06em; - white-space: nowrap; -} - /* ---------- attitude gauge ---------- */ .sx-attitude-gauge { diff --git a/frontend/src/features/rocket-simulator/sceneMath.ts b/frontend/src/features/rocket-simulator/sceneMath.ts index 6bd8b48..d3acbc7 100644 --- a/frontend/src/features/rocket-simulator/sceneMath.ts +++ b/frontend/src/features/rocket-simulator/sceneMath.ts @@ -1,4 +1,4 @@ -import type { RocketAppearance, RocketBoosters, RocketConfig, SimulationState } from './types'; +import type { RocketBoosters, RocketConfig, SimulationState } from './types'; /** * Visual constants for the 3D flight scene. The rocket is always drawn at a @@ -21,8 +21,10 @@ export interface VehicleLayoutInfo { enginesPerBooster: number; /** 芯级一级发动机数量(不含助推器) */ coreEngines: number; - /** 一级顶部是否画栅格翼(回收构型特征件) */ - gridFins: boolean; + /** 芯级(一级)是否可回收,可回收就在顶部画栅格翼 */ + coreRecoverable: boolean; + /** 助推器是否可回收,可回收就在每枚助推器顶部画栅格翼 */ + boosterRecoverable: boolean; /** 助推器直径 / 芯级直径 */ boosterDiameterRatio: number; } @@ -46,18 +48,17 @@ export interface StageGeometry extends VehicleLayoutInfo { /** * 构型全部来自登记值:助推器的数量 / 每台的发动机数 / 直径比在 boosters 里, - * 芯级机数就是 stage_1.engine_count(阿丽亚娜 6 这类「一级 = 助推器组合」的型号 - * 芯级为 0 台)。剩下只有栅格翼需要猜:细长且多发动机的单芯级(猎鹰 9)才会带。 + * 芯级机数就是 stage_1.engine_count(一组机数记 0 的型号就是纯助推器组合), + * 栅格翼长在可回收的那一级上,因此芯级取一级的 recoverable、助推器取助推器的。 */ export function resolveVehicleLayout( coreEngines: number, - slenderness: number, boosters: RocketBoosters | null | undefined, - appearance: RocketAppearance = {}, + stageRecoverable: boolean | null | undefined, ): VehicleLayoutInfo { const count = Math.round(boosters?.count ?? 0); - // 没登记时按通用规律推断:细长且多发动机的单芯级才带栅格翼 - const gridFins = appearance.grid_fins ?? (count === 0 && coreEngines >= 8 && slenderness >= 15); + const coreRecoverable = stageRecoverable ?? false; + const boosterRecoverable = count > 0 && (boosters?.recoverable ?? false); if (count <= 0) { return { @@ -65,7 +66,8 @@ export function resolveVehicleLayout( boosterCount: 0, enginesPerBooster: 0, coreEngines: Math.max(0, Math.round(coreEngines)), - gridFins, + coreRecoverable, + boosterRecoverable, boosterDiameterRatio: DEFAULT_BOOSTER_DIAMETER_RATIO, }; } @@ -75,7 +77,8 @@ export function resolveVehicleLayout( boosterCount: count, enginesPerBooster: Math.max(1, Math.round(boosters!.engine_count)), coreEngines: Math.max(0, Math.round(coreEngines)), - gridFins, + coreRecoverable, + boosterRecoverable, boosterDiameterRatio: clamp(boosters!.diameter_ratio ?? DEFAULT_BOOSTER_DIAMETER_RATIO, 0.2, 1), }; } @@ -145,14 +148,7 @@ export function stage1EngineGroups(layout: VehicleLayoutInfo): EngineGroup[] { /** 便捷入口:直接由火箭配置判断布局(供物理模拟与仪表盘复用)。 */ export function layoutForRocket(rocket: RocketConfig) { - const heightM = Math.max(1, rocket.height_m); - const diameterM = Math.max(0.5, rocket.diameter_m); - return resolveVehicleLayout( - rocket.stage_1.engine_count, - clamp(heightM / diameterM, 4, 26), - rocket.boosters, - rocket.appearance ?? {}, - ); + return resolveVehicleLayout(rocket.stage_1.engine_count, rocket.boosters, rocket.stage_1.recoverable); } /** @@ -219,7 +215,7 @@ export function stageGeometry(rocket: RocketConfig): StageGeometry { stage1Engines, stage2Engines: Math.max(1, Math.round(rocket.stage_2.engine_count)), fairing: hasFairing, - ...resolveVehicleLayout(stage1Engines, slenderness, rocket.boosters, rocket.appearance ?? {}), + ...resolveVehicleLayout(stage1Engines, rocket.boosters, rocket.stage_1.recoverable), }; } @@ -237,18 +233,26 @@ export interface EnginePlacement { /** * 按真实发动机数量排布喷管: - * 1 台居中;2~4 台对称;5~9 台为「1 台中心 + 外圈」;10 台以上再加中心机。 + * 1 台居中;2~4 台对称;6 台按星舰飞船构型(2 外侧摇摆 + 2 内侧固定 + 2 台真空型居中); + * 其余 5~9 台为「1 台中心 + 外圈」,10 台以上再加中心机, + * 20 台以上按超重型构型排三圈(外圈 20 摇摆 / 中圈 10 固定 / 中心 3 固定)。 * count 是「一组(芯级或某个助推器)」的机数,不是整箭总数。 */ export function engineLayout(count: number, radius: number): EnginePlacement[] { const total = Math.round(count); if (total <= 0) return []; const ringRadius = radius * 0.7; - const make = (x: number, z: number, bellRadius: number, cant = 0): EnginePlacement => ({ + const make = ( + x: number, + z: number, + bellRadius: number, + cant = 0, + bellElongation = 2.1, + ): EnginePlacement => ({ x, z, bellRadius, - bellLength: bellRadius * 2.1, + bellLength: bellRadius * bellElongation, cant, }); @@ -268,8 +272,47 @@ export function engineLayout(count: number, radius: number): EnginePlacement[] { }); } - // 20 台以上(超重型 33 台)与真实一样用 3 台中心机,10 台以上用 2 台 - const coreCount = total > 20 ? 3 : total > 9 ? 2 : 1; + // 星舰飞船 6 台:外侧 2 台海平面型可摇摆(沿 X 轴),内侧 2 台海平面型固定(沿 Z 轴), + // 中间 2 台真空型(RV)并排,喷管放大加长以体现真空延伸段。 + if (total === 6) { + const seaLevel = radius * 0.155; + const vacuum = radius * 0.22; + return [ + make(-radius * 0.68, 0, seaLevel, 0.1), + make(radius * 0.68, 0, seaLevel, 0.1), + make(0, -radius * 0.42, seaLevel), + make(0, radius * 0.42, seaLevel), + make(-radius * 0.24, 0, vacuum, 0, 3.4), + make(radius * 0.24, 0, vacuum, 0, 3.4), + ]; + } + + // 超重型 33 台 = 外圈 20(可摇摆)+ 中圈 10(固定)+ 中心 3(固定)。 + // 三圈都是同型机,共用最挤的那一圈放得下的喷管尺寸;相邻圈错开相位,别排成一条径向直线。 + if (total >= 20) { + const rings = [ + { count: Math.min(20, total), ringRadius: radius * 0.72, phase: Math.PI / 20, cant: 0.09 }, + { count: Math.min(10, Math.max(0, total - 20)), ringRadius: radius * 0.44, phase: 0, cant: 0 }, + { count: Math.max(0, total - 30), ringRadius: radius * 0.16, phase: -Math.PI / 2, cant: 0 }, + ]; + const bell = Math.min( + radius * 0.22, + ...rings + .filter((ring) => ring.count > 1) + .map((ring) => (Math.PI * ring.ringRadius) / (ring.count * 1.15)), + ); + const clustered: EnginePlacement[] = []; + rings.forEach((ring) => { + for (let index = 0; index < ring.count; index += 1) { + const angle = ring.phase + (index / ring.count) * Math.PI * 2; + clustered.push(make(Math.cos(angle) * ring.ringRadius, Math.sin(angle) * ring.ringRadius, bell, ring.cant)); + } + }); + return clustered; + } + + // 10 台以上用 2 台中心机,其余 1 台 + const coreCount = total > 9 ? 2 : 1; const outerCount = total - coreCount; const outerBell = Math.min(radius * 0.3, (2 * Math.PI * ringRadius) / (outerCount * 2.45)); const coreBell = Math.min(radius * 0.34, outerBell * 1.12); diff --git a/frontend/src/features/rocket-simulator/types.ts b/frontend/src/features/rocket-simulator/types.ts index 53cfa87..eccad03 100644 --- a/frontend/src/features/rocket-simulator/types.ts +++ b/frontend/src/features/rocket-simulator/types.ts @@ -7,15 +7,16 @@ export interface RocketStage { /** 本级发动机数量;一级的这里是芯级自己的台数,不含助推器 */ engine_count: number; length_ratio: number; + /** 本级是否可回收;一级的栅格翼据此绘制(脱离前收拢、分离后展开),二级暂不表现回收 */ + recoverable?: boolean | null; } /** * 纯外观特征(可选):只登记无法从参数推导的特征件。 - * 助推器数量、每台的发动机数与直径都在 boosters 里登记,这里不重复存放。 + * 助推器数量、每台的发动机数与直径都在 boosters 里登记,栅格翼由可回收标记 + * (stage_1 / boosters 的 recoverable)决定,这里不重复存放。 */ export interface RocketAppearance { - /** 一级顶部栅格翼(回收构型) */ - grid_fins?: boolean | null; /** 是否有载荷整流罩;false 表示二级自身带头锥与襟翼(星舰) */ fairing?: boolean | null; } @@ -29,6 +30,8 @@ export interface RocketAppearance { */ export interface RocketBoosters { count: number; + /** 助推器是否可回收,据此在助推器顶部画栅格翼 */ + recoverable?: boolean | null; /** 每台助推器的发动机数量 */ engine_count: number; /** 助推器直径 / 芯级直径,留空按 0.45 绘制 */ diff --git a/frontend/src/features/rocket-simulator/useRocketSimulation.ts b/frontend/src/features/rocket-simulator/useRocketSimulation.ts index b486bd3..1377835 100644 --- a/frontend/src/features/rocket-simulator/useRocketSimulation.ts +++ b/frontend/src/features/rocket-simulator/useRocketSimulation.ts @@ -55,10 +55,16 @@ export function useRocketSimulation(rocket: RocketConfig) { commit(next); }, [commit]); + // 换型号才重置任务。父组件会重新取参数(切回标签页时),那属于同一型号的数据更新, + // 直接 reset 会把正在飞的任务打断;新的质量 / 推力在下一次重置或重新点火时生效。 useEffect(() => { rocketRef.current = rocket; + }, [rocket]); + + const rocketCode = rocket.code; + useEffect(() => { reset(); - }, [rocket, reset]); + }, [rocketCode, reset]); const toggle = useCallback(() => { const current = stateRef.current; diff --git a/frontend/src/pages/RocketSimulator.tsx b/frontend/src/pages/RocketSimulator.tsx index 4ddc827..7df95d8 100644 --- a/frontend/src/pages/RocketSimulator.tsx +++ b/frontend/src/pages/RocketSimulator.tsx @@ -157,6 +157,23 @@ function StackDiagram({ rocket }: { rocket: RocketConfig }) { fill="#b9c2c8" /> + {/* 栅格翼:助推器自己可回收才画,贴在顶部两侧 */} + {geo.boosterRecoverable && + [-1, 1].map((side) => { + const w = Math.max(3, br * 1.2); + const h = Math.max(2, br * 0.8); + return ( + + ); + })} {engineBells(geo.enginesPerBooster, geo.radius * geo.boosterDiameterRatio * 1.08, boosterTop + boosterH)} ); @@ -169,8 +186,8 @@ function StackDiagram({ rocket }: { rocket: RocketConfig }) { - {/* 栅格翼(细长单芯级回收构型) */} - {geo.gridFins && [-1, 1].map((direction) => { + {/* 栅格翼:一级登记为可回收才画 */} + {geo.coreRecoverable && [-1, 1].map((direction) => { const x = CX + direction * r1; const y = s1Top + s1H * 0.06; return ( @@ -402,6 +419,19 @@ function FlightData({ state, history, totalFuelPct, stage1FuelPct, stage2FuelPct /* Small mission-control widgets */ /* ------------------------------------------------------------------ */ +/** + * 仪表组的排列顺序:芯级放在正中,助推器平均分成左右两组挂在两侧, + * 与 3D 箭体的 boosterAngle(左右均分)同一口径,扫一眼就能对上号。 + * 只调整显示顺序,不改变每组的数量与大小。 + */ +function bankOrder(groups: EngineGroup[]): EngineGroup[] { + const strapped = groups.filter((group) => group.kind === 'booster'); + if (strapped.length < 2) return groups; + const others = groups.filter((group) => group.kind !== 'booster'); + const leftCount = Math.floor(strapped.length / 2); + return [...strapped.slice(0, leftCount), ...others, ...strapped.slice(leftCount)]; +} + /** * 发动机仪表组:芯级与每个助推器各自画一个圆环,助推器圆的直径按 * 「助推器直径 / 芯级直径」等比缩放,全部排在同一行、底部对齐, @@ -420,23 +450,20 @@ function EngineBank({ /** 助推器在一级飞行中段先分离,分离后其发动机不再点火 */ boostersLit?: boolean; }) { + // 芯级居中、助推器左右均分,只影响这一行的显示顺序 + const units = bankOrder(groups); const isLit = (group: EngineGroup) => (group.kind === 'core' ? lit : boostersLit); const total = groups.reduce((sum, group) => sum + group.engines, 0); const firing = groups.filter(isLit).reduce((sum, group) => sum + group.engines, 0); - const coreEngines = groups.filter((group) => group.kind === 'core').reduce((sum, group) => sum + group.engines, 0); - const boosters = groups.filter((group) => group.kind === 'booster'); // 直径以芯级为基准(radius = 1),按左侧栏可用宽度整体缩放,四助推器也放得下 const gap = 10; - const diameterSum = groups.reduce((sum, group) => sum + group.radius, 0); - const base = Math.max(18, Math.min(58, (268 - gap * (groups.length - 1)) / diameterSum)); - const caption = boosters.length > 0 - ? `${label} · 芯级 ${coreEngines} + 助推器 ${boosters.length}×${boosters[0].engines} · 共 ${total} 台` - : `${label} · 共 ${total} 台`; + const diameterSum = units.reduce((sum, group) => sum + group.radius, 0); + const base = Math.max(18, Math.min(58, (268 - gap * (units.length - 1)) / diameterSum)); return (
- {groups.map((group, index) => { + {units.map((group, index) => { const unit = base * group.radius; // 机数多的舱室缩小点位,避免十几个点糊成一团 const orbit = unit * 0.28; @@ -466,7 +493,6 @@ function EngineBank({ ); })}
- {caption}
); } @@ -646,6 +672,20 @@ export function RocketSimulator() { fetchRocketConfigs().then(setRockets).catch(() => setError('火箭配置加载失败,请确认数据库升级脚本已执行。')); }, []); + // 管理端改完参数、后端启动补完助推器口径之后,本页不会自己发现数据变了: + // 箭体外形与仪表圈都按打开页面时取到的直径比绘制,于是管理端明明填了 1, + // 这里还按旧值画。切回本页时静默重取一次,取到空列表则维持原状。 + useEffect(() => { + const refetch = () => { + if (document.visibilityState !== 'visible') return; + fetchRocketConfigs() + .then((next) => { if (next.length > 0) setRockets(next); }) + .catch(() => undefined); + }; + document.addEventListener('visibilitychange', refetch); + return () => document.removeEventListener('visibilitychange', refetch); + }, []); + if (error) return ; if (rockets.length === 0) return
正在载入发射任务
; return ; diff --git a/frontend/src/pages/admin/Rockets.tsx b/frontend/src/pages/admin/Rockets.tsx index 2ce2ece..cf1cbc9 100644 --- a/frontend/src/pages/admin/Rockets.tsx +++ b/frontend/src/pages/admin/Rockets.tsx @@ -35,6 +35,11 @@ function StageFields({ index }: { index: 1 | 2 }) { /> + {index === 1 && ( + + + + )} ); } @@ -42,14 +47,13 @@ function StageFields({ index }: { index: 1 | 2 }) { /** * 外观特征件(可选)。 * - * 助推器数量、每台的发动机数与直径都在「助推器参数」里登记,这里只放 - * 无法从参数推导的特征件;栅格翼留空时按「细长且多发动机的单芯级」推断。 + * 助推器数量、每台的发动机数与直径都在「助推器参数」里登记,栅格翼长在可回收的 + * 那一级上、由一级与助推器的「可回收」开关决定,这里只放整流罩这类特征件。 */ function AppearanceFields() { return ( - - + ); @@ -71,6 +75,7 @@ function BoosterFields() { + @@ -171,7 +176,17 @@ export function Rockets() { { title: '制造方', dataIndex: 'manufacturer', width: 180, render: (value) => value || '-' }, { title: '发射场', key: 'launch_site', width: 180, render: (_, record) =>
{record.launch_site_name}
{record.launch_latitude_deg.toFixed(4)}, {record.launch_longitude_deg.toFixed(4)}
}, { title: '总体尺寸', key: 'size', width: 130, render: (_, record) => {record.height_m} m × {record.diameter_m} m }, - { title: '一级', key: 'stage1', width: 170, render: (_, record) => {record.stage_1.engine_count} 台 / {(record.stage_1.max_thrust_n / 1_000_000).toFixed(2)} MN }, + // 一级只登记芯级,助推器单列在下一行,否则猎鹰重型这类型号看着像「助推器数据缺失」 + { + title: '一级', key: 'stage1', width: 190, render: (_, record) => ( +
+
芯级 {record.stage_1.engine_count} 台 / {(record.stage_1.max_thrust_n / 1_000_000).toFixed(2)} MN
+ {record.boosters?.count + ?
助推器 {record.boosters.count}×{record.boosters.engine_count ?? '-'} 台 · 直径比 {record.boosters.diameter_ratio ?? '-'}
+ : null} +
+ ), + }, { title: '二级', key: 'stage2', width: 170, render: (_, record) => {record.stage_2.engine_count} 台 / {(record.stage_2.max_thrust_n / 1_000_000).toFixed(2)} MN }, { title: '目标轨道', dataIndex: 'target_orbit_km', width: 110, render: (value) => `${value} km` }, ]; @@ -218,7 +233,9 @@ export function Rockets() { { key: 'stage2', label: '二级参数', children: }, { key: 'boosters', label: '助推器参数(可选)', children: }, { key: 'appearance', label: '外观特征件(可选)', children: }, - ]} /> + // 每个页签都预先渲染:没打开的页签字段不会登记,保存时 validateFields + // 拿不到那一组,会把助推器 / 外观参数当成「用户清空了」整体提交 null + ].map((item) => ({ ...item, forceRender: true }))} />