Removing requirement that priority be an integer

This commit is contained in:
David Masad
2021-02-06 11:08:23 -05:00
parent a37c3477b8
commit 1f128089d9
7 changed files with 43 additions and 53 deletions

View File

@ -113,11 +113,9 @@ StoryManager.getAllStorylets = function(tag=null) {
for (let key in this.storylets) {
storylet = this.storylets[key];
if (tag === null || ("tags" in storylet && storylet.tags.includes(tag))) {
// TODO: If using yield, this part will change
//storylets = storylet.generate();
for (let boundStorylet of storylet.generate()) {
//boundStorylet = storylets[i];
if (!("priority" in boundStorylet)) boundStorylet.priority = 0;
if (!("priority" in boundStorylet)) boundStorylet.priority = 1;
allStorylets.push(boundStorylet);
}
}
@ -133,9 +131,6 @@ StoryManager.getStorylets = function(n=null, tag=null, respect_interrupt=true) {
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;
allStorylets = this.getAllStorylets(tag);
@ -155,16 +150,18 @@ StoryManager.getStorylets = function(n=null, tag=null, respect_interrupt=true) {
}
else n = allStorylets.length;
let selectedStorylets = [];
// First sort by ascending priority:
allStorylets.sort((a, b) => b.priority - a.priority);
let currentPriority = allStorylets[0].priority;
// Select by strict priority; randomize among matching priority.
let priorities = Array.from(new Set(allStorylets.map(x => x.priority)));
priorities.sort((a, b) => b - a);
let currentPriority = 0;
while (selectedStorylets.length < n) {
let priorityStorylets = allStorylets.filter(s => s.priority==currentPriority);
let priorityStorylets = allStorylets.filter(s => s.priority==priorities[currentPriority]);
priorityStorylets.sort((a, b) => (Math.random() - Math.random()));
let nAdd = Math.min(n - selectedStorylets.length, priorityStorylets.length);
for (let i=0; i<nAdd; i++) selectedStorylets.push(priorityStorylets.pop());
currentPriority--;
currentPriority++;
}
return selectedStorylets;
}