diff --git a/migrations/028_merit_nodes.sql b/migrations/028_merit_nodes.sql new file mode 100644 index 0000000..647530a --- /dev/null +++ b/migrations/028_merit_nodes.sql @@ -0,0 +1,18 @@ +-- Merit nodes: a ceremony node that awards an achievement when the playthrough +-- reaches it (Scene 8 — "The Barricelli Luggage"). The awarded flag is authored on +-- the node; the runtime grants it on arrival with node provenance. Merit nodes may +-- also carry a component_key for their ceremony presentation (e.g. a 3D model). + +ALTER TABLE osint.story_nodes DROP CONSTRAINT story_nodes_node_type_check; +ALTER TABLE osint.story_nodes ADD CONSTRAINT story_nodes_node_type_check + CHECK (node_type IN ('cutscene','dialogue','level','det_gate','llm_gate','merit')); + +-- Let a merit node carry a ceremony component_key (was cutscene/gate only). +ALTER TABLE osint.story_nodes DROP CONSTRAINT story_nodes_check1; +ALTER TABLE osint.story_nodes ADD CONSTRAINT story_nodes_check1 + CHECK (component_key IS NULL OR node_type IN ('cutscene','det_gate','llm_gate','merit')); + +ALTER TABLE osint.story_nodes ADD COLUMN awards_flag TEXT + CHECK (awards_flag IS NULL OR awards_flag ~ '^[a-z][a-z0-9_.-]{0,63}$'); +ALTER TABLE osint.story_nodes ADD CONSTRAINT story_nodes_awards_flag_type + CHECK (awards_flag IS NULL OR node_type = 'merit'); diff --git a/server/index.ts b/server/index.ts index e7d0814..f66b647 100644 --- a/server/index.ts +++ b/server/index.ts @@ -30,7 +30,7 @@ const textExtractor = createTextExtractorFromEnv() const levels = createLevelRepository(pool, editingEnabled, objectStorage) const narrative = createNarrativeRepository(pool, objectStorage) const storyGraph = createStoryGraphRepository(pool) -const STORY_NODE_TYPES: StoryNodeType[] = ['cutscene', 'dialogue', 'level', 'det_gate', 'llm_gate'] +const STORY_NODE_TYPES: StoryNodeType[] = ['cutscene', 'dialogue', 'level', 'det_gate', 'llm_gate', 'merit'] function wantsEdit(req: express.Request) { return editingEnabled && req.query.edit === '1' && hasAdminClaim(req) diff --git a/server/narrativeRepository.ts b/server/narrativeRepository.ts index 16c13e6..dd21c53 100644 --- a/server/narrativeRepository.ts +++ b/server/narrativeRepository.ts @@ -15,8 +15,9 @@ export type RuntimeUtterance = { poseUrl: string | null; text: string; childIds: string[]; terminalKey: string | null } export type RuntimeNode = { - id: string; kind: 'cutscene' | 'dialogue' | 'level'; label: string + id: string; kind: 'cutscene' | 'dialogue' | 'level' | 'merit'; label: string componentKey?: string | null; levelSlug?: string | null; musicUrl?: string | null; musicVolume?: number + awardsFlag?: string | null utterances?: RuntimeUtterance[]; rootId?: string | null } export type PlaythroughState = { playthrough: PlaythroughSummary; node: RuntimeNode | null } @@ -64,7 +65,14 @@ export interface NarrativeRepository { deletePose(npcId: string, poseKey: string): Promise } -type GraphNodeRow = { id: string; node_type: string; label: string; component_key: string | null; level_template_version_id: string | null; music_asset_id: string | null; music_volume: number } +type GraphNodeRow = { id: string; node_type: string; label: string; component_key: string | null; level_template_version_id: string | null; music_asset_id: string | null; music_volume: number; awards_flag?: string | null } + +// Grant a merit node's achievement to the player on arrival (idempotent, with node +// provenance). Called from the write paths that move current_node_id onto a node. +async function awardMeritWithin(client: PoolClient, playthroughId: string, node: { id: string; node_type: string; awards_flag?: string | null }) { + if (node.node_type !== 'merit' || !node.awards_flag) return + await client.query('INSERT INTO osint.achievements (playthrough_id,flag_key,awarded_by_node_id) VALUES ($1,$2,$3) ON CONFLICT (playthrough_id,flag_key) DO NOTHING', [playthroughId, node.awards_flag, node.id]) +} export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStorage): NarrativeRepository { // ---- Runtime: walking the story graph ------------------------------------- @@ -101,13 +109,14 @@ export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStora } async function resolveNodeForPlay(nodeId: string, levelSlug: string | null): Promise { - const node = (await pool.query('SELECT id,node_type,label,component_key,level_template_version_id,music_asset_id,music_volume FROM osint.story_nodes WHERE id=$1', [nodeId])).rows[0] + const node = (await pool.query('SELECT id,node_type,label,component_key,level_template_version_id,music_asset_id,music_volume,awards_flag FROM osint.story_nodes WHERE id=$1', [nodeId])).rows[0] if (!node) return null const musicUrl = node.music_asset_id ? `/api/assets/${node.music_asset_id}` : null const musicVolume = node.music_volume / 100 if (node.node_type === 'cutscene') return { id: node.id, kind: 'cutscene', label: node.label, componentKey: node.component_key, musicUrl, musicVolume } if (node.node_type === 'level') return { id: node.id, kind: 'level', label: node.label, levelSlug, musicUrl, musicVolume } if (node.node_type === 'dialogue') return { id: node.id, kind: 'dialogue', label: node.label, musicUrl, musicVolume, ...(await resolveDialogueGraph(node.id)) } + if (node.node_type === 'merit') return { id: node.id, kind: 'merit', label: node.label, componentKey: node.component_key, awardsFlag: node.awards_flag, musicUrl, musicVolume } return null // gates are auto-resolved during advance and never surfaced } @@ -115,7 +124,7 @@ export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStora async function resolveThroughGates(client: PoolClient, nodeId: string | null): Promise { let current = nodeId for (let guard = 0; guard < 50 && current; guard++) { - const node = (await client.query('SELECT id,node_type,label,component_key,level_template_version_id FROM osint.story_nodes WHERE id=$1', [current])).rows[0] + const node = (await client.query('SELECT id,node_type,label,component_key,level_template_version_id,awards_flag FROM osint.story_nodes WHERE id=$1', [current])).rows[0] if (!node) return null if (node.node_type !== 'det_gate' && node.node_type !== 'llm_gate') return node const next = await client.query<{ to_node_id: string | null }>('SELECT to_node_id FROM osint.story_node_terminals WHERE parent_node_id=$1 ORDER BY sort_order LIMIT 1', [current]) @@ -224,6 +233,7 @@ export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStora playthroughId = randomUUID() await client.query('INSERT INTO osint.playthroughs (id,user_id,mystery_id,current_node_id,current_level_id) VALUES ($1,$2,$3,$4,$5)', [playthroughId, userId, mystery.id, entry.id, levelId]) + await awardMeritWithin(client, playthroughId, entry) await client.query('COMMIT') } catch (error) { await client.query('ROLLBACK'); throw error } finally { client.release() } return stateForPlaythrough(playthroughId) @@ -263,12 +273,13 @@ export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStora `SELECT p.mystery_id, m.slug AS mystery_slug FROM osint.playthroughs p JOIN osint.mysteries m ON m.id=p.mystery_id WHERE p.id=$1 AND p.user_id=$2 FOR UPDATE OF p`, [playthroughId, userId])).rows[0] if (!playthrough) { await client.query('ROLLBACK'); return { ok: false, error: 'Playthrough not found' } } - const node = (await client.query<{ id: string; node_type: string; level_template_version_id: string | null }>( - 'SELECT id,node_type,level_template_version_id FROM osint.story_nodes WHERE id=$1 AND mystery_id=$2', [nodeId, playthrough.mystery_id])).rows[0] + const node = (await client.query<{ id: string; node_type: string; level_template_version_id: string | null; awards_flag: string | null }>( + 'SELECT id,node_type,level_template_version_id,awards_flag FROM osint.story_nodes WHERE id=$1 AND mystery_id=$2', [nodeId, playthrough.mystery_id])).rows[0] if (!node) { await client.query('ROLLBACK'); return { ok: false, error: 'Node not found' } } const levelId = node.node_type === 'level' && node.level_template_version_id ? await instantiateLevel(client, node.level_template_version_id, playthrough.mystery_slug) : null await client.query(`UPDATE osint.playthroughs SET status='active',current_node_id=$2,current_level_id=$3,updated_at=NOW() WHERE id=$1`, [playthroughId, node.id, levelId]) + await awardMeritWithin(client, playthroughId, node) await client.query('COMMIT') } catch (error) { await client.query('ROLLBACK'); throw error } finally { client.release() } const state = await stateForPlaythrough(playthroughId) @@ -298,6 +309,7 @@ export function createNarrativeRepository(pool: Pool, objectStorage: ObjectStora const levelId = target.node_type === 'level' && target.level_template_version_id ? await instantiateLevel(client, target.level_template_version_id, playthrough.mystery_slug) : null await client.query('UPDATE osint.playthroughs SET current_node_id=$2,current_level_id=$3,updated_at=NOW() WHERE id=$1', [playthroughId, target.id, levelId]) + await awardMeritWithin(client, playthroughId, target) } await client.query('COMMIT') } catch (error) { await client.query('ROLLBACK'); throw error } finally { client.release() } diff --git a/server/storyGraphRepository.ts b/server/storyGraphRepository.ts index 9c3d4f7..bfc74c1 100644 --- a/server/storyGraphRepository.ts +++ b/server/storyGraphRepository.ts @@ -9,11 +9,12 @@ export type GraphSpecNode = { } export type GraphSpec = { entry: string; nodes: GraphSpecNode[] } -export type StoryNodeType = 'cutscene' | 'dialogue' | 'level' | 'det_gate' | 'llm_gate' +export type StoryNodeType = 'cutscene' | 'dialogue' | 'level' | 'det_gate' | 'llm_gate' | 'merit' export type TerminalDto = { id: string; terminalKey: string; label: string; toNodeId: string | null; sortOrder: number } export type StoryNodeDto = { id: string; nodeType: StoryNodeType; label: string; hasUtterances: boolean xpos: number; ypos: number; levelTemplateVersionId: string | null; componentKey: string | null; musicAssetId: string | null; musicVolume: number + awardsFlag: string | null terminals: TerminalDto[] } export type StoryGraphDto = { mysteryId: string; entryNodeId: string | null; nodes: StoryNodeDto[] } @@ -32,12 +33,13 @@ const DEFAULT_TERMINALS: Record level: [{ key: 'report_back', label: 'Report back' }], det_gate: [{ key: 'pass', label: 'Pass' }], llm_gate: [{ key: 'pass', label: 'Pass' }], + merit: [{ key: 'continue', label: 'Continue' }], } export interface StoryGraphRepository { getGraph(mysteryId: string): Promise createNode(mysteryId: string, input: { nodeType: StoryNodeType; xpos: number; ypos: number; label?: string }): Promise - updateNode(nodeId: string, input: Partial<{ label: string; xpos: number; ypos: number; hasUtterances: boolean; componentKey: string | null; levelTemplateVersionId: string | null; musicAssetId: string | null; musicVolume: number }>): Promise + updateNode(nodeId: string, input: Partial<{ label: string; xpos: number; ypos: number; hasUtterances: boolean; componentKey: string | null; levelTemplateVersionId: string | null; musicAssetId: string | null; musicVolume: number; awardsFlag: string | null }>): Promise deleteNode(nodeId: string): Promise addTerminal(nodeId: string, input: { terminalKey: string; label?: string }): Promise updateTerminal(terminalId: string, input: Partial<{ label: string; sortOrder: number; toNodeId: string | null }>): Promise<{ ok: boolean; error?: string }> @@ -61,8 +63,8 @@ export function createStoryGraphRepository(pool: Pool): StoryGraphRepository { const mystery = await pool.query<{ id: string; entry_node_id: string | null }>('SELECT id,entry_node_id FROM osint.mysteries WHERE id=$1', [mysteryId]) if (!mystery.rows[0]) return null const [nodes, terminals] = await Promise.all([ - pool.query<{ id: string; node_type: StoryNodeType; label: string; has_utterances: boolean; xpos: number; ypos: number; level_template_version_id: string | null; component_key: string | null; music_asset_id: string | null; music_volume: number }>( - 'SELECT id,node_type,label,has_utterances,xpos,ypos,level_template_version_id,component_key,music_asset_id,music_volume FROM osint.story_nodes WHERE mystery_id=$1 ORDER BY created_at', [mysteryId]), + pool.query<{ id: string; node_type: StoryNodeType; label: string; has_utterances: boolean; xpos: number; ypos: number; level_template_version_id: string | null; component_key: string | null; music_asset_id: string | null; music_volume: number; awards_flag: string | null }>( + 'SELECT id,node_type,label,has_utterances,xpos,ypos,level_template_version_id,component_key,music_asset_id,music_volume,awards_flag FROM osint.story_nodes WHERE mystery_id=$1 ORDER BY created_at', [mysteryId]), pool.query<{ id: string; parent_node_id: string; terminal_key: string; label: string; to_node_id: string | null; sort_order: number }>( `SELECT t.id,t.parent_node_id,t.terminal_key,t.label,t.to_node_id,t.sort_order FROM osint.story_node_terminals t JOIN osint.story_nodes n ON n.id=t.parent_node_id WHERE n.mystery_id=$1 ORDER BY t.sort_order,t.terminal_key`, [mysteryId]), @@ -78,6 +80,7 @@ export function createStoryGraphRepository(pool: Pool): StoryGraphRepository { nodes: nodes.rows.map(row => ({ id: row.id, nodeType: row.node_type, label: row.label, hasUtterances: row.has_utterances, xpos: row.xpos, ypos: row.ypos, levelTemplateVersionId: row.level_template_version_id, componentKey: row.component_key, musicAssetId: row.music_asset_id, musicVolume: row.music_volume, + awardsFlag: row.awards_flag, terminals: byNode.get(row.id) || [], })), } @@ -119,6 +122,7 @@ export function createStoryGraphRepository(pool: Pool): StoryGraphRepository { if (input.levelTemplateVersionId !== undefined) set('level_template_version_id', input.levelTemplateVersionId || null) if (input.musicAssetId !== undefined) set('music_asset_id', input.musicAssetId || null) if (input.musicVolume !== undefined) set('music_volume', Math.max(0, Math.min(100, Math.round(input.musicVolume)))) + if (input.awardsFlag !== undefined) set('awards_flag', input.awardsFlag?.trim() || null) if (sets.length) await pool.query(`UPDATE osint.story_nodes SET ${sets.join(',')} WHERE id=$1`, values) const graph = await loadGraph(mysteryId) return graph?.nodes.find(node => node.id === nodeId) ?? null diff --git a/src/narrative.tsx b/src/narrative.tsx index 86dafa1..1557f9f 100644 --- a/src/narrative.tsx +++ b/src/narrative.tsx @@ -2,7 +2,7 @@ import { useEffect, useMemo, useRef, useState, type FC } from 'react' import { audio } from './audio' export type RuntimeUtterance = { id: string; utterer: 'npc' | 'player'; speaker: { name: string; role: string }; poseUrl: string | null; text: string; childIds: string[]; terminalKey: string | null } -export type RuntimeNode = { id: string; kind: 'cutscene' | 'dialogue' | 'level'; label: string; componentKey?: string | null; levelSlug?: string | null; musicUrl?: string | null; musicVolume?: number; utterances?: RuntimeUtterance[]; rootId?: string | null } +export type RuntimeNode = { id: string; kind: 'cutscene' | 'dialogue' | 'level' | 'merit'; label: string; componentKey?: string | null; levelSlug?: string | null; musicUrl?: string | null; musicVolume?: number; awardsFlag?: string | null; utterances?: RuntimeUtterance[]; rootId?: string | null } export type PlaythroughSummary = { id: string; mysterySlug: string; levelSlug: string | null; status: string } export type PlaythroughState = { playthrough: PlaythroughSummary; node: RuntimeNode | null } @@ -48,6 +48,25 @@ const GlassHarbourDiversion: FC<{ onComplete: () => void }> = ({ onComplete }) = const CUTSCENE_REGISTRY: Record void }>> = { 'glass-harbour-diversion': GlassHarbourDiversion } export const CUTSCENE_COMPONENT_KEYS = Object.keys(CUTSCENE_REGISTRY) +// Merit ceremony components, keyed by a merit node's component_key (e.g. a 3D +// award model). The achievement itself is granted server-side on arrival; this is +// purely the presentation of receiving it. +const MERIT_REGISTRY: Record void }>> = {} +export const MERIT_COMPONENT_KEYS = Object.keys(MERIT_REGISTRY) + +export function MeritHost({ componentKey, label, awardsFlag, onComplete }: { componentKey: string | null | undefined; label: string; awardsFlag?: string | null; onComplete: () => void }) { + const Component = componentKey ? MERIT_REGISTRY[componentKey] : undefined + if (Component) return + return
+
+ ◆ MERIT AWARDED ◆ +

{label}

+ {awardsFlag &&

🏅 {awardsFlag}

} + +
+
+} + export function CutsceneHost({ componentKey, label, onComplete }: { componentKey: string | null | undefined; label: string; onComplete: () => void }) { const Component = componentKey ? CUTSCENE_REGISTRY[componentKey] : undefined if (Component) return diff --git a/src/play.tsx b/src/play.tsx index bbede76..c074154 100644 --- a/src/play.tsx +++ b/src/play.tsx @@ -1,5 +1,5 @@ import { useCallback, useEffect, useState } from 'react' -import { CutsceneHost, DialoguePlayer, type PlaythroughState } from './narrative' +import { CutsceneHost, DialoguePlayer, MeritHost, type PlaythroughState } from './narrative' type MysterySummary = { slug: string; title: string } @@ -93,6 +93,7 @@ export function Play() { const node = state?.node if (!node) return
GU

GLITCH UNIVERSITY NETWORK TERMINAL

{status || 'OPENING CASE FILE…'}
if (node.kind === 'cutscene') return { void advance() }} /> + if (node.kind === 'merit') return { void advance() }} /> if (node.kind === 'dialogue' && node.utterances) return { void advance(terminalKey) }} /> return
GU
{node.label}
} diff --git a/src/styles.css b/src/styles.css index d5e8fb9..3a3e27a 100644 --- a/src/styles.css +++ b/src/styles.css @@ -520,6 +520,9 @@ button:focus-visible { outline: 2px solid #e99a44; outline-offset: 2px; } /* Story-graph runtime: cutscene title card + report-back */ .cutscene-card { position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; background: #04110e; cursor: pointer; animation: dialogue-in .3s ease; } +.merit-card { background: radial-gradient(120% 90% at 50% 35%, #16240f 0%, #0a1408 60%, #04110e 100%); } +.merit-card .merit-eyebrow { color: #cdea6a; } +.merit-flag { font: 13px IBM Plex Mono; letter-spacing: .12em; color: #d58a46; margin: 0; } .title-card-inner { display: grid; justify-items: center; text-align: center; gap: 18px; animation: title-rise 1.1s cubic-bezier(.2,.7,.2,1); } @keyframes title-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } } .title-card-inner small { font: 10px IBM Plex Mono; letter-spacing: .3em; color: #7f9a92; text-transform: uppercase; }