2019-01-03 05:13:42 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
|
|
|
const Events = require('./events.js');
|
|
|
|
const Config = require('./config.js').get;
|
2019-01-05 19:18:44 +00:00
|
|
|
const {
|
|
|
|
getConfigPath,
|
|
|
|
getFullConfig,
|
|
|
|
} = require('./config_util.js');
|
2019-01-03 05:13:42 +00:00
|
|
|
const UserDb = require('./database.js').dbs.user;
|
2019-01-05 17:49:19 +00:00
|
|
|
const {
|
|
|
|
getISOTimestampString
|
|
|
|
} = require('./database.js');
|
2019-01-03 05:13:42 +00:00
|
|
|
const UserInterruptQueue = require('./user_interrupt_queue.js');
|
|
|
|
const {
|
|
|
|
getConnectionByUserId
|
|
|
|
} = require('./client_connections.js');
|
2019-01-04 04:03:08 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
2019-01-05 17:49:19 +00:00
|
|
|
const {
|
|
|
|
Errors,
|
|
|
|
ErrorReasons
|
|
|
|
} = require('./enig_error.js');
|
2019-01-04 04:03:08 +00:00
|
|
|
const { getThemeArt } = require('./theme.js');
|
|
|
|
const { pipeToAnsi } = require('./color_codes.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
2019-01-05 17:49:19 +00:00
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const Log = require('./logger.js').log;
|
2019-01-05 19:18:44 +00:00
|
|
|
const ConfigCache = require('./config_cache.js');
|
2019-01-03 05:13:42 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
2019-01-04 04:03:08 +00:00
|
|
|
const async = require('async');
|
|
|
|
const moment = require('moment');
|
2019-01-05 19:18:44 +00:00
|
|
|
const paths = require('path');
|
2019-01-03 05:13:42 +00:00
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
class Achievement {
|
|
|
|
constructor(data) {
|
|
|
|
this.data = data;
|
2019-01-06 17:41:04 +00:00
|
|
|
|
|
|
|
// achievements are retroactive by default
|
|
|
|
this.data.retroactive = _.get(this.data, 'retroactive', true);
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static factory(data) {
|
|
|
|
let achievement;
|
|
|
|
switch(data.type) {
|
2019-01-07 04:56:12 +00:00
|
|
|
case Achievement.Types.UserStatSet :
|
|
|
|
case Achievement.Types.UserStatInc :
|
|
|
|
achievement = new UserStatAchievement(data);
|
|
|
|
break;
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
default : return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(achievement.isValid()) {
|
|
|
|
return achievement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Types() {
|
|
|
|
return {
|
2019-01-07 04:56:12 +00:00
|
|
|
UserStatSet : 'userStatSet',
|
|
|
|
UserStatInc : 'userStatInc',
|
2019-01-05 17:49:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
switch(this.data.type) {
|
2019-01-07 04:56:12 +00:00
|
|
|
case Achievement.Types.UserStatSet :
|
|
|
|
case Achievement.Types.UserStatInc :
|
2019-01-05 17:49:19 +00:00
|
|
|
if(!_.isString(this.data.statName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!_.isObject(this.data.match)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default : return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMatchDetails(/*matchAgainst*/) {
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidMatchDetails(details) {
|
2019-01-10 03:07:59 +00:00
|
|
|
if(!details || !_.isString(details.title) || !_.isString(details.text) || !_.isNumber(details.points)) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (_.isString(details.globalText) || !details.globalText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserStatAchievement extends Achievement {
|
|
|
|
constructor(data) {
|
|
|
|
super(data);
|
2019-01-06 17:41:04 +00:00
|
|
|
|
|
|
|
// sort match keys for quick match lookup
|
|
|
|
this.matchKeys = Object.keys(this.data.match || {}).map(k => parseInt(k)).sort( (a, b) => b - a);
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
|
|
|
if(!super.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !Object.keys(this.data.match).some(k => !parseInt(k));
|
|
|
|
}
|
|
|
|
|
|
|
|
getMatchDetails(matchValue) {
|
2019-01-10 03:07:59 +00:00
|
|
|
let ret = [];
|
2019-01-06 17:41:04 +00:00
|
|
|
let matchField = this.matchKeys.find(v => matchValue >= v);
|
2019-01-05 17:49:19 +00:00
|
|
|
if(matchField) {
|
|
|
|
const match = this.data.match[matchField];
|
2019-01-10 03:07:59 +00:00
|
|
|
matchField = parseInt(matchField);
|
|
|
|
if(this.isValidMatchDetails(match) && !isNaN(matchField)) {
|
|
|
|
ret = [ match, matchField, matchValue ];
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 03:07:59 +00:00
|
|
|
return ret;
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-03 05:13:42 +00:00
|
|
|
class Achievements {
|
|
|
|
constructor(events) {
|
|
|
|
this.events = events;
|
|
|
|
}
|
|
|
|
|
|
|
|
init(cb) {
|
2019-01-05 19:18:44 +00:00
|
|
|
let achievementConfigPath = _.get(Config(), 'general.achievementFile');
|
|
|
|
if(!achievementConfigPath) {
|
2019-01-05 23:55:37 +00:00
|
|
|
Log.info('Achievements are not configured');
|
2019-01-05 19:18:44 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
achievementConfigPath = getConfigPath(achievementConfigPath); // qualify
|
|
|
|
|
|
|
|
const configLoaded = (achievementConfig) => {
|
|
|
|
if(true !== achievementConfig.enabled) {
|
2019-01-05 23:55:37 +00:00
|
|
|
Log.info('Achievements are not enabled');
|
2019-01-07 04:56:12 +00:00
|
|
|
this.stopMonitoringUserStatEvents();
|
2019-01-05 19:18:44 +00:00
|
|
|
delete this.achievementConfig;
|
|
|
|
} else {
|
2019-01-05 23:55:37 +00:00
|
|
|
Log.info('Achievements are enabled');
|
2019-01-05 19:18:44 +00:00
|
|
|
this.achievementConfig = achievementConfig;
|
2019-01-07 04:56:12 +00:00
|
|
|
this.monitorUserStatEvents();
|
2019-01-05 19:18:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const changed = ( { fileName, fileRoot } ) => {
|
|
|
|
const reCachedPath = paths.join(fileRoot, fileName);
|
|
|
|
if(reCachedPath === achievementConfigPath) {
|
|
|
|
getFullConfig(achievementConfigPath, (err, achievementConfig) => {
|
|
|
|
if(err) {
|
|
|
|
return Log.error( { error : err.message }, 'Failed to reload achievement config from cache');
|
|
|
|
}
|
|
|
|
configLoaded(achievementConfig);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConfigCache.getConfigWithOptions(
|
|
|
|
{
|
|
|
|
filePath : achievementConfigPath,
|
|
|
|
forceReCache : true,
|
|
|
|
callback : changed,
|
|
|
|
},
|
|
|
|
(err, achievementConfig) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
configLoaded(achievementConfig);
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
loadAchievementHitCount(user, achievementTag, field, cb) {
|
2019-01-03 05:13:42 +00:00
|
|
|
UserDb.get(
|
|
|
|
`SELECT COUNT() AS count
|
|
|
|
FROM user_achievement
|
2019-01-14 01:19:00 +00:00
|
|
|
WHERE user_id = ? AND achievement_tag = ? AND match = ?;`,
|
2019-01-05 17:49:19 +00:00
|
|
|
[ user.userId, achievementTag, field],
|
2019-01-03 05:13:42 +00:00
|
|
|
(err, row) => {
|
2019-01-10 03:07:59 +00:00
|
|
|
return cb(err, row ? row.count : 0);
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
record(info, cb) {
|
|
|
|
StatLog.incrementUserStat(info.client.user, UserProps.AchievementTotalCount, 1);
|
|
|
|
StatLog.incrementUserStat(info.client.user, UserProps.AchievementTotalPoints, info.details.points);
|
|
|
|
|
|
|
|
UserDb.run(
|
2019-01-14 01:19:00 +00:00
|
|
|
`INSERT INTO user_achievement (user_id, achievement_tag, timestamp, match)
|
|
|
|
VALUES (?, ?, ?, ?);`,
|
|
|
|
[ info.client.user.userId, info.achievementTag, getISOTimestampString(info.timestamp), info.matchField ],
|
2019-01-05 17:49:19 +00:00
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2019-01-05 19:18:44 +00:00
|
|
|
this.events.emit(
|
2019-01-05 17:49:19 +00:00
|
|
|
Events.getSystemEvents().UserAchievementEarned,
|
|
|
|
{
|
|
|
|
user : info.client.user,
|
|
|
|
achievementTag : info.achievementTag,
|
|
|
|
points : info.details.points,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
display(info, cb) {
|
|
|
|
this.createAchievementInterruptItems(info, (err, interruptItems) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(interruptItems.local) {
|
|
|
|
UserInterruptQueue.queue(interruptItems.local, { clients : info.client } );
|
|
|
|
}
|
|
|
|
|
|
|
|
if(interruptItems.global) {
|
|
|
|
UserInterruptQueue.queue(interruptItems.global, { omit : info.client } );
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-01-06 17:41:04 +00:00
|
|
|
recordAndDisplayAchievement(info, cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
(callback) => {
|
|
|
|
return this.record(info, callback);
|
|
|
|
},
|
|
|
|
(callback) => {
|
|
|
|
return this.display(info, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
monitorUserStatEvents() {
|
|
|
|
if(this.userStatEventListeners) {
|
2019-01-05 19:18:44 +00:00
|
|
|
return; // already listening
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
const listenEvents = [
|
|
|
|
Events.getSystemEvents().UserStatSet,
|
|
|
|
Events.getSystemEvents().UserStatIncrement
|
|
|
|
];
|
|
|
|
|
|
|
|
this.userStatEventListeners = this.events.addMultipleEventListener(listenEvents, userStatEvent => {
|
2019-01-05 17:49:19 +00:00
|
|
|
if([ UserProps.AchievementTotalCount, UserProps.AchievementTotalPoints ].includes(userStatEvent.statName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
if(!_.isNumber(userStatEvent.statValue) && !_.isNumber(userStatEvent.statIncrementBy)) {
|
2019-01-03 05:13:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
// :TODO: Make this code generic - find + return factory created object
|
2019-01-03 05:13:42 +00:00
|
|
|
const achievementTag = _.findKey(
|
2019-01-05 19:18:44 +00:00
|
|
|
_.get(this.achievementConfig, 'achievements', {}),
|
2019-01-03 05:13:42 +00:00
|
|
|
achievement => {
|
|
|
|
if(false === achievement.enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-07 04:56:12 +00:00
|
|
|
return [ Achievement.Types.UserStatSet, Achievement.Types.UserStatInc ].includes(achievement.type) &&
|
2019-01-03 05:13:42 +00:00
|
|
|
achievement.statName === userStatEvent.statName;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if(!achievementTag) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-05 19:18:44 +00:00
|
|
|
const achievement = Achievement.factory(this.achievementConfig.achievements[achievementTag]);
|
2019-01-05 17:49:19 +00:00
|
|
|
if(!achievement) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-04 04:03:08 +00:00
|
|
|
|
2019-01-10 03:07:59 +00:00
|
|
|
const statValue = parseInt(Achievement.Types.UserStatSet === achievement.data.type ?
|
|
|
|
userStatEvent.statValue :
|
|
|
|
userStatEvent.statIncrementBy
|
2019-01-07 04:56:12 +00:00
|
|
|
);
|
|
|
|
if(isNaN(statValue)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
const [ details, matchField, matchValue ] = achievement.getMatchDetails(statValue);
|
2019-01-10 03:07:59 +00:00
|
|
|
if(!details) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return;
|
2019-01-04 04:03:08 +00:00
|
|
|
}
|
2019-01-05 17:49:19 +00:00
|
|
|
|
2019-01-10 03:07:59 +00:00
|
|
|
async.series(
|
2019-01-05 17:49:19 +00:00
|
|
|
[
|
|
|
|
(callback) => {
|
|
|
|
this.loadAchievementHitCount(userStatEvent.user, achievementTag, matchField, (err, count) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(count > 0 ? Errors.General('Achievement already acquired', ErrorReasons.TooMany) : null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(callback) => {
|
|
|
|
const client = getConnectionByUserId(userStatEvent.user.userId);
|
|
|
|
if(!client) {
|
|
|
|
return callback(Errors.UnexpectedState('Failed to get client for user ID'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const info = {
|
|
|
|
achievementTag,
|
|
|
|
achievement,
|
|
|
|
details,
|
|
|
|
client,
|
2019-01-05 19:18:44 +00:00
|
|
|
matchField, // match - may be in odd format
|
|
|
|
matchValue, // actual value
|
|
|
|
achievedValue : matchField, // achievement value met
|
|
|
|
user : userStatEvent.user,
|
|
|
|
timestamp : moment(),
|
2019-01-05 17:49:19 +00:00
|
|
|
};
|
|
|
|
|
2019-01-06 17:41:04 +00:00
|
|
|
const achievementsInfo = [ info ];
|
|
|
|
if(true === achievement.data.retroactive) {
|
|
|
|
// For userStat, any lesser match keys(values) are also met. Example:
|
|
|
|
// matchKeys: [ 500, 200, 100, 20, 10, 2 ]
|
|
|
|
// ^---- we met here
|
|
|
|
// ^------------^ retroactive range
|
|
|
|
//
|
|
|
|
const index = achievement.matchKeys.findIndex(v => v < matchField);
|
2019-01-10 03:07:59 +00:00
|
|
|
if(index > -1 && Array.isArray(achievement.matchKeys)) {
|
|
|
|
achievement.matchKeys.slice(index).forEach(k => {
|
|
|
|
const [ det, fld, val ] = achievement.getMatchDetails(k);
|
|
|
|
if(det) {
|
|
|
|
achievementsInfo.push(Object.assign(
|
|
|
|
{},
|
|
|
|
info,
|
|
|
|
{
|
|
|
|
details : det,
|
|
|
|
matchField : fld,
|
|
|
|
achievedValue : fld,
|
|
|
|
matchValue : val,
|
|
|
|
}
|
|
|
|
));
|
|
|
|
}
|
|
|
|
});
|
2019-01-06 17:41:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverse achievementsInfo so we display smallest > largest
|
|
|
|
achievementsInfo.reverse();
|
|
|
|
|
2019-01-10 03:07:59 +00:00
|
|
|
async.eachSeries(achievementsInfo, (achInfo, nextAchInfo) => {
|
|
|
|
return this.recordAndDisplayAchievement(achInfo, err => {
|
|
|
|
return nextAchInfo(err);
|
|
|
|
});
|
2019-01-06 17:41:04 +00:00
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
2019-01-05 17:49:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err && ErrorReasons.TooMany !== err.reasonCode) {
|
|
|
|
Log.warn( { error : err.message, userStatEvent }, 'Error handling achievement for user stat event');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2019-01-04 04:03:08 +00:00
|
|
|
});
|
|
|
|
}
|
2019-01-03 05:13:42 +00:00
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
stopMonitoringUserStatEvents() {
|
|
|
|
if(this.userStatEventListeners) {
|
|
|
|
this.events.removeMultipleEventListener(this.userStatEventListeners);
|
|
|
|
delete this.userStatEventListeners;
|
2019-01-05 19:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 05:51:16 +00:00
|
|
|
getFormatObject(info) {
|
|
|
|
return {
|
|
|
|
userName : info.user.username,
|
|
|
|
userRealName : info.user.properties[UserProps.RealName],
|
|
|
|
userLocation : info.user.properties[UserProps.Location],
|
|
|
|
userAffils : info.user.properties[UserProps.Affiliations],
|
|
|
|
nodeId : info.client.node,
|
|
|
|
title : info.details.title,
|
|
|
|
text : info.global ? info.details.globalText : info.details.text,
|
|
|
|
points : info.details.points,
|
|
|
|
achievedValue : info.achievedValue,
|
|
|
|
matchField : info.matchField,
|
|
|
|
matchValue : info.matchValue,
|
|
|
|
timestamp : moment(info.timestamp).format(info.dateTimeFormat),
|
|
|
|
boardName : Config().general.boardName,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormattedTextFor(info, textType, defaultSgr = '|07') {
|
2019-01-05 23:55:37 +00:00
|
|
|
const themeDefaults = _.get(info.client.currentTheme, 'achievements.defaults', {});
|
2019-01-11 03:34:52 +00:00
|
|
|
const textTypeSgr = themeDefaults[`${textType}SGR`] || defaultSgr;
|
2019-01-04 04:03:08 +00:00
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
const formatObj = this.getFormatObject(info);
|
2019-01-04 04:03:08 +00:00
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
const wrap = (input) => {
|
|
|
|
const re = new RegExp(`{(${Object.keys(formatObj).join('|')})([^}]*)}`, 'g');
|
|
|
|
return input.replace(re, (m, formatVar, formatOpts) => {
|
|
|
|
const varSgr = themeDefaults[`${formatVar}SGR`] || textTypeSgr;
|
|
|
|
let r = `${varSgr}{${formatVar}`;
|
|
|
|
if(formatOpts) {
|
|
|
|
r += formatOpts;
|
|
|
|
}
|
|
|
|
return `${r}}${textTypeSgr}`;
|
|
|
|
});
|
|
|
|
};
|
2019-01-04 04:03:08 +00:00
|
|
|
|
2019-01-11 03:34:52 +00:00
|
|
|
return stringFormat(`${textTypeSgr}${wrap(info.details[textType])}`, formatObj);
|
2019-01-05 23:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createAchievementInterruptItems(info, cb) {
|
|
|
|
info.dateTimeFormat =
|
|
|
|
info.details.dateTimeFormat ||
|
|
|
|
info.achievement.dateTimeFormat ||
|
|
|
|
info.client.currentTheme.helpers.getDateTimeFormat();
|
|
|
|
|
|
|
|
const title = this.getFormattedTextFor(info, 'title');
|
|
|
|
const text = this.getFormattedTextFor(info, 'text');
|
2019-01-04 04:03:08 +00:00
|
|
|
|
|
|
|
let globalText;
|
|
|
|
if(info.details.globalText) {
|
2019-01-05 23:55:37 +00:00
|
|
|
globalText = this.getFormattedTextFor(info, 'globalText');
|
2019-01-04 04:03:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getArt = (name, callback) => {
|
|
|
|
const spec =
|
|
|
|
_.get(info.details, `art.${name}`) ||
|
|
|
|
_.get(info.achievement, `art.${name}`) ||
|
2019-01-05 19:18:44 +00:00
|
|
|
_.get(this.achievementConfig, `art.${name}`);
|
2019-01-04 04:03:08 +00:00
|
|
|
if(!spec) {
|
|
|
|
return callback(null);
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
2019-01-04 04:03:08 +00:00
|
|
|
const getArtOpts = {
|
|
|
|
name : spec,
|
|
|
|
client : this.client,
|
|
|
|
random : false,
|
|
|
|
};
|
|
|
|
getThemeArt(getArtOpts, (err, artInfo) => {
|
|
|
|
// ignore errors
|
|
|
|
return callback(artInfo ? artInfo.data : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const interruptItems = {};
|
|
|
|
let itemTypes = [ 'local' ];
|
|
|
|
if(globalText) {
|
|
|
|
itemTypes.push('global');
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each(itemTypes, (itemType, nextItemType) => {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
(callback) => {
|
2019-01-05 17:49:19 +00:00
|
|
|
getArt(`${itemType}Header`, headerArt => {
|
2019-01-04 04:03:08 +00:00
|
|
|
return callback(null, headerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, callback) => {
|
2019-01-05 17:49:19 +00:00
|
|
|
getArt(`${itemType}Footer`, footerArt => {
|
2019-01-04 04:03:08 +00:00
|
|
|
return callback(null, headerArt, footerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, footerArt, callback) => {
|
|
|
|
const itemText = 'global' === itemType ? globalText : text;
|
|
|
|
interruptItems[itemType] = {
|
|
|
|
text : `${title}\r\n${itemText}`,
|
|
|
|
pause : true,
|
|
|
|
};
|
|
|
|
if(headerArt || footerArt) {
|
2019-01-06 05:51:16 +00:00
|
|
|
const themeDefaults = _.get(info.client.currentTheme, 'achievements.defaults', {});
|
2019-01-11 04:41:32 +00:00
|
|
|
const defaultContentsFormat = '{title}\r\n{message}';
|
2019-01-06 05:51:16 +00:00
|
|
|
const contentsFormat = 'global' === itemType ?
|
|
|
|
themeDefaults.globalFormat || defaultContentsFormat :
|
|
|
|
themeDefaults.format || defaultContentsFormat;
|
|
|
|
|
|
|
|
const formatObj = Object.assign(this.getFormatObject(info), {
|
|
|
|
title : this.getFormattedTextFor(info, 'title', ''), // ''=defaultSgr
|
|
|
|
message : itemText,
|
|
|
|
});
|
|
|
|
|
|
|
|
const contents = pipeToAnsi(stringFormat(contentsFormat, formatObj));
|
|
|
|
|
2019-01-06 02:04:19 +00:00
|
|
|
interruptItems[itemType].contents =
|
2019-01-06 05:51:16 +00:00
|
|
|
`${headerArt || ''}\r\n${contents}\r\n${footerArt || ''}`;
|
2019-01-04 04:03:08 +00:00
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return nextItemType(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, interruptItems);
|
2019-01-03 05:13:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let achievements;
|
|
|
|
|
|
|
|
exports.moduleInitialize = (initInfo, cb) => {
|
|
|
|
achievements = new Achievements(initInfo.events);
|
|
|
|
return achievements.init(cb);
|
|
|
|
};
|