// Sponsor and starter contract selection with live 3D GLTF payload preview. // Self explanatory import { api } from "../api.js"; import { navigate } from "../router.js"; import { refreshMe, refreshWorld } from "../session.js"; import { state } from "../state.js"; import { encodeModelPath, loadFittedModel } from "../three/modelLoader.js"; import { createView } from "../three/sceneManager.js"; import { getDifficultyProfile, normalizeDifficulty, } from "../../shared/physics.js"; import type { AgencyState, ContractOffer } from "../../shared/types.js"; import { clear, fmtMoney, fmtNum, h, term, toast } from "../ui.js"; export function renderOnboarding(root: HTMLElement): () => void { const sponsors = state.sponsors; const isSandbox = normalizeDifficulty(state.agency?.difficulty) === "sandbox"; const starterMissions = isSandbox ? state.missions : state.missions.filter((m) => m.tier <= 1); let selectedSponsorId = sponsors[0]?.id ?? "nasa"; let selectedMissionId = starterMissions[0]?.id ?? state.missions[0]?.id ?? ""; const previewBox = h("div", { class: "onboard-preview-box" }); const view = createView(previewBox, { fov: 42 }); view.camera.position.set(16, 10, 18); view.controls.target.set(0, 0, 0); view.controls.autoRotate = true; view.controls.autoRotateSpeed = 1.1; let currentPreviewModel: any = null; const previewCaption = h( "div", { class: "mono dim", style: { fontSize: "11px" } }, "", ); async function showPayloadPreview(missionId: string) { const mission = state.missions.find((m) => m.id === missionId); const payloadPart = state.parts.find((p) => p.id === mission?.recommendedPayloadId) ?? state.parts.find((p) => p.kind === "payload"); if (currentPreviewModel) { view.scene.remove(currentPreviewModel); currentPreviewModel = null; } if (payloadPart?.modelFile) { previewCaption.textContent = `Loading NASA 3D payload: ${payloadPart.name}...`; try { const mesh = await loadFittedModel( encodeModelPath(payloadPart.modelFile), { height: 9, radius: 5.5, anchorBottom: false, }, ); view.scene.add(mesh); currentPreviewModel = mesh; previewCaption.textContent = `${payloadPart.name} • ${fmtNum(mission?.payloadMass ?? payloadPart.dryMass)} kg`; } catch (err) { previewCaption.textContent = `GLB preview unavailable (${(err as Error).message})`; } } else { previewCaption.textContent = mission ? `${mission.targetName} Payload` : "Payload Preview"; } } const sponsorCardsEl = h("div", { class: "card-grid", style: { gridTemplateColumns: "repeat(auto-fill, minmax(230px, 1fr))" }, }); const missionCardsEl = h("div", { class: "card-grid", style: { gridTemplateColumns: "repeat(auto-fill, minmax(260px, 1fr))" }, }); const summaryBox = h("div", { class: "panel", style: { padding: "14px" } }); const confirmBtn = h( "button", { class: "btn primary", style: { width: "100%" } }, "Sign Contract & Open Hangar", ); function renderSelections() { clear(sponsorCardsEl); for (const sp of sponsors) { const card = h( "div", { class: `card ${sp.id === selectedSponsorId ? "selected" : ""}`, onClick: () => { selectedSponsorId = sp.id; renderSelections(); }, }, h( "div", { class: "spread" }, h("h3", {}, sp.name), h("span", { class: "badge info" }, sp.flag), ), h("p", { class: "sub" }, sp.description), h( "div", { class: "meta" }, h("span", {}, `Budget ${Math.round(sp.budgetMult * 100)}%`), h("span", {}, `Waiver ${Math.round(sp.waiverCostMult * 100)}%`), h("span", {}, `Rep ${Math.round(sp.reputationMult * 100)}%`), ), ); sponsorCardsEl.appendChild(card); } clear(missionCardsEl); for (const m of starterMissions) { const card = h( "div", { class: `card ${m.id === selectedMissionId ? "selected" : ""}`, onClick: () => { selectedMissionId = m.id; renderSelections(); void showPayloadPreview(m.id); }, }, h( "div", { class: "spread" }, h("h3", {}, m.name), h("span", { class: "badge ok" }, m.targetName), ), h("p", { class: "sub" }, m.description), h( "div", { class: "meta" }, h("span", {}, `Budget ${fmtMoney(m.budget)}`), h("span", {}, term(`Δv ${fmtNum(m.requiredDv)} m/s`, "req_dv")), h("span", {}, `Payload ${fmtNum(m.payloadMass)} kg`), ), ); missionCardsEl.appendChild(card); } const sp = sponsors.find((s) => s.id === selectedSponsorId); const ms = state.missions.find((m) => m.id === selectedMissionId); const profile = getDifficultyProfile(state.agency?.difficulty); const seedBudget = sp && ms ? Math.round(profile.startingFunds * sp.budgetMult + ms.budget * 0.35) : profile.startingFunds; clear(summaryBox); summaryBox.appendChild( h( "div", { class: "col" }, h("h3", {}, "Program Grant Summary"), h( "div", { class: "stat-row" }, h("span", { class: "k" }, term("Difficulty Tier", "difficulty")), h("span", { class: "v info" }, profile.label), ), h( "div", { class: "stat-row" }, h("span", { class: "k" }, "Sponsor"), h("span", { class: "v accent" }, sp?.name ?? "-"), ), h( "div", { class: "stat-row" }, h("span", { class: "k" }, "Initial Contract"), h("span", { class: "v" }, ms?.name ?? "-"), ), h( "div", { class: "stat-row" }, h("span", { class: "k" }, "Seed + 35% Advance"), h("span", { class: "v green" }, fmtMoney(seedBudget)), ), h( "div", { class: "stat-row" }, h("span", { class: "k" }, term("Target Orbit", "apoapsis")), h( "span", { class: "v" }, `${ms?.targetOrbit?.periapsisKm ?? 200}×${ms?.targetOrbit?.apoapsisKm ?? 220} km`, ), ), ), ); } confirmBtn.addEventListener("click", async () => { if (!selectedSponsorId || !selectedMissionId) { toast("Select both a sponsor and an inaugural mission.", "err"); return; } confirmBtn.disabled = true; try { const res = await api.post<{ agency: AgencyState; contract: ContractOffer; }>("/api/agency/onboard", { sponsorId: selectedSponsorId, missionId: selectedMissionId, }); state.agency = res.agency; await refreshMe(); await refreshWorld(); toast( `Funded by ${selectedSponsorId.toUpperCase()} — proceed to build your first launch vehicle.`, "ok", ); navigate("dashboard"); } catch (err) { toast((err as Error).message, "err"); confirmBtn.disabled = false; } }); renderSelections(); if (selectedMissionId) void showPayloadPreview(selectedMissionId); const leftCol = h( "div", { class: "onboard-scroll col" }, h( "div", { class: "spread" }, h( "div", { class: "col", style: { gap: "4px" } }, h("h2", {}, "Inaugural Funding & Mission Charter"), h("p", { class: "dim" }, `Agency: ${state.agency?.name ?? "Unknown"}`), ), h("span", { class: "badge info" }, "Step 1 of 2"), ), h("h3", { class: "accent" }, "1. Select Space Sponsor"), sponsorCardsEl, h( "h3", { class: "accent", style: { marginTop: "8px" } }, "2. Select Inaugural Tier-1 Contract", ), missionCardsEl, ); const rightCol = h( "div", { class: "onboard-preview" }, h("h3", {}, "Recommended Payload Hardware (3D)"), previewBox, previewCaption, summaryBox, confirmBtn, ); root.appendChild( h("div", { class: "screen onboard-layout" }, leftCol, rightCol), ); return () => { view.dispose(); }; }