You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.1 KiB
JavaScript

"use strict";
8 years ago
var async = require('async');
var plugins = require('../plugins');
var db = require('../database');
8 years ago
var rewards = module.exports;
rewards.save = function (data, callback) {
8 years ago
async.each(data, function save(data, next) {
if (!Object.keys(data.rewards).length) {
return next();
}
var rewardsData = data.rewards;
delete data.rewards;
8 years ago
async.waterfall([
function (next) {
if (!parseInt(data.id, 10)) {
db.incrObjectField('global', 'rewards:id', next);
} else {
next(null, data.id);
}
},
function (rid, next) {
data.id = rid;
async.series([
function (next) {
rewards.delete(data, next);
},
function (next) {
db.setAdd('rewards:list', data.id, next);
},
function (next) {
db.setObject('rewards:id:' + data.id, data, next);
},
function (next) {
db.setObject('rewards:id:' + data.id + ':rewards', rewardsData, next);
},
8 years ago
], next);
},
8 years ago
], next);
8 years ago
}, function (err) {
if (err) {
return callback(err);
}
10 years ago
saveConditions(data, callback);
});
};
rewards.delete = function (data, callback) {
10 years ago
async.parallel([
function (next) {
10 years ago
db.setRemove('rewards:list', data.id, next);
},
function (next) {
10 years ago
db.delete('rewards:id:' + data.id, next);
},
function (next) {
10 years ago
db.delete('rewards:id:' + data.id + ':rewards', next);
},
10 years ago
], callback);
};
rewards.get = function (callback) {
async.parallel({
active: getActiveRewards,
conditions: function (next) {
plugins.fireHook('filter:rewards.conditions', [], next);
},
conditionals: function (next) {
plugins.fireHook('filter:rewards.conditionals', [], next);
},
rewards: function (next) {
plugins.fireHook('filter:rewards.rewards', [], next);
},
}, callback);
};
10 years ago
function saveConditions(data, callback) {
8 years ago
var rewardsPerCondition = {};
async.waterfall([
function (next) {
db.delete('conditions:active', next);
},
function (next) {
var conditions = [];
10 years ago
8 years ago
data.forEach(function (reward) {
conditions.push(reward.condition);
rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || [];
rewardsPerCondition[reward.condition].push(reward.id);
});
10 years ago
8 years ago
db.setAdd('conditions:active', conditions, next);
},
function (next) {
async.each(Object.keys(rewardsPerCondition), function (condition, next) {
db.setAdd('condition:' + condition + ':rewards', rewardsPerCondition[condition], next);
}, next);
},
8 years ago
], function (err) {
callback(err);
10 years ago
});
}
function getActiveRewards(callback) {
var activeRewards = [];
function load(id, next) {
async.parallel({
main: function (next) {
db.getObject('rewards:id:' + id, next);
},
rewards: function (next) {
db.getObject('rewards:id:' + id + ':rewards', next);
},
}, function (err, data) {
10 years ago
if (data.main) {
data.main.disabled = data.main.disabled === 'true';
10 years ago
data.main.rewards = data.rewards;
activeRewards.push(data.main);
}
next(err);
});
}
db.getSetMembers('rewards:list', function (err, rewards) {
if (err) {
return callback(err);
}
async.eachSeries(rewards, load, function (err) {
callback(err, activeRewards);
});
});
}