assets/scripts.js: Improve precondition handling

This commit is contained in:
Vladimir Panteleev 2019-07-28 08:16:31 +00:00
parent bc81c00be8
commit 59782b0707

View file

@ -39,14 +39,14 @@ function preconditionToJS(cond) {
} }
} }
function checkPrecondition(segmentId) { function checkPrecondition(preconditionId) {
let precondition = bv.preconditions[segmentId]; let precondition = bv.preconditions[preconditionId];
if (precondition) { if (precondition) {
let cond = preconditionToJS(precondition); let cond = preconditionToJS(precondition);
let match = eval(cond); let match = eval(cond);
console.log(cond, '==', match); console.log(preconditionId, ':', cond, '==', match);
return match; return match;
} }
@ -54,29 +54,29 @@ function checkPrecondition(segmentId) {
return true; return true;
} }
function findSegment(id) { function resolveSegmentGroup(sg) {
if (id.startsWith('nsg-')) { let results = [];
id = id.substr(4); for (let v of segmentGroups[sg]) {
} if (v.precondition) {
if (segmentMap.segments[id]) { if (!checkPrecondition(v.precondition))
// check precondition continue;
return id; }
} if (v.segmentGroup) {
results.push(resolveSegmentGroup(v.segmentGroup));
if (segmentGroups[id]) { } else if (v.segment) {
for (let v of segmentGroups[id]) { // TODO: does the included precondition override or
if (v.segmentGroup) { // complement the segment precondition?
return findSegment(v.segmentGroup); if (!checkPrecondition(v.segment))
} else if (v.segment) { continue;
// check precondition results.push(v.segment);
return v.segment; } else {
} else { if (!checkPrecondition(v))
if (checkPrecondition(v)) continue;
return v; results.push(v);
}
} }
} }
return id; console.log('segment group', sg, '=>', results);
return results[0];
} }
/// Returns the segment ID at the given timestamp. /// Returns the segment ID at the given timestamp.
@ -322,11 +322,14 @@ function ontimeupdate(evt) {
function playNextSegment() { function playNextSegment() {
if (nextChoice >= 0) { if (nextChoice >= 0) {
let x = currentChoiceMoment.choices[nextChoice]; let x = currentChoiceMoment.choices[nextChoice];
let choiceId = x.segmentId ? x.segmentId : (x.sg ? x.sg : x.id); if (x.segmentId)
var segmentId = findSegment(choiceId); nextSegment = x.segmentId;
console.log('choice', choiceId, 'nextSegment', segmentId); else if (x.sg)
nextSegment = resolveSegmentGroup(x.sg);
else
nextSegment = null;
console.log('choice', nextChoice, 'nextSegment', nextSegment);
applyImpression(x.impressionData); applyImpression(x.impressionData);
nextSegment = segmentId;
} }
let breadcrumb = 'breadcrumb_' + nextSegment; let breadcrumb = 'breadcrumb_' + nextSegment;
@ -336,7 +339,9 @@ function playNextSegment() {
segmentTransition = true; segmentTransition = true;
let segment = nextSegment; let segment = nextSegment;
nextSegment = null; nextSegment = null;
return playSegment(segment, true); if (segment)
return playSegment(segment, true);
return false;
} }
function jumpForward() { function jumpForward() {