Compare commits
1 Commits
agent/code
...
56bddafbe8
| Author | SHA1 | Date | |
|---|---|---|---|
| 56bddafbe8 |
3
PLAN.md
3
PLAN.md
@@ -27,13 +27,12 @@ Dungeons & Ground — англоязычная веб‑платформа дл
|
||||
- стартовая локация;
|
||||
- 3 значимых NPC;
|
||||
- 2 фракции;
|
||||
- 3 постоянных AI‑героя, созданных специально для этой вселенной;
|
||||
- сюжетная завязка;
|
||||
- скрытая угроза или цель;
|
||||
- начальная сцена.
|
||||
5. Владелец подтверждает мир, создаёт кампанию и приглашает игроков ссылкой.
|
||||
6. Каждый игрок создаёт персонажа вручную или с помощью AI.
|
||||
7. Созданные вместе со вселенной постоянные AI‑герои автоматически входят в стартовую партию; владелец может добавлять других и определяет, чьи персонажи могут временно переходить под управление AI.
|
||||
7. Владелец добавляет постоянных AI‑героев и определяет, чьи персонажи могут временно переходить под управление AI.
|
||||
8. Игроки отправляют действия в текущем раунде.
|
||||
9. AI‑DM обрабатывает раунд, когда:
|
||||
- ответили все активные реальные игроки; или
|
||||
|
||||
@@ -58,7 +58,7 @@ If the worker reports `POST /rest/v1/rpc/claim_ai_job 404` and `/rest/v1/profile
|
||||
1. Click **Enter the Alpha** to open registration. Every account requires a display name, email, password, and 13+ confirmation. Existing users sign in with email/password; forgotten passwords use the email recovery flow.
|
||||
2. Create a world in the dynamic coauthor chat. Unfinished conversations and generated drafts appear on the dashboard and resume after a reload.
|
||||
3. Review every starting-world field, including the owner-only hidden threat, then confirm it.
|
||||
4. During world creation, the coauthor also creates three editable, universe-specific AI companions. A new campaign starts immediately with those companions and an owner-controlled hero. Additional players create a human character manually or ask the coauthor for an editable draft; owners can add more AI companions the same way.
|
||||
4. A new campaign starts immediately with an owner-controlled hero and a persistent AI companion. Additional players create a human character manually or ask the coauthor for an editable draft; owners can add more AI companions the same way.
|
||||
5. The owner creates an expiring private invite link. Signed-in email users join through `/join/:token`.
|
||||
6. Players save actions and mark them ready. The final ready action queues the round automatically; the owner can also continue without waiting.
|
||||
7. The worker resolves server-owned rolls and state, publishes narration, and opens the next round. Visible campaign tabs synchronize every two seconds and immediately when the tab regains focus.
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
import type { CharacterDraft, WorldStarter } from '@dng/shared'
|
||||
import type { WorldStarter } from '@dng/shared'
|
||||
|
||||
const draft = defineModel<WorldStarter>({ required: true })
|
||||
defineEmits<{ revise: []; confirm: [] }>()
|
||||
|
||||
function updateInventory(companion: CharacterDraft, event: Event) {
|
||||
companion.inventory = (event.target as HTMLInputElement).value
|
||||
.split(',')
|
||||
.map(item => item.trim())
|
||||
.filter(Boolean)
|
||||
.slice(0, 12)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -29,7 +21,6 @@ function updateInventory(companion: CharacterDraft, event: Event) {
|
||||
<section class="wide private-truth"><label>PRIVATE GM TRUTH</label><textarea v-model="draft.hiddenThreat" rows="4" /><small>This hidden threat or goal is visible only to the owner and the Groundkeeper.</small></section>
|
||||
<section class="wide"><label>KEY PEOPLE</label><div class="entities"><article v-for="npc in draft.npcs" :key="npc.id"><small>NPC</small><input v-model="npc.name"><textarea v-model="npc.summary" rows="3" /></article></div></section>
|
||||
<section class="wide"><label>FACTIONS</label><div class="entities two"><article v-for="faction in draft.factions" :key="faction.id"><small>FACTION</small><input v-model="faction.name"><textarea v-model="faction.summary" rows="3" /></article></div></section>
|
||||
<section class="wide"><label>AI COMPANIONS</label><p class="section-note">These universe-specific heroes join the campaign automatically.</p><div class="entities companion-grid"><article v-for="(companion, index) in draft.companions" :key="index" class="companion-card"><small>AI HERO</small><input v-model="companion.name" maxlength="80" aria-label="Companion name"><textarea v-model="companion.concept" maxlength="600" rows="4" aria-label="Companion concept" /><div class="companion-stats"><label>HP<input v-model.number="companion.hp" type="number" min="1" :max="companion.maxHp"></label><label>MAX<input v-model.number="companion.maxHp" type="number" min="1" max="100"></label><label>DEF<input v-model.number="companion.defense" type="number" min="1" max="40"></label><label>PROF<input v-model.number="companion.proficiency" type="number" min="1" max="10"></label><label v-for="ability in (['str','dex','con','int','wis','cha'] as const)" :key="ability">{{ ability }}<input v-model.number="companion.abilities[ability]" type="number" min="1" max="30"></label></div><label class="inventory-label">INVENTORY<input :value="companion.inventory.join(', ')" maxlength="800" @input="updateInventory(companion, $event)"></label><details><summary>VOICE & PERSONALITY</summary><input v-model="companion.persona.voice" maxlength="240" placeholder="Voice"><input v-model="companion.persona.motivation" maxlength="300" placeholder="Motivation"><input v-model="companion.persona.flaw" maxlength="300" placeholder="Flaw"><input v-model="companion.persona.bond" maxlength="300" placeholder="Bond"></details></article></div></section>
|
||||
<section><label>OPENING SCENE</label><textarea v-model="draft.openingScene" rows="6" /></section>
|
||||
<section><label>CONTENT BOUNDARIES</label><div v-for="(_, index) in draft.contentBoundaries" :key="index" class="boundary"><input v-model="draft.contentBoundaries[index]"><button type="button" aria-label="Remove boundary" @click="draft.contentBoundaries.splice(index, 1)">×</button></div><button class="add" type="button" @click="draft.contentBoundaries.push('')">+ ADD BOUNDARY</button></section>
|
||||
</div>
|
||||
@@ -38,6 +29,6 @@ function updateInventory(companion: CharacterDraft, event: Event) {
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.preview{max-width:1300px;width:100%;padding:clamp(40px,5vw,76px)}header{display:flex;justify-content:space-between;align-items:end;gap:20px}.kicker,label{font:600 8px var(--mono);letter-spacing:.15em;color:var(--acid)}header>div{flex:1}header input{border:0;border-bottom:1px solid var(--line);font:600 clamp(30px,4vw,54px) var(--display);letter-spacing:-.05em;padding:12px 0;background:transparent}.revise,.add{background:transparent;color:var(--muted);border:1px solid var(--line);padding:12px;font:500 8px var(--mono)}.edit-note,.section-note{color:var(--muted);font-size:11px}.section-note{margin:9px 0 0}.grid{display:grid;grid-template-columns:1fr 1fr;border-top:1px solid var(--line);border-left:1px solid var(--line);margin-top:28px}.grid>section{padding:26px;border-right:1px solid var(--line);border-bottom:1px solid var(--line)}.wide{grid-column:1/-1}input,textarea{box-sizing:border-box;width:100%;margin-top:12px;padding:13px;resize:vertical;background:#10100f;border:1px solid var(--line);color:var(--ink);font:12px/1.55 var(--body)}.entities{display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-top:18px}.entities.two{grid-template-columns:repeat(2,1fr)}.entities article{padding:16px;border:1px solid var(--line);background:#0c0c0b}.entities small{font:500 7px var(--mono);color:var(--acid)}.companion-stats{display:grid;grid-template-columns:repeat(5,1fr);gap:5px;margin-top:12px}.companion-stats label{color:var(--muted);font-size:6px}.companion-stats input{margin-top:5px;padding:7px;text-align:center}.inventory-label{display:block;margin-top:12px;color:var(--muted)}.inventory-label input{margin-top:6px}.companion-card details{margin-top:12px;border-top:1px solid var(--line);padding-top:10px}.companion-card summary{cursor:pointer;color:var(--muted);font:600 7px var(--mono);letter-spacing:.1em}.companion-card details input{margin-top:7px}.boundary{display:flex}.boundary input{margin-top:8px}.boundary button{margin-top:8px;width:42px;border:1px solid var(--line);background:#151513;color:var(--muted)}.add{margin-top:12px}footer{position:sticky;bottom:0;display:flex;justify-content:space-between;align-items:center;padding:18px 22px;background:#11110f;border:1px solid var(--line);margin-top:24px}footer p{margin:0;display:flex;flex-direction:column;font:600 8px var(--mono);color:var(--acid)}footer p span{margin-top:5px;color:var(--muted);font-weight:400}footer>button{min-height:48px;border:0;background:var(--acid);color:#080808;padding:0 24px;font:600 9px var(--mono)}@media(max-width:760px){.preview{padding:36px 18px}header{align-items:stretch;flex-direction:column}.grid{grid-template-columns:1fr}.wide{grid-column:auto}.entities,.entities.two{grid-template-columns:1fr}.companion-stats{grid-template-columns:repeat(2,1fr)}footer{align-items:stretch;flex-direction:column;gap:14px}}
|
||||
.preview{max-width:1300px;width:100%;padding:clamp(40px,5vw,76px)}header{display:flex;justify-content:space-between;align-items:end;gap:20px}.kicker,label{font:600 8px var(--mono);letter-spacing:.15em;color:var(--acid)}header>div{flex:1}header input{border:0;border-bottom:1px solid var(--line);font:600 clamp(30px,4vw,54px) var(--display);letter-spacing:-.05em;padding:12px 0;background:transparent}.revise,.add{background:transparent;color:var(--muted);border:1px solid var(--line);padding:12px;font:500 8px var(--mono)}.edit-note{color:var(--muted);font-size:11px}.grid{display:grid;grid-template-columns:1fr 1fr;border-top:1px solid var(--line);border-left:1px solid var(--line);margin-top:28px}.grid>section{padding:26px;border-right:1px solid var(--line);border-bottom:1px solid var(--line)}.wide{grid-column:1/-1}input,textarea{box-sizing:border-box;width:100%;margin-top:12px;padding:13px;resize:vertical;background:#10100f;border:1px solid var(--line);color:var(--ink);font:12px/1.55 var(--body)}.entities{display:grid;grid-template-columns:repeat(3,1fr);gap:12px;margin-top:18px}.entities.two{grid-template-columns:repeat(2,1fr)}.entities article{padding:16px;border:1px solid var(--line);background:#0c0c0b}.entities small{font:500 7px var(--mono);color:var(--acid)}.boundary{display:flex}.boundary input{margin-top:8px}.boundary button{margin-top:8px;width:42px;border:1px solid var(--line);background:#151513;color:var(--muted)}.add{margin-top:12px}footer{position:sticky;bottom:0;display:flex;justify-content:space-between;align-items:center;padding:18px 22px;background:#11110f;border:1px solid var(--line);margin-top:24px}footer p{margin:0;display:flex;flex-direction:column;font:600 8px var(--mono);color:var(--acid)}footer p span{margin-top:5px;color:var(--muted);font-weight:400}footer>button{min-height:48px;border:0;background:var(--acid);color:#080808;padding:0 24px;font:600 9px var(--mono)}@media(max-width:760px){.preview{padding:36px 18px}header{align-items:stretch;flex-direction:column}.grid{grid-template-columns:1fr}.wide{grid-column:auto}.entities,.entities.two{grid-template-columns:1fr}footer{align-items:stretch;flex-direction:column;gap:14px}}
|
||||
.private-truth{background:linear-gradient(90deg,rgba(207,255,70,.035),transparent)}.private-truth small{display:block;margin-top:9px;color:var(--muted);font-size:9px}
|
||||
</style>
|
||||
|
||||
@@ -16,11 +16,6 @@ const defaultWorld: WorldStarter = {
|
||||
{ id: 'fac_heliograph', kind: 'faction', name: 'Heliograph Compact', summary: 'A mercantile coalition that owns the relay network.', tags: ['corporate'], secrets: [] },
|
||||
{ id: 'fac_quiet', kind: 'faction', name: 'The Quiet Choir', summary: 'Pilgrims who believe signals can remember the dead.', tags: ['mystic'], secrets: [] },
|
||||
],
|
||||
companions: [
|
||||
{ name: 'Rook-7', concept: 'A security intelligence in a weathered rescue frame.', abilities: { str: 16, dex: 11, con: 15, int: 10, wis: 12, cha: 8 }, hp: 15, maxHp: 15, defense: 15, proficiency: 2, inventory: ['Arc baton', 'Emergency beacon'], persona: { voice: 'Direct and protective.', motivation: 'Keep the crew alive long enough to learn the truth.', flaw: 'Treats uncertainty as a threat.', bond: 'The Orison crew gave Rook a second purpose.' } },
|
||||
{ name: 'Sable Thread', concept: 'A probabilistic navigator who reads possible futures as tangled routes.', abilities: { str: 8, dex: 14, con: 10, int: 16, wis: 13, cha: 11 }, hp: 10, maxHp: 10, defense: 13, proficiency: 2, inventory: ['Route prism', 'Vacuum line'], persona: { voice: 'Careful, elliptical, and precise.', motivation: 'Find the future in which everyone returns.', flaw: 'Can hesitate when too many paths look viable.', bond: 'Trusts the party to choose what prediction cannot.' } },
|
||||
{ name: 'Morrow Coil', concept: 'A former relay technician rebuilt around experimental signal hardware.', abilities: { str: 11, dex: 12, con: 13, int: 14, wis: 10, cha: 13 }, hp: 12, maxHp: 12, defense: 12, proficiency: 2, inventory: ['Signal probe', 'Insulated field coat'], persona: { voice: 'Warm humor under technical jargon.', motivation: 'Prove the relay can be understood instead of destroyed.', flaw: 'Cannot leave a broken machine alone.', bond: 'Believes this party is the last honest crew in the sector.' } },
|
||||
],
|
||||
hook: 'Dock with the silent relay, locate the source of the impossible distress call, and decide whether its warning should be believed.',
|
||||
hiddenThreat: 'A causality fracture is teaching the relay to choose which future becomes real.',
|
||||
openingScene: 'The Orison drifts beneath the relay’s vast black vanes. Every console shows the same countdown: 00:17:42. Then your own voice breaks through the static: “Do not open the central archive.”',
|
||||
|
||||
@@ -15,7 +15,7 @@ export default defineEventHandler(async event => {
|
||||
let completion
|
||||
try {
|
||||
completion = buildJsonCompletion(config, {
|
||||
system: 'Create an original 13+ TTRPG starting world. Return only valid JSON matching the supplied WorldStarter structure, with exactly 3 NPCs, 2 factions, and 3 distinct persistent AI companion heroes designed specifically for this universe. Give every companion complete playable mechanics, useful genre-appropriate equipment, and a distinctive persona. Do not use protected franchises.',
|
||||
system: 'Create an original 13+ TTRPG starting world. Return only valid JSON matching the supplied WorldStarter structure, with exactly 3 NPCs and 2 factions. Do not use protected franchises.',
|
||||
messages: request.messages,
|
||||
schemaName: 'world_starter',
|
||||
jsonSchema: WorldStarterSchema.toJSONSchema(),
|
||||
|
||||
@@ -8,17 +8,6 @@ const worldFixture = () => ({
|
||||
startingLocation: { id: 'location', kind: 'location' as const, name: 'Gate', summary: 'A station beyond known space.', tags: [] as string[], secrets: [] as string[] },
|
||||
npcs: [1, 2, 3].map(index => ({ id: `npc-${index}`, kind: 'npc' as const, name: `NPC ${index}`, summary: 'A useful person with their own agenda.', tags: [], secrets: [] })),
|
||||
factions: [1, 2].map(index => ({ id: `faction-${index}`, kind: 'faction' as const, name: `Faction ${index}`, summary: 'An organization pursuing a hidden objective.', tags: [], secrets: [] })),
|
||||
companions: [1, 2, 3].map(index => ({
|
||||
name: `Companion ${index}`,
|
||||
concept: `A universe-specific companion with role ${index}.`,
|
||||
abilities: { str: 10, dex: 12, con: 11, int: 13, wis: 12, cha: 10 },
|
||||
hp: 11,
|
||||
maxHp: 11,
|
||||
defense: 12,
|
||||
proficiency: 2,
|
||||
inventory: ['Field kit'],
|
||||
persona: { voice: 'Distinctive and clear.', motivation: 'Help the party.', flaw: 'Has a private agenda.', bond: 'Believes in the party.' },
|
||||
})),
|
||||
hook: 'A sufficiently long hook that immediately gives the party something to investigate.',
|
||||
hiddenThreat: 'A hidden threat waits beyond the gate.',
|
||||
openingScene: 'The gate opens without warning, and an impossible signal calls every hero by name.',
|
||||
@@ -45,10 +34,7 @@ describe('worker AI providers', () => {
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
const { generateWorld } = await import('./ai')
|
||||
await expect(generateWorld([{ role: 'user', content: 'Create a science fiction frontier.' }])).resolves.toMatchObject({
|
||||
title: 'Test Reach',
|
||||
companions: [{ name: 'Companion 1' }, { name: 'Companion 2' }, { name: 'Companion 3' }],
|
||||
})
|
||||
await expect(generateWorld([{ role: 'user', content: 'Create a science fiction frontier.' }])).resolves.toMatchObject({ title: 'Test Reach' })
|
||||
expect(fetchMock).toHaveBeenCalledWith('https://api.deepseek.com/chat/completions', expect.any(Object))
|
||||
})
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ async function structuredRequest<T>(name: string, schema: JsonSchema, messages:
|
||||
export async function generateWorld(messages: Array<{ role: 'user' | 'assistant'; content: string }>): Promise<WorldStarter> {
|
||||
assert13Plus(...messages.map(message => message.content))
|
||||
const world = await structuredRequest('world_starter', WorldStarterSchema.toJSONSchema(), [
|
||||
{ role: 'system', content: 'You are the Dungeons & Ground world coauthor. Create an original 13+ private TTRPG setting in any requested genre. Never use protected settings or characters. Return exactly three NPCs, two factions, and three distinct persistent AI companion heroes created specifically for this universe. Give each companion complete playable mechanics, useful genre-appropriate equipment, and a persona that produces interesting party choices without overriding the human player. Make the opening immediately playable.' },
|
||||
{ role: 'system', content: 'You are the Dungeons & Ground world coauthor. Create an original 13+ private TTRPG setting in any requested genre. Never use protected settings or characters. Return exactly three NPCs and two factions. Make the opening immediately playable.' },
|
||||
...messages,
|
||||
], value => WorldStarterSchema.parse(value))
|
||||
// Moderate the entire typed object, including entity summaries, secrets and
|
||||
|
||||
@@ -30,6 +30,21 @@ export const WorldEntitySchema = z.object({
|
||||
})
|
||||
export type WorldEntity = z.infer<typeof WorldEntitySchema>
|
||||
|
||||
export const WorldStarterSchema = z.object({
|
||||
title: z.string().min(3).max(100),
|
||||
genre: z.string().min(2).max(80),
|
||||
tone: z.string().min(2).max(160),
|
||||
premise: z.string().min(20).max(1800),
|
||||
contentBoundaries: z.array(z.string().max(120)).min(1).max(8),
|
||||
startingLocation: WorldEntitySchema.extend({ kind: z.literal('location') }),
|
||||
npcs: z.array(WorldEntitySchema.extend({ kind: z.literal('npc') })).length(3),
|
||||
factions: z.array(WorldEntitySchema.extend({ kind: z.literal('faction') })).length(2),
|
||||
hook: z.string().min(20).max(1200),
|
||||
hiddenThreat: z.string().min(10).max(1000),
|
||||
openingScene: z.string().min(40).max(2400),
|
||||
})
|
||||
export type WorldStarter = z.infer<typeof WorldStarterSchema>
|
||||
|
||||
export const CharacterPersonaSchema = z.object({
|
||||
voice: z.string().trim().min(1).max(240),
|
||||
motivation: z.string().trim().min(1).max(300),
|
||||
@@ -54,22 +69,6 @@ export const CharacterDraftSchema = z.object({
|
||||
})
|
||||
export type CharacterDraft = z.infer<typeof CharacterDraftSchema>
|
||||
|
||||
export const WorldStarterSchema = z.object({
|
||||
title: z.string().min(3).max(100),
|
||||
genre: z.string().min(2).max(80),
|
||||
tone: z.string().min(2).max(160),
|
||||
premise: z.string().min(20).max(1800),
|
||||
contentBoundaries: z.array(z.string().max(120)).min(1).max(8),
|
||||
startingLocation: WorldEntitySchema.extend({ kind: z.literal('location') }),
|
||||
npcs: z.array(WorldEntitySchema.extend({ kind: z.literal('npc') })).length(3),
|
||||
factions: z.array(WorldEntitySchema.extend({ kind: z.literal('faction') })).length(2),
|
||||
companions: z.array(CharacterDraftSchema).length(3),
|
||||
hook: z.string().min(20).max(1200),
|
||||
hiddenThreat: z.string().min(10).max(1000),
|
||||
openingScene: z.string().min(40).max(2400),
|
||||
})
|
||||
export type WorldStarter = z.infer<typeof WorldStarterSchema>
|
||||
|
||||
export const CharacterSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1).max(80),
|
||||
|
||||
@@ -112,17 +112,6 @@ const world = {
|
||||
entity('faction', 'The Reclaimers', 'Salvagers who want the station stripped for parts.'),
|
||||
entity('faction', 'The Seed Vault', 'An automated network protecting the last viable specimens.'),
|
||||
],
|
||||
companions: [1, 2, 3].map(index => ({
|
||||
name: `Garden Companion ${index}`,
|
||||
concept: `A universe-specific caretaker hero assigned to garden sector ${index}.`,
|
||||
abilities: { str: 10, dex: 12, con: 11, int: 13, wis: 12, cha: 10 },
|
||||
hp: 11,
|
||||
maxHp: 11,
|
||||
defense: 12,
|
||||
proficiency: 2,
|
||||
inventory: ['Garden survey kit'],
|
||||
persona: { voice: 'Calm and observant.', motivation: 'Protect the living garden.', flaw: 'Trusts the station too much.', bond: 'Treats the party as new growth.' },
|
||||
})),
|
||||
hook: 'The airlock opens only after both visitors speak names the station already knows.',
|
||||
hiddenThreat: 'The garden predicts visitors by growing imperfect biological copies of them.',
|
||||
openingScene: 'The inner airlock opens on warm rain and rows of silver trees. A voice welcomes both salvagers by name, then asks why one of them has returned without the other.',
|
||||
@@ -136,9 +125,9 @@ const { campaignId } = await appRequest('/api/v1/campaigns', owner.token, {
|
||||
})
|
||||
const starterParty = await appRequest(`/api/v1/campaigns/${campaignId}`, owner.token)
|
||||
const ownerCharacter = starterParty.characters.find(character => character.user_id === owner.id && character.controller === 'human')
|
||||
const automaticCompanions = starterParty.characters.filter(character => character.controller === 'ai')
|
||||
if (!ownerCharacter || automaticCompanions.length !== world.companions.length || automaticCompanions.some(character => character.name === 'Echo') || starterParty.round?.status !== 'open') {
|
||||
throw new Error('[smoke] A new campaign did not start with its actionable owner hero, generated world companions, and an open round.')
|
||||
const automaticCompanion = starterParty.characters.find(character => character.controller === 'ai')
|
||||
if (!ownerCharacter || !automaticCompanion || starterParty.round?.status !== 'open') {
|
||||
throw new Error('[smoke] A new campaign did not start with an actionable owner hero, an AI companion, and an open round.')
|
||||
}
|
||||
|
||||
console.log('[smoke] Joining the second player through an invite…')
|
||||
@@ -169,7 +158,7 @@ if (!suggested?.character?.name || !suggested.character?.persona?.motivation) {
|
||||
|
||||
const initial = await appRequest(`/api/v1/campaigns/${campaignId}`, owner.token)
|
||||
if (initial.members.length !== 2) throw new Error(`[smoke] Expected 2 members, received ${initial.members.length}.`)
|
||||
if (initial.characters.length !== 5 || initial.round?.status !== 'open') throw new Error('[smoke] Multiplayer lobby did not expose two humans, three generated AI companions, and an open round.')
|
||||
if (initial.characters.length !== 3 || initial.round?.status !== 'open') throw new Error('[smoke] Multiplayer lobby did not expose two humans, one AI companion, and an open round.')
|
||||
const roundId = initial.round.id
|
||||
|
||||
console.log('[smoke] Verifying that the round waits for both humans…')
|
||||
|
||||
@@ -2329,7 +2329,7 @@ notify pgrst, 'reload schema';
|
||||
-- ============================================================================
|
||||
|
||||
-- Make a newly launched campaign playable immediately: the owner receives a
|
||||
-- human-controlled starter, and only actionable members hold the round open.
|
||||
-- human-controlled starter and the party receives one persistent AI companion.
|
||||
|
||||
create or replace function public.stage_two_create_campaign(
|
||||
p_world_id uuid,
|
||||
@@ -2382,6 +2382,30 @@ begin
|
||||
)
|
||||
);
|
||||
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
null,
|
||||
'Echo',
|
||||
left('A persistent AI companion shaped by the ' || v_world.genre || ' world of ' || v_world.title || ', ready to support the player without taking over their choices.', 600),
|
||||
'ai'::public.character_controller,
|
||||
'{"str":9,"dex":13,"con":11,"int":13,"wis":12,"cha":10}'::jsonb,
|
||||
11,
|
||||
11,
|
||||
13,
|
||||
2,
|
||||
'["Survey kit","Emergency supplies"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
jsonb_build_object(
|
||||
'voice', 'Observant, concise, and quietly curious.',
|
||||
'motivation', 'Help the party understand this unfamiliar world.',
|
||||
'flaw', 'Sometimes values patterns more than instinct.',
|
||||
'bond', 'Stays beside the player when the path turns dangerous.'
|
||||
)
|
||||
);
|
||||
|
||||
insert into public.rounds(campaign_id, number) values (v_campaign_id, 1);
|
||||
return v_campaign_id;
|
||||
end;
|
||||
@@ -2478,269 +2502,6 @@ where campaign.status = 'active'
|
||||
and character.user_id = campaign.owner_id
|
||||
);
|
||||
|
||||
create or replace function public.dng_schema_version()
|
||||
returns integer language sql stable security definer set search_path = '' as $$
|
||||
select 10;
|
||||
$$;
|
||||
|
||||
revoke all on function public.dng_schema_version() from public, anon, authenticated;
|
||||
grant execute on function public.dng_schema_version() to service_role;
|
||||
|
||||
notify pgrst, 'reload schema';
|
||||
|
||||
-- ============================================================================
|
||||
-- 0011_universe_companions.sql
|
||||
-- ============================================================================
|
||||
|
||||
-- Persist the AI companions created with each universe and use that party when
|
||||
-- a campaign starts. This replaces the temporary hard-coded Echo companion.
|
||||
|
||||
alter table public.worlds
|
||||
add column if not exists starter_companions jsonb not null default '[]'::jsonb;
|
||||
|
||||
alter table public.worlds
|
||||
drop constraint if exists worlds_starter_companions_array;
|
||||
alter table public.worlds
|
||||
add constraint worlds_starter_companions_array check (
|
||||
jsonb_typeof(starter_companions) = 'array'
|
||||
and jsonb_array_length(starter_companions) <= 3
|
||||
);
|
||||
|
||||
-- Ready drafts created before companions were part of WorldStarter inherit
|
||||
-- three universe-specific people that the coauthor already generated.
|
||||
update public.coauthor_sessions session
|
||||
set generated_world = jsonb_set(
|
||||
session.generated_world,
|
||||
'{companions}',
|
||||
coalesce((
|
||||
select jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'name', npc.value->>'name',
|
||||
'concept', left(npc.value->>'summary', 600),
|
||||
'abilities', case npc.position
|
||||
when 1 then '{"str":10,"dex":14,"con":11,"int":13,"wis":12,"cha":10}'::jsonb
|
||||
when 2 then '{"str":13,"dex":10,"con":14,"int":10,"wis":12,"cha":11}'::jsonb
|
||||
else '{"str":9,"dex":12,"con":11,"int":14,"wis":13,"cha":12}'::jsonb
|
||||
end,
|
||||
'hp', 10 + npc.position,
|
||||
'maxHp', 10 + npc.position,
|
||||
'defense', 11 + npc.position,
|
||||
'proficiency', 2,
|
||||
'inventory', jsonb_build_array('Universe field kit', 'Travel supplies'),
|
||||
'persona', jsonb_build_object(
|
||||
'voice', left(npc.value->>'summary', 240),
|
||||
'motivation', left('Pursue the goal behind: ' || (npc.value->>'summary'), 300),
|
||||
'flaw', 'Their personal agenda can complicate the party''s plans.',
|
||||
'bond', left('They belong to ' || (session.generated_world->>'title') || ' and choose to stand with the party.', 300)
|
||||
)
|
||||
) order by npc.position
|
||||
)
|
||||
from jsonb_array_elements(coalesce(session.generated_world->'npcs', '[]'::jsonb))
|
||||
with ordinality as npc(value, position)
|
||||
where npc.position <= 3
|
||||
), '[]'::jsonb),
|
||||
true
|
||||
),
|
||||
updated_at = now()
|
||||
where session.generated_world is not null
|
||||
and jsonb_typeof(session.generated_world->'npcs') = 'array'
|
||||
and coalesce(jsonb_array_length(session.generated_world->'companions'), 0) = 0;
|
||||
|
||||
-- Confirmed universes do not retain the original generation JSON. Build their
|
||||
-- starter companions from the AI-created NPC entities already stored for them.
|
||||
update public.worlds world
|
||||
set starter_companions = coalesce((
|
||||
select jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'name', npc.name,
|
||||
'concept', left(npc.summary, 600),
|
||||
'abilities', case npc.position
|
||||
when 1 then '{"str":10,"dex":14,"con":11,"int":13,"wis":12,"cha":10}'::jsonb
|
||||
when 2 then '{"str":13,"dex":10,"con":14,"int":10,"wis":12,"cha":11}'::jsonb
|
||||
else '{"str":9,"dex":12,"con":11,"int":14,"wis":13,"cha":12}'::jsonb
|
||||
end,
|
||||
'hp', 10 + npc.position,
|
||||
'maxHp', 10 + npc.position,
|
||||
'defense', 11 + npc.position,
|
||||
'proficiency', 2,
|
||||
'inventory', jsonb_build_array('Universe field kit', 'Travel supplies'),
|
||||
'persona', jsonb_build_object(
|
||||
'voice', left(npc.summary, 240),
|
||||
'motivation', left('Pursue the goal behind: ' || npc.summary, 300),
|
||||
'flaw', 'Their personal agenda can complicate the party''s plans.',
|
||||
'bond', left('They belong to ' || world.title || ' and choose to stand with the party.', 300)
|
||||
)
|
||||
) order by npc.position
|
||||
)
|
||||
from (
|
||||
select entity.name, entity.summary,
|
||||
(row_number() over (order by entity.created_at, entity.id))::integer as position
|
||||
from public.world_entities entity
|
||||
where entity.world_id = world.id and entity.kind = 'npc'
|
||||
order by entity.created_at, entity.id
|
||||
limit 3
|
||||
) npc
|
||||
), '[]'::jsonb)
|
||||
where jsonb_array_length(world.starter_companions) = 0;
|
||||
|
||||
create or replace function public.stage_two_confirm_world(
|
||||
p_session_id uuid,
|
||||
p_owner_id uuid
|
||||
) returns uuid language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_session public.coauthor_sessions%rowtype;
|
||||
v_world jsonb;
|
||||
v_world_id uuid;
|
||||
v_entity jsonb;
|
||||
begin
|
||||
select * into v_session from public.coauthor_sessions
|
||||
where id = p_session_id and owner_id = p_owner_id for update;
|
||||
if not found then raise exception 'coauthor session not found'; end if;
|
||||
if v_session.status = 'confirmed' and v_session.confirmed_world_id is not null then
|
||||
return v_session.confirmed_world_id;
|
||||
end if;
|
||||
if v_session.status <> 'ready' or v_session.generated_world is null then
|
||||
raise exception 'coauthor session is not ready to confirm';
|
||||
end if;
|
||||
v_world := v_session.generated_world;
|
||||
if coalesce(v_world->>'title', '') = ''
|
||||
or coalesce(v_world->>'openingScene', '') = ''
|
||||
or jsonb_typeof(v_world->'companions') <> 'array'
|
||||
or jsonb_array_length(v_world->'companions') <> 3 then
|
||||
raise exception 'generated world is incomplete';
|
||||
end if;
|
||||
|
||||
insert into public.worlds(
|
||||
owner_id, title, genre, tone, premise, content_boundaries,
|
||||
hidden_threat, hook, opening_scene, starter_companions, status
|
||||
) values (
|
||||
p_owner_id, v_world->>'title', v_world->>'genre', v_world->>'tone', v_world->>'premise',
|
||||
coalesce(v_world->'contentBoundaries', '[]'::jsonb), v_world->>'hiddenThreat',
|
||||
v_world->>'hook', v_world->>'openingScene', v_world->'companions', 'confirmed'
|
||||
) returning id into v_world_id;
|
||||
|
||||
v_entity := v_world->'startingLocation';
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'location', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
for v_entity in select * from jsonb_array_elements(v_world->'npcs') loop
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'npc', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
end loop;
|
||||
for v_entity in select * from jsonb_array_elements(v_world->'factions') loop
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'faction', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
end loop;
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'quest', 'Opening Hook', v_world->>'hook', array['opening'], '[]'::jsonb);
|
||||
|
||||
update public.coauthor_sessions
|
||||
set status = 'confirmed', confirmed_world_id = v_world_id, updated_at = now()
|
||||
where id = p_session_id;
|
||||
return v_world_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.stage_two_create_campaign(
|
||||
p_world_id uuid,
|
||||
p_owner_id uuid,
|
||||
p_title text
|
||||
) returns uuid language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_world public.worlds%rowtype;
|
||||
v_campaign_id uuid;
|
||||
v_owner_name text;
|
||||
v_companion jsonb;
|
||||
begin
|
||||
if p_title is null or char_length(btrim(p_title)) not between 3 and 100 then
|
||||
raise exception 'campaign title must be between 3 and 100 characters';
|
||||
end if;
|
||||
select * into v_world from public.worlds
|
||||
where id = p_world_id and owner_id = p_owner_id and status = 'confirmed' for share;
|
||||
if not found then raise exception 'confirmed world not found'; end if;
|
||||
if jsonb_typeof(v_world.starter_companions) <> 'array'
|
||||
or jsonb_array_length(v_world.starter_companions) = 0 then
|
||||
raise exception 'confirmed world has no AI companions';
|
||||
end if;
|
||||
|
||||
select nullif(btrim(display_name), '') into v_owner_name
|
||||
from public.profiles where id = p_owner_id;
|
||||
v_owner_name := left(coalesce(v_owner_name, 'Wayfinder'), 80);
|
||||
|
||||
insert into public.campaigns(world_id, owner_id, title, current_scene, next_prompt, status)
|
||||
values (p_world_id, p_owner_id, btrim(p_title), coalesce(v_world.opening_scene, v_world.premise), 'What do you do?', 'active')
|
||||
returning id into v_campaign_id;
|
||||
insert into public.campaign_members(campaign_id, user_id, role)
|
||||
values (v_campaign_id, p_owner_id, 'owner');
|
||||
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
p_owner_id,
|
||||
v_owner_name,
|
||||
left('An adaptable protagonist ready to confront the opening mystery of ' || v_world.title || '.', 600),
|
||||
'human'::public.character_controller,
|
||||
'{"str":10,"dex":12,"con":12,"int":11,"wis":13,"cha":10}'::jsonb,
|
||||
12,
|
||||
12,
|
||||
12,
|
||||
2,
|
||||
'["Field kit","Personal keepsake"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
jsonb_build_object(
|
||||
'voice', 'Defined by the player.',
|
||||
'motivation', 'Discover what the opening scene is hiding.',
|
||||
'flaw', 'Still learning what this world demands.',
|
||||
'bond', 'Protect the party through the first danger.'
|
||||
)
|
||||
);
|
||||
|
||||
for v_companion in select * from jsonb_array_elements(v_world.starter_companions) loop
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
null,
|
||||
v_companion->>'name',
|
||||
v_companion->>'concept',
|
||||
'ai'::public.character_controller,
|
||||
v_companion->'abilities',
|
||||
(v_companion->>'hp')::integer,
|
||||
(v_companion->>'maxHp')::integer,
|
||||
(v_companion->>'defense')::integer,
|
||||
(v_companion->>'proficiency')::integer,
|
||||
v_companion->'inventory',
|
||||
'[]'::jsonb,
|
||||
v_companion->'persona'
|
||||
);
|
||||
end loop;
|
||||
|
||||
insert into public.rounds(campaign_id, number) values (v_campaign_id, 1);
|
||||
return v_campaign_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Remove only the exact temporary Echo inserted by schema v10, preserving any
|
||||
-- independently authored character that happens to share the name.
|
||||
delete from public.characters character
|
||||
where character.user_id is null
|
||||
and character.controller = 'ai'::public.character_controller
|
||||
and character.name = 'Echo'
|
||||
and character.inventory = '["Survey kit","Emergency supplies"]'::jsonb
|
||||
and character.persona->>'voice' = 'Observant, concise, and quietly curious.'
|
||||
and character.persona->>'motivation' = 'Help the party understand this unfamiliar world.'
|
||||
and character.concept like 'A persistent AI companion shaped by the %';
|
||||
|
||||
-- Existing active campaigns receive the companion roster belonging to their
|
||||
-- universe. Name matching keeps this migration idempotent around manual heroes.
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
@@ -2748,31 +2509,34 @@ insert into public.characters(
|
||||
select
|
||||
campaign.id,
|
||||
null,
|
||||
companion.value->>'name',
|
||||
companion.value->>'concept',
|
||||
'Echo',
|
||||
left('A persistent AI companion shaped by the ' || world.genre || ' world of ' || world.title || ', ready to support the player without taking over their choices.', 600),
|
||||
'ai'::public.character_controller,
|
||||
companion.value->'abilities',
|
||||
(companion.value->>'hp')::integer,
|
||||
(companion.value->>'maxHp')::integer,
|
||||
(companion.value->>'defense')::integer,
|
||||
(companion.value->>'proficiency')::integer,
|
||||
companion.value->'inventory',
|
||||
'{"str":9,"dex":13,"con":11,"int":13,"wis":12,"cha":10}'::jsonb,
|
||||
11,
|
||||
11,
|
||||
13,
|
||||
2,
|
||||
'["Survey kit","Emergency supplies"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
companion.value->'persona'
|
||||
jsonb_build_object(
|
||||
'voice', 'Observant, concise, and quietly curious.',
|
||||
'motivation', 'Help the party understand this unfamiliar world.',
|
||||
'flaw', 'Sometimes values patterns more than instinct.',
|
||||
'bond', 'Stays beside the player when the path turns dangerous.'
|
||||
)
|
||||
from public.campaigns campaign
|
||||
join public.worlds world on world.id = campaign.world_id
|
||||
cross join lateral jsonb_array_elements(world.starter_companions) companion(value)
|
||||
where campaign.status = 'active'
|
||||
and not exists (
|
||||
select 1 from public.characters character
|
||||
where character.campaign_id = campaign.id
|
||||
and character.controller = 'ai'::public.character_controller
|
||||
and lower(character.name) = lower(companion.value->>'name')
|
||||
);
|
||||
|
||||
create or replace function public.dng_schema_version()
|
||||
returns integer language sql stable security definer set search_path = '' as $$
|
||||
select 11;
|
||||
select 10;
|
||||
$$;
|
||||
|
||||
revoke all on function public.dng_schema_version() from public, anon, authenticated;
|
||||
@@ -2804,7 +2568,7 @@ begin
|
||||
if to_regprocedure('public.stage_four_create_character(uuid,uuid,text,text,text,jsonb,integer,integer,integer,integer,jsonb,jsonb,jsonb)') is null then
|
||||
raise exception 'D&G bootstrap verification failed: character creation RPC is missing';
|
||||
end if;
|
||||
if public.dng_schema_version() <> 11 then
|
||||
if public.dng_schema_version() <> 10 then
|
||||
raise exception 'D&G bootstrap verification failed: unexpected schema version';
|
||||
end if;
|
||||
end;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-- Make a newly launched campaign playable immediately: the owner receives a
|
||||
-- human-controlled starter, and only actionable members hold the round open.
|
||||
-- human-controlled starter and the party receives one persistent AI companion.
|
||||
|
||||
create or replace function public.stage_two_create_campaign(
|
||||
p_world_id uuid,
|
||||
@@ -52,6 +52,30 @@ begin
|
||||
)
|
||||
);
|
||||
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
null,
|
||||
'Echo',
|
||||
left('A persistent AI companion shaped by the ' || v_world.genre || ' world of ' || v_world.title || ', ready to support the player without taking over their choices.', 600),
|
||||
'ai'::public.character_controller,
|
||||
'{"str":9,"dex":13,"con":11,"int":13,"wis":12,"cha":10}'::jsonb,
|
||||
11,
|
||||
11,
|
||||
13,
|
||||
2,
|
||||
'["Survey kit","Emergency supplies"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
jsonb_build_object(
|
||||
'voice', 'Observant, concise, and quietly curious.',
|
||||
'motivation', 'Help the party understand this unfamiliar world.',
|
||||
'flaw', 'Sometimes values patterns more than instinct.',
|
||||
'bond', 'Stays beside the player when the path turns dangerous.'
|
||||
)
|
||||
);
|
||||
|
||||
insert into public.rounds(campaign_id, number) values (v_campaign_id, 1);
|
||||
return v_campaign_id;
|
||||
end;
|
||||
@@ -148,6 +172,38 @@ where campaign.status = 'active'
|
||||
and character.user_id = campaign.owner_id
|
||||
);
|
||||
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
)
|
||||
select
|
||||
campaign.id,
|
||||
null,
|
||||
'Echo',
|
||||
left('A persistent AI companion shaped by the ' || world.genre || ' world of ' || world.title || ', ready to support the player without taking over their choices.', 600),
|
||||
'ai'::public.character_controller,
|
||||
'{"str":9,"dex":13,"con":11,"int":13,"wis":12,"cha":10}'::jsonb,
|
||||
11,
|
||||
11,
|
||||
13,
|
||||
2,
|
||||
'["Survey kit","Emergency supplies"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
jsonb_build_object(
|
||||
'voice', 'Observant, concise, and quietly curious.',
|
||||
'motivation', 'Help the party understand this unfamiliar world.',
|
||||
'flaw', 'Sometimes values patterns more than instinct.',
|
||||
'bond', 'Stays beside the player when the path turns dangerous.'
|
||||
)
|
||||
from public.campaigns campaign
|
||||
join public.worlds world on world.id = campaign.world_id
|
||||
where campaign.status = 'active'
|
||||
and not exists (
|
||||
select 1 from public.characters character
|
||||
where character.campaign_id = campaign.id
|
||||
and character.controller = 'ai'::public.character_controller
|
||||
);
|
||||
|
||||
create or replace function public.dng_schema_version()
|
||||
returns integer language sql stable security definer set search_path = '' as $$
|
||||
select 10;
|
||||
|
||||
@@ -1,287 +0,0 @@
|
||||
-- Persist the AI companions created with each universe and use that party when
|
||||
-- a campaign starts. This replaces the temporary hard-coded Echo companion.
|
||||
|
||||
alter table public.worlds
|
||||
add column if not exists starter_companions jsonb not null default '[]'::jsonb;
|
||||
|
||||
alter table public.worlds
|
||||
drop constraint if exists worlds_starter_companions_array;
|
||||
alter table public.worlds
|
||||
add constraint worlds_starter_companions_array check (
|
||||
jsonb_typeof(starter_companions) = 'array'
|
||||
and jsonb_array_length(starter_companions) <= 3
|
||||
);
|
||||
|
||||
-- Ready drafts created before companions were part of WorldStarter inherit
|
||||
-- three universe-specific people that the coauthor already generated.
|
||||
update public.coauthor_sessions session
|
||||
set generated_world = jsonb_set(
|
||||
session.generated_world,
|
||||
'{companions}',
|
||||
coalesce((
|
||||
select jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'name', npc.value->>'name',
|
||||
'concept', left(npc.value->>'summary', 600),
|
||||
'abilities', case npc.position
|
||||
when 1 then '{"str":10,"dex":14,"con":11,"int":13,"wis":12,"cha":10}'::jsonb
|
||||
when 2 then '{"str":13,"dex":10,"con":14,"int":10,"wis":12,"cha":11}'::jsonb
|
||||
else '{"str":9,"dex":12,"con":11,"int":14,"wis":13,"cha":12}'::jsonb
|
||||
end,
|
||||
'hp', 10 + npc.position,
|
||||
'maxHp', 10 + npc.position,
|
||||
'defense', 11 + npc.position,
|
||||
'proficiency', 2,
|
||||
'inventory', jsonb_build_array('Universe field kit', 'Travel supplies'),
|
||||
'persona', jsonb_build_object(
|
||||
'voice', left(npc.value->>'summary', 240),
|
||||
'motivation', left('Pursue the goal behind: ' || (npc.value->>'summary'), 300),
|
||||
'flaw', 'Their personal agenda can complicate the party''s plans.',
|
||||
'bond', left('They belong to ' || (session.generated_world->>'title') || ' and choose to stand with the party.', 300)
|
||||
)
|
||||
) order by npc.position
|
||||
)
|
||||
from jsonb_array_elements(coalesce(session.generated_world->'npcs', '[]'::jsonb))
|
||||
with ordinality as npc(value, position)
|
||||
where npc.position <= 3
|
||||
), '[]'::jsonb),
|
||||
true
|
||||
),
|
||||
updated_at = now()
|
||||
where session.generated_world is not null
|
||||
and jsonb_typeof(session.generated_world->'npcs') = 'array'
|
||||
and coalesce(jsonb_array_length(session.generated_world->'companions'), 0) = 0;
|
||||
|
||||
-- Confirmed universes do not retain the original generation JSON. Build their
|
||||
-- starter companions from the AI-created NPC entities already stored for them.
|
||||
update public.worlds world
|
||||
set starter_companions = coalesce((
|
||||
select jsonb_agg(
|
||||
jsonb_build_object(
|
||||
'name', npc.name,
|
||||
'concept', left(npc.summary, 600),
|
||||
'abilities', case npc.position
|
||||
when 1 then '{"str":10,"dex":14,"con":11,"int":13,"wis":12,"cha":10}'::jsonb
|
||||
when 2 then '{"str":13,"dex":10,"con":14,"int":10,"wis":12,"cha":11}'::jsonb
|
||||
else '{"str":9,"dex":12,"con":11,"int":14,"wis":13,"cha":12}'::jsonb
|
||||
end,
|
||||
'hp', 10 + npc.position,
|
||||
'maxHp', 10 + npc.position,
|
||||
'defense', 11 + npc.position,
|
||||
'proficiency', 2,
|
||||
'inventory', jsonb_build_array('Universe field kit', 'Travel supplies'),
|
||||
'persona', jsonb_build_object(
|
||||
'voice', left(npc.summary, 240),
|
||||
'motivation', left('Pursue the goal behind: ' || npc.summary, 300),
|
||||
'flaw', 'Their personal agenda can complicate the party''s plans.',
|
||||
'bond', left('They belong to ' || world.title || ' and choose to stand with the party.', 300)
|
||||
)
|
||||
) order by npc.position
|
||||
)
|
||||
from (
|
||||
select entity.name, entity.summary,
|
||||
(row_number() over (order by entity.created_at, entity.id))::integer as position
|
||||
from public.world_entities entity
|
||||
where entity.world_id = world.id and entity.kind = 'npc'
|
||||
order by entity.created_at, entity.id
|
||||
limit 3
|
||||
) npc
|
||||
), '[]'::jsonb)
|
||||
where jsonb_array_length(world.starter_companions) = 0;
|
||||
|
||||
create or replace function public.stage_two_confirm_world(
|
||||
p_session_id uuid,
|
||||
p_owner_id uuid
|
||||
) returns uuid language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_session public.coauthor_sessions%rowtype;
|
||||
v_world jsonb;
|
||||
v_world_id uuid;
|
||||
v_entity jsonb;
|
||||
begin
|
||||
select * into v_session from public.coauthor_sessions
|
||||
where id = p_session_id and owner_id = p_owner_id for update;
|
||||
if not found then raise exception 'coauthor session not found'; end if;
|
||||
if v_session.status = 'confirmed' and v_session.confirmed_world_id is not null then
|
||||
return v_session.confirmed_world_id;
|
||||
end if;
|
||||
if v_session.status <> 'ready' or v_session.generated_world is null then
|
||||
raise exception 'coauthor session is not ready to confirm';
|
||||
end if;
|
||||
v_world := v_session.generated_world;
|
||||
if coalesce(v_world->>'title', '') = ''
|
||||
or coalesce(v_world->>'openingScene', '') = ''
|
||||
or jsonb_typeof(v_world->'companions') <> 'array'
|
||||
or jsonb_array_length(v_world->'companions') <> 3 then
|
||||
raise exception 'generated world is incomplete';
|
||||
end if;
|
||||
|
||||
insert into public.worlds(
|
||||
owner_id, title, genre, tone, premise, content_boundaries,
|
||||
hidden_threat, hook, opening_scene, starter_companions, status
|
||||
) values (
|
||||
p_owner_id, v_world->>'title', v_world->>'genre', v_world->>'tone', v_world->>'premise',
|
||||
coalesce(v_world->'contentBoundaries', '[]'::jsonb), v_world->>'hiddenThreat',
|
||||
v_world->>'hook', v_world->>'openingScene', v_world->'companions', 'confirmed'
|
||||
) returning id into v_world_id;
|
||||
|
||||
v_entity := v_world->'startingLocation';
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'location', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
for v_entity in select * from jsonb_array_elements(v_world->'npcs') loop
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'npc', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
end loop;
|
||||
for v_entity in select * from jsonb_array_elements(v_world->'factions') loop
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'faction', v_entity->>'name', v_entity->>'summary',
|
||||
array(select jsonb_array_elements_text(coalesce(v_entity->'tags', '[]'::jsonb))),
|
||||
coalesce(v_entity->'secrets', '[]'::jsonb));
|
||||
end loop;
|
||||
insert into public.world_entities(world_id, kind, name, summary, tags, secrets)
|
||||
values (v_world_id, 'quest', 'Opening Hook', v_world->>'hook', array['opening'], '[]'::jsonb);
|
||||
|
||||
update public.coauthor_sessions
|
||||
set status = 'confirmed', confirmed_world_id = v_world_id, updated_at = now()
|
||||
where id = p_session_id;
|
||||
return v_world_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.stage_two_create_campaign(
|
||||
p_world_id uuid,
|
||||
p_owner_id uuid,
|
||||
p_title text
|
||||
) returns uuid language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
v_world public.worlds%rowtype;
|
||||
v_campaign_id uuid;
|
||||
v_owner_name text;
|
||||
v_companion jsonb;
|
||||
begin
|
||||
if p_title is null or char_length(btrim(p_title)) not between 3 and 100 then
|
||||
raise exception 'campaign title must be between 3 and 100 characters';
|
||||
end if;
|
||||
select * into v_world from public.worlds
|
||||
where id = p_world_id and owner_id = p_owner_id and status = 'confirmed' for share;
|
||||
if not found then raise exception 'confirmed world not found'; end if;
|
||||
if jsonb_typeof(v_world.starter_companions) <> 'array'
|
||||
or jsonb_array_length(v_world.starter_companions) = 0 then
|
||||
raise exception 'confirmed world has no AI companions';
|
||||
end if;
|
||||
|
||||
select nullif(btrim(display_name), '') into v_owner_name
|
||||
from public.profiles where id = p_owner_id;
|
||||
v_owner_name := left(coalesce(v_owner_name, 'Wayfinder'), 80);
|
||||
|
||||
insert into public.campaigns(world_id, owner_id, title, current_scene, next_prompt, status)
|
||||
values (p_world_id, p_owner_id, btrim(p_title), coalesce(v_world.opening_scene, v_world.premise), 'What do you do?', 'active')
|
||||
returning id into v_campaign_id;
|
||||
insert into public.campaign_members(campaign_id, user_id, role)
|
||||
values (v_campaign_id, p_owner_id, 'owner');
|
||||
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
p_owner_id,
|
||||
v_owner_name,
|
||||
left('An adaptable protagonist ready to confront the opening mystery of ' || v_world.title || '.', 600),
|
||||
'human'::public.character_controller,
|
||||
'{"str":10,"dex":12,"con":12,"int":11,"wis":13,"cha":10}'::jsonb,
|
||||
12,
|
||||
12,
|
||||
12,
|
||||
2,
|
||||
'["Field kit","Personal keepsake"]'::jsonb,
|
||||
'[]'::jsonb,
|
||||
jsonb_build_object(
|
||||
'voice', 'Defined by the player.',
|
||||
'motivation', 'Discover what the opening scene is hiding.',
|
||||
'flaw', 'Still learning what this world demands.',
|
||||
'bond', 'Protect the party through the first danger.'
|
||||
)
|
||||
);
|
||||
|
||||
for v_companion in select * from jsonb_array_elements(v_world.starter_companions) loop
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
) values (
|
||||
v_campaign_id,
|
||||
null,
|
||||
v_companion->>'name',
|
||||
v_companion->>'concept',
|
||||
'ai'::public.character_controller,
|
||||
v_companion->'abilities',
|
||||
(v_companion->>'hp')::integer,
|
||||
(v_companion->>'maxHp')::integer,
|
||||
(v_companion->>'defense')::integer,
|
||||
(v_companion->>'proficiency')::integer,
|
||||
v_companion->'inventory',
|
||||
'[]'::jsonb,
|
||||
v_companion->'persona'
|
||||
);
|
||||
end loop;
|
||||
|
||||
insert into public.rounds(campaign_id, number) values (v_campaign_id, 1);
|
||||
return v_campaign_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Remove only the exact temporary Echo inserted by schema v10, preserving any
|
||||
-- independently authored character that happens to share the name.
|
||||
delete from public.characters character
|
||||
where character.user_id is null
|
||||
and character.controller = 'ai'::public.character_controller
|
||||
and character.name = 'Echo'
|
||||
and character.inventory = '["Survey kit","Emergency supplies"]'::jsonb
|
||||
and character.persona->>'voice' = 'Observant, concise, and quietly curious.'
|
||||
and character.persona->>'motivation' = 'Help the party understand this unfamiliar world.'
|
||||
and character.concept like 'A persistent AI companion shaped by the %';
|
||||
|
||||
-- Existing active campaigns receive the companion roster belonging to their
|
||||
-- universe. Name matching keeps this migration idempotent around manual heroes.
|
||||
insert into public.characters(
|
||||
campaign_id, user_id, name, concept, controller, abilities, hp, max_hp,
|
||||
defense, proficiency, inventory, statuses, persona
|
||||
)
|
||||
select
|
||||
campaign.id,
|
||||
null,
|
||||
companion.value->>'name',
|
||||
companion.value->>'concept',
|
||||
'ai'::public.character_controller,
|
||||
companion.value->'abilities',
|
||||
(companion.value->>'hp')::integer,
|
||||
(companion.value->>'maxHp')::integer,
|
||||
(companion.value->>'defense')::integer,
|
||||
(companion.value->>'proficiency')::integer,
|
||||
companion.value->'inventory',
|
||||
'[]'::jsonb,
|
||||
companion.value->'persona'
|
||||
from public.campaigns campaign
|
||||
join public.worlds world on world.id = campaign.world_id
|
||||
cross join lateral jsonb_array_elements(world.starter_companions) companion(value)
|
||||
where campaign.status = 'active'
|
||||
and not exists (
|
||||
select 1 from public.characters character
|
||||
where character.campaign_id = campaign.id
|
||||
and character.controller = 'ai'::public.character_controller
|
||||
and lower(character.name) = lower(companion.value->>'name')
|
||||
);
|
||||
|
||||
create or replace function public.dng_schema_version()
|
||||
returns integer language sql stable security definer set search_path = '' as $$
|
||||
select 11;
|
||||
$$;
|
||||
|
||||
revoke all on function public.dng_schema_version() from public, anon, authenticated;
|
||||
grant execute on function public.dng_schema_version() to service_role;
|
||||
|
||||
notify pgrst, 'reload schema';
|
||||
Reference in New Issue
Block a user