From 00e15a6757a134afc4fdec2c0d5dc6edbb88f13f Mon Sep 17 00:00:00 2001 From: OldHawk Date: Thu, 24 May 2018 18:55:07 +0800 Subject: [PATCH] feat(score): transfer users score with fixed ratio to inviter per month first day --- config/env/torrents.js | 24 ++++++++++++++------ config/lib/cron-job.js | 50 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/config/env/torrents.js b/config/env/torrents.js index 885aa31f..10344861 100644 --- a/config/env/torrents.js +++ b/config/env/torrents.js @@ -428,21 +428,31 @@ module.exports = { * * score system settings * - * @levelStep: value of each level step, default 500 - * @scoreLogDays: setting of days to write score detail log to db, because the data is too more too big, do not to set a big value - * @action: score change action list - * @name: action name - * @value: action score value - * @enable: action enable status, if false, system will not change user`s score at this action - * NOTE: ENABLE VALUE OF DEFAULTACTION MUST BE TRUE + * @levelStep: value of each level step, default 500 + * @scoreLogDays: setting of days to write score detail log to db, because the data is too more too big, do not to set a big value + * @transfer: setting of transfer score to inviter per month + * @enable: setting whether to enable transfer + * @transRatio: setting transfer ratio, the user`s score of this ratio will be subtract and add into the inviter`s account + * @action: score change action list + * @name: action name + * @value: action score value + * @enable: action enable status, if false, system will not change user`s score at this action + * NOTE: ENABLE VALUE OF DEFAULTACTION MUST BE TRUE */ score: { levelStep: 1000, scoreLogDays: 10, + transfer: { + enable: true, + transRatio: 0.05 + }, action: { defaultAction: {name: 'defaultAction', value: 0, enable: true}, adminModify: {name: 'adminModify', value: 0, enable: true}, + transferScoreIntoInviterFrom: {name: 'transferScoreIntoInviterFrom', value: 0, enable: true}, + transferScoreIntoInviterTo: {name: 'transferScoreIntoInviterTo', value: 0, enable: true}, + uploadTorrent: {name: 'uploadTorrent', value: 50, enable: true}, uploadTorrentBeDeleted: {name: 'uploadTorrentBeDeleted', value: -50, enable: true}, uploadTorrentBeRecommend: {name: 'uploadTorrentBeRecommend', value: 10, enable: true}, diff --git a/config/lib/cron-job.js b/config/lib/cron-job.js index 0f2fd9b0..12e7c97e 100644 --- a/config/lib/cron-job.js +++ b/config/lib/cron-job.js @@ -20,6 +20,7 @@ var path = require('path'), UserMonthsLog = mongoose.model('UserMonthsLog'), ScoreLog = mongoose.model('ScoreLog'), Trace = mongoose.model('Trace'), + scoreUpdate = require(path.resolve('./config/lib/score')).update, backup = require('mongodb-backup'); var mtDebug = require(path.resolve('./config/lib/debug')); @@ -89,6 +90,10 @@ module.exports = function (app) { cronJobs.push(countUsersHnrWarning()); } + if (scoreConfig.transfer.enable) { + cronJobs.push(transferUserScoreToInviter()); + } + if (supportConfig.mailTicketSupportService) { cronJobs.push(listenServiceEmail()); } @@ -339,6 +344,51 @@ function countUsersHnrWarning() { return cronJob; } +/** + * transferUserScoreToInviter + */ +function transferUserScoreToInviter() { + var cronJob = new CronJob({ + cronTime: '00 00 2 1 * *', + onTick: function () { + logger.info(chalk.green('transferUserScoreToInviter: process!')); + + var mom = moment().utcOffset(appConfig.dbTimeZone); + var y = mom.get('year'); + var m = mom.get('month') + 1; + + UserMonthsLog.find({ + year: y, + month: m - 1 + }).populate({ + path: 'user', + select: 'username displayName profileImageURL isVip score uploaded downloaded invited_by', + populate: { + path: 'invited_by', + select: 'username displayName profileImageURL isVip score uploaded downloaded' + } + }).exec(function (err, logs) { + if (logs) { + logs.forEach(function (l) { + if (l.score > 0 && l.user.invited_by) { + var transValue = Math.round(l.score * scoreConfig.transfer.transRatio * 100) / 100; + + scoreUpdate(undefined, l.user, scoreConfig.action.transferScoreIntoInviterFrom, -(transValue)); + scoreUpdate(undefined, l.user.invited_by, scoreConfig.action.transferScoreIntoInviterTo, transValue); + } + }); + } + }); + }, + start: false, + timeZone: appConfig.cronTimeZone + }); + + cronJob.start(); + + return cronJob; +} + /** * listenServiceEmail */