Initial support for random 'next', 'action', ...

This commit is contained in:
Bryan Ashby 2019-09-03 19:52:04 -06:00
parent 1e08810188
commit 16f4d62548
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
2 changed files with 37 additions and 4 deletions

View File

@ -7,6 +7,9 @@ const {
Errors, Errors,
ErrorReasons ErrorReasons
} = require('./enig_error.js'); } = require('./enig_error.js');
const {
getResolvedSpec
} = require('./menu_util.js');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
@ -53,7 +56,7 @@ module.exports = class MenuStack {
next(cb) { next(cb) {
const currentModuleInfo = this.top(); const currentModuleInfo = this.top();
const menuConfig = currentModuleInfo.instance.menuConfig; const menuConfig = currentModuleInfo.instance.menuConfig;
const nextMenu = this.client.acs.getConditionalValue(menuConfig.next, 'next'); const nextMenu = getResolvedSpec(this.client, menuConfig.next, 'next');
if(!nextMenu) { if(!nextMenu) {
return cb(Array.isArray(menuConfig.next) ? return cb(Array.isArray(menuConfig.next) ?
Errors.MenuStack('No matching condition for "next"', ErrorReasons.NoConditionMatch) : Errors.MenuStack('No matching condition for "next"', ErrorReasons.NoConditionMatch) :

View File

@ -17,6 +17,7 @@ const _ = require('lodash');
exports.loadMenu = loadMenu; exports.loadMenu = loadMenu;
exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap; exports.getFormConfigByIDAndMap = getFormConfigByIDAndMap;
exports.handleAction = handleAction; exports.handleAction = handleAction;
exports.getResolvedSpec = getResolvedSpec;
exports.handleNext = handleNext; exports.handleNext = handleNext;
function getMenuConfig(client, name, cb) { function getMenuConfig(client, name, cb) {
@ -172,7 +173,7 @@ function handleAction(client, formData, conf, cb) {
return cb(Errors.MissingParam('Missing config')); return cb(Errors.MissingParam('Missing config'));
} }
const action = client.acs.getConditionalValue(conf.action, 'action'); // handle any conditionals const action = getResolvedSpec(client, conf.action, 'action'); // random/conditionals/etc.
const actionAsset = asset.parseAsset(action); const actionAsset = asset.parseAsset(action);
if(!_.isObject(actionAsset)) { if(!_.isObject(actionAsset)) {
return cb(Errors.Invalid('Unable to parse "conf.action"')); return cb(Errors.Invalid('Unable to parse "conf.action"'));
@ -216,9 +217,38 @@ function handleAction(client, formData, conf, cb) {
} }
} }
function handleNext(client, nextSpec, conf, cb) { function getResolvedSpec(client, spec, memberName) {
nextSpec = client.acs.getConditionalValue(nextSpec, 'next'); // handle any conditionals //
// 'next', 'action', etc. can come in various flavors:
// (1) Simple string:
// next: foo
// (2) Array of objects with 'acs' checks; any object missing 'acs'
// is assumed to be "true":
// next: [
// {
// acs: AR2
// next: foo
// }
// {
// next: baz
// }
// ]
// (3) Simple array of strings. A random selection will be made:
// next: [ "foo", "baz", "fizzbang" ]
//
if(!Array.isArray(spec)) {
return spec; // (1) simple string, as-is
}
if(_.isObject(spec[0])) {
return client.acs.getConditionalValue(spec, memberName); // (2) ACS conditionals
}
return spec[Math.floor(Math.random() * spec.length)]; // (3) random
}
function handleNext(client, nextSpec, conf, cb) {
nextSpec = getResolvedSpec(client, nextSpec, 'next');
const nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu'); const nextAsset = asset.getAssetWithShorthand(nextSpec, 'menu');
// :TODO: getAssetWithShorthand() can return undefined - handle it! // :TODO: getAssetWithShorthand() can return undefined - handle it!