more tweaks
All checks were successful
ci/woodpecker/cron/ci Pipeline was successful

This commit is contained in:
2026-01-19 22:37:53 -05:00
parent 9332ac6f94
commit 96223b81e6
4 changed files with 373 additions and 55 deletions

View File

@@ -47,6 +47,18 @@ function parseObjects(raw, type = "rooms") {
return null; return null;
} }
// For other types, use original logic // For other types, use original logic
if (type === "treasure") {
const parts = entry.split(/[—]/);
if (parts.length >= 2) {
const cleanName = parts[0].trim();
if (cleanName.toLowerCase().includes('treasure name') || cleanName.toLowerCase().includes('actual ')) {
return null;
}
let desc = parts.slice(1).join(' ').trim();
desc = desc.replace(/^description\s*:?\s*/i, '').trim();
return { name: cleanName, description: desc };
}
}
const [name, ...descParts] = entry.split(/[-–—:]/); const [name, ...descParts] = entry.split(/[-–—:]/);
const cleanName = name.trim(); const cleanName = name.trim();
// Skip placeholder names // Skip placeholder names
@@ -56,7 +68,8 @@ function parseObjects(raw, type = "rooms") {
cleanName.toLowerCase().includes('actual ')) { cleanName.toLowerCase().includes('actual ')) {
return null; return null;
} }
const desc = descParts.join(" ").trim(); let desc = descParts.join(" ").trim();
if (type === "npcs") desc = desc.replace(/^description\s*:?\s*/i, '').trim();
const obj = { name: cleanName }; const obj = { name: cleanName };
if (type === "rooms") return { ...obj, description: desc }; if (type === "rooms") return { ...obj, description: desc };
if (type === "npcs") return { ...obj, trait: desc }; if (type === "npcs") return { ...obj, trait: desc };
@@ -67,6 +80,18 @@ function parseObjects(raw, type = "rooms") {
} }
const parseEncounterText = (text, idx) => { const parseEncounterText = (text, idx) => {
// Handle "Encounter N Name Room Name Details" format
const encounterMatch = text.match(/Encounter\s+(\d+)\s+(.+?)\s+(?:Room Name|Location)\s+(.+?)\s+Details\s+(.+)/i);
if (encounterMatch) {
const [, , name, location, details] = encounterMatch;
return { name: name.trim(), details: `${location.trim()}: ${details.trim()}` };
}
// Handle "Encounter N Name: Location: Details" format
const colonFormat = text.match(/Encounter\s+\d+\s+(.+?):\s*(.+?):\s*(.+)/i);
if (colonFormat) {
const [, name, location, details] = colonFormat;
return { name: name.trim(), details: `${location.trim()}: ${details.trim()}` };
}
const match = text.match(/^(\d+)\s+(.+?)(?::\s*(.+))?$/); const match = text.match(/^(\d+)\s+(.+?)(?::\s*(.+))?$/);
if (match) { if (match) {
const [, , name, details] = match; const [, , name, details] = match;
@@ -75,7 +100,7 @@ const parseEncounterText = (text, idx) => {
const colonSplit = text.split(/[:]/); const colonSplit = text.split(/[:]/);
if (colonSplit.length > 1) { if (colonSplit.length > 1) {
return { return {
name: colonSplit[0].replace(/^\d+\s+/, "").trim(), name: colonSplit[0].replace(/^\d+\s+|Encounter\s+\d+\s+/i, "").trim(),
details: colonSplit.slice(1).join(":").trim() details: colonSplit.slice(1).join(":").trim()
}; };
} }
@@ -86,17 +111,18 @@ const parseEncounterText = (text, idx) => {
details: text.replace(/^\d+\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\s*/, "").trim() details: text.replace(/^\d+\s+[A-Z][a-z]+(?:\s+[A-Z][a-z]+)*\s*/, "").trim()
}; };
} }
return { name: `Encounter ${idx + 1}`, details: text.replace(/^\d+\s+/, "").trim() }; return { name: `Encounter ${idx + 1}`, details: text.replace(/^\d+\s+|Encounter\s+\d+\s+/i, "").trim() };
}; };
const splitCombinedEncounters = (encounters) => { const splitCombinedEncounters = (encounters) => {
const shouldSplit = encounters.length === 1 && (encounters[0].name === "1" || encounters[0].details.match(/\d+\s+[A-Z]/)); if (encounters.length === 0) return [];
const shouldSplit = encounters.length === 1 && (encounters[0].name === "1" || encounters[0].details?.match(/\d+\s+[A-Z]/) || encounters[0].details?.includes('Encounter'));
if (!shouldSplit) return encounters; if (!shouldSplit) return encounters;
console.warn("Encounters appear combined, attempting to split..."); console.warn("Encounters appear combined, attempting to split...");
const combinedText = encounters[0].details || ""; const combinedText = encounters[0].details || "";
const split = combinedText.split(/(?=\d+\s+[A-Z][a-z])/).filter(Boolean); const split = combinedText.split(/(?=Encounter\s+\d+|\d+\s+[A-Z][a-z])/i).filter(Boolean);
return (split.length > 1 || (split.length === 1 && combinedText.length > 100)) return (split.length > 1 || (split.length === 1 && combinedText.length > 100))
? split.map(parseEncounterText).filter(e => e?.name && e?.details?.length > 10) ? split.map((text, idx) => parseEncounterText(text, idx)).filter(e => e?.name && e?.details?.length > 10)
: encounters; : encounters;
}; };
@@ -458,7 +484,7 @@ function validateAndFixContent(dungeonData) {
export async function generateDungeon() { export async function generateDungeon() {
// Step 1: Titles // Step 1: Titles
const generatedTitlesRaw = await callOllama( const generatedTitlesRaw = await callOllama(
`Generate 50 short, punchy dungeon titles (max 5 words each), numbered as a plain text list. `Generate 50 different and unique short, punchy dungeon titles (2-4 words maximum, single evocative name, no colons), numbered as a plain text list.
Each title should come from a different style or theme. Make the set varied and evocative. For example: Each title should come from a different style or theme. Make the set varied and evocative. For example:
- OSR / classic tabletop: gritty, mysterious, old-school - OSR / classic tabletop: gritty, mysterious, old-school
@@ -474,7 +500,14 @@ Avoid repeating materials or adjectives. Absolutely do not use the words "Obsidi
); );
const generatedTitles = parseList(generatedTitlesRaw); const generatedTitles = parseList(generatedTitlesRaw);
console.log("Generated Titles:", generatedTitles); console.log("Generated Titles:", generatedTitles);
const title = generatedTitles[Math.floor(Math.random() * generatedTitles.length)]; let title = generatedTitles[Math.floor(Math.random() * generatedTitles.length)];
// Remove colons, dashes, and other separators, but keep the full title
title = title.split(/[:—–-]/)[0].trim();
// Only limit to 4 words if it's clearly too long (more than 6 words)
const titleWords = title.split(/\s+/);
if (titleWords.length > 6) {
title = titleWords.slice(0, 4).join(' ');
}
console.log("Selected title:", title); console.log("Selected title:", title);
// Step 2: Core Concepts // Step 2: Core Concepts
@@ -494,7 +527,7 @@ Example:
const flavorHooksRaw = await callOllama( const flavorHooksRaw = await callOllama(
`Based on the title "${title}" and these core concepts: `Based on the title "${title}" and these core concepts:
${coreConcepts} ${coreConcepts}
Write a single evocative paragraph describing the location. Maximum 2 sentences. Maximum 100 words. Then, generate 4-5 short adventure hooks or rumors. Write a single evocative paragraph describing the location. Maximum 2 sentences. Maximum 50-60 words. Then, generate 4-5 short adventure hooks or rumors.
The hooks should reference the central conflict, faction, and dynamic element. Hooks should suggest different approaches (stealth, diplomacy, force, exploration) and create anticipation. The hooks should reference the central conflict, faction, and dynamic element. Hooks should suggest different approaches (stealth, diplomacy, force, exploration) and create anticipation.
EXAMPLE OF GOOD HOOK: EXAMPLE OF GOOD HOOK:
@@ -506,8 +539,11 @@ Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered
undefined, 5, "Step 3: Flavor & Hooks" undefined, 5, "Step 3: Flavor & Hooks"
); );
const [flavorSection, hooksSection] = flavorHooksRaw.split(/Hooks & Rumors[:\n]/i); const [flavorSection, hooksSection] = flavorHooksRaw.split(/Hooks & Rumors[:\n]/i);
const flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, "")); let flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, ""));
const hooksRumors = parseList(hooksSection || ""); const words = flavor.split(/\s+/);
if (words.length > 60) flavor = words.slice(0, 60).join(' ') + '...';
let hooksRumors = parseList(hooksSection || "");
hooksRumors = hooksRumors.map(h => h.replace(/^[^:]+:\s*/, '').trim());
console.log("Flavor Text:", flavor); console.log("Flavor Text:", flavor);
console.log("Hooks & Rumors:", hooksRumors); console.log("Hooks & Rumors:", hooksRumors);
@@ -632,12 +668,14 @@ EXAMPLE NPC:
EXAMPLE TREASURE: EXAMPLE TREASURE:
"Whispering Blade — This dagger amplifies the wielder's voice to a deafening roar when drawn. Found in the Guardian Golem's chamber, it was used to command the construct. The blade is cursed: each use permanently reduces the wielder's hearing. Can be used to shatter glass or stun enemies, but the curse cannot be removed." "Whispering Blade — This dagger amplifies the wielder's voice to a deafening roar when drawn. Found in the Guardian Golem's chamber, it was used to command the construct. The blade is cursed: each use permanently reduces the wielder's hearing. Can be used to shatter glass or stun enemies, but the curse cannot be removed."
- **Strictly 1 Random Events Table:** A d6 table (exactly 6 entries) with random events/wandering encounters. Each entry MUST: - **Strictly 1 Random Events Table:** A d6 table (EXACTLY 6 entries, no more, no less) with random events/wandering encounters. Each entry MUST:
- Provide interesting complications or opportunities (not just combat) - Provide interesting complications or opportunities (not just combat)
- Tie to the core concepts and dynamic element - Tie to the core concepts and dynamic element
- Add replayability and surprise - Add replayability and surprise
- Be 15-20 words maximum - Be 15-20 words maximum
Format as numbered 1-6 list under "Random Events:" label. - Be UNIQUE and DIFFERENT from each other (no duplicates or generic placeholders)
- Be SPECIFIC to this dungeon's theme, conflict, and dynamic element
Format as numbered 1-6 list under "Random Events:" label. Each event must be a complete, interesting sentence describing what happens.
CRITICAL: Each item must be on its own numbered line. DO NOT combine multiple items into a single numbered entry. CRITICAL: Each item must be on its own numbered line. DO NOT combine multiple items into a single numbered entry.
@@ -678,10 +716,57 @@ Random Events:
CRITICAL: Every name must be unique and creative. Never use generic placeholders like "Location Name", "NPC Name", "Encounter Name", or "Treasure Name". Use actual descriptive names that fit the dungeon's theme. CRITICAL: Every name must be unique and creative. Never use generic placeholders like "Location Name", "NPC Name", "Encounter Name", or "Treasure Name". Use actual descriptive names that fit the dungeon's theme.
CRITICAL: Ensure all spelling is correct. Double-check all words, especially proper nouns, character names, and location names. Verify consistency of names across all sections. CRITICAL: Ensure all spelling is correct. Double-check all words, especially proper nouns, character names, and location names. Verify consistency of names across all sections.
Output as five separate numbered lists with these exact labels: "Locations:", "Encounters:", "NPCs:", "Treasures:", and "Random Events:". Each item must be on its own line starting with a number. Do not combine items. Do not use any bolding, preambles, or extra text. Do not use em-dashes (—) in encounters or NPCs, only use colons for those sections. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`, CRITICAL: You MUST output exactly five separate sections with these exact labels on their own lines:
"Locations:"
"Encounters:"
"NPCs:"
"Treasures:"
"Random Events:"
Each section must start with its label on its own line, followed by numbered items. Do NOT combine sections. Do NOT embed encounters in location descriptions. Each item must be on its own numbered line. Do not use any bolding, preambles, or extra text. Do not use em-dashes (—) in encounters or NPCs, only use colons for those sections. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
undefined, 5, "Step 5: Main Content" undefined, 5, "Step 5: Main Content"
); );
const [intermediateRoomsSection, encountersSection, npcsSection, treasureSection, randomEventsSection] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:|Random Events:/i); let [intermediateRoomsSection, encountersSection, npcsSection, treasureSection, randomEventsSection] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:|Random Events:/i);
// Ensure random events section is properly extracted (handle case where label might be missing)
if (!randomEventsSection && mainContentRaw.toLowerCase().includes('random')) {
const randomMatch = mainContentRaw.match(/Random Events?[:\s]*\n?([^]*?)(?=Locations?:|Encounters?:|NPCs?:|Treasures?:|$)/i);
if (randomMatch) {
randomEventsSection = randomMatch[1];
}
}
// Ensure NPCs section is properly extracted
if (!npcsSection && mainContentRaw.toLowerCase().includes('npc')) {
const npcMatch = mainContentRaw.match(/NPCs?[:\s]*\n?([^]*?)(?=Treasures?:|Random Events?:|Locations?:|Encounters?:|$)/i);
if (npcMatch) {
npcsSection = npcMatch[1];
}
}
// If sections are missing, try to extract from combined output
if (!encountersSection && intermediateRoomsSection.includes('Encounter')) {
const encounterMatches = intermediateRoomsSection.match(/Encounter\s+\d+[^]*?(?=Encounter\s+\d+|NPCs?:|Treasures?:|Random Events?:|Location \d+|$)/gi);
if (encounterMatches && encounterMatches.length > 0) {
encountersSection = encounterMatches.map((m, i) => {
// Convert "Encounter N Name Room Name Location Details" to "N. Name: Location: Details"
const match = m.match(/Encounter\s+(\d+)\s+(.+?)\s+(?:Room Name|Location)\s+(.+?)\s+Details\s+(.+)/i);
if (match) {
const [, num, name, location, details] = match;
return `${num}. ${name.trim()}: ${location.trim()}: ${details.trim().substring(0, 200)}`;
}
// Try format without "Room Name"
const simpleMatch = m.match(/Encounter\s+(\d+)\s+(.+?)\s+([A-Z][^:]+?)\s+Details\s+(.+)/i);
if (simpleMatch) {
const [, num, name, location, details] = simpleMatch;
return `${num}. ${name.trim()}: ${location.trim()}: ${details.trim().substring(0, 200)}`;
}
return `${i + 1}. ${m.trim()}`;
}).join('\n');
intermediateRoomsSection = intermediateRoomsSection.replace(/Encounter\s+\d+[^]*?(?=Encounter\s+\d+|NPCs?:|Treasures?:|Random Events?:|Location \d+|$)/gi, '');
}
}
const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Locations:/i, ""), "rooms"); const intermediateRooms = parseObjects(intermediateRoomsSection.replace(/Locations:/i, ""), "rooms");
// Limit to exactly 3 intermediate rooms to ensure total of 5 rooms (entrance + 3 intermediate + climax) // Limit to exactly 3 intermediate rooms to ensure total of 5 rooms (entrance + 3 intermediate + climax)
const limitedIntermediateRooms = intermediateRooms.slice(0, 3); const limitedIntermediateRooms = intermediateRooms.slice(0, 3);
@@ -689,10 +774,104 @@ Output as five separate numbered lists with these exact labels: "Locations:", "E
console.warn(`Expected exactly 3 intermediate locations but got ${intermediateRooms.length}, limiting to first 3`); console.warn(`Expected exactly 3 intermediate locations but got ${intermediateRooms.length}, limiting to first 3`);
} }
const rooms = [entranceRoom, ...limitedIntermediateRooms, climaxRoom]; const rooms = [entranceRoom, ...limitedIntermediateRooms, climaxRoom];
const encounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters")); let encounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters"));
const npcs = splitCombinedNPCs(parseObjects(npcsSection || "", "npcs")); let npcs = parseObjects(npcsSection || "", "npcs");
const treasure = splitCombinedTreasures(parseObjects(treasureSection || "", "treasure")); const treasure = parseObjects(treasureSection || "", "treasure");
const randomEvents = parseList(randomEventsSection || "");
// Pad NPCs to at least 4 if needed (only if we have some NPCs already)
if (npcs.length > 0 && npcs.length < 4) {
const factionName = coreConcepts.match(/Primary Faction[:\s]+([^.]+)/i)?.[1]?.trim() || 'the primary faction';
while (npcs.length < 4) {
npcs.push({
name: `NPC ${npcs.length + 1}`,
trait: `A member of ${factionName.toLowerCase()} with unknown motives.`
});
}
}
// Pad encounters to exactly 6 (only pad if we have at least 1 real encounter)
if (encounters.length > 0 && encounters.length < 6) {
const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences';
const conflict = coreConcepts.match(/Central Conflict[:\s]+([^.]+)/i)?.[1]?.trim() || 'a threat';
while (encounters.length < 6) {
const roomIndex = encounters.length % rooms.length;
const roomName = rooms[roomIndex]?.name || 'Unknown Location';
// Use more descriptive fallback names based on room and theme
const fallbackNames = [
`${roomName} Guardian`,
`${roomName} Threat`,
`${roomName} Challenge`,
`${dynamicElement.split(' ')[0]} Manifestation`,
`${conflict.split(' ')[0]} Encounter`,
`${roomName} Hazard`
];
encounters.push({
name: fallbackNames[encounters.length % fallbackNames.length],
details: `An encounter related to ${dynamicElement.toLowerCase()} occurs here.`
});
}
} else if (encounters.length === 0) {
// If no encounters at all, create 6 basic ones
const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences';
for (let i = 0; i < 6; i++) {
const roomIndex = i % rooms.length;
const roomName = rooms[roomIndex]?.name || 'Unknown Location';
encounters.push({
name: `${roomName} Encounter`,
details: `An encounter related to ${dynamicElement.toLowerCase()} occurs here.`
});
}
}
let randomEvents = parseList(randomEventsSection || "");
// Filter out placeholder events and strip any title prefixes
randomEvents = randomEvents
.filter(e =>
e &&
e.toLowerCase() !== 'a random event occurs' &&
e.toLowerCase() !== 'a random event occurs.' &&
!e.toLowerCase().includes('placeholder') &&
e.length > 10
)
.map(e => {
// Strip title prefixes like "Event 1:", "Random Event:", "Guardian Golem:", "Location Name:", etc.
// Remove numbered prefixes, random event labels, and location/encounter name prefixes
let cleaned = e.replace(/^(Event\s+\d+[:\s]+|Random\s+Event[:\s]+|Random\s+Events?[:\s]+)/i, '').trim();
// Remove location/encounter name prefixes (format: "Name: Location: description" or "Name: description")
// If it starts with a capitalized phrase followed by colon, it might be a title
const titleMatch = cleaned.match(/^([A-Z][^:]{3,40}):\s*(.+)$/);
if (titleMatch) {
const potentialTitle = titleMatch[1];
const rest = titleMatch[2];
// If the rest also has a colon, the first part is likely a title to remove
if (rest.includes(':')) {
cleaned = rest;
} else {
// Check if it looks like a location name (common patterns)
if (potentialTitle.match(/\b(Chamber|Room|Tower|Hall|Temple|Shrine|Crypt|Tomb|Vault|Gate|Bridge|Path|Corridor|Labyrinth|Dungeon|Cavern|Cave|Grotto|Sanctum|Shrine|Altar|Pit|Well|Pool|Fountain|Garden|Courtyard|Plaza|Square|Arena|Colosseum|Theater|Library|Laboratory|Workshop|Forge|Smithy|Barracks|Armory|Treasury|Storehouse|Warehouse|Cellar|Basement|Attic|Loft|Garret|Turret|Spire|Keep|Fortress|Castle|Palace|Manor|Mansion|Estate|Villa|Cottage|Hut|Shack|Tent|Pavilion|Gazebo|Pergola|Arbor|Bower|Grotto|Grot|Den|Lair|Nest|Burrow|Hollow|Recess|Niche|Alcove|Cubby|Cubbyhole|Closet|Wardrobe|Cupboard|Cabinet|Drawer|Shelf|Rack|Peg|Hook|Nail|Screw|Bolt|Rivet|Pin|Clip|Clamp|Brace|Bracket|Support|Prop|Pillar|Column|Post|Pole|Rod|Stick|Staff|Wand|Scepter|Mace|Club|Hammer|Axe|Sword|Dagger|Knife|Blade|Edge|Point|Tip|End|Top|Bottom|Side|Front|Back|Left|Right|Center|Middle|Core|Heart|Soul|Spirit|Mind|Body|Head|Neck|Chest|Torso|Waist|Hip|Thigh|Knee|Leg|Foot|Toe|Heel|Ankle|Shin|Calf|Thigh|Buttock|Backside|Rear|Behind|Aft|Stern|Bow|Port|Starboard|Fore|Aft|Midship|Amidship|Amidships|Amidship|Amidships)\b/i)) {
cleaned = rest;
}
}
}
return cleaned.trim();
});
randomEvents = randomEvents.slice(0, 6);
// Generate context-aware fallbacks if needed (only if we have some events already)
if (randomEvents.length > 0 && randomEvents.length < 6) {
const dynamicElement = coreConcepts.match(/Dynamic Element[:\s]+([^.]+)/i)?.[1]?.trim() || 'strange occurrences';
const conflict = coreConcepts.match(/Central Conflict[:\s]+([^.]+)/i)?.[1]?.trim() || 'a mysterious threat';
const fallbackEvents = [
`The ${dynamicElement.toLowerCase()} causes unexpected changes in the environment.`,
`A sign of ${conflict.toLowerCase()} appears, requiring immediate attention.`,
`The dungeon shifts, revealing a previously hidden passage or danger.`,
`An NPC from the primary faction appears with urgent information.`,
`The power source fluctuates, creating temporary hazards or opportunities.`,
`Echoes of past events manifest, providing clues or complications.`
];
while (randomEvents.length < 6) {
randomEvents.push(fallbackEvents[randomEvents.length % fallbackEvents.length]);
}
}
[[encounters, 6, 'encounters'], [npcs, 4, 'NPCs'], [treasure, 4, 'treasures'], [randomEvents, 6, 'random events']] [[encounters, 6, 'encounters'], [npcs, 4, 'NPCs'], [treasure, 4, 'treasures'], [randomEvents, 6, 'random events']]
.filter(([arr, expected]) => arr.length < expected && arr.length > 0) .filter(([arr, expected]) => arr.length < expected && arr.length > 0)

View File

@@ -63,8 +63,8 @@ export function dungeonTemplate(data) {
padding: 0; padding: 0;
font-family: ${bodyFont}; font-family: ${bodyFont};
color: #1a1a1a; color: #1a1a1a;
font-size: 0.75em; font-size: 0.7em;
line-height: 1.4em; line-height: 1.35em;
} }
.content-page { .content-page {
height: 100vh; height: 100vh;
@@ -91,7 +91,7 @@ export function dungeonTemplate(data) {
font-family: ${quoteFont}; font-family: ${quoteFont};
margin: 0.3em 0 0.6em; margin: 0.3em 0 0.6em;
font-size: 0.85em; font-size: 0.85em;
line-height: 1.4em; line-height: 1.35em;
} }
.columns { .columns {
display: grid; display: grid;
@@ -102,7 +102,7 @@ export function dungeonTemplate(data) {
.col { .col {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 0.3em; gap: 0.25em;
overflow-wrap: break-word; overflow-wrap: break-word;
word-break: normal; word-break: normal;
hyphens: auto; hyphens: auto;
@@ -110,7 +110,7 @@ export function dungeonTemplate(data) {
.section-block { .section-block {
break-inside: avoid; break-inside: avoid;
page-break-inside: avoid; page-break-inside: avoid;
margin-bottom: 0.4em; margin-bottom: 0.3em;
} }
h2 { h2 {
font-family: ${headingFont}; font-family: ${headingFont};
@@ -129,29 +129,29 @@ export function dungeonTemplate(data) {
page-break-inside: avoid; page-break-inside: avoid;
} }
.room h3 { .room h3 {
margin: 0.15em 0 0.08em; margin: 0.1em 0 0.05em;
font-size: 0.95em; font-size: 0.95em;
font-weight: bold; font-weight: bold;
color: #1a1a1a; color: #1a1a1a;
} }
.room p { .room p {
margin: 0 0 0.35em; margin: 0 0 0.25em;
font-size: 0.9em; font-size: 0.85em;
font-weight: normal; font-weight: normal;
line-height: 1.35em; line-height: 1.3em;
} }
.encounter, .npc, .treasure, .plot-resolution { .encounter, .npc, .treasure, .plot-resolution {
margin: 0 0 0.35em; margin: 0 0 0.35em;
break-inside: avoid; break-inside: avoid;
page-break-inside: avoid; page-break-inside: avoid;
font-size: 0.9em; font-size: 0.85em;
line-height: 1.35em; line-height: 1.3em;
} }
.random-events { .random-events {
margin: 0.2em 0; margin: 0.2em 0;
break-inside: avoid; break-inside: avoid;
page-break-inside: avoid; page-break-inside: avoid;
font-size: 0.9em; font-size: 0.85em;
} }
.random-events table { .random-events table {
margin-top: 0.15em; margin-top: 0.15em;
@@ -228,8 +228,8 @@ export function dungeonTemplate(data) {
} }
li { li {
margin: 0.08em 0; margin: 0.08em 0;
font-size: 0.9em; font-size: 0.85em;
line-height: 1.35em; line-height: 1.3em;
} }
</style> </style>
</head> </head>
@@ -268,12 +268,30 @@ export function dungeonTemplate(data) {
${data.rooms && data.rooms.length > 0 ? ` ${data.rooms && data.rooms.length > 0 ? `
<div class="section-block"> <div class="section-block">
<h2>Locations</h2> <h2>Locations</h2>
${data.rooms.map(room => ` ${data.rooms.map(room => {
let desc = room.description || '';
// Truncate to 2 sentences max
const sentences = desc.match(/[^.!?]+[.!?]+/g) || [desc];
if (sentences.length > 2) {
desc = sentences.slice(0, 2).join(' ').trim();
}
// Also limit by character count (~150 chars)
if (desc.length > 150) {
desc = desc.substring(0, 147).trim();
const lastPeriod = desc.lastIndexOf('.');
if (lastPeriod > 100) {
desc = desc.substring(0, lastPeriod + 1);
} else {
desc += '...';
}
}
return `
<div class="room"> <div class="room">
<h3>${escapeHtml(room.name)}</h3> <h3>${escapeHtml(room.name)}</h3>
<p>${escapeHtml(room.description)}</p> <p>${escapeHtml(desc)}</p>
</div> </div>
`).join('')} `;
}).join('')}
</div> </div>
` : ''} ` : ''}
</div> </div>
@@ -287,7 +305,20 @@ export function dungeonTemplate(data) {
${data.encounters.map((encounter, index) => { ${data.encounters.map((encounter, index) => {
// Truncate details to 4 sentences max to prevent overflow // Truncate details to 4 sentences max to prevent overflow
let details = encounter.details || ''; let details = encounter.details || '';
// Keep location prefix in details (format: "Location Name: details") // Remove encounter name if it appears at start
if (details.toLowerCase().startsWith(encounter.name.toLowerCase())) {
details = details.substring(encounter.name.length).replace(/^:\s*/, '').trim();
}
// Remove location prefix if present (format: "Location Name: description")
// Handle multiple colons - strip the first one that looks like a location
const locationMatch = details.match(/^([^:]+):\s*(.+)$/);
if (locationMatch) {
const potentialLocation = locationMatch[1].trim();
// If it looks like a location name (capitalized, not too long), remove it
if (potentialLocation.length > 3 && potentialLocation.length < 50 && /^[A-Z]/.test(potentialLocation)) {
details = locationMatch[2].trim();
}
}
// Split into sentences and keep only first 4 // Split into sentences and keep only first 4
const sentences = details.match(/[^.!?]+[.!?]+/g) || [details]; const sentences = details.match(/[^.!?]+[.!?]+/g) || [details];
if (sentences.length > 4) { if (sentences.length > 4) {
@@ -322,22 +353,38 @@ export function dungeonTemplate(data) {
${data.treasure && data.treasure.length > 0 ? ` ${data.treasure && data.treasure.length > 0 ? `
<div class="section-block"> <div class="section-block">
<h2>Treasure</h2> <h2>Treasure</h2>
${data.treasure.map(item => ` ${data.treasure.map(item => {
<div class="treasure"> if (typeof item === 'object' && item.name && item.description) {
${typeof item === 'object' && item.name ? `<strong>${escapeHtml(item.name)}</strong> — ${escapeHtml(item.description)}` : escapeHtml(item)} return `<div class="treasure"><strong>${escapeHtml(item.name)}</strong> — ${escapeHtml(item.description)}</div>`;
</div> } else if (typeof item === 'string') {
`).join('')} // Handle string format "Name — Description"
const parts = item.split(/[—–-]/);
if (parts.length >= 2) {
return `<div class="treasure"><strong>${escapeHtml(parts[0].trim())}</strong> — ${escapeHtml(parts.slice(1).join(' ').trim())}</div>`;
}
return `<div class="treasure">${escapeHtml(item)}</div>`;
}
return '';
}).filter(Boolean).join('')}
</div> </div>
` : ''} ` : ''}
${data.npcs && data.npcs.length > 0 ? ` ${data.npcs && data.npcs.length > 0 ? `
<div class="section-block"> <div class="section-block">
<h2>NPCs</h2> <h2>NPCs</h2>
${data.npcs.map(npc => ` ${data.npcs.map(npc => {
<div class="npc"> if (typeof npc === 'object' && npc.name && npc.trait) {
<strong>${escapeHtml(npc.name)}</strong>: ${escapeHtml(npc.trait)} return `<div class="npc"><strong>${escapeHtml(npc.name)}</strong>: ${escapeHtml(npc.trait)}</div>`;
</div> } else if (typeof npc === 'string') {
`).join('')} // Handle string format "Name: Description"
const parts = npc.split(/:/);
if (parts.length >= 2) {
return `<div class="npc"><strong>${escapeHtml(parts[0].trim())}</strong>: ${escapeHtml(parts.slice(1).join(':').trim())}</div>`;
}
return `<div class="npc">${escapeHtml(npc)}</div>`;
}
return '';
}).filter(Boolean).join('')}
</div> </div>
` : ''} ` : ''}
@@ -345,19 +392,19 @@ export function dungeonTemplate(data) {
<div class="section-block"> <div class="section-block">
<h2>Plot Resolutions</h2> <h2>Plot Resolutions</h2>
${data.plotResolutions.map(resolution => { ${data.plotResolutions.map(resolution => {
// Truncate to 3 sentences max to prevent overflow // Truncate to 2 sentences max to prevent overflow
let text = resolution || ''; let text = resolution || '';
// Split into sentences and keep only first 3 // Split into sentences and keep only first 2
const sentences = text.match(/[^.!?]+[.!?]+/g) || [text]; const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
if (sentences.length > 3) { if (sentences.length > 2) {
text = sentences.slice(0, 3).join(' ').trim(); text = sentences.slice(0, 2).join(' ').trim();
} }
// Also limit by character count as fallback (max ~200 chars) // Also limit by character count as fallback (max ~150 chars)
if (text.length > 200) { if (text.length > 150) {
text = text.substring(0, 197).trim(); text = text.substring(0, 147).trim();
// Try to end at a sentence boundary // Try to end at a sentence boundary
const lastPeriod = text.lastIndexOf('.'); const lastPeriod = text.lastIndexOf('.');
if (lastPeriod > 150) { if (lastPeriod > 100) {
text = text.substring(0, lastPeriod + 1); text = text.substring(0, lastPeriod + 1);
} else { } else {
text += '...'; text += '...';

View File

@@ -5,6 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"test:integration": "node --test test/integration.test.js",
"lint": "eslint .", "lint": "eslint .",
"start": "node index.js" "start": "node index.js"
}, },

91
test/integration.test.js Normal file
View File

@@ -0,0 +1,91 @@
import { test } from "node:test";
import assert from "node:assert";
import { generateDungeon } from "../dungeonGenerator.js";
import { generatePDF } from "../generatePDF.js";
import fs from "fs/promises";
import path from "path";
const OLLAMA_API_URL = process.env.OLLAMA_API_URL;
test("Integration tests", { skip: !OLLAMA_API_URL }, async (t) => {
let dungeonData;
await t.test("Generate dungeon", async () => {
dungeonData = await generateDungeon();
assert(dungeonData, "Dungeon data should be generated");
});
await t.test("Title is 2-4 words, no colons", () => {
assert(dungeonData.title, "Title should exist");
const words = dungeonData.title.split(/\s+/);
assert(words.length >= 2 && words.length <= 4, `Title should be 2-4 words, got ${words.length}: "${dungeonData.title}"`);
assert(!dungeonData.title.includes(":"), `Title should not contain colons: "${dungeonData.title}"`);
});
await t.test("Flavor text is ≤60 words", () => {
assert(dungeonData.flavor, "Flavor text should exist");
const words = dungeonData.flavor.split(/\s+/);
assert(words.length <= 60, `Flavor text should be ≤60 words, got ${words.length}`);
});
await t.test("Hooks have no title prefixes", () => {
assert(dungeonData.hooksRumors, "Hooks should exist");
dungeonData.hooksRumors.forEach((hook, i) => {
assert(!hook.match(/^[^:]+:\s/), `Hook ${i + 1} should not have title prefix: "${hook}"`);
});
});
await t.test("Exactly 6 random events", () => {
assert(dungeonData.randomEvents, "Random events should exist");
assert.strictEqual(dungeonData.randomEvents.length, 6, `Should have exactly 6 random events, got ${dungeonData.randomEvents.length}`);
});
await t.test("Encounter details don't include encounter name", () => {
assert(dungeonData.encounters, "Encounters should exist");
dungeonData.encounters.forEach((encounter) => {
if (encounter.details) {
const detailsLower = encounter.details.toLowerCase();
const nameLower = encounter.name.toLowerCase();
assert(!detailsLower.startsWith(nameLower), `Encounter "${encounter.name}" details should not start with encounter name: "${encounter.details}"`);
}
});
});
await t.test("Treasure uses em-dash format, no 'description' text", () => {
assert(dungeonData.treasure, "Treasure should exist");
dungeonData.treasure.forEach((item, i) => {
if (typeof item === "object" && item.description) {
assert(!item.description.toLowerCase().startsWith("description"), `Treasure ${i + 1} description should not start with 'description': "${item.description}"`);
}
});
});
await t.test("NPCs have no 'description' text", () => {
assert(dungeonData.npcs, "NPCs should exist");
dungeonData.npcs.forEach((npc, i) => {
if (npc.trait) {
assert(!npc.trait.toLowerCase().startsWith("description"), `NPC ${i + 1} trait should not start with 'description': "${npc.trait}"`);
}
});
});
await t.test("PDF fits on one page", async () => {
const testPdfPath = path.join(process.cwd(), "test-output.pdf");
try {
await generatePDF(dungeonData, testPdfPath);
const pdfBuffer = await fs.readFile(testPdfPath);
// Check PDF page count by counting "%%EOF" markers (rough estimate)
const pdfText = pdfBuffer.toString("binary");
const pageCount = (pdfText.match(/\/Type\s*\/Page[^s]/g) || []).length;
// Should be 1 page for content, or 2 if map exists
const expectedPages = dungeonData.map ? 2 : 1;
assert(pageCount <= expectedPages, `PDF should have ≤${expectedPages} page(s), got ${pageCount}`);
} finally {
try {
await fs.unlink(testPdfPath);
} catch {
// Ignore cleanup errors
}
}
});
});