Compare commits
10 Commits
2025-10-29
...
2026-01-20
| Author | SHA1 | Date | |
|---|---|---|---|
| 96223b81e6 | |||
| 9332ac6f94 | |||
| c54b1a6082 | |||
| 3b91ce3068 | |||
| c7bb0f04df | |||
| 05526b06d6 | |||
| af447da042 | |||
| c48188792d | |||
| 1059eced53 | |||
|
|
96480a351f |
10
.gitignore
vendored
10
.gitignore
vendored
@@ -2,3 +2,13 @@
|
||||
*.png
|
||||
.env
|
||||
node_modules/**
|
||||
|
||||
# macOS dotfiles
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
.env.example
|
||||
|
||||
25
README.md
25
README.md
@@ -29,7 +29,7 @@ Scrollsmith is a Node.js tool for generating Dungeons & Dragons one-page dungeon
|
||||
OLLAMA_API_URL=http://localhost:3000/api/chat/completions
|
||||
OLLAMA_API_KEY=your_api_key_here
|
||||
COMFYUI_URL=http://192.168.1.124:8188
|
||||
````
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -43,6 +43,27 @@ npm install
|
||||
|
||||
---
|
||||
|
||||
## API Configuration
|
||||
|
||||
The client automatically infers the API type from the endpoint URL, making it flexible for different deployment scenarios.
|
||||
|
||||
### Direct Ollama API
|
||||
For direct Ollama API calls, set:
|
||||
```env
|
||||
OLLAMA_API_URL=http://localhost:11434/api/generate
|
||||
```
|
||||
|
||||
### Open WebUI API
|
||||
For Open WebUI API calls, set:
|
||||
```env
|
||||
OLLAMA_API_URL=http://localhost:3000/api/chat/completions
|
||||
OLLAMA_API_KEY=your_open_webui_api_key
|
||||
```
|
||||
|
||||
> Note: The API type is automatically inferred from the endpoint URL. If the URL contains `/api/chat/completions`, it uses Open WebUI API. If it contains `/api/generate`, it uses direct Ollama API. No `OLLAMA_API_TYPE` environment variable is required.
|
||||
|
||||
---
|
||||
|
||||
## Usage
|
||||
|
||||
1. Make sure your Ollama server is running and `.env` is configured.
|
||||
@@ -78,4 +99,4 @@ Optional: update the map path in `index.js` if you have a local dungeon map.
|
||||
|
||||
## License
|
||||
|
||||
PROPRIETARY
|
||||
PROPRIETARY
|
||||
@@ -19,25 +19,472 @@ function parseList(raw) {
|
||||
}
|
||||
|
||||
function parseObjects(raw, type = "rooms") {
|
||||
let cleanedRaw = raw.replace(/Intermediate Rooms:/i, "").replace(/Climax Room:/i, "").trim();
|
||||
return cleanedRaw
|
||||
.split(/\n?\d+[).]\s+/)
|
||||
.map(entry => cleanText(entry))
|
||||
.filter(Boolean)
|
||||
.map(entry => {
|
||||
const [name, ...descParts] = entry.split(/[-–—:]/);
|
||||
const desc = descParts.join(" ").trim();
|
||||
if (type === "rooms") return { name: name.trim(), description: desc };
|
||||
if (type === "encounters") return { name: name.trim(), details: desc };
|
||||
if (type === "npcs") return { name: name.trim(), trait: desc };
|
||||
return entry;
|
||||
const cleanedRaw = raw.replace(/Intermediate Rooms:/i, "").replace(/Climax Room:/i, "").trim();
|
||||
const mapper = (entry) => {
|
||||
if (type === "encounters") {
|
||||
// For encounters, format is "Encounter Name: Location Name: details"
|
||||
const parts = entry.split(/:/);
|
||||
if (parts.length >= 3) {
|
||||
const name = parts[0].trim();
|
||||
// Skip placeholder names
|
||||
if (name.toLowerCase().includes('location name') || name.toLowerCase().includes('encounter name')) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name: name,
|
||||
details: parts.slice(1).join(":").trim()
|
||||
};
|
||||
} else if (parts.length === 2) {
|
||||
const name = parts[0].trim();
|
||||
if (name.toLowerCase().includes('location name') || name.toLowerCase().includes('encounter name')) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
name: name,
|
||||
details: parts[1].trim()
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// 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 cleanName = name.trim();
|
||||
// Skip placeholder names
|
||||
if (cleanName.toLowerCase().includes('location name') ||
|
||||
cleanName.toLowerCase().includes('npc name') ||
|
||||
cleanName.toLowerCase().includes('treasure name') ||
|
||||
cleanName.toLowerCase().includes('actual ')) {
|
||||
return null;
|
||||
}
|
||||
let desc = descParts.join(" ").trim();
|
||||
if (type === "npcs") desc = desc.replace(/^description\s*:?\s*/i, '').trim();
|
||||
const obj = { name: cleanName };
|
||||
if (type === "rooms") return { ...obj, description: desc };
|
||||
if (type === "npcs") return { ...obj, trait: desc };
|
||||
if (type === "treasure") return { ...obj, description: desc };
|
||||
return null;
|
||||
};
|
||||
return cleanedRaw.split(/\n?\d+[).]\s+/).map(cleanText).filter(Boolean).map(mapper).filter(Boolean);
|
||||
}
|
||||
|
||||
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*(.+))?$/);
|
||||
if (match) {
|
||||
const [, , name, details] = match;
|
||||
return name && details ? { name: name.trim(), details: details.trim() } : null;
|
||||
}
|
||||
const colonSplit = text.split(/[:]/);
|
||||
if (colonSplit.length > 1) {
|
||||
return {
|
||||
name: colonSplit[0].replace(/^\d+\s+|Encounter\s+\d+\s+/i, "").trim(),
|
||||
details: colonSplit.slice(1).join(":").trim()
|
||||
};
|
||||
}
|
||||
const nameMatch = text.match(/^\d+\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)/);
|
||||
if (nameMatch) {
|
||||
return {
|
||||
name: nameMatch[1],
|
||||
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+|Encounter\s+\d+\s+/i, "").trim() };
|
||||
};
|
||||
|
||||
const splitCombinedEncounters = (encounters) => {
|
||||
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;
|
||||
console.warn("Encounters appear combined, attempting to split...");
|
||||
const combinedText = encounters[0].details || "";
|
||||
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))
|
||||
? split.map((text, idx) => parseEncounterText(text, idx)).filter(e => e?.name && e?.details?.length > 10)
|
||||
: encounters;
|
||||
};
|
||||
|
||||
const splitCombinedNPCs = (npcs) => {
|
||||
const shouldSplit = npcs.length === 1 && npcs[0].trait?.length > 80;
|
||||
if (!shouldSplit) return npcs;
|
||||
console.warn("NPCs appear combined, attempting to split...");
|
||||
const split = npcs[0].trait.split(/(?=[A-Z][a-z]+\s+[A-Z][a-z]+\s*:)/).filter(Boolean);
|
||||
return split.length > 1
|
||||
? split.map(text => {
|
||||
const [name, ...traitParts] = text.split(/[:]/);
|
||||
return { name: name.trim(), trait: traitParts.join(":").trim() };
|
||||
}).filter(n => n.name && n.trait?.length > 10)
|
||||
: npcs;
|
||||
};
|
||||
|
||||
const parseTreasureText = (text, idx, splitTreasures) => {
|
||||
if (idx === splitTreasures.length - 1 && text.length < 40) {
|
||||
return { name: splitTreasures[idx - 1]?.split(/\s+/).slice(-2).join(" ") || `Treasure ${idx}`, description: text };
|
||||
}
|
||||
const dashSplit = text.split(/[—]/);
|
||||
if (dashSplit.length === 2) return { name: dashSplit[0].trim(), description: dashSplit[1].trim() };
|
||||
if (text.length < 30 && /^[A-Z]/.test(text)) return { name: text.trim(), description: "" };
|
||||
return null;
|
||||
};
|
||||
|
||||
const splitCombinedTreasures = (treasure) => {
|
||||
const shouldSplit = treasure.length === 1 && treasure[0].description?.length > 60;
|
||||
if (!shouldSplit) return treasure;
|
||||
console.warn("Treasures appear combined, attempting to split...");
|
||||
const split = treasure[0].description.split(/\s+—\s+/).filter(Boolean);
|
||||
if (split.length <= 1) return treasure;
|
||||
const parsed = split.map((text, idx) => parseTreasureText(text, idx, split)).filter(t => t?.name && t?.description);
|
||||
if (parsed.length > 0) return parsed;
|
||||
const nameDescPairs = treasure[0].description.match(/([A-Z][^—]+?)\s+—\s+([^—]+?)(?=\s+[A-Z][^—]+\s+—|$)/g);
|
||||
return nameDescPairs
|
||||
? nameDescPairs.map(pair => {
|
||||
const match = pair.match(/([^—]+)\s+—\s+(.+)/);
|
||||
return match ? { name: match[1].trim(), description: match[2].trim() } : null;
|
||||
}).filter(t => t)
|
||||
: treasure;
|
||||
};
|
||||
|
||||
function extractCanonicalNames(dungeonData) {
|
||||
const names = {
|
||||
npcs: [],
|
||||
rooms: [],
|
||||
factions: []
|
||||
};
|
||||
|
||||
// Extract NPC names
|
||||
if (dungeonData.npcs) {
|
||||
dungeonData.npcs.forEach(npc => {
|
||||
if (npc.name) names.npcs.push(npc.name.trim());
|
||||
});
|
||||
}
|
||||
|
||||
// Extract room names
|
||||
if (dungeonData.rooms) {
|
||||
dungeonData.rooms.forEach(room => {
|
||||
if (room.name) names.rooms.push(room.name.trim());
|
||||
});
|
||||
}
|
||||
|
||||
// Extract faction names from core concepts (if available)
|
||||
if (dungeonData.coreConcepts) {
|
||||
const factionMatch = dungeonData.coreConcepts.match(/Primary Faction[:\s]+([^.]+)/i);
|
||||
if (factionMatch) {
|
||||
names.factions.push(factionMatch[1].trim());
|
||||
}
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
|
||||
function validateNameConsistency(dungeonData) {
|
||||
const canonicalNames = extractCanonicalNames(dungeonData);
|
||||
const fixes = [];
|
||||
|
||||
// Fix NPC names in all text fields - ensure consistency across all references
|
||||
canonicalNames.npcs.forEach(canonicalName => {
|
||||
// Check and fix in flavor text
|
||||
if (dungeonData.flavor) {
|
||||
const original = dungeonData.flavor;
|
||||
// Use canonical name as the source of truth
|
||||
dungeonData.flavor = dungeonData.flavor.replace(new RegExp(canonicalName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), canonicalName);
|
||||
if (original !== dungeonData.flavor) {
|
||||
fixes.push(`Fixed NPC name in flavor text: ${canonicalName}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Check and fix in hooks
|
||||
if (dungeonData.hooksRumors) {
|
||||
dungeonData.hooksRumors = dungeonData.hooksRumors.map(hook => {
|
||||
const original = hook;
|
||||
const fixed = hook.replace(new RegExp(canonicalName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), canonicalName);
|
||||
if (original !== fixed) {
|
||||
fixes.push(`Fixed NPC name in hook: ${canonicalName}`);
|
||||
}
|
||||
return fixed;
|
||||
});
|
||||
}
|
||||
|
||||
// Check and fix in encounters
|
||||
if (dungeonData.encounters) {
|
||||
dungeonData.encounters.forEach(encounter => {
|
||||
if (encounter.details) {
|
||||
const original = encounter.details;
|
||||
encounter.details = encounter.details.replace(new RegExp(canonicalName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), canonicalName);
|
||||
if (original !== encounter.details) {
|
||||
fixes.push(`Fixed NPC name in encounter: ${canonicalName}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Check and fix in plot resolutions
|
||||
if (dungeonData.plotResolutions) {
|
||||
dungeonData.plotResolutions = dungeonData.plotResolutions.map(resolution => {
|
||||
const original = resolution;
|
||||
const fixed = resolution.replace(new RegExp(canonicalName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), canonicalName);
|
||||
if (original !== fixed) {
|
||||
fixes.push(`Fixed NPC name in plot resolution: ${canonicalName}`);
|
||||
}
|
||||
return fixed;
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Fix room names in encounters and other text
|
||||
canonicalNames.rooms.forEach(canonicalRoom => {
|
||||
if (dungeonData.encounters) {
|
||||
dungeonData.encounters.forEach(encounter => {
|
||||
if (encounter.details) {
|
||||
const original = encounter.details;
|
||||
encounter.details = encounter.details.replace(new RegExp(canonicalRoom.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi'), canonicalRoom);
|
||||
if (original !== encounter.details) {
|
||||
fixes.push(`Fixed room name in encounter: ${canonicalRoom}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return fixes;
|
||||
}
|
||||
|
||||
function standardizeEncounterLocations(encounters, rooms) {
|
||||
if (!encounters || !rooms) return { encounters, fixes: [] };
|
||||
|
||||
const roomNames = rooms.map(r => r.name.trim());
|
||||
const fixes = [];
|
||||
const fixedEncounters = encounters.map(encounter => {
|
||||
if (!encounter.details) return encounter;
|
||||
|
||||
let details = encounter.details.trim();
|
||||
const original = details;
|
||||
|
||||
// Check if details start with a room name
|
||||
for (const roomName of roomNames) {
|
||||
// Check for room name at start (with or without colon)
|
||||
const roomNameRegex = new RegExp(`^${roomName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\s*:?\\s*`, 'i');
|
||||
if (roomNameRegex.test(details)) {
|
||||
// Already has location, ensure format is "Location: Description"
|
||||
if (!details.match(new RegExp(`^${roomName.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}:`, 'i'))) {
|
||||
details = details.replace(roomNameRegex, `${roomName}: `);
|
||||
fixes.push(`Standardized location format for encounter: ${encounter.name}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (original !== details) {
|
||||
encounter.details = details;
|
||||
}
|
||||
|
||||
return encounter;
|
||||
});
|
||||
|
||||
return { encounters: fixedEncounters, fixes };
|
||||
}
|
||||
|
||||
// Content validation functions
|
||||
function validateContentCompleteness(dungeonData) {
|
||||
const issues = [];
|
||||
const checks = [
|
||||
['title', 0, 'Missing title'],
|
||||
['flavor', 20, 'Flavor text too short'],
|
||||
['hooksRumors', 4, 'Expected at least 4 hooks'],
|
||||
['rooms', 5, 'Expected at least 5 rooms'],
|
||||
['encounters', 6, 'Expected at least 6 encounters'],
|
||||
['npcs', 4, 'Expected at least 4 NPCs'],
|
||||
['treasure', 4, 'Expected at least 4 treasures'],
|
||||
['randomEvents', 6, 'Expected 6 random events'],
|
||||
['plotResolutions', 4, 'Expected at least 4 plot resolutions']
|
||||
];
|
||||
|
||||
checks.forEach(([key, min, msg]) => {
|
||||
const val = dungeonData[key];
|
||||
if (!val || (Array.isArray(val) ? val.length < min : val.trim().length < min)) {
|
||||
issues.push(`${msg}${Array.isArray(val) ? `, got ${val?.length || 0}` : ''}`);
|
||||
}
|
||||
});
|
||||
|
||||
// Check descriptions
|
||||
dungeonData.rooms?.forEach((r, i) => {
|
||||
if (!r.description || r.description.trim().length < 20) {
|
||||
issues.push(`Room ${i + 1} (${r.name}) description too short`);
|
||||
}
|
||||
});
|
||||
dungeonData.encounters?.forEach((e, i) => {
|
||||
if (!e.details || e.details.trim().length < 30) {
|
||||
issues.push(`Encounter ${i + 1} (${e.name}) details too short`);
|
||||
}
|
||||
});
|
||||
dungeonData.npcs?.forEach((n, i) => {
|
||||
if (!n.trait || n.trait.trim().length < 30) {
|
||||
issues.push(`NPC ${i + 1} (${n.name}) description too short`);
|
||||
}
|
||||
});
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
function validateContentQuality(dungeonData) {
|
||||
const issues = [];
|
||||
const vagueWords = /\b(some|various|several|many|few|things|stuff|items|objects)\b/gi;
|
||||
|
||||
const checkVague = (text, ctx) => {
|
||||
if (!text) return;
|
||||
const matches = text.match(vagueWords);
|
||||
if (matches?.length > 2) {
|
||||
issues.push(`${ctx} contains vague language: "${matches.slice(0, 3).join('", "')}"`);
|
||||
}
|
||||
};
|
||||
|
||||
checkVague(dungeonData.flavor, 'Flavor text');
|
||||
dungeonData.rooms?.forEach(r => checkVague(r.description, `Room "${r.name}"`));
|
||||
dungeonData.encounters?.forEach(e => checkVague(e.details, `Encounter "${e.name}"`));
|
||||
dungeonData.npcs?.forEach(n => checkVague(n.trait, `NPC "${n.name}"`));
|
||||
dungeonData.rooms?.forEach(r => {
|
||||
if (r.description?.length < 50) {
|
||||
issues.push(`Room "${r.name}" description too short`);
|
||||
}
|
||||
});
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
function validateContentStructure(dungeonData) {
|
||||
const issues = [];
|
||||
|
||||
dungeonData.rooms?.forEach((r, i) => {
|
||||
if (!r.name?.trim()) issues.push(`Room ${i + 1} missing name`);
|
||||
if (r.name?.split(/\s+/).length > 6) issues.push(`Room "${r.name}" name too long`);
|
||||
});
|
||||
|
||||
dungeonData.encounters?.forEach((e, i) => {
|
||||
if (!e.name?.trim()) issues.push(`Encounter ${i + 1} missing name`);
|
||||
if (e.name?.split(/\s+/).length > 6) issues.push(`Encounter "${e.name}" name too long`);
|
||||
if (e.details && !e.details.match(/^[^:]+:\s/)) {
|
||||
issues.push(`Encounter "${e.name}" details missing location prefix`);
|
||||
}
|
||||
});
|
||||
|
||||
dungeonData.npcs?.forEach((n, i) => {
|
||||
if (!n.name?.trim()) issues.push(`NPC ${i + 1} missing name`);
|
||||
if (n.name?.split(/\s+/).length > 4) issues.push(`NPC "${n.name}" name too long`);
|
||||
});
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
function validateNarrativeCoherence(dungeonData) {
|
||||
const issues = [];
|
||||
const factionMatch = dungeonData.coreConcepts?.match(/Primary Faction[:\s]+([^.]+)/i);
|
||||
const factionName = factionMatch?.[1]?.trim();
|
||||
|
||||
if (dungeonData.encounters && dungeonData.rooms) {
|
||||
const roomNames = dungeonData.rooms.map(r => r.name.trim().toLowerCase());
|
||||
dungeonData.encounters.forEach(e => {
|
||||
const locMatch = e.details?.match(/^([^:]+):/);
|
||||
if (locMatch) {
|
||||
const locName = locMatch[1].trim().toLowerCase();
|
||||
if (!roomNames.some(rn => locName.includes(rn) || rn.includes(locName))) {
|
||||
issues.push(`Encounter "${e.name}" references unknown location "${locMatch[1]}"`);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (factionName) {
|
||||
const factionLower = factionName.toLowerCase();
|
||||
let refs = 0;
|
||||
dungeonData.npcs?.forEach(n => {
|
||||
if (n.trait?.toLowerCase().includes(factionLower)) refs++;
|
||||
});
|
||||
dungeonData.encounters?.forEach(e => {
|
||||
if (e.details?.toLowerCase().includes(factionLower)) refs++;
|
||||
});
|
||||
if (refs < 2) {
|
||||
issues.push(`Faction "${factionName}" poorly integrated (${refs} references)`);
|
||||
}
|
||||
}
|
||||
|
||||
return issues;
|
||||
}
|
||||
|
||||
function validateAndFixContent(dungeonData) {
|
||||
const allFixes = [];
|
||||
const allIssues = [];
|
||||
|
||||
// Validate name consistency
|
||||
const nameFixes = validateNameConsistency(dungeonData);
|
||||
allFixes.push(...nameFixes);
|
||||
|
||||
// Standardize encounter locations and add missing ones
|
||||
if (dungeonData.encounters && dungeonData.rooms) {
|
||||
const roomNames = dungeonData.rooms.map(r => r.name.trim());
|
||||
dungeonData.encounters.forEach((encounter, idx) => {
|
||||
if (!encounter.details) return;
|
||||
// If encounter doesn't start with a location, assign one
|
||||
if (!encounter.details.match(/^[^:]+:\s/)) {
|
||||
// Assign to a random room, or cycle through rooms
|
||||
const roomIdx = idx % roomNames.length;
|
||||
const roomName = roomNames[roomIdx];
|
||||
encounter.details = `${roomName}: ${encounter.details}`;
|
||||
allFixes.push(`Added location "${roomName}" to encounter "${encounter.name}"`);
|
||||
}
|
||||
});
|
||||
const locationResult = standardizeEncounterLocations(dungeonData.encounters, dungeonData.rooms);
|
||||
dungeonData.encounters = locationResult.encounters;
|
||||
allFixes.push(...locationResult.fixes);
|
||||
}
|
||||
|
||||
// Run content validation
|
||||
const completenessIssues = validateContentCompleteness(dungeonData);
|
||||
const qualityIssues = validateContentQuality(dungeonData);
|
||||
const structureIssues = validateContentStructure(dungeonData);
|
||||
const coherenceIssues = validateNarrativeCoherence(dungeonData);
|
||||
|
||||
allIssues.push(...completenessIssues, ...qualityIssues, ...structureIssues, ...coherenceIssues);
|
||||
|
||||
if (allFixes.length > 0) {
|
||||
console.log("\n[Validation] Applied fixes:");
|
||||
allFixes.forEach(fix => console.log(` - ${fix}`));
|
||||
}
|
||||
|
||||
if (allIssues.length > 0) {
|
||||
console.log("\n[Validation] Content quality issues found:");
|
||||
allIssues.forEach(issue => console.warn(` ⚠ ${issue}`));
|
||||
} else {
|
||||
console.log("\n[Validation] Content quality checks passed");
|
||||
}
|
||||
|
||||
return dungeonData;
|
||||
}
|
||||
|
||||
export async function generateDungeon() {
|
||||
// Step 1: Titles
|
||||
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:
|
||||
|
||||
- OSR / classic tabletop: gritty, mysterious, old-school
|
||||
@@ -47,12 +494,20 @@ Each title should come from a different style or theme. Make the set varied and
|
||||
- Weird fantasy: uncanny, surreal, unsettling
|
||||
- Whimsical: fun, quirky, playful
|
||||
|
||||
CRITICAL: Ensure all spelling is correct. Double-check all words before outputting.
|
||||
Avoid repeating materials or adjectives. Absolutely do not use the words "Obsidian" or "Clockwork" in any title. Do not include explanations, markdown, or preambles. Do not include the style or theme in parenthesis. Only the 50 numbered titles.`,
|
||||
undefined, 5, "Step 1: Titles"
|
||||
);
|
||||
const generatedTitles = parseList(generatedTitlesRaw);
|
||||
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);
|
||||
|
||||
// Step 2: Core Concepts
|
||||
@@ -72,14 +527,23 @@ Example:
|
||||
const flavorHooksRaw = await callOllama(
|
||||
`Based on the title "${title}" and these core concepts:
|
||||
${coreConcepts}
|
||||
Write a single evocative paragraph describing the location. Maximum 2 sentences. Maximum 100 words. Then, generate 3 short adventure hooks or rumors.
|
||||
The hooks should reference the central conflict, faction, and dynamic element.
|
||||
Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered list for the hooks. Plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
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.
|
||||
|
||||
EXAMPLE OF GOOD HOOK:
|
||||
"A merchant's cart was found abandoned near the entrance, its cargo of rare herbs scattered. The merchant's journal mentions strange lights in the depths and a warning about 'the watchers'."
|
||||
|
||||
CRITICAL: Hooks must be concise to fit in a single column on a one-page dungeon layout. Each hook must be 25-30 words maximum. Be specific with details, not vague.
|
||||
CRITICAL: Ensure all spelling is correct. Double-check all words, especially proper nouns and technical terms.
|
||||
Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered list for the hooks. Plain text only. Do not use em-dashes (—) anywhere in the output. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
undefined, 5, "Step 3: Flavor & Hooks"
|
||||
);
|
||||
const [flavorSection, hooksSection] = flavorHooksRaw.split(/Hooks & Rumors[:\n]/i);
|
||||
const flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, ""));
|
||||
const hooksRumors = parseList(hooksSection || "");
|
||||
let flavor = cleanText(flavorSection.replace(/Description[:\n]*/i, ""));
|
||||
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("Hooks & Rumors:", hooksRumors);
|
||||
|
||||
@@ -88,18 +552,62 @@ Output two sections labeled "Description:" and "Hooks & Rumors:". Use a numbered
|
||||
`Based on the title "${title}", description "${flavor}", and these core concepts:
|
||||
${coreConcepts}
|
||||
Generate two key rooms that define the dungeon's narrative arc.
|
||||
1. **Entrance Room:** Give it a name and a description that sets the tone and introduces the environmental hazard.
|
||||
2. **Climax Room:** Give it a name and a description that includes the primary faction and the central conflict.
|
||||
Output as two numbered items, plain text only. Do not use bolded headings. Do not include any intro or other text. Only the numbered list. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
CRITICAL: These rooms need rich environmental and tactical details with multiple interaction possibilities.
|
||||
|
||||
EXAMPLE OF GOOD ROOM DESCRIPTION:
|
||||
"Chamber of Echoes: Flickering torchlight casts dancing shadows across moss-covered walls. A constant dripping echoes from stalactites overhead, and the air smells of damp earth and ozone. Three stone pillars provide cover, while a raised dais in the center offers high ground. A rusted lever on the west wall controls a hidden portcullis. The floor is slick with moisture, making movement difficult."
|
||||
|
||||
1. Entrance Room: Give it a name (max 5 words) and a description (25-35 words) that MUST include:
|
||||
- Immediate observable features and environmental details (lighting, sounds, smells, textures, temperature, visibility)
|
||||
- Interactable elements that players can use (levers, objects, portals, mechanisms, environmental hazards)
|
||||
- Tactical considerations (cover, elevation, movement restrictions, line of sight)
|
||||
- Sets the tone and introduces the environmental hazard/dynamic element
|
||||
|
||||
2. Climax Room: Give it a name (max 5 words) and a description (25-35 words) that MUST include:
|
||||
- Connection to the primary faction and the central conflict
|
||||
- Rich environmental and tactical details
|
||||
- Multiple approach options or solutions
|
||||
- Tactical considerations and environmental factors that affect gameplay
|
||||
|
||||
EXACT FORMAT REQUIRED - each room on its own numbered line:
|
||||
1. Room Name: Description text here.
|
||||
2. Room Name: Description text here.
|
||||
|
||||
CRITICAL: Ensure all spelling is correct. Double-check all words before outputting.
|
||||
CRITICAL: Be specific and concrete. Avoid vague words like "some", "various", "several" without details.
|
||||
Output ONLY the two numbered items, one per line. Use colons (:) to separate room names from descriptions, not em-dashes. Do not use em-dashes (—) anywhere. Do not combine items. Do not use bolded headings. Do not include any intro or other text. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
undefined, 5, "Step 4: Key Rooms"
|
||||
);
|
||||
const [entranceSection, climaxSection] = keyRoomsRaw.split(/\n?2[).] /); // Split on "2. " to separate the two rooms
|
||||
const entranceRoom = parseObjects(entranceSection, "rooms")[0];
|
||||
const climaxRoom = parseObjects(`1. ${climaxSection}`, "rooms")[0]; // Prepend "1. " to make parsing consistent
|
||||
|
||||
// Fix placeholder names by extracting from description
|
||||
const fixRoomName = (room) => {
|
||||
if (!room) return room;
|
||||
if (room.name && (room.name.toLowerCase().includes('room name') || room.name.toLowerCase() === 'room name')) {
|
||||
// Extract room name from description (first 2-4 words before "Description" or similar)
|
||||
const desc = room.description || '';
|
||||
const nameMatch = desc.match(/^([^:]+?)(?:\s+Description|\s*:)/i) || desc.match(/^([A-Z][^.!?]{5,40}?)(?:\s+is\s|\.)/);
|
||||
if (nameMatch) {
|
||||
room.name = nameMatch[1].trim().replace(/^(The|A|An)\s+/i, '').trim();
|
||||
room.description = desc.replace(new RegExp(`^${nameMatch[1]}\\s*(Description|:)?\\s*`, 'i'), '').trim();
|
||||
} else {
|
||||
// Fallback: use first few words of description
|
||||
const words = desc.split(/\s+/).slice(0, 4).join(' ');
|
||||
room.name = words.replace(/^(The|A|An)\s+/i, '').trim();
|
||||
}
|
||||
}
|
||||
return room;
|
||||
};
|
||||
|
||||
if (entranceRoom) fixRoomName(entranceRoom);
|
||||
if (climaxRoom) fixRoomName(climaxRoom);
|
||||
|
||||
console.log("Entrance Room:", entranceRoom);
|
||||
console.log("Climax Room:", climaxRoom);
|
||||
|
||||
// Step 5: Main Content (Locations, Encounters, NPCs, Treasures)
|
||||
// Step 5: Main Content (Locations, Encounters, NPCs, Treasures, Random Events)
|
||||
const mainContentRaw = await callOllama(
|
||||
`Based on the following dungeon elements and the need for narrative flow:
|
||||
Title: "${title}"
|
||||
@@ -109,38 +617,319 @@ ${coreConcepts}
|
||||
Entrance Room: ${JSON.stringify(entranceRoom)}
|
||||
Climax Room: ${JSON.stringify(climaxRoom)}
|
||||
|
||||
Generate the rest of the dungeon's content to fill the space between the entrance and the climax.
|
||||
- **Strictly 3 Locations:** Each with a name and a short description (max 20 words). The description must be a single sentence. It should contain an environmental feature, a puzzle, or an element that connects to the core concepts or the final room.
|
||||
- **Strictly 4 Encounters:** Name and details. At least two encounters must be directly tied to the primary faction.
|
||||
- **Strictly 3 NPCs:** Proper name and a trait. One NPC should be a member of the primary faction, one should be a potential ally, and one should be a rival.
|
||||
- **Strictly 3 Treasures:** Name and a description that includes a danger or side-effect. Each treasure should be thematically tied to a specific encounter or room.
|
||||
Output as four separate numbered lists. Label the lists as "Locations:", "Encounters:", "NPCs:", and "Treasures:". Do not use any bolding, preambles, or extra text. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
Generate the rest of the dungeon's content to fill the space between the entrance and the climax. CRITICAL: All content must fit on a single one-page dungeon layout with three columns. Keep descriptions rich and evocative with tactical/environmental details.
|
||||
|
||||
- **Strictly 3 Locations (EXACTLY 3, no more, no less):** Each with a name (max 6 words) and a description (25-35 words). Each room MUST include:
|
||||
- Rich environmental features that affect gameplay (lighting, sounds, smells, textures, temperature, visibility)
|
||||
- Interactable elements that players can use (levers, objects, portals, mechanisms, environmental hazards)
|
||||
- Multiple approaches or solutions to challenges in the room
|
||||
- Tactical considerations (cover, elevation, movement restrictions, line of sight)
|
||||
- Hidden aspects discoverable through interaction or investigation
|
||||
Format as "Name: description" using colons, NOT em-dashes.
|
||||
|
||||
EXAMPLE LOCATION:
|
||||
"Whispering Gallery: Dim phosphorescent fungi line the walls, casting an eerie green glow. The air hums with a low-frequency vibration that makes conversation difficult. Two collapsed pillars create natural cover, while a narrow ledge 10 feet up offers a sniper position. A hidden pressure plate near the entrance triggers a portcullis trap."
|
||||
|
||||
- **Strictly 6 Encounters:** Numbered 1-6 (for d6 rolling). Name (max 6 words) and details (2 sentences MAX, approximately 25-40 words). Each encounter MUST:
|
||||
- Start with the room/location name followed by a colon, then the details (e.g., "Location Name: Details text")
|
||||
- The location name must match one of the actual room names from this dungeon
|
||||
- Include environmental hazards/opportunities (cover, elevation, traps, interactable objects, terrain features)
|
||||
- Include tactical considerations (positioning, line of sight, escape routes, bottlenecks, high ground)
|
||||
- Offer multiple resolution options (combat, negotiation, stealth, puzzle-solving, environmental manipulation, timing-based solutions)
|
||||
- Include consequences and outcomes tied to player choices
|
||||
- Integrate with the environmental dynamic element from core concepts
|
||||
- At least two encounters must be directly tied to the primary faction
|
||||
Format as "Name: Location Name: details" using colons, NOT em-dashes. CRITICAL: Always start encounter details with the location name and a colon.
|
||||
|
||||
EXAMPLE ENCOUNTER:
|
||||
"Guardian Golem: Chamber of Echoes: The golem activates when the lever is pulled, blocking the exit. It's vulnerable to water damage from the dripping stalactites. Players can use the pillars for cover or try to disable it by breaking the rune on its back. If defeated peacefully, it reveals a hidden passage."
|
||||
|
||||
- **Strictly 4-5 NPCs:** Proper name (max 4 words) and a description (50-65 words). Each NPC MUST include:
|
||||
- Clear motivation or goal
|
||||
- Relationship to primary faction
|
||||
- How they can help or hinder the party
|
||||
- Quirks or memorable traits
|
||||
- Multiple interaction possibilities (negotiation, intimidation, help, betrayal)
|
||||
- One NPC should be a key figure tied to the central conflict
|
||||
- One should be a member of the primary faction, one should be a potential ally, one should be a rival
|
||||
Format as "Name: description" using colons, NOT em-dashes.
|
||||
|
||||
EXAMPLE NPC:
|
||||
"Kaelen the Warden: A former guard who was left behind when the faction retreated. He knows the secret passages but demands the party help him escape. He's paranoid and checks over his shoulder constantly. Can be bribed with food or convinced through shared stories of betrayal. Will turn on the party if he thinks they're working with the faction."
|
||||
|
||||
- **Strictly 4-5 Treasures:** Name (max 5 words) and a description (30-40 words). Each treasure MUST:
|
||||
- Include a clear danger or side-effect
|
||||
- Be connected to a specific encounter, NPC, or room
|
||||
- Have story significance beyond just value
|
||||
- Have potential for creative use beyond obvious purpose
|
||||
- Some should be cursed, have activation requirements, or serve dual purposes
|
||||
Format as "Name — Description" using em-dash.
|
||||
|
||||
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."
|
||||
|
||||
- **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)
|
||||
- Tie to the core concepts and dynamic element
|
||||
- Add replayability and surprise
|
||||
- Be 15-20 words maximum
|
||||
- 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.
|
||||
|
||||
EXACT FORMAT REQUIRED (DO NOT use placeholder names like "Location Name", "NPC Name", or "Treasure Name" - use actual creative names):
|
||||
Locations:
|
||||
1. Actual Room Name: Description text.
|
||||
2. Actual Room Name: Description text.
|
||||
3. Actual Room Name: Description text.
|
||||
|
||||
Encounters:
|
||||
1. Actual Encounter Name: Actual Room Name: Details text.
|
||||
2. Actual Encounter Name: Actual Room Name: Details text.
|
||||
3. Actual Encounter Name: Actual Room Name: Details text.
|
||||
4. Actual Encounter Name: Actual Room Name: Details text.
|
||||
5. Actual Encounter Name: Actual Room Name: Details text.
|
||||
6. Actual Encounter Name: Actual Room Name: Details text.
|
||||
|
||||
NPCs:
|
||||
1. Actual Character Name: Description text.
|
||||
2. Actual Character Name: Description text.
|
||||
3. Actual Character Name: Description text.
|
||||
4. Actual Character Name: Description text.
|
||||
|
||||
Treasures:
|
||||
1. Actual Item Name — Description text.
|
||||
2. Actual Item Name — Description text.
|
||||
3. Actual Item Name — Description text.
|
||||
4. Actual Item Name — Description text.
|
||||
|
||||
Random Events:
|
||||
1. Event description.
|
||||
2. Event description.
|
||||
3. Event description.
|
||||
4. Event description.
|
||||
5. Event description.
|
||||
6. Event description.
|
||||
|
||||
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: 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"
|
||||
);
|
||||
const [intermediateRoomsSection, encountersSection, npcsSection, treasureSection] = mainContentRaw.split(/Encounters:|NPCs:|Treasures?:/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 rooms = [entranceRoom, ...intermediateRooms, climaxRoom];
|
||||
const encounters = parseObjects(encountersSection || "", "encounters");
|
||||
const npcs = parseObjects(npcsSection || "", "npcs");
|
||||
const treasure = parseList(treasureSection || "");
|
||||
// Limit to exactly 3 intermediate rooms to ensure total of 5 rooms (entrance + 3 intermediate + climax)
|
||||
const limitedIntermediateRooms = intermediateRooms.slice(0, 3);
|
||||
if (intermediateRooms.length > 3) {
|
||||
console.warn(`Expected exactly 3 intermediate locations but got ${intermediateRooms.length}, limiting to first 3`);
|
||||
}
|
||||
const rooms = [entranceRoom, ...limitedIntermediateRooms, climaxRoom];
|
||||
let encounters = splitCombinedEncounters(parseObjects(encountersSection || "", "encounters"));
|
||||
let npcs = parseObjects(npcsSection || "", "npcs");
|
||||
const treasure = parseObjects(treasureSection || "", "treasure");
|
||||
|
||||
// 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']]
|
||||
.filter(([arr, expected]) => arr.length < expected && arr.length > 0)
|
||||
.forEach(([arr, expected, name]) => console.warn(`Expected at least ${expected} ${name} but got ${arr.length}`));
|
||||
console.log("Rooms:", rooms);
|
||||
console.log("Encounters:", encounters);
|
||||
console.log("NPCs:", npcs);
|
||||
console.log("Treasure:", treasure);
|
||||
console.log("Random Events:", randomEvents);
|
||||
|
||||
// Step 6: Player Choices and Consequences
|
||||
const npcNamesList = npcs.map(n => n.name).join(", ");
|
||||
const factionName = coreConcepts.match(/Primary Faction[:\s]+([^.]+)/i)?.[1]?.trim() || "the primary faction";
|
||||
|
||||
const plotResolutionsRaw = await callOllama(
|
||||
`Based on all of the following elements, suggest 3 possible, non-conflicting story climaxes or plot resolutions for adventurers exploring this location. Each resolution must provide a meaningful choice with a tangible consequence, directly related to the Central Conflict, the Primary Faction, or the NPCs.
|
||||
`Based on all of the following elements, suggest 4-5 possible, non-conflicting story climaxes or plot resolutions for adventurers exploring this location. Each resolution must provide a meaningful choice with a tangible consequence, directly related to the Central Conflict, the Primary Faction, or the NPCs.
|
||||
|
||||
Dungeon Elements:
|
||||
${JSON.stringify({ title, flavor, hooksRumors, rooms, encounters, treasure, npcs, coreConcepts }, null, 2)}
|
||||
|
||||
Start each item with phrases like "The adventurers could" or "The PCs might". Deepen the narrative texture and allow for roleplay and tactical creativity. Keep each item short (max 2 sentences). Output as a numbered list, plain text only. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
CRITICAL: This content must fit in a single column on a one-page dungeon layout. Keep descriptions meaningful but concise.
|
||||
Start each item with phrases like "The adventurers could" or "The adventurers might". Do not use "PCs" or "player characters" - always use "adventurers" instead.
|
||||
|
||||
EXAMPLE PLOT RESOLUTION:
|
||||
"The adventurers could ally with the primary faction, gaining access to their resources but becoming enemies of the rival group. This choice unlocks new areas but closes off diplomatic solutions with other NPCs."
|
||||
|
||||
IMPORTANT: When referencing NPCs, use these exact names with correct spelling: ${npcNamesList}. When referencing the faction, use: ${factionName}. Ensure all names are spelled consistently and correctly.
|
||||
CRITICAL: Double-check all spelling before outputting. Verify all proper nouns match exactly as provided above.
|
||||
|
||||
Each resolution MUST:
|
||||
- Offer meaningful choice with clear consequences
|
||||
- Integrate NPCs, faction dynamics, and player actions
|
||||
- Include failure states or unexpected outcomes as options
|
||||
- Reflect different approaches players might take
|
||||
Keep each item to 1-2 sentences MAX (approximately 15-25 words). Be extremely concise. Output as a numbered list, plain text only. Do not use em-dashes (—) anywhere in the output. Absolutely do not use the word "Obsidian" or "obsidian" anywhere in the output.`,
|
||||
undefined, 5, "Step 6: Plot Resolutions"
|
||||
);
|
||||
const plotResolutions = parseList(plotResolutionsRaw);
|
||||
console.log("Plot Resolutions:", plotResolutions);
|
||||
|
||||
// Step 7: Validation and Content Fixing
|
||||
console.log("\n[Validation] Running content validation and fixes...");
|
||||
const dungeonData = {
|
||||
title,
|
||||
flavor,
|
||||
map: "map.png",
|
||||
hooksRumors,
|
||||
rooms,
|
||||
encounters,
|
||||
treasure,
|
||||
npcs,
|
||||
plotResolutions,
|
||||
randomEvents,
|
||||
coreConcepts
|
||||
};
|
||||
|
||||
const validatedData = validateAndFixContent(dungeonData);
|
||||
|
||||
console.log("\nDungeon generation complete!");
|
||||
return { title, flavor, map: "map.png", hooksRumors, rooms, encounters, treasure, npcs, plotResolutions };
|
||||
return validatedData;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,18 @@ function pickRandom(arr) {
|
||||
return arr[Math.floor(Math.random() * arr.length)];
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
const map = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
};
|
||||
return String(text).replace(/[&<>"']/g, m => map[m]);
|
||||
}
|
||||
|
||||
export function dungeonTemplate(data) {
|
||||
const bodyFonts = [
|
||||
"'Lora', serif",
|
||||
@@ -19,13 +31,6 @@ export function dungeonTemplate(data) {
|
||||
"'Playfair Display', serif"
|
||||
];
|
||||
|
||||
const tableFonts = [
|
||||
"'Alegreya Sans', sans-serif",
|
||||
"'Cabin', sans-serif",
|
||||
"'IBM Plex Sans', sans-serif",
|
||||
"'Cormorant Garamond', serif"
|
||||
];
|
||||
|
||||
const quoteFonts = [
|
||||
"'Playfair Display', serif",
|
||||
"'Libre Baskerville', serif",
|
||||
@@ -34,9 +39,11 @@ export function dungeonTemplate(data) {
|
||||
|
||||
const bodyFont = pickRandom(bodyFonts);
|
||||
const headingFont = pickRandom(headingFonts);
|
||||
const tableFont = pickRandom(tableFonts);
|
||||
const quoteFont = pickRandom(quoteFonts);
|
||||
|
||||
// Check if we have a map image to include
|
||||
const hasMap = data.map && typeof data.map === 'string' && data.map.startsWith('data:image/');
|
||||
|
||||
return `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -57,174 +64,372 @@ export function dungeonTemplate(data) {
|
||||
font-family: ${bodyFont};
|
||||
color: #1a1a1a;
|
||||
font-size: 0.7em;
|
||||
line-height: 1.25em;
|
||||
line-height: 1.35em;
|
||||
}
|
||||
.content-page {
|
||||
height: 100vh;
|
||||
box-sizing: border-box;
|
||||
padding: 1.5cm;
|
||||
padding: 1.2cm;
|
||||
page-break-after: always;
|
||||
overflow: hidden;
|
||||
overflow: visible;
|
||||
break-inside: avoid;
|
||||
}
|
||||
h1 {
|
||||
font-family: ${headingFont};
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-size: 2em;
|
||||
margin: 0.2em 0 0.3em;
|
||||
font-size: 1.8em;
|
||||
margin: 0.15em 0 0.2em;
|
||||
color: #1a1a1a;
|
||||
border-bottom: 2px solid #1a1a1a;
|
||||
padding-bottom: 0.2em;
|
||||
padding-bottom: 0.15em;
|
||||
letter-spacing: 0.1em;
|
||||
}
|
||||
.flavor {
|
||||
text-align: center;
|
||||
font-style: italic;
|
||||
font-family: ${quoteFont};
|
||||
margin: 0.4em 0 0.8em;
|
||||
font-size: 0.9em;
|
||||
margin: 0.3em 0 0.6em;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.35em;
|
||||
}
|
||||
.columns {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 1fr 1fr;
|
||||
gap: 0.5cm;
|
||||
gap: 0.4cm;
|
||||
align-items: start;
|
||||
}
|
||||
.col {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.15em;
|
||||
gap: 0.25em;
|
||||
overflow-wrap: break-word;
|
||||
word-break: normal;
|
||||
hyphens: auto;
|
||||
}
|
||||
.section-block {
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
margin-bottom: 0.3em;
|
||||
}
|
||||
h2 {
|
||||
font-family: ${headingFont};
|
||||
font-size: 1.0em;
|
||||
margin: 0.3em 0 0.1em;
|
||||
font-size: 1.05em;
|
||||
margin: 0.2em 0 0.2em;
|
||||
color: #1a1a1a;
|
||||
border-bottom: 1px solid #1a1a1a;
|
||||
padding-bottom: 0.1em;
|
||||
padding-bottom: 0.08em;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
.room {
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
.room h3 {
|
||||
margin: 0.2em 0 0.05em;
|
||||
margin: 0.1em 0 0.05em;
|
||||
font-size: 0.95em;
|
||||
font-weight: bold;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
.room p {
|
||||
text-align: justify;
|
||||
word-wrap: break-word;
|
||||
margin: 0.1em 0 0.3em;
|
||||
margin: 0 0 0.25em;
|
||||
font-size: 0.85em;
|
||||
font-weight: normal;
|
||||
line-height: 1.3em;
|
||||
}
|
||||
ul {
|
||||
padding-left: 1em;
|
||||
margin: 0.1em 0 0.3em;
|
||||
.encounter, .npc, .treasure, .plot-resolution {
|
||||
margin: 0 0 0.35em;
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.3em;
|
||||
}
|
||||
li {
|
||||
margin-bottom: 0.2em;
|
||||
.random-events {
|
||||
margin: 0.2em 0;
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
font-size: 0.85em;
|
||||
}
|
||||
.random-events table {
|
||||
margin-top: 0.15em;
|
||||
}
|
||||
.encounter strong, .npc strong, .treasure strong {
|
||||
font-weight: bold;
|
||||
}
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-family: ${tableFont};
|
||||
font-size: 0.8em;
|
||||
}
|
||||
th,
|
||||
td {
|
||||
margin: 0.2em 0;
|
||||
font-size: 0.85em;
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
border: 1px solid #1a1a1a;
|
||||
padding: 0.2em;
|
||||
}
|
||||
table th {
|
||||
font-family: ${headingFont};
|
||||
text-align: left;
|
||||
border-bottom: 1px solid #1a1a1a;
|
||||
padding: 0.15em 0.3em;
|
||||
font-size: 0.85em;
|
||||
text-transform: uppercase;
|
||||
font-weight: bold;
|
||||
}
|
||||
table td {
|
||||
padding: 0.25em 0.4em;
|
||||
vertical-align: top;
|
||||
line-height: 1.3em;
|
||||
border-bottom: 1px solid #1a1a1a;
|
||||
word-wrap: break-word;
|
||||
overflow-wrap: break-word;
|
||||
word-break: normal;
|
||||
}
|
||||
th {
|
||||
background: #e0e0e0;
|
||||
table tr:last-child td {
|
||||
border-bottom: 1px solid #1a1a1a;
|
||||
}
|
||||
table tr:hover {
|
||||
background: rgba(0, 0, 0, 0.05);
|
||||
table td:first-child {
|
||||
font-weight: bold;
|
||||
width: 2em;
|
||||
text-align: center;
|
||||
border-right: 1px solid #1a1a1a;
|
||||
}
|
||||
.encounters-table td:nth-child(2) {
|
||||
font-weight: bold;
|
||||
min-width: 20%;
|
||||
max-width: 30%;
|
||||
padding-right: 0.5em;
|
||||
border-right: 1px solid #1a1a1a;
|
||||
}
|
||||
.encounters-table td:nth-child(3) {
|
||||
width: auto;
|
||||
font-size: 0.8em;
|
||||
line-height: 1.3em;
|
||||
}
|
||||
.map-page {
|
||||
height: 210mm;
|
||||
width: 297mm;
|
||||
box-sizing: border-box;
|
||||
padding: 1.5cm;
|
||||
position: relative;
|
||||
display: block;
|
||||
}
|
||||
.map-image-container {
|
||||
position: absolute;
|
||||
top: 1.5cm;
|
||||
left: 1.5cm;
|
||||
right: 1.5cm;
|
||||
bottom: 3cm;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
page-break-before: always;
|
||||
}
|
||||
.map-page img {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
object-fit: contain;
|
||||
}
|
||||
.map-page footer {
|
||||
position: absolute;
|
||||
bottom: 1.5cm;
|
||||
left: 1.5cm;
|
||||
right: 1.5cm;
|
||||
.map-container {
|
||||
text-align: center;
|
||||
font-size: 0.65em;
|
||||
color: #555;
|
||||
font-style: italic;
|
||||
margin: 1em 0;
|
||||
}
|
||||
.map-container img {
|
||||
max-width: 100%;
|
||||
max-height: calc(100vh - 3cm);
|
||||
border: 1px solid #1a1a1a;
|
||||
}
|
||||
ul {
|
||||
margin: 0.2em 0;
|
||||
padding-left: 1.2em;
|
||||
}
|
||||
li {
|
||||
margin: 0.08em 0;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.3em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content-page">
|
||||
<h1>${data.title}</h1>
|
||||
<p class="flavor">${data.flavor}</p>
|
||||
<h1>${escapeHtml(data.title)}</h1>
|
||||
${data.flavor ? `<p class="flavor">${escapeHtml(data.flavor)}</p>` : ''}
|
||||
|
||||
<div class="columns">
|
||||
<div class="col">
|
||||
<h2>Adventure Hooks & Rumors</h2>
|
||||
<ul>${data.hooksRumors.map(item => `<li>${item}</li>`).join("")}</ul>
|
||||
<h2>Locations</h2>
|
||||
${data.rooms.map((room, i) => `<div class="room">
|
||||
<h3>${i + 1}. ${room.name}</h3>
|
||||
<p>${room.description}</p>
|
||||
</div>`).join("")}
|
||||
${data.hooksRumors && data.hooksRumors.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>Hooks & Rumors</h2>
|
||||
<ul>
|
||||
${data.hooksRumors.map(hook => `<li>${escapeHtml(hook)}</li>`).join('')}
|
||||
</ul>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
${data.randomEvents && data.randomEvents.length > 0 ? `
|
||||
<div class="section-block random-events">
|
||||
<h2>Random Events (d6)</h2>
|
||||
<table>
|
||||
<tbody>
|
||||
${data.randomEvents.map((event, index) => `
|
||||
<tr>
|
||||
<td>${index + 1}</td>
|
||||
<td>${escapeHtml(event)}</td>
|
||||
</tr>
|
||||
`).join('')}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
` : ''}
|
||||
|
||||
${data.rooms && data.rooms.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>Locations</h2>
|
||||
${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">
|
||||
<h3>${escapeHtml(room.name)}</h3>
|
||||
<p>${escapeHtml(desc)}</p>
|
||||
</div>
|
||||
`;
|
||||
}).join('')}
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<h2>Encounters</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Details</th>
|
||||
</tr>
|
||||
${data.encounters.map(e => `<tr>
|
||||
<td>${e.name}</td>
|
||||
<td>${e.details}</td>
|
||||
</tr>`).join("")}
|
||||
</table>
|
||||
<h2>Treasure</h2>
|
||||
<ul>${data.treasure.map(t => {
|
||||
const [name, ...descParts] = t.split(/[-–—:]/);
|
||||
const description = descParts.join(" ").trim();
|
||||
return `<li><b>${name.trim()}</b>: ${description}</li>`;
|
||||
}).join("")}</ul>
|
||||
${data.encounters && data.encounters.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>Encounters (d6)</h2>
|
||||
<table class="encounters-table">
|
||||
<tbody>
|
||||
${data.encounters.map((encounter, index) => {
|
||||
// Truncate details to 4 sentences max to prevent overflow
|
||||
let details = encounter.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
|
||||
const sentences = details.match(/[^.!?]+[.!?]+/g) || [details];
|
||||
if (sentences.length > 4) {
|
||||
details = sentences.slice(0, 4).join(' ').trim();
|
||||
}
|
||||
// Also limit by character count as fallback (max ~350 chars)
|
||||
if (details.length > 350) {
|
||||
details = details.substring(0, 347).trim();
|
||||
// Try to end at a sentence boundary
|
||||
const lastPeriod = details.lastIndexOf('.');
|
||||
if (lastPeriod > 280) {
|
||||
details = details.substring(0, lastPeriod + 1);
|
||||
} else {
|
||||
details += '...';
|
||||
}
|
||||
}
|
||||
return `
|
||||
<tr>
|
||||
<td>${index + 1}</td>
|
||||
<td><strong>${escapeHtml(encounter.name)}</strong></td>
|
||||
<td>${escapeHtml(details)}</td>
|
||||
</tr>
|
||||
`;
|
||||
}).join('')}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<h2>NPCs</h2>
|
||||
<ul>${data.npcs.map(n => `<li><b>${n.name}</b>: ${n.trait}</li>`).join("")}</ul>
|
||||
<h2>Plot Resolutions</h2>
|
||||
<ul>${data.plotResolutions.map(p => `<li>${p}</li>`).join("")}</ul>
|
||||
${data.treasure && data.treasure.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>Treasure</h2>
|
||||
${data.treasure.map(item => {
|
||||
if (typeof item === 'object' && item.name && item.description) {
|
||||
return `<div class="treasure"><strong>${escapeHtml(item.name)}</strong> — ${escapeHtml(item.description)}</div>`;
|
||||
} else if (typeof item === 'string') {
|
||||
// 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>
|
||||
` : ''}
|
||||
|
||||
${data.npcs && data.npcs.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>NPCs</h2>
|
||||
${data.npcs.map(npc => {
|
||||
if (typeof npc === 'object' && npc.name && npc.trait) {
|
||||
return `<div class="npc"><strong>${escapeHtml(npc.name)}</strong>: ${escapeHtml(npc.trait)}</div>`;
|
||||
} else if (typeof npc === 'string') {
|
||||
// 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>
|
||||
` : ''}
|
||||
|
||||
${data.plotResolutions && data.plotResolutions.length > 0 ? `
|
||||
<div class="section-block">
|
||||
<h2>Plot Resolutions</h2>
|
||||
${data.plotResolutions.map(resolution => {
|
||||
// Truncate to 2 sentences max to prevent overflow
|
||||
let text = resolution || '';
|
||||
// Split into sentences and keep only first 2
|
||||
const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
|
||||
if (sentences.length > 2) {
|
||||
text = sentences.slice(0, 2).join(' ').trim();
|
||||
}
|
||||
// Also limit by character count as fallback (max ~150 chars)
|
||||
if (text.length > 150) {
|
||||
text = text.substring(0, 147).trim();
|
||||
// Try to end at a sentence boundary
|
||||
const lastPeriod = text.lastIndexOf('.');
|
||||
if (lastPeriod > 100) {
|
||||
text = text.substring(0, lastPeriod + 1);
|
||||
} else {
|
||||
text += '...';
|
||||
}
|
||||
}
|
||||
return `
|
||||
<div class="plot-resolution">
|
||||
${escapeHtml(text)}
|
||||
</div>
|
||||
`;
|
||||
}).join('')}
|
||||
</div>
|
||||
` : ''}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="map-page">
|
||||
<div class="map-image-container">
|
||||
<img src="${data.map}" alt="Dungeon Map">
|
||||
|
||||
${hasMap ? `
|
||||
<div class="content-page map-page">
|
||||
<div class="map-container">
|
||||
<img src="${data.map}" alt="Dungeon Map" />
|
||||
</div>
|
||||
</div>
|
||||
<footer>Scrollsmith • © ${new Date().getFullYear()}</footer>
|
||||
</div>
|
||||
` : ''}
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -1,21 +1,34 @@
|
||||
import puppeteer from "puppeteer";
|
||||
import { dungeonTemplate } from "./dungeonTemplate.js";
|
||||
|
||||
import fs from 'fs/promises';
|
||||
import fs from "fs/promises";
|
||||
|
||||
export async function generatePDF(data, outputPath = "dungeon.pdf") {
|
||||
const browser = await puppeteer.launch({
|
||||
args: ['--no-sandbox', '--disable-setuid-sandbox']
|
||||
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
||||
});
|
||||
|
||||
const page = await browser.newPage();
|
||||
|
||||
// Convert image to base64
|
||||
const imageBuffer = await fs.readFile(data.map);
|
||||
const base64Image = `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
||||
data.map = base64Image;
|
||||
const toBase64DataUrl = (buffer) =>
|
||||
`data:image/png;base64,${buffer.toString("base64")}`;
|
||||
|
||||
const html = dungeonTemplate(data);
|
||||
const readImageData = async (path) =>
|
||||
fs
|
||||
.readFile(path)
|
||||
.then(toBase64DataUrl)
|
||||
.catch(() => {
|
||||
console.warn(
|
||||
"Warning: Could not read image file, proceeding without map in PDF",
|
||||
);
|
||||
return null;
|
||||
});
|
||||
|
||||
const imageData = data.map ? await readImageData(data.map) : null;
|
||||
const dataWithImage = imageData
|
||||
? { ...data, map: imageData }
|
||||
: (({ map, ...rest }) => rest)(data); // eslint-disable-line no-unused-vars
|
||||
|
||||
const html = dungeonTemplate(dataWithImage);
|
||||
await page.setContent(html, { waitUntil: "networkidle0" });
|
||||
|
||||
await page.pdf({
|
||||
@@ -23,7 +36,7 @@ export async function generatePDF(data, outputPath = "dungeon.pdf") {
|
||||
format: "A4",
|
||||
landscape: true,
|
||||
printBackground: true,
|
||||
preferCSSPageSize: true
|
||||
preferCSSPageSize: true,
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
@@ -2,8 +2,9 @@ import sharp from 'sharp';
|
||||
import path from "path";
|
||||
import { mkdir, writeFile } from "fs/promises";
|
||||
import { fileURLToPath } from "url";
|
||||
import { callOllama } from "./ollamaClient.js";
|
||||
import { callOllama, OLLAMA_MODEL } from "./ollamaClient.js";
|
||||
|
||||
const COMFYUI_ENABLED = process.env.COMFYUI_ENABLED !== 'false';
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const COMFYUI_URL = process.env.COMFYUI_URL || "http://localhost:8188";
|
||||
|
||||
@@ -62,7 +63,9 @@ Input:
|
||||
${flavor}
|
||||
|
||||
Output:`,
|
||||
"gemma3n:e4b", 3, "Generate Visual Prompt"
|
||||
OLLAMA_MODEL,
|
||||
3,
|
||||
"Generate Visual Prompt"
|
||||
);
|
||||
|
||||
const accentColor = selectRandomAccentColor();
|
||||
@@ -164,6 +167,9 @@ async function waitForImage(promptId, timeout = 900000) {
|
||||
|
||||
while (Date.now() - start < timeout) {
|
||||
const res = await fetch(`${COMFYUI_URL}/history`);
|
||||
if (!res.ok) {
|
||||
throw new Error(`ComfyUI history request failed: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
const data = await res.json();
|
||||
const historyEntry = data[promptId];
|
||||
|
||||
@@ -203,7 +209,7 @@ async function generateImageViaComfyUI(prompt, filename) {
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`ComfyUI error: ${res.statusText}`);
|
||||
throw new Error(`ComfyUI error: ${res.status} ${res.statusText}`);
|
||||
}
|
||||
|
||||
const { prompt_id } = await res.json();
|
||||
@@ -228,6 +234,11 @@ async function generateImageViaComfyUI(prompt, filename) {
|
||||
export async function generateDungeonImages({ flavor }) {
|
||||
console.log("Generating dungeon image...");
|
||||
|
||||
if (!COMFYUI_ENABLED) {
|
||||
console.log("ComfyUI image generation disabled via .env; using existing upscaled image.");
|
||||
return path.join(__dirname, "dungeon_upscaled.png");
|
||||
}
|
||||
|
||||
const finalPrompt = await generateVisualPrompt(flavor);
|
||||
console.log("Engineered visual prompt:\n", finalPrompt);
|
||||
|
||||
|
||||
17
index.js
17
index.js
@@ -1,18 +1,28 @@
|
||||
import 'dotenv/config';
|
||||
import "dotenv/config";
|
||||
import { generateDungeon } from "./dungeonGenerator.js";
|
||||
import { generateDungeonImages } from "./imageGenerator.js";
|
||||
import { generatePDF } from "./generatePDF.js";
|
||||
import { OLLAMA_MODEL, initializeModel } from "./ollamaClient.js";
|
||||
|
||||
// Utility to create a filesystem-safe filename from the dungeon title
|
||||
function slugify(text) {
|
||||
return text
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-') // replace non-alphanumeric with hyphens
|
||||
.replace(/^-+|-+$/g, ''); // trim leading/trailing hyphens
|
||||
.replace(/[^a-z0-9]+/g, "-") // replace non-alphanumeric with hyphens
|
||||
.replace(/^-+|-+$/g, ""); // trim leading/trailing hyphens
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
if (!process.env.OLLAMA_API_URL) {
|
||||
throw new Error("OLLAMA_API_URL environment variable is required");
|
||||
}
|
||||
console.log("Using Ollama API URL:", process.env.OLLAMA_API_URL);
|
||||
|
||||
// Initialize model (will fetch default from API or use fallback)
|
||||
await initializeModel();
|
||||
console.log("Using Ollama model:", OLLAMA_MODEL);
|
||||
|
||||
// Generate the dungeon data
|
||||
const dungeonData = await generateDungeon();
|
||||
|
||||
@@ -30,5 +40,6 @@ function slugify(text) {
|
||||
console.log(`Dungeon PDF successfully generated: ${filename}`);
|
||||
} catch (err) {
|
||||
console.error("Error generating dungeon:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
})();
|
||||
|
||||
121
ollamaClient.js
121
ollamaClient.js
@@ -1,47 +1,76 @@
|
||||
const OLLAMA_API_URL = process.env.OLLAMA_API_URL;
|
||||
const OLLAMA_API_KEY = process.env.OLLAMA_API_KEY;
|
||||
export let OLLAMA_MODEL = process.env.OLLAMA_MODEL || "gemma3:latest";
|
||||
|
||||
async function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
export async function initializeModel() {
|
||||
if (process.env.OLLAMA_MODEL) return;
|
||||
try {
|
||||
const isOpenWebUI = OLLAMA_API_URL?.includes("/api/chat/completions");
|
||||
const baseUrl = OLLAMA_API_URL?.replace(/\/api\/.*$/, "");
|
||||
const url = isOpenWebUI ? `${baseUrl}/api/v1/models` : `${baseUrl}/api/tags`;
|
||||
const headers = isOpenWebUI && OLLAMA_API_KEY
|
||||
? { "Authorization": `Bearer ${OLLAMA_API_KEY}` }
|
||||
: {};
|
||||
const res = await fetch(url, { headers });
|
||||
if (res.ok) {
|
||||
const data = await res.json();
|
||||
const model = isOpenWebUI
|
||||
? data.data?.[0]?.id || data.data?.[0]?.name
|
||||
: data.models?.[0]?.name;
|
||||
if (model) {
|
||||
OLLAMA_MODEL = model;
|
||||
console.log(`Using default model: ${model}`);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
console.warn(`Could not fetch default model, using: ${OLLAMA_MODEL}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Utility: strip markdown artifacts
|
||||
function cleanText(str) {
|
||||
return str
|
||||
.replace(/^#+\s*/gm, "") // remove headers
|
||||
.replace(/\*\*(.*?)\*\*/g, "$1") // remove bold
|
||||
.replace(/[*_`]/g, "") // remove stray formatting
|
||||
.replace(/\s+/g, " ") // normalize whitespace
|
||||
.replace(/^#+\s*/gm, "")
|
||||
.replace(/\*\*(.*?)\*\*/g, "$1")
|
||||
.replace(/[*_`]/g, "")
|
||||
.replace(/\s+/g, " ")
|
||||
.trim();
|
||||
}
|
||||
|
||||
export async function callOllama(prompt, model = "gemma3n:e4b", retries = 5, stepName = "unknown") {
|
||||
const isUsingOpenWebUI = !!OLLAMA_API_KEY;
|
||||
function inferApiType(url) {
|
||||
if (!url) return "ollama-generate";
|
||||
if (url.includes("/api/chat/completions")) return "open-webui";
|
||||
if (url.includes("/api/chat")) return "ollama-chat";
|
||||
return "ollama-generate";
|
||||
}
|
||||
|
||||
async function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function callOllamaBase(prompt, model, retries, stepName, apiType) {
|
||||
const isUsingOpenWebUI = apiType === "open-webui";
|
||||
const isUsingOllamaChat = apiType === "ollama-chat";
|
||||
|
||||
for (let attempt = 1; attempt <= retries; attempt++) {
|
||||
try {
|
||||
const promptCharCount = prompt.length;
|
||||
const promptWordCount = prompt.split(/\s+/).length;
|
||||
|
||||
console.log(`\n[${stepName}] Sending prompt (attempt ${attempt}/${retries})`);
|
||||
console.log(`Prompt: ${promptCharCount} chars, ~${promptWordCount} words`);
|
||||
console.log(
|
||||
`\n[${stepName}] Sending prompt (attempt ${attempt}/${retries})`,
|
||||
);
|
||||
console.log(
|
||||
`Prompt: ${promptCharCount} chars, ~${promptWordCount} words`,
|
||||
);
|
||||
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
|
||||
if (isUsingOpenWebUI) {
|
||||
if (isUsingOpenWebUI && OLLAMA_API_KEY) {
|
||||
headers["Authorization"] = `Bearer ${OLLAMA_API_KEY}`;
|
||||
}
|
||||
|
||||
const body = isUsingOpenWebUI
|
||||
? {
|
||||
model,
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
}
|
||||
: {
|
||||
model,
|
||||
messages: [{ role: "user", content: prompt }],
|
||||
stream: false,
|
||||
};
|
||||
const body = isUsingOpenWebUI || isUsingOllamaChat
|
||||
? { model, messages: [{ role: "user", content: prompt }] }
|
||||
: { model, prompt, stream: false };
|
||||
|
||||
const response = await fetch(OLLAMA_API_URL, {
|
||||
method: "POST",
|
||||
@@ -49,24 +78,34 @@ export async function callOllama(prompt, model = "gemma3n:e4b", retries = 5, ste
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error(`Ollama request failed: ${response.status} ${response.statusText}`);
|
||||
if (!response.ok) {
|
||||
let errorDetails = "";
|
||||
try {
|
||||
const errorData = await response.text();
|
||||
errorDetails = errorData ? `: ${errorData}` : "";
|
||||
} catch {
|
||||
// Ignore errors reading error response
|
||||
}
|
||||
throw new Error(
|
||||
`Ollama request failed: ${response.status} ${response.statusText}${errorDetails}`,
|
||||
);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
const rawText = isUsingOpenWebUI
|
||||
? data.choices?.[0]?.message?.content
|
||||
: data.message?.content;
|
||||
: isUsingOllamaChat
|
||||
? data.message?.content
|
||||
: data.response;
|
||||
|
||||
if (!rawText) throw new Error("No response from Ollama");
|
||||
|
||||
const cleaned = cleanText(rawText);
|
||||
|
||||
console.log(`[${stepName}] Received: ${rawText.length} chars, ~${rawText.split(/\s+/).length} words`);
|
||||
// console.log(`Raw output:\n${rawText}\n`);
|
||||
// console.log(`Cleaned output:\n${cleaned}\n`);
|
||||
console.log(
|
||||
`[${stepName}] Received: ${rawText.length} chars, ~${rawText.split(/\s+/).length} words`,
|
||||
);
|
||||
|
||||
return cleaned;
|
||||
|
||||
} catch (err) {
|
||||
console.warn(`[${stepName}] Attempt ${attempt} failed: ${err.message}`);
|
||||
if (attempt === retries) throw err;
|
||||
@@ -76,3 +115,23 @@ export async function callOllama(prompt, model = "gemma3n:e4b", retries = 5, ste
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function callOllama(
|
||||
prompt,
|
||||
model = OLLAMA_MODEL,
|
||||
retries = 5,
|
||||
stepName = "unknown",
|
||||
) {
|
||||
const apiType = inferApiType(OLLAMA_API_URL);
|
||||
return callOllamaBase(prompt, model, retries, stepName, apiType);
|
||||
}
|
||||
|
||||
export async function callOllamaExplicit(
|
||||
prompt,
|
||||
model = OLLAMA_MODEL,
|
||||
retries = 5,
|
||||
stepName = "unknown",
|
||||
apiType = "ollama-generate",
|
||||
) {
|
||||
return callOllamaBase(prompt, model, retries, stepName, apiType);
|
||||
}
|
||||
|
||||
606
package-lock.json
generated
606
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -5,6 +5,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test:integration": "node --test test/integration.test.js",
|
||||
"lint": "eslint .",
|
||||
"start": "node index.js"
|
||||
},
|
||||
@@ -19,6 +20,6 @@
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.34.0",
|
||||
"eslint": "^9.34.0",
|
||||
"globals": "^16.3.0"
|
||||
"globals": "^17.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
91
test/integration.test.js
Normal file
91
test/integration.test.js
Normal 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
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user