From 1f128089d923b29d93694a1e068e8fde02857702 Mon Sep 17 00:00:00 2001 From: David Masad Date: Sat, 6 Feb 2021 11:08:23 -0500 Subject: [PATCH] Removing requirement that priority be an integer --- docs/Tutorial.md | 6 +++--- examples/duchess_party.html | 23 ++++++++++------------- examples/duchess_party.js | 4 ++-- examples/simple_space_example.html | 19 ++++++++----------- examples/tutorial.html | 23 ++++++++++------------- examples/tutorial.tw | 4 ++-- storymanager.js | 17 ++++++++--------- 7 files changed, 43 insertions(+), 53 deletions(-) diff --git a/docs/Tutorial.md b/docs/Tutorial.md index f4b2a5e..1122685 100644 --- a/docs/Tutorial.md +++ b/docs/Tutorial.md @@ -49,7 +49,7 @@ StoryManager.storylets["Conversation"] = { passage: "Conversation", description: "Talk to " + character.name, character: character, - priority: 0 + priority: 1 } yield storylet; } @@ -119,7 +119,7 @@ StoryManager.storylets["Conversation"] = { let storylet = { passage: "Conversation", description: "Talk to " + character.name, - priority: 0, + priority: 1, character: character } @@ -254,7 +254,7 @@ StoryManager.storylets["Conversation topic"] = { yield { passage: "Conversation topic", description: "Talk about " + topic, - priority: 0, + priority: 1, topic: topic }; } diff --git a/examples/duchess_party.html b/examples/duchess_party.html index e49c745..e45a7b9 100644 --- a/examples/duchess_party.html +++ b/examples/duchess_party.html @@ -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 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 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 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