2018-06-16 16:01:08 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const Errors = require('../core/enig_error.js').Errors;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const { getMessageAreaByTag } = require('./message_area.js');
|
2018-06-16 16:01:08 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2018-06-16 16:01:08 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +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);
|
2023-08-24 00:06:48 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.config = Object.assign({}, _.get(options, 'menuConfig.config'), {
|
|
|
|
extraArgs: options.extraArgs,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
let handler =
|
|
|
|
{
|
|
|
|
extraArgs: self.showByExtraArgs,
|
|
|
|
sequence: self.showBySequence,
|
|
|
|
random: self.showByRandom,
|
|
|
|
fileBaseArea: self.showByFileBaseArea,
|
|
|
|
messageConf: self.showByMessageConf,
|
|
|
|
messageArea: self.showByMessageArea,
|
|
|
|
}[self.config.method] || self.showRandomArt;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
handler = handler.bind(self);
|
|
|
|
|
|
|
|
return handler(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err && !self.config.optional) {
|
|
|
|
self.client.log.warn('Error during init sequence', {
|
|
|
|
error: err.message,
|
|
|
|
});
|
|
|
|
return self.prevMenu(() => {
|
|
|
|
/* dummy */
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.finishedLoading();
|
2022-06-05 20:04:25 +00:00
|
|
|
return self.autoNextMenu(() => {
|
|
|
|
/* dummy */
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
showByExtraArgs(cb) {
|
2019-05-25 04:26:27 +00:00
|
|
|
const artData = _.get(this.config, 'extraArgs.artData');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Buffer.isBuffer(artData)) {
|
2019-05-25 04:26:27 +00:00
|
|
|
const options = {
|
2022-06-05 20:04:25 +00:00
|
|
|
pause: this.shouldPause(),
|
|
|
|
desc: 'extraArgs',
|
2019-05-25 04:26:27 +00:00
|
|
|
};
|
|
|
|
return this.displaySingleArtWithOptions(artData, options, cb);
|
|
|
|
}
|
|
|
|
|
2018-11-18 04:04:00 +00:00
|
|
|
this.getArtKeyValue(this.config.key, (err, artSpec) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
const options = {
|
2022-06-05 20:04:25 +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) {
|
2018-11-18 04:04:00 +00:00
|
|
|
this.getArtKeyValue('areaTag', (err, key) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.displaySingleArtByConfigPath(
|
|
|
|
['fileBase', 'areas', key, 'art'],
|
|
|
|
cb
|
|
|
|
);
|
2018-07-22 21:58:13 +00:00
|
|
|
});
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-07-22 21:58:13 +00:00
|
|
|
showByMessageConf(cb) {
|
2018-11-18 04:04:00 +00:00
|
|
|
this.getArtKeyValue('confTag', (err, key) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-07-22 21:58:13 +00:00
|
|
|
return cb(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +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-11-18 04:04:00 +00:00
|
|
|
this.getArtKeyValue('areaTag', (err, key) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-07-23 01:06:43 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const area = getMessageAreaByTag(key);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!area) {
|
2018-07-23 01:06:43 +00:00
|
|
|
return cb(Errors.DoesNotExist(`No area by areaTag ${key} found`));
|
|
|
|
}
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!artSpec) {
|
2018-07-22 21:58:13 +00:00
|
|
|
return cb(Errors.MissingConfig(`No art defined at path ${desc}`));
|
|
|
|
}
|
|
|
|
const options = {
|
|
|
|
desc,
|
2022-06-05 20:04:25 +00:00
|
|
|
pause: this.shouldPause(),
|
2018-07-22 21:58:13 +00:00
|
|
|
};
|
|
|
|
return this.displaySingleArtWithOptions(artSpec, options, cb);
|
|
|
|
}
|
|
|
|
|
2018-11-18 04:04:00 +00:00
|
|
|
getArtKeyValue(defaultKey, cb) {
|
|
|
|
const key = this.config.key || defaultKey;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isString(key)) {
|
|
|
|
return cb(
|
|
|
|
Errors.MissingConfig(
|
|
|
|
'Config option "key" is required for method "extraArgs"'
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const path = key.split('.');
|
2022-06-05 20:04:25 +00:00
|
|
|
const artKey = _.get(this.config, ['extraArgs'].concat(path));
|
|
|
|
if (!_.isString(artKey)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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/
|
2022-06-05 20:04:25 +00:00
|
|
|
self.displayAsset(artSpec, self.menuConfig.config, (err, artData) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
const mciData = { menu: artData.mciMap };
|
|
|
|
if (
|
|
|
|
self.client.term.termHeight > 0 &&
|
|
|
|
artData.height > self.client.term.termHeight
|
|
|
|
) {
|
|
|
|
// We must have scrolled, adjust the positioning for pause
|
|
|
|
artData.height = self.client.term.termHeight;
|
|
|
|
}
|
|
|
|
const pausePosition = { row: artData.height + 1, col: 1 };
|
|
|
|
return callback(null, mciData, pausePosition);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function afterArtDisplayed(mciData, pausePosition, callback) {
|
|
|
|
self.mciReady(mciData, err => {
|
|
|
|
return callback(err, pausePosition);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function displayPauseIfRequested(pausePosition, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!options.pause) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
return self.pausePrompt(pausePosition, callback);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ artSpec, error: err.message },
|
|
|
|
`Failed to display "${options.desc}" art`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2018-06-16 16:01:08 +00:00
|
|
|
};
|