2019-01-03 05:13:42 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const Events = require('./events.js');
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const ConfigLoader = require('./config_loader');
|
|
|
|
const { getConfigPath } = require('./config_util');
|
|
|
|
const UserDb = require('./database.js').dbs.user;
|
|
|
|
const { getISOTimestampString } = require('./database.js');
|
|
|
|
const UserInterruptQueue = require('./user_interrupt_queue.js');
|
|
|
|
const { getConnectionByUserId } = require('./client_connections.js');
|
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const { Errors, ErrorReasons } = require('./enig_error.js');
|
|
|
|
const { getThemeArt } = require('./theme.js');
|
|
|
|
const { pipeToAnsi, stripMciColorCodes } = require('./color_codes.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const Log = require('./logger.js').log;
|
2019-01-03 05:13:42 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
const moment = require('moment');
|
2019-01-03 05:13:42 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.getAchievementsEarnedByUser = getAchievementsEarnedByUser;
|
2019-01-23 04:54:37 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!data) {
|
2019-01-26 19:04:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-01-05 17:49:19 +00:00
|
|
|
let achievement;
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (data.type) {
|
|
|
|
case Achievement.Types.UserStatSet:
|
|
|
|
case Achievement.Types.UserStatInc:
|
|
|
|
case Achievement.Types.UserStatIncNewVal:
|
2019-01-07 04:56:12 +00:00
|
|
|
achievement = new UserStatAchievement(data);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
return;
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (achievement.isValid()) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return achievement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Types() {
|
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
UserStatSet: 'userStatSet',
|
|
|
|
UserStatInc: 'userStatInc',
|
|
|
|
UserStatIncNewVal: 'userStatIncNewVal',
|
2019-01-05 17:49:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid() {
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (this.data.type) {
|
|
|
|
case Achievement.Types.UserStatSet:
|
|
|
|
case Achievement.Types.UserStatInc:
|
|
|
|
case Achievement.Types.UserStatIncNewVal:
|
|
|
|
if (!_.isString(this.data.statName)) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isObject(this.data.match)) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
getMatchDetails(/*matchAgainst*/) {}
|
2019-01-05 17:49:19 +00:00
|
|
|
|
|
|
|
isValidMatchDetails(details) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!details ||
|
|
|
|
!_.isString(details.title) ||
|
|
|
|
!_.isString(details.text) ||
|
|
|
|
!_.isNumber(details.points)
|
|
|
|
) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return _.isString(details.globalText) || !details.globalText;
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UserStatAchievement extends Achievement {
|
|
|
|
constructor(data) {
|
|
|
|
super(data);
|
2019-01-06 17:41:04 +00:00
|
|
|
|
|
|
|
// sort match keys for quick match lookup
|
2022-06-05 20:04:25 +00:00
|
|
|
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() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!super.isValid()) {
|
2019-01-05 17:49:19 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (matchField) {
|
2019-01-05 17:49:19 +00:00
|
|
|
const match = this.data.match[matchField];
|
2019-01-10 03:07:59 +00:00
|
|
|
matchField = parseInt(matchField);
|
2022-06-05 20:04:25 +00:00
|
|
|
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;
|
2020-06-16 03:22:38 +00:00
|
|
|
this.enabled = false;
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 04:54:37 +00:00
|
|
|
getAchievementByTag(tag) {
|
2020-06-16 03:22:38 +00:00
|
|
|
return this.config.get().achievements[tag];
|
2019-01-21 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isEnabled() {
|
2020-06-16 03:22:38 +00:00
|
|
|
return this.enabled;
|
2019-01-21 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 05:13:42 +00:00
|
|
|
init(cb) {
|
2020-06-16 03:22:38 +00:00
|
|
|
const configPath = this._getConfigPath();
|
|
|
|
if (!configPath) {
|
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);
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:22:38 +00:00
|
|
|
const configLoaded = () => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true !== this.config.get().enabled) {
|
2019-01-05 23:55:37 +00:00
|
|
|
Log.info('Achievements are not enabled');
|
2020-06-16 03:22:38 +00:00
|
|
|
this.enabled = false;
|
2019-01-07 04:56:12 +00:00
|
|
|
this.stopMonitoringUserStatEvents();
|
2019-01-05 19:18:44 +00:00
|
|
|
} else {
|
2019-01-05 23:55:37 +00:00
|
|
|
Log.info('Achievements are enabled');
|
2020-06-16 03:22:38 +00:00
|
|
|
this.enabled = true;
|
2019-01-07 04:56:12 +00:00
|
|
|
this.monitorUserStatEvents();
|
2019-01-05 19:18:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-17 06:10:51 +00:00
|
|
|
this.config = new ConfigLoader({
|
2022-06-05 20:04:25 +00:00
|
|
|
onReload: err => {
|
2020-06-16 03:22:38 +00:00
|
|
|
if (!err) {
|
|
|
|
configLoaded();
|
2019-01-05 19:18:44 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2020-06-17 06:10:51 +00:00
|
|
|
});
|
2019-01-05 19:18:44 +00:00
|
|
|
|
2020-06-16 03:22:38 +00:00
|
|
|
this.config.init(configPath, err => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
2019-01-05 19:18:44 +00:00
|
|
|
}
|
2020-06-16 03:22:38 +00:00
|
|
|
|
|
|
|
configLoaded();
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_getConfigPath() {
|
|
|
|
const path = _.get(Config(), 'general.achievementFile');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!path) {
|
2020-06-16 03:22:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
return getConfigPath(path); // qualify
|
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 = ?;`,
|
2022-06-05 20:04:25 +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-21 04:58:00 +00:00
|
|
|
record(info, localInterruptItem, cb) {
|
2019-01-05 17:49:19 +00:00
|
|
|
StatLog.incrementUserStat(info.client.user, UserProps.AchievementTotalCount, 1);
|
2022-06-05 20:04:25 +00:00
|
|
|
StatLog.incrementUserStat(
|
|
|
|
info.client.user,
|
|
|
|
UserProps.AchievementTotalPoints,
|
|
|
|
info.details.points
|
|
|
|
);
|
2019-01-05 17:49:19 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const cleanTitle = stripMciColorCodes(localInterruptItem.title);
|
|
|
|
const cleanText = stripMciColorCodes(localInterruptItem.achievText);
|
2019-01-31 06:36:00 +00:00
|
|
|
|
2019-01-21 04:58:00 +00:00
|
|
|
const recordData = [
|
2022-06-05 20:04:25 +00:00
|
|
|
info.client.user.userId,
|
|
|
|
info.achievementTag,
|
|
|
|
getISOTimestampString(info.timestamp),
|
|
|
|
info.matchField,
|
|
|
|
cleanTitle,
|
|
|
|
cleanText,
|
|
|
|
info.details.points,
|
2019-01-21 04:58:00 +00:00
|
|
|
];
|
|
|
|
|
2019-01-05 17:49:19 +00:00
|
|
|
UserDb.run(
|
2019-01-21 04:58:00 +00:00
|
|
|
`INSERT OR IGNORE INTO user_achievement (user_id, achievement_tag, timestamp, match, title, text, points)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?);`,
|
|
|
|
recordData,
|
2019-01-05 17:49:19 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-01-05 17:49:19 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.events.emit(Events.getSystemEvents().UserAchievementEarned, {
|
|
|
|
user: info.client.user,
|
|
|
|
achievementTag: info.achievementTag,
|
|
|
|
points: info.details.points,
|
|
|
|
title: cleanTitle,
|
|
|
|
text: cleanText,
|
|
|
|
});
|
2019-01-05 17:49:19 +00:00
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-21 04:58:00 +00:00
|
|
|
display(info, interruptItems, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (interruptItems.local) {
|
|
|
|
UserInterruptQueue.queue(interruptItems.local, { clients: info.client });
|
2019-01-21 04:58:00 +00:00
|
|
|
}
|
2019-01-05 17:49:19 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (interruptItems.global) {
|
|
|
|
UserInterruptQueue.queue(interruptItems.global, { omit: info.client });
|
2019-01-21 04:58:00 +00:00
|
|
|
}
|
2019-01-05 17:49:19 +00:00
|
|
|
|
2019-01-21 04:58:00 +00:00
|
|
|
return cb(null);
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 17:41:04 +00:00
|
|
|
recordAndDisplayAchievement(info, cb) {
|
2019-01-21 04:58:00 +00:00
|
|
|
async.waterfall(
|
2019-01-06 17:41:04 +00:00
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2019-01-21 04:58:00 +00:00
|
|
|
return this.createAchievementInterruptItems(info, callback);
|
2019-01-06 17:41:04 +00:00
|
|
|
},
|
2019-01-21 04:58:00 +00:00
|
|
|
(interruptItems, callback) => {
|
|
|
|
this.record(info, interruptItems.local, err => {
|
|
|
|
return callback(err, interruptItems);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(interruptItems, callback) => {
|
|
|
|
return this.display(info, interruptItems, callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2019-01-06 17:41:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-07 04:56:12 +00:00
|
|
|
monitorUserStatEvents() {
|
2022-06-05 20:04:25 +00:00
|
|
|
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,
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.getSystemEvents().UserStatIncrement,
|
2019-01-07 04:56:12 +00:00
|
|
|
];
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.userStatEventListeners = this.events.addMultipleEventListener(
|
|
|
|
listenEvents,
|
|
|
|
userStatEvent => {
|
|
|
|
if (
|
|
|
|
[
|
|
|
|
UserProps.AchievementTotalCount,
|
|
|
|
UserProps.AchievementTotalPoints,
|
|
|
|
].includes(userStatEvent.statName)
|
|
|
|
) {
|
|
|
|
return;
|
2019-01-25 04:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
!_.isNumber(userStatEvent.statValue) &&
|
|
|
|
!_.isNumber(userStatEvent.statIncrementBy)
|
|
|
|
) {
|
|
|
|
return;
|
2019-01-25 04:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// :TODO: Make this code generic - find + return factory created object
|
|
|
|
const achievementTags = Object.keys(
|
|
|
|
_.pickBy(
|
|
|
|
_.get(this.config.get(), 'achievements', {}),
|
|
|
|
achievement => {
|
|
|
|
if (false === achievement.enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const acceptedTypes = [
|
|
|
|
Achievement.Types.UserStatSet,
|
|
|
|
Achievement.Types.UserStatInc,
|
|
|
|
Achievement.Types.UserStatIncNewVal,
|
|
|
|
];
|
|
|
|
return (
|
|
|
|
acceptedTypes.includes(achievement.type) &&
|
|
|
|
achievement.statName === userStatEvent.statName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
)
|
2019-01-25 04:53:31 +00:00
|
|
|
);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === achievementTags.length) {
|
|
|
|
return;
|
2019-01-25 04:53:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
achievementTags,
|
|
|
|
(achievementTag, nextAchievementTag) => {
|
|
|
|
const achievement = Achievement.factory(
|
|
|
|
this.getAchievementByTag(achievementTag)
|
|
|
|
);
|
|
|
|
if (!achievement) {
|
|
|
|
return nextAchievementTag(null);
|
|
|
|
}
|
2019-01-25 04:53:31 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const statValue = parseInt(
|
|
|
|
[
|
|
|
|
Achievement.Types.UserStatSet,
|
|
|
|
Achievement.Types.UserStatIncNewVal,
|
|
|
|
].includes(achievement.data.type)
|
|
|
|
? userStatEvent.statValue
|
|
|
|
: userStatEvent.statIncrementBy
|
|
|
|
);
|
|
|
|
if (isNaN(statValue)) {
|
|
|
|
return nextAchievementTag(null);
|
|
|
|
}
|
2019-01-25 04:53:31 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const [details, matchField, matchValue] =
|
|
|
|
achievement.getMatchDetails(statValue);
|
|
|
|
if (!details) {
|
|
|
|
return nextAchievementTag(null);
|
|
|
|
}
|
2019-01-25 04:53:31 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
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'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2019-01-05 17:49:19 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const info = {
|
|
|
|
achievementTag,
|
|
|
|
achievement,
|
|
|
|
details,
|
|
|
|
client,
|
|
|
|
matchField, // match - may be in odd format
|
|
|
|
matchValue, // actual value
|
|
|
|
achievedValue: matchField, // achievement value met
|
|
|
|
user: userStatEvent.user,
|
|
|
|
timestamp: moment(),
|
|
|
|
};
|
|
|
|
|
|
|
|
const achievementsInfo = [info];
|
|
|
|
return callback(null, achievementsInfo, info);
|
|
|
|
},
|
|
|
|
(achievementsInfo, basicInfo, callback) => {
|
|
|
|
if (true !== achievement.data.retroactive) {
|
|
|
|
return callback(null, achievementsInfo);
|
|
|
|
}
|
2019-01-31 04:01:49 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const index = achievement.matchKeys.findIndex(
|
|
|
|
v => v < matchField
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
-1 === index ||
|
|
|
|
!Array.isArray(achievement.matchKeys)
|
|
|
|
) {
|
|
|
|
return callback(null, achievementsInfo);
|
2019-01-10 03:07:59 +00:00
|
|
|
}
|
2019-01-06 17:41:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// For userStat, any lesser match keys(values) are also met. Example:
|
|
|
|
// matchKeys: [ 500, 200, 100, 20, 10, 2 ]
|
|
|
|
// ^---- we met here
|
|
|
|
// ^------------^ retroactive range
|
|
|
|
//
|
|
|
|
async.eachSeries(
|
|
|
|
achievement.matchKeys.slice(index),
|
|
|
|
(k, nextKey) => {
|
|
|
|
const [det, fld, val] =
|
|
|
|
achievement.getMatchDetails(k);
|
|
|
|
if (!det) {
|
|
|
|
return nextKey(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadAchievementHitCount(
|
|
|
|
userStatEvent.user,
|
|
|
|
achievementTag,
|
|
|
|
fld,
|
|
|
|
(err, count) => {
|
|
|
|
if (!err || (count && 0 === count)) {
|
|
|
|
achievementsInfo.push(
|
|
|
|
Object.assign({}, basicInfo, {
|
|
|
|
details: det,
|
|
|
|
matchField: fld,
|
|
|
|
achievedValue: fld,
|
|
|
|
matchValue: val,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextKey(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
return callback(null, achievementsInfo);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(achievementsInfo, callback) => {
|
|
|
|
// reverse achievementsInfo so we display smallest > largest
|
|
|
|
achievementsInfo.reverse();
|
|
|
|
|
|
|
|
async.eachSeries(
|
|
|
|
achievementsInfo,
|
|
|
|
(achInfo, nextAchInfo) => {
|
|
|
|
return this.recordAndDisplayAchievement(
|
|
|
|
achInfo,
|
|
|
|
err => {
|
|
|
|
return nextAchInfo(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
],
|
2019-01-31 04:01:49 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err && ErrorReasons.TooMany !== err.reasonCode) {
|
|
|
|
Log.warn(
|
|
|
|
{ error: err.message, userStatEvent },
|
|
|
|
'Error handling achievement for user stat event'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return nextAchievementTag(null); // always try the next, regardless
|
|
|
|
}
|
|
|
|
);
|
2019-01-05 17:49:19 +00:00
|
|
|
}
|
2019-01-31 04:01:49 +00:00
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|
|
|
|
);
|
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() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.userStatEventListeners) {
|
2019-01-07 04:56:12 +00:00
|
|
|
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 {
|
2022-06-05 20:04:25 +00:00
|
|
|
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,
|
2019-01-21 04:58:00 +00:00
|
|
|
//text : info.global ? info.details.globalText : info.details.text,
|
2022-06-05 20:04:25 +00:00
|
|
|
points: info.details.points,
|
|
|
|
achievedValue: info.achievedValue,
|
|
|
|
matchField: info.matchField,
|
|
|
|
matchValue: info.matchValue,
|
|
|
|
timestamp: moment(info.timestamp).format(info.dateTimeFormat),
|
|
|
|
boardName: Config().general.boardName,
|
2019-01-06 05:51:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getFormattedTextFor(info, textType, defaultSgr = '|07') {
|
2022-06-05 20:04:25 +00:00
|
|
|
const themeDefaults = _.get(
|
|
|
|
info.client.currentTheme,
|
|
|
|
'achievements.defaults',
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const wrap = input => {
|
2019-01-11 03:34:52 +00:00
|
|
|
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}`;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (formatOpts) {
|
2019-01-11 03:34:52 +00:00
|
|
|
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');
|
2022-06-05 20:04:25 +00:00
|
|
|
const text = this.getFormattedTextFor(info, 'text');
|
2019-01-04 04:03:08 +00:00
|
|
|
|
|
|
|
let globalText;
|
2022-06-05 20:04:25 +00:00
|
|
|
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}`) ||
|
2020-06-16 03:22:38 +00:00
|
|
|
_.get(this.config.get(), `art.${name}`);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!spec) {
|
2019-01-04 04:03:08 +00:00
|
|
|
return callback(null);
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
2019-01-04 04:03:08 +00:00
|
|
|
const getArtOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: spec,
|
|
|
|
client: this.client,
|
|
|
|
random: false,
|
2019-01-04 04:03:08 +00:00
|
|
|
};
|
|
|
|
getThemeArt(getArtOpts, (err, artInfo) => {
|
|
|
|
// ignore errors
|
|
|
|
return callback(artInfo ? artInfo.data : null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const interruptItems = {};
|
2022-06-05 20:04:25 +00:00
|
|
|
let itemTypes = ['local'];
|
|
|
|
if (globalText) {
|
2019-01-04 04:03:08 +00:00
|
|
|
itemTypes.push('global');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
itemTypes,
|
|
|
|
(itemType, nextItemType) => {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
callback => {
|
|
|
|
getArt(`${itemType}Header`, headerArt => {
|
|
|
|
return callback(null, headerArt);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(headerArt, callback) => {
|
|
|
|
getArt(`${itemType}Footer`, footerArt => {
|
|
|
|
return callback(null, headerArt, footerArt);
|
2019-01-06 05:51:16 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
|
|
|
(headerArt, footerArt, callback) => {
|
|
|
|
const itemText = 'global' === itemType ? globalText : text;
|
|
|
|
interruptItems[itemType] = {
|
|
|
|
title,
|
|
|
|
achievText: itemText,
|
|
|
|
text: `${title}\r\n${itemText}`,
|
|
|
|
pause: true,
|
|
|
|
};
|
|
|
|
if (headerArt || footerArt) {
|
|
|
|
const themeDefaults = _.get(
|
|
|
|
info.client.currentTheme,
|
|
|
|
'achievements.defaults',
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
const defaultContentsFormat = '{title}\r\n{message}';
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
);
|
2019-01-06 05:51:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const contents = pipeToAnsi(
|
|
|
|
stringFormat(contentsFormat, formatObj)
|
|
|
|
);
|
2019-01-06 05:51:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
interruptItems[itemType].contents = `${
|
|
|
|
headerArt || ''
|
|
|
|
}\r\n${contents}\r\n${footerArt || ''}`;
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return nextItemType(err);
|
2019-01-04 04:03:08 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, interruptItems);
|
|
|
|
}
|
|
|
|
);
|
2019-01-03 05:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 04:54:37 +00:00
|
|
|
let achievementsInstance;
|
|
|
|
|
|
|
|
function getAchievementsEarnedByUser(userId, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!achievementsInstance) {
|
2019-01-23 04:54:37 +00:00
|
|
|
return cb(Errors.UnexpectedState('Achievements not initialized'));
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDb.all(
|
|
|
|
`SELECT achievement_tag, timestamp, match, title, text, points
|
|
|
|
FROM user_achievement
|
|
|
|
WHERE user_id = ?
|
|
|
|
ORDER BY DATETIME(timestamp);`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[userId],
|
2019-01-23 04:54:37 +00:00
|
|
|
(err, rows) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-01-23 04:54:37 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const earned = rows
|
|
|
|
.map(row => {
|
|
|
|
const achievement = Achievement.factory(
|
|
|
|
achievementsInstance.getAchievementByTag(row.achievement_tag)
|
|
|
|
);
|
|
|
|
if (!achievement) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-23 04:54:37 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const earnedInfo = {
|
|
|
|
achievementTag: row.achievement_tag,
|
|
|
|
type: achievement.data.type,
|
|
|
|
retroactive: achievement.data.retroactive,
|
|
|
|
title: row.title,
|
|
|
|
text: row.text,
|
|
|
|
points: row.points,
|
|
|
|
timestamp: moment(row.timestamp),
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (earnedInfo.type) {
|
|
|
|
case [Achievement.Types.UserStatSet]:
|
|
|
|
case [Achievement.Types.UserStatInc]:
|
|
|
|
case [Achievement.Types.UserStatIncNewVal]:
|
|
|
|
earnedInfo.statName = achievement.data.statName;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-23 04:54:37 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return earnedInfo;
|
|
|
|
})
|
|
|
|
.filter(a => a); // remove any empty records (ie: no achievement.hjson entry exists anymore).
|
2019-01-23 04:54:37 +00:00
|
|
|
|
|
|
|
return cb(null, earned);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2019-01-03 05:13:42 +00:00
|
|
|
|
|
|
|
exports.moduleInitialize = (initInfo, cb) => {
|
2019-01-23 04:54:37 +00:00
|
|
|
achievementsInstance = new Achievements(initInfo.events);
|
2022-06-05 20:04:25 +00:00
|
|
|
achievementsInstance.init(err => {
|
|
|
|
if (err) {
|
2019-01-21 04:58:00 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
2019-01-03 05:13:42 +00:00
|
|
|
};
|