Skip to content

Node.js based forum software built for the modern web

rewards: big feature sprint

psychobunny949fcbbf11b6
diff --git a/src/rewards/admin.js b/src/rewards/admin.js
index dd1acf6180..67271d2695 100644
--- a/src/rewards/admin.js
+++ b/src/rewards/admin.js
@@ -74,7 +74,7 @@ function saveConditions(data, callback) {
data.forEach(function(reward) {
conditions.push(reward.condition);
rewardsPerCondition[reward.condition] = rewardsPerCondition[reward.condition] || [];
- rewardsPerCondition[reward.condition].push(reward.rid);
+ rewardsPerCondition[reward.condition].push(reward.id);
});
db.setAdd('conditions:active', conditions, callback);
diff --git a/src/rewards/index.js b/src/rewards/index.js
index ba68fae92c..821d093d20 100644
--- a/src/rewards/index.js
+++ b/src/rewards/index.js
@@ -1,7 +1,100 @@
"use strict";
-var rewards = {};
+var rewards = {},
+ db = require('../database'),
+ plugins = require('../plugins'),
+ async = require('async');
+rewards.checkConditionAndRewardUser = function(uid, condition, method, callback) {
+ async.waterfall([
+ function(next) {
+ isConditionActive(condition, function(err, isActive) {
+ if (!isActive) {
+ return back(err);
+ }
+
+ next(err);
+ });
+ },
+ function(next) {
+ getIDsByCondition(condition, function(err, ids) {
+ next(err, ids);
+ });
+ },
+ function(next, ids) {
+ filterIncompleteIDs(uid, ids, function(err, filtered) {
+ if (!filtered || !filtered.length) {
+ return back(err);
+ }
+
+ next(err, filtered);
+ });
+ },
+ function(next, ids) {
+ getRewardDataByIDs(ids, next);
+ },
+ function(next, rewards) {
+ async.filter(rewards, function(reward, next) {
+ checkCondition(reward, method, next);
+ }, function(err, eligible) {
+ giveRewards(uid, eligible, next);
+ });
+ }
+ ], back);
+
+
+ function back(err) {
+ if (typeof callback === 'function') {
+ callback(err);
+ }
+ }
+};
+
+function isConditionActive(condition, callback) {
+ db.isSetMember('conditions:active', condition, callback);
+}
+
+function getIDsByCondition(condition, callback) {
+ db.getObject('condition:' + condition + ':rewards', callback);
+}
+
+function filterIncompleteIDs(ids, callback) {
+ // todo
+ callback(false, ids);
+}
+
+function getRewardDataByIDs(ids, callback) {
+ ids.map(function(id) {
+ return 'rewards:id:' + id;
+ });
+
+ db.getObjects(ids, callback);
+}
+
+function getRewardsByRewardData(rewards, callback) {
+ rewards.map(function(reward) {
+ return 'rewards:id:' + reward.id + ':rewards';
+ });
+
+ db.getObjects(rewards, callback);
+}
+
+function checkCondition(reward, method, callback) {
+ method(function(err, value) {
+ plugins.fireHook('filter:rewards.checkConditional:' + reward.conditional, {left: value, right: reward.value}, callback);
+ });
+}
+
+function giveRewards(uid, rewards, callback) {
+ getRewardsByRewardData(rewards, function(err, rewardData) {
+ async.each(rewards, function(reward, next) {
+ var index = rewards.indexOf(reward);
+
+ plugins.fireHook('action:rewards.award:' + reward.rid, {uid: uid, reward: rewardData[index]});
+ });
+ });
+}
+
module.exports = rewards;
\ No newline at end of file