Another API change

This commit is contained in:
David Masad
2021-01-28 20:54:56 -05:00
parent 4c353ecfe3
commit 37c5740060
4 changed files with 61 additions and 17 deletions

View File

@@ -121,19 +121,39 @@ StoryManager.getAllStorylets = function() {
return allStorylets;
}
StoryManager.getNStorylets = function(n) {
StoryManager.getStorylets = function(n=null, tag=null, respect_interrupt=true)
{
/*
Get N storylets, prioritizing the highest-priority ones first.
Get n storylets, prioritizing the highest-priority ones first.
n: if not null, return at most n storylets
tag: Only get storylets matching this tag
respect_interrupt: if true, any interrupt storylet overrides n and priority
For now assume that priorities are integers, but it would be nice
to be more flexible.
*/
let allStorylets = this.getAllStorylets();
n = Math.min(n, allStorylets.length);
if (n == 0) return [];
let allStorylets;
if (tag == null) allStorylets = this.getAllStorylets();
// TODO: get tagged storylets
// Check for interruptions
// TODO: Handle more than one interruption
if (respect_interrupt)
for (let storylet in allStorylets)
if (storylet.interrupt) return [storylet];
// Get n stories in priority order
if (n != null) {
n = Math.min(n, allStorylets.length);
if (n == 0) return [];
}
else n = allStorylets.length;
let selectedStorylets = [];
// First sort by ascending priority:
allStorylets.sort((a, b) => b.priority - a.priority);
let currentPriority = allStorylets[0].priority;
while (selectedStorylets.length < n) {
let priorityStorylets = allStorylets.filter(s => s.priority==currentPriority);
@@ -180,7 +200,7 @@ StoryManager.storylets["Conversation"] = {
&lt;&lt;/capture&gt;&gt;
&lt;&lt;/for&gt;&gt;
&lt;&lt;/widget&gt;&gt;</tw-passagedata><tw-passagedata pid="2" name="Start" tags="" position="225,100" size="100,100">You stand at the edge of the grand ballroom in the Duchess&#39;s palace.&lt;br&gt;
&lt;&lt;set _possibleStories = window.SM.getNStorylets(3)&gt;&gt;
&lt;&lt;set _possibleStories = window.SM.getStorylets(3)&gt;&gt;
&lt;&lt;ShowStoryletLinks _possibleStories&gt;&gt;</tw-passagedata><tw-passagedata pid="3" name="Conversation" tags="" position="350,100" size="100,100">&lt;&lt;set $talkingTo = $currentStorylet.character&gt;&gt;
You make polite conversation with $talkingTo.name. &lt;br&gt;
[[Keep circulating | Start]]</tw-passagedata></tw-storydata>