Adapt StoryManager for Snowman

This commit is contained in:
2025-09-13 21:19:15 -07:00
parent 74c27cba67
commit e5b8f7db4f
3 changed files with 78 additions and 46 deletions

View File

@ -3,6 +3,8 @@
// -----------------------------------------------------------------------
var StoryManager = {};
StoryManager.storylets = {};
StoryManager.nextStoryletCache = [];
StoryManager.currentStorylet = Object.create(null);
StoryManager.getAllStorylets = function(tag=null) {
let allStorylets = [];
@ -99,32 +101,54 @@ StoryManager.weightedRandom = function(allStorylets, n) {
return selectedStorylets;
}
StoryManager.setCurrentStorylet = function(id) {
if (Object.hasOwn(StoryManager.nextStoryletCache, id)) {
StoryManager.currentStorylet = StoryManager.nextStoryletCache[id]
StoryManager.nextStoryletCache = Object.create(null);
} else {
console.log("Error: didn't find storylet cache entry: " + id);
}
}
StoryManager.getStoryletLinks = function(n, tag, selection) {
var links = _.map(StoryManager.getStorylets(n, tag, selection), function(storylet) {
// Maybe store the current storylet in a map generating a unique ID, then
// add some code below looking it up via that ID?
//
// How to keep storylets from building up over time?
// Clear them when a passage is left
var id = _.uniqueId("storylet-key_")
StoryManager.nextStoryletCache[id] = storylet;
return "<div onclick=\"window.SM.setCurrentStorylet('" + id + "');\" >[[" + storylet.description + "|" + storylet.passage + "]]<br></div>"
})
return _.reduce(links, function(memo, link) { return memo + link }, "")
}
// Set up macros
// ---------------------------------------------------------------
Macro.add("getStoryletLinks", {
handler: function() {
let n, tag, selection;
[n=null, tag=null, selection="ordered"] = this.args;
State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
$(this.output).wiki(`\
<<for _storylet range _nextStorylets>> \
<<capture _storylet>> \
[[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
<</capture>> \
<</for>> \
`)
}
});
// Macro.add("getStoryletLinks", {
// handler: function() {
// let n, tag, selection;
// [n=null, tag=null, selection="ordered"] = this.args;
// State.temporary.nextStorylets = StoryManager.getStorylets(n, tag, selection);
// $(this.output).wiki(`\
// <<for _storylet range _nextStorylets>> \
// <<capture _storylet>> \
// [[_storylet.description|_storylet.passage][$currentStorylet=_storylet]]<br>
// <</capture>> \
// <</for>> \
// `)
// }
// });
Macro.add("linkToNextStorylet", {
handler: function() {
let text, tag, selection;
[text, tag=null, selection="weighted"] = this.args;
State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
$(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
}
});
// Macro.add("linkToNextStorylet", {
// handler: function() {
// let text, tag, selection;
// [text, tag=null, selection="weighted"] = this.args;
// State.temporary.nextStorylet = StoryManager.getStorylets(1, tag, selection)[0];
// $(this.output).wiki(`<<capture _nextStorylet>>[[${text}|_nextStorylet.passage][$currentStorylet=_nextStorylet]]<</capture>>`);
// }
// });
window.SM = StoryManager;