* Code cleanup. WIP theme stuff. Better CPR handling, etc.

This commit is contained in:
NuSkooler 2014-10-29 22:23:44 -06:00
parent 7cfe72d53b
commit 1ef9a4a1ce
11 changed files with 147 additions and 125 deletions

View file

@ -10,6 +10,7 @@ var async = require('async');
exports.getThemeInfo = getThemeInfo;
exports.getThemeArt = getThemeArt;
exports.getRandomTheme = getRandomTheme;
// getThemeInfo(themeName)
@ -29,7 +30,7 @@ function getThemeInfo(themeID, cb) {
} else {
try {
var info = JSON.parse(data);
return info;
cb(null, info);
} catch(e) {
cb(err);
}
@ -37,6 +38,70 @@ function getThemeInfo(themeID, cb) {
});
}
var availableThemes;
function loadAvailableThemes(cb) {
// lazy init
async.waterfall(
[
function getDir(callback) {
fs.readdir(Config.paths.art, function onReadDir(err, files) {
callback(err, files);
});
},
function filterFiles(files, callback) {
var filtered = files.filter(function onFilter(file) {
return fs.statSync(paths.join(Config.paths.art, file)).isDirectory();
});
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;
}
callback(null);
});
});
}
],
function onComplete(err) {
if(err) {
cb(err);
return;
}
if(!availableThemes) {
cb(new Error('No themes found'));
return;
}
cb(null);
}
);
}
function getRandomTheme(cb) {
var themeIds;
if(availableThemes) {
themeIds = Object.keys(availableThemes);
cb(null, themeIds[Math.floor(Math.random() * themeIds.length)]);
} else {
loadAvailableThemes(function onThemes(err) {
if(err) {
cb(err);
} else {
themeIds = Object.keys(availableThemes);
cb(null, themeIds[Math.floor(Math.random() * themeIds.length)]);
}
});
}
}
function getThemeArt(name, themeID, options, cb) {
// allow options to be optional
if(typeof cb === 'undefined') {