2018-11-13 05:03:28 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const Art = require('./art.js');
|
|
|
|
const { getActiveConnections } = require('./client_connections.js');
|
|
|
|
const ANSI = require('./ansi_term.js');
|
|
|
|
const { pipeToAnsi } = require('./color_codes.js');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
2018-11-13 05:03:28 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
module.exports = class UserInterruptQueue {
|
2018-11-13 05:03:28 +00:00
|
|
|
constructor(client) {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client = client;
|
|
|
|
this.queue = [];
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
static queue(interruptItem, opts) {
|
|
|
|
opts = opts || {};
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!opts.clients) {
|
2018-12-01 06:20:44 +00:00
|
|
|
let omitNodes = [];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (Array.isArray(opts.omit)) {
|
2018-12-01 06:20:44 +00:00
|
|
|
omitNodes = opts.omit;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (opts.omit) {
|
|
|
|
omitNodes = [opts.omit];
|
2018-12-01 06:20:44 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
omitNodes = omitNodes.map(n => (_.isNumber(n) ? n : n.node));
|
|
|
|
opts.clients = getActiveConnections(true).filter(
|
|
|
|
ac => !omitNodes.includes(ac.node)
|
|
|
|
);
|
2018-12-01 06:20:44 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!Array.isArray(opts.clients)) {
|
|
|
|
opts.clients = [opts.clients];
|
2018-12-01 06:20:44 +00:00
|
|
|
}
|
|
|
|
opts.clients.forEach(c => {
|
|
|
|
c.interruptQueue.queueItem(interruptItem);
|
2018-11-13 05:03:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
queueItem(interruptItem) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isString(interruptItem.contents) && !_.isString(interruptItem.text)) {
|
2018-11-14 04:11:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-11-15 05:47:20 +00:00
|
|
|
|
|
|
|
// pause defaulted on
|
2018-11-13 05:03:28 +00:00
|
|
|
interruptItem.pause = _.get(interruptItem, 'pause', true);
|
2018-11-15 05:47:20 +00:00
|
|
|
|
2019-01-30 04:31:39 +00:00
|
|
|
try {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.currentMenuModule.attemptInterruptNow(
|
|
|
|
interruptItem,
|
|
|
|
(err, ateIt) => {
|
|
|
|
if (err) {
|
|
|
|
// :TODO: Log me
|
|
|
|
} else if (true !== ateIt) {
|
|
|
|
this.queue.push(interruptItem);
|
|
|
|
}
|
2019-01-30 04:31:39 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
2019-01-30 04:31:39 +00:00
|
|
|
this.queue.push(interruptItem);
|
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasItems() {
|
|
|
|
return this.queue.length > 0;
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
displayNext(options, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!cb && _.isFunction(options)) {
|
2018-12-01 06:20:44 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
const interruptItem = this.queue.pop();
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!interruptItem) {
|
2018-11-13 05:03:28 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2018-12-01 06:20:44 +00:00
|
|
|
Object.assign(interruptItem, options);
|
2018-11-15 05:04:29 +00:00
|
|
|
return interruptItem ? this.displayWithItem(interruptItem, cb) : cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayWithItem(interruptItem, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (interruptItem.cls) {
|
2019-01-10 03:06:30 +00:00
|
|
|
this.client.term.rawWrite(ANSI.resetScreen());
|
2018-11-13 05:03:28 +00:00
|
|
|
} else {
|
|
|
|
this.client.term.rawWrite('\r\n\r\n');
|
|
|
|
}
|
|
|
|
|
2019-01-04 04:02:42 +00:00
|
|
|
const maybePauseAndFinish = () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (interruptItem.pause) {
|
|
|
|
this.client.currentMenuModule.pausePrompt(() => {
|
2019-01-04 04:02:42 +00:00
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (interruptItem.contents) {
|
2018-11-14 04:11:33 +00:00
|
|
|
Art.display(this.client, interruptItem.contents, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-15 05:47:20 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
//this.client.term.rawWrite('\r\n\r\n'); // :TODO: Prob optional based on contents vs text
|
2019-01-04 04:02:42 +00:00
|
|
|
maybePauseAndFinish();
|
2018-11-14 04:11:33 +00:00
|
|
|
});
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
this.client.term.write(
|
|
|
|
pipeToAnsi(`${interruptItem.text}\r\n\r\n`, this.client),
|
|
|
|
true,
|
|
|
|
() => {
|
|
|
|
maybePauseAndFinish();
|
|
|
|
}
|
|
|
|
);
|
2018-11-14 04:11:33 +00:00
|
|
|
}
|
2018-11-13 05:03:28 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
};
|