2018-06-16 16:01:08 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const Errors = require('../core/enig_error.js').Errors;
|
|
|
|
const ANSI = require('./ansi_term.js');
|
|
|
|
const Config = require('./config.js').get;
|
2018-07-23 01:06:43 +00:00
|
|
|
const {
|
|
|
|
getMessageAreaByTag
|
|
|
|
} = require('./message_area.js');
|
2018-06-16 16:01:08 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2018-06-16 16:01:08 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Show Art',
|
|
|
|
desc : 'Module for more advanced methods of displaying art',
|
|
|
|
author : 'NuSkooler',
|
2018-06-16 16:01:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class ShowArtModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), { extraArgs : options.extraArgs });
|
|
|
|
|
|
|
|
this.config.method = this.config.method || 'random';
|
|
|
|
this.config.optional = _.get(this.config, 'optional', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function before(callback) {
|
|
|
|
return self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function showArt(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// How we show art depends on our configuration
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let handler = {
|
2018-06-23 03:26:46 +00:00
|
|
|
extraArgs : self.showByExtraArgs,
|
|
|
|
sequence : self.showBySequence,
|
|
|
|
random : self.showByRandom,
|
|
|
|
fileBaseArea : self.showByFileBaseArea,
|
2018-07-22 21:58:13 +00:00
|
|
|
messageConf : self.showByMessageConf,
|
|
|
|
messageArea : self.showByMessageArea,
|
2018-06-22 05:15:04 +00:00
|
|
|
}[self.config.method] || self.showRandomArt;
|
|
|
|
|
|
|
|
handler = handler.bind(self);
|
|
|
|
|
|
|
|
return handler(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err && !self.config.optional) {
|
|
|
|
self.client.log.warn('Error during init sequence', { error : err.message } );
|
|
|
|
return self.prevMenu( () => { /* dummy */ } );
|
|
|
|
}
|
|
|
|
|
|
|
|
self.finishedLoading();
|
|
|
|
return self.autoNextMenu( () => { /* dummy */ } );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
showByExtraArgs(cb) {
|
|
|
|
this.getArtKeyValue( (err, artSpec) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
const options = {
|
2018-06-23 03:26:46 +00:00
|
|
|
pause : this.shouldPause(),
|
|
|
|
desc : 'extraArgs',
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
return this.displaySingleArtWithOptions(artSpec, options, cb);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
showBySequence(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
showByRandom(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
showByFileBaseArea(cb) {
|
|
|
|
this.getArtKeyValue( (err, key) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2018-07-22 21:58:13 +00:00
|
|
|
return this.displaySingleArtByConfigPath( [ 'fileBase', 'areas', key, 'art' ], cb);
|
|
|
|
});
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-22 21:58:13 +00:00
|
|
|
showByMessageConf(cb) {
|
|
|
|
this.getArtKeyValue( (err, key) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-07-22 21:58:13 +00:00
|
|
|
return this.displaySingleArtByConfigPath( [ 'messageConferences', key, 'art' ], cb);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-22 21:58:13 +00:00
|
|
|
showByMessageArea(cb) {
|
2018-07-23 01:06:43 +00:00
|
|
|
this.getArtKeyValue( (err, key) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const area = getMessageAreaByTag(key);
|
|
|
|
if(!area) {
|
|
|
|
return cb(Errors.DoesNotExist(`No area by areaTag ${key} found`));
|
|
|
|
}
|
2018-08-04 17:49:44 +00:00
|
|
|
return cb(null); // :TODO: REMOVE ME --- currently NYI
|
2018-07-23 01:06:43 +00:00
|
|
|
});
|
2018-07-22 21:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displaySingleArtByConfigPath(configPath, cb) {
|
|
|
|
const desc = configPath.join('.');
|
|
|
|
const artSpec = _.get(Config(), configPath);
|
|
|
|
if(!artSpec) {
|
|
|
|
return cb(Errors.MissingConfig(`No art defined at path ${desc}`));
|
|
|
|
}
|
|
|
|
const options = {
|
|
|
|
desc,
|
|
|
|
pause : this.shouldPause(),
|
|
|
|
};
|
|
|
|
return this.displaySingleArtWithOptions(artSpec, options, cb);
|
|
|
|
}
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
getArtKeyValue(cb) {
|
|
|
|
const key = this.config.key;
|
|
|
|
if(!_.isString(key)) {
|
|
|
|
return cb(Errors.MissingConfig('Config option "key" is required for method "extraArgs"'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const path = key.split('.');
|
|
|
|
const artKey = _.get(this.config, [ 'extraArgs' ].concat(path) );
|
|
|
|
if(!_.isString(artKey)) {
|
|
|
|
return cb(Errors.MissingParam(`Invalid or missing "extraArgs.${key}" value`));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, artKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
displaySingleArtWithOptions(artSpec, options, cb) {
|
|
|
|
const self = this;
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function art(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: we really need a way to supply an explicit path to look in, e.g. general/area_art/
|
2018-06-22 05:15:04 +00:00
|
|
|
self.displayAsset(
|
|
|
|
artSpec,
|
2018-08-05 20:06:30 +00:00
|
|
|
self.menuConfig.config,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, artData) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
const mciData = { menu : artData.mciMap };
|
|
|
|
return callback(null, mciData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function recordCursorPosition(mciData, callback) {
|
|
|
|
if(!options.pause) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null, mciData, null); // cursor position not needed
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.client.once('cursor position report', pos => {
|
|
|
|
const pausePosition = { row : pos[0], col : 1 };
|
|
|
|
return callback(null, mciData, pausePosition);
|
|
|
|
});
|
|
|
|
|
|
|
|
self.client.term.rawWrite(ANSI.queryPos());
|
|
|
|
},
|
|
|
|
function afterArtDisplayed(mciData, pausePosition, callback) {
|
|
|
|
self.mciReady(mciData, err => {
|
|
|
|
return callback(err, pausePosition);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function displayPauseIfRequested(pausePosition, callback) {
|
|
|
|
if(!options.pause) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
return self.pausePrompt(pausePosition, callback);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { artSpec, error : err.message }, `Failed to display "${options.desc}" art`);
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-06-16 16:01:08 +00:00
|
|
|
};
|