refactor: async/await rewards
parent
8b7f6566cc
commit
b110aec6ed
@ -1,123 +1,78 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
var async = require('async');
|
const db = require('../database');
|
||||||
var db = require('../database');
|
const plugins = require('../plugins');
|
||||||
var plugins = require('../plugins');
|
|
||||||
|
|
||||||
var rewards = module.exports;
|
const rewards = module.exports;
|
||||||
|
|
||||||
rewards.checkConditionAndRewardUser = function (uid, condition, method, callback) {
|
rewards.checkConditionAndRewardUser = async function (uid, condition, method) {
|
||||||
callback = callback || function () {};
|
const isActive = await isConditionActive(condition);
|
||||||
|
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
isConditionActive(condition, next);
|
|
||||||
},
|
|
||||||
function (isActive, next) {
|
|
||||||
if (!isActive) {
|
if (!isActive) {
|
||||||
return callback();
|
return;
|
||||||
}
|
|
||||||
getIDsByCondition(condition, next);
|
|
||||||
},
|
|
||||||
function (ids, next) {
|
|
||||||
getRewardDataByIDs(ids, next);
|
|
||||||
},
|
|
||||||
function (rewards, next) {
|
|
||||||
filterCompletedRewards(uid, rewards, next);
|
|
||||||
},
|
|
||||||
function (rewards, next) {
|
|
||||||
if (!rewards || !rewards.length) {
|
|
||||||
return callback();
|
|
||||||
}
|
|
||||||
|
|
||||||
async.filter(rewards, function (reward, next) {
|
|
||||||
if (!reward) {
|
|
||||||
return next(null, false);
|
|
||||||
}
|
}
|
||||||
|
const ids = await getIDsByCondition(condition);
|
||||||
checkCondition(reward, method, next);
|
let rewardData = await getRewardDataByIDs(ids);
|
||||||
}, function (err, eligible) {
|
rewardData = await filterCompletedRewards(uid, rewardData);
|
||||||
if (err || !eligible) {
|
rewardData = rewardData.filter(Boolean);
|
||||||
return next(false);
|
if (!rewardData || !rewardData.length) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
const eligible = await Promise.all(rewardData.map(reward => checkCondition(reward, method)));
|
||||||
giveRewards(uid, eligible, next);
|
const eligibleRewards = rewardData.filter((reward, index) => eligible[index]);
|
||||||
});
|
await giveRewards(uid, eligibleRewards);
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function isConditionActive(condition, callback) {
|
async function isConditionActive(condition) {
|
||||||
db.isSetMember('conditions:active', condition, callback);
|
return await db.isSetMember('conditions:active', condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIDsByCondition(condition, callback) {
|
async function getIDsByCondition(condition) {
|
||||||
db.getSetMembers('condition:' + condition + ':rewards', callback);
|
return await db.getSetMembers('condition:' + condition + ':rewards');
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterCompletedRewards(uid, rewards, callback) {
|
async function filterCompletedRewards(uid, rewards) {
|
||||||
async.waterfall([
|
const data = await db.getSortedSetRangeByScoreWithScores('uid:' + uid + ':rewards', 0, -1, 1, '+inf');
|
||||||
function (next) {
|
const userRewards = {};
|
||||||
db.getSortedSetRangeByScoreWithScores('uid:' + uid + ':rewards', 0, -1, 1, '+inf', next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
var userRewards = {};
|
|
||||||
|
|
||||||
data.forEach(function (obj) {
|
data.forEach(function (obj) {
|
||||||
userRewards[obj.value] = parseInt(obj.score, 10);
|
userRewards[obj.value] = parseInt(obj.score, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
rewards = rewards.filter(function (reward) {
|
return rewards.filter(function (reward) {
|
||||||
if (!reward) {
|
if (!reward) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var claimable = parseInt(reward.claimable, 10);
|
const claimable = parseInt(reward.claimable, 10);
|
||||||
return claimable === 0 || (!userRewards[reward.id] || userRewards[reward.id] < reward.claimable);
|
return claimable === 0 || (!userRewards[reward.id] || userRewards[reward.id] < reward.claimable);
|
||||||
});
|
});
|
||||||
|
|
||||||
next(null, rewards);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRewardDataByIDs(ids, callback) {
|
async function getRewardDataByIDs(ids) {
|
||||||
db.getObjects(ids.map(function (id) {
|
return await db.getObjects(ids.map(id => 'rewards:id:' + id));
|
||||||
return 'rewards:id:' + id;
|
|
||||||
}), callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRewardsByRewardData(rewards, callback) {
|
async function getRewardsByRewardData(rewards) {
|
||||||
db.getObjects(rewards.map(function (reward) {
|
return await db.getObjects(rewards.map(reward => 'rewards:id:' + reward.id + ':rewards'));
|
||||||
return 'rewards:id:' + reward.id + ':rewards';
|
|
||||||
}), callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkCondition(reward, method, callback) {
|
async function checkCondition(reward, method) {
|
||||||
async.waterfall([
|
if (method.constructor && method.constructor.name !== 'AsyncFunction') {
|
||||||
function (next) {
|
method = util.promisify(method);
|
||||||
method(next);
|
}
|
||||||
},
|
const value = await method();
|
||||||
function (value, next) {
|
const bool = await plugins.fireHook('filter:rewards.checkConditional:' + reward.conditional, { left: value, right: reward.value });
|
||||||
plugins.fireHook('filter:rewards.checkConditional:' + reward.conditional, { left: value, right: reward.value }, next);
|
return bool;
|
||||||
},
|
|
||||||
function (bool, next) {
|
|
||||||
next(null, bool);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function giveRewards(uid, rewards, callback) {
|
async function giveRewards(uid, rewards) {
|
||||||
async.waterfall([
|
const rewardData = await getRewardsByRewardData(rewards);
|
||||||
function (next) {
|
await Promise.all(rewards.map(async function (reward) {
|
||||||
getRewardsByRewardData(rewards, next);
|
|
||||||
},
|
|
||||||
function (rewardData, next) {
|
|
||||||
async.each(rewards, function (reward, next) {
|
|
||||||
plugins.fireHook('action:rewards.award:' + reward.rid, { uid: uid, reward: rewardData[rewards.indexOf(reward)] });
|
plugins.fireHook('action:rewards.award:' + reward.rid, { uid: uid, reward: rewardData[rewards.indexOf(reward)] });
|
||||||
db.sortedSetIncrBy('uid:' + uid + ':rewards', 1, reward.id, next);
|
await db.sortedSetIncrBy('uid:' + uid + ':rewards', 1, reward.id);
|
||||||
}, next);
|
}));
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require('../promisify')(rewards);
|
||||||
|
Loading…
Reference in New Issue