Use new ConfigLoader for achievements

This commit is contained in:
Bryan Ashby 2020-06-15 21:22:38 -06:00
parent 8808447343
commit 683075f4ca
No known key found for this signature in database
GPG Key ID: B49EB437951D2542
1 changed files with 35 additions and 41 deletions

View File

@ -4,10 +4,8 @@
// ENiGMA½ // ENiGMA½
const Events = require('./events.js'); const Events = require('./events.js');
const Config = require('./config.js').get; const Config = require('./config.js').get;
const { const ConfigLoader = require('./config_loader');
getConfigPath, const { getConfigPath } = require('./config_util');
getFullConfig,
} = require('./config_util.js');
const UserDb = require('./database.js').dbs.user; const UserDb = require('./database.js').dbs.user;
const { const {
getISOTimestampString getISOTimestampString
@ -29,13 +27,11 @@ const {
const stringFormat = require('./string_format.js'); const stringFormat = require('./string_format.js');
const StatLog = require('./stat_log.js'); const StatLog = require('./stat_log.js');
const Log = require('./logger.js').log; const Log = require('./logger.js').log;
const ConfigCache = require('./config_cache.js');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
const async = require('async'); const async = require('async');
const moment = require('moment'); const moment = require('moment');
const paths = require('path');
exports.getAchievementsEarnedByUser = getAchievementsEarnedByUser; exports.getAchievementsEarnedByUser = getAchievementsEarnedByUser;
@ -136,63 +132,61 @@ class UserStatAchievement extends Achievement {
class Achievements { class Achievements {
constructor(events) { constructor(events) {
this.events = events; this.events = events;
this.enabled = false;
} }
getAchievementByTag(tag) { getAchievementByTag(tag) {
return this.achievementConfig.achievements[tag]; return this.config.get().achievements[tag];
} }
isEnabled() { isEnabled() {
return !_.isUndefined(this.achievementConfig); return this.enabled;
} }
init(cb) { init(cb) {
let achievementConfigPath = _.get(Config(), 'general.achievementFile'); const configPath = this._getConfigPath();
if(!achievementConfigPath) { if (!configPath) {
Log.info('Achievements are not configured'); Log.info('Achievements are not configured');
return cb(null); return cb(null);
} }
achievementConfigPath = getConfigPath(achievementConfigPath); // qualify
const configLoaded = (achievementConfig) => { const configLoaded = () => {
if(true !== achievementConfig.enabled) { if(true !== this.config.get().enabled) {
Log.info('Achievements are not enabled'); Log.info('Achievements are not enabled');
this.enabled = false;
this.stopMonitoringUserStatEvents(); this.stopMonitoringUserStatEvents();
delete this.achievementConfig;
} else { } else {
Log.info('Achievements are enabled'); Log.info('Achievements are enabled');
this.achievementConfig = achievementConfig; this.enabled = true;
this.monitorUserStatEvents(); this.monitorUserStatEvents();
} }
}; };
const changed = ( { fileName, fileRoot } ) => { const configOptions = {
const reCachedPath = paths.join(fileRoot, fileName); onReload : err => {
if(reCachedPath === achievementConfigPath) { if (!err) {
getFullConfig(achievementConfigPath, (err, achievementConfig) => { configLoaded();
if(err) { }
return Log.error( { error : err.message }, 'Failed to reload achievement config from cache'); },
}
configLoaded(achievementConfig);
});
}
}; };
ConfigCache.getConfigWithOptions( this.config = new ConfigLoader(configOptions);
{ this.config.init(configPath, err => {
filePath : achievementConfigPath, if (err) {
forceReCache : true, return cb(err);
callback : changed,
},
(err, achievementConfig) => {
if(err) {
return cb(err);
}
configLoaded(achievementConfig);
return cb(null);
} }
);
configLoaded();
return cb(null);
});
}
_getConfigPath() {
const path = _.get(Config(), 'general.achievementFile');
if(!path) {
return;
}
return getConfigPath(path); // qualify
} }
loadAchievementHitCount(user, achievementTag, field, cb) { loadAchievementHitCount(user, achievementTag, field, cb) {
@ -298,7 +292,7 @@ class Achievements {
// :TODO: Make this code generic - find + return factory created object // :TODO: Make this code generic - find + return factory created object
const achievementTags = Object.keys(_.pickBy( const achievementTags = Object.keys(_.pickBy(
_.get(this.achievementConfig, 'achievements', {}), _.get(this.config.get(), 'achievements', {}),
achievement => { achievement => {
if(false === achievement.enabled) { if(false === achievement.enabled) {
return false; return false;
@ -498,7 +492,7 @@ class Achievements {
const spec = const spec =
_.get(info.details, `art.${name}`) || _.get(info.details, `art.${name}`) ||
_.get(info.achievement, `art.${name}`) || _.get(info.achievement, `art.${name}`) ||
_.get(this.achievementConfig, `art.${name}`); _.get(this.config.get(), `art.${name}`);
if(!spec) { if(!spec) {
return callback(null); return callback(null);
} }