2014-10-27 04:06:41 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Config = require('./config.js').config;
|
|
|
|
var art = require('./art.js');
|
|
|
|
var miscUtil = require('./misc_util.js');
|
2015-04-04 20:41:04 +00:00
|
|
|
var Log = require('./logger.js').log;
|
2015-04-16 04:46:45 +00:00
|
|
|
|
2014-10-27 04:06:41 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var paths = require('path');
|
|
|
|
var async = require('async');
|
2015-04-16 04:46:45 +00:00
|
|
|
var _ = require('lodash');
|
2014-10-27 04:06:41 +00:00
|
|
|
|
|
|
|
exports.getThemeInfo = getThemeInfo;
|
|
|
|
exports.getThemeArt = getThemeArt;
|
2014-10-30 04:23:44 +00:00
|
|
|
exports.getRandomTheme = getRandomTheme;
|
2014-10-29 11:30:20 +00:00
|
|
|
exports.initAvailableThemes = initAvailableThemes;
|
2014-11-05 06:50:42 +00:00
|
|
|
exports.displayThemeArt = displayThemeArt;
|
2014-10-27 04:06:41 +00:00
|
|
|
|
|
|
|
function getThemeInfo(themeID, cb) {
|
2014-10-29 11:30:20 +00:00
|
|
|
var path = paths.join(Config.paths.themes, themeID, 'theme_info.json');
|
2014-10-27 04:06:41 +00:00
|
|
|
|
|
|
|
fs.readFile(path, function onData(err, data) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
try {
|
2015-04-16 04:46:45 +00:00
|
|
|
// :TODO: strip comments/etc. ala menu.json
|
2014-10-27 04:06:41 +00:00
|
|
|
var info = JSON.parse(data);
|
2014-10-30 04:23:44 +00:00
|
|
|
cb(null, info);
|
2014-10-27 04:06:41 +00:00
|
|
|
} catch(e) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
var availableThemes = {};
|
2014-10-30 04:23:44 +00:00
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
function initAvailableThemes(cb) {
|
2014-10-30 04:23:44 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getDir(callback) {
|
2014-10-31 04:59:21 +00:00
|
|
|
fs.readdir(Config.paths.themes, function onReadDir(err, files) {
|
2014-10-30 04:23:44 +00:00
|
|
|
callback(err, files);
|
|
|
|
});
|
|
|
|
},
|
2014-10-31 04:59:21 +00:00
|
|
|
function filterFiles(files, callback) {
|
2014-10-30 04:23:44 +00:00
|
|
|
var filtered = files.filter(function onFilter(file) {
|
2014-10-29 11:30:20 +00:00
|
|
|
return fs.statSync(paths.join(Config.paths.themes, file)).isDirectory();
|
2014-10-30 04:23:44 +00:00
|
|
|
});
|
|
|
|
callback(null, filtered);
|
|
|
|
},
|
|
|
|
function populateAvailable(filtered, callback) {
|
|
|
|
filtered.forEach(function onTheme(themeId) {
|
|
|
|
getThemeInfo(themeId, function onThemeInfo(err, info) {
|
|
|
|
if(!err) {
|
|
|
|
if(!availableThemes) {
|
|
|
|
availableThemes = {};
|
|
|
|
}
|
|
|
|
availableThemes[themeId] = info;
|
2015-04-04 20:41:04 +00:00
|
|
|
Log.debug( { info : info }, 'Theme loaded');
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-10-31 04:59:21 +00:00
|
|
|
callback(null);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function onComplete(err) {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:30:20 +00:00
|
|
|
cb(null, availableThemes.length);
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getRandomTheme(cb) {
|
2014-11-10 04:24:09 +00:00
|
|
|
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
|
2014-10-29 11:30:20 +00:00
|
|
|
var themeIds = Object.keys(availableThemes);
|
2014-10-30 04:23:44 +00:00
|
|
|
cb(null, themeIds[Math.floor(Math.random() * themeIds.length)]);
|
|
|
|
} else {
|
2014-10-29 11:30:20 +00:00
|
|
|
cb(new Error('No themes available'));
|
2014-10-30 04:23:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 04:06:41 +00:00
|
|
|
function getThemeArt(name, themeID, options, cb) {
|
|
|
|
// allow options to be optional
|
2015-04-16 04:46:45 +00:00
|
|
|
if(_.isUndefined(cb)) {
|
2014-10-27 04:06:41 +00:00
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// set/override some options
|
|
|
|
options.asAnsi = true;
|
|
|
|
options.readSauce = true; // can help with encoding
|
|
|
|
options.random = miscUtil.valueWithDefault(options.random, true);
|
2014-10-29 11:30:20 +00:00
|
|
|
options.basePath = paths.join(Config.paths.themes, themeID);
|
2014-10-27 04:06:41 +00:00
|
|
|
|
2014-11-10 04:24:09 +00:00
|
|
|
art.getArt(name, options, function onThemeArt(err, artInfo) {
|
2014-10-27 04:06:41 +00:00
|
|
|
if(err) {
|
2014-10-29 11:30:20 +00:00
|
|
|
// try fallback of art directory
|
2014-10-27 04:06:41 +00:00
|
|
|
options.basePath = Config.paths.art;
|
2014-11-10 04:24:09 +00:00
|
|
|
art.getArt(name, options, function onFallbackArt(err, artInfo) {
|
2014-10-27 04:06:41 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2014-11-10 04:24:09 +00:00
|
|
|
cb(null, artInfo);
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-11-10 04:24:09 +00:00
|
|
|
cb(null, artInfo);
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|
|
|
|
});
|
2014-11-05 06:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function displayThemeArt(name, client, cb) {
|
2014-11-10 04:24:09 +00:00
|
|
|
getThemeArt(name, client.user.properties.art_theme_id, function onArt(err, artInfo) {
|
2014-11-05 06:50:42 +00:00
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
2014-11-10 04:24:09 +00:00
|
|
|
var iceColors = false;
|
|
|
|
if(artInfo.sauce && artInfo.sauce.ansiFlags) {
|
|
|
|
if(artInfo.sauce.ansiFlags & (1 << 0)) {
|
|
|
|
iceColors = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
art.display( { art : artInfo.data, client : client, iceColors : iceColors }, function onDisplayed(err, mci) {
|
2015-03-24 05:12:19 +00:00
|
|
|
cb(err, mci, artInfo);
|
2014-11-05 06:50:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-10-27 04:06:41 +00:00
|
|
|
}
|