feat(users): calculate the user hourly scores income

This commit is contained in:
OldHawk
2018-06-03 23:51:31 +08:00
parent 717acfc98b
commit b9900a9b7c
23 changed files with 385 additions and 47 deletions

View File

@@ -6,10 +6,12 @@
.controller('ScoreController', ScoreController);
ScoreController.$inject = ['$rootScope', '$scope', '$state', '$translate', '$timeout', 'Authentication', '$window', 'ScoreLevelService', 'getStorageLangService',
'MeanTorrentConfig', 'ModalConfirmService', 'NotifycationService', 'InvitationsService', '$templateRequest', 'marked', '$filter'];
'MeanTorrentConfig', 'ModalConfirmService', 'NotifycationService', 'InvitationsService', '$templateRequest', 'marked', '$filter', 'PeersService', 'moment',
'DebugConsoleService'];
function ScoreController($rootScope, $scope, $state, $translate, $timeout, Authentication, $window, ScoreLevelService, getStorageLangService, MeanTorrentConfig,
ModalConfirmService, NotifycationService, InvitationsService, $templateRequest, marked, $filter) {
ModalConfirmService, NotifycationService, InvitationsService, $templateRequest, marked, $filter, PeersService, moment,
mtDebug) {
var vm = this;
vm.scoreConfig = MeanTorrentConfig.meanTorrentConfig.score;
vm.inviteConfig = MeanTorrentConfig.meanTorrentConfig.invite;
@@ -111,7 +113,10 @@
closeButtonText: $translate.instant('EXCHANGE_INVITATION_CONFIRM_CANCEL'),
actionButtonText: $translate.instant('EXCHANGE_INVITATION_CONFIRM_OK'),
headerText: $translate.instant('EXCHANGE_INVITATION_CONFIRM_HEADER_TEXT'),
bodyText: $translate.instant('EXCHANGE_INVITATION_CONFIRM_BODY_TEXT', {score: vm.inviteConfig.scoreExchange, hours: vm.inviteConfig.expires_str})
bodyText: $translate.instant('EXCHANGE_INVITATION_CONFIRM_BODY_TEXT', {
score: vm.inviteConfig.scoreExchange,
hours: vm.inviteConfig.expires_str
})
};
ModalConfirmService.showModal({}, modalOptions)
@@ -138,5 +143,212 @@
}
});
};
/**
* getMyPeers
*/
vm.getMyPeers = function () {
PeersService.getMyPeers(function (items) {
vm.myPeers = items;
mtDebug.info(items);
vm.countByStatus = $filter('countBy')(vm.myPeers, 'peer_status');
vm.groupByStatus = $filter('groupBy')(vm.myPeers, 'peer_status');
vm.sumCUSpeed = vm.myPeers.map(function (x) {
return x.peer_cuspeed;
}).reduce(function (a, b) {
return a + b;
});
vm.sumCDSpeed = vm.myPeers.map(function (x) {
return x.peer_cdspeed;
}).reduce(function (a, b) {
return a + b;
});
});
};
/**
* getMarkedCurrDownloadingString
* @returns {*}
*/
vm.getMarkedCurrDownloadingString = function () {
if (vm.countByStatus) {
var tmp = $translate.instant('CURR_LEECHING_TORRENTS', {count_leech: vm.countByStatus.leecher || 0});
return marked(tmp, {sanitize: false});
}
};
/**
* getMarkedCurrSeedingString
* @returns {*}
*/
vm.getMarkedCurrSeedingString = function () {
if (vm.countByStatus) {
var tmp = $translate.instant('CURR_SEEDING_TORRENTS', {count_seed: vm.countByStatus.seeder || 0});
return marked(tmp, {sanitize: false});
}
};
/**
* getMarkedVipStatusString
* @returns {*}
*/
vm.getMarkedVipStatusString = function () {
var tmp = $translate.instant('CURR_VIP_STATE', {vip_status: vm.user.isVip ? 'VALUE_TRUE' : 'VALUE_FALSE'});
return marked(tmp, {sanitize: false});
};
/**
* getMarkedCurrUpSpeedString
* @returns {*}
*/
vm.getMarkedCurrUpSpeedString = function () {
var tmp = $translate.instant('CURR_UP_TOTAL_SPEED', {up_speed: vm.sumCUSpeed});
return marked(tmp, {sanitize: false});
};
/**
* getMarkedCurrDownSpeedString
* @returns {*}
*/
vm.getMarkedCurrDownSpeedString = function () {
var tmp = $translate.instant('CURR_DOWN_TOTAL_SPEED', {down_speed: vm.sumCDSpeed});
return marked(tmp, {sanitize: false});
};
/**
* getMarkedCurrScoreString
* @returns {*}
*/
vm.getMarkedCurrScoreString = function () {
var timedScore = getTimedScore();
var speedScore = getSpeedScore();
var seedScoreTotal = timedScore + speedScore;
seedScoreTotal = Math.round(seedScoreTotal * 100) / 100;
var tmp = $translate.instant('CURR_SCORE_INCOME_HOURS', {score_hour: seedScoreTotal});
return marked(tmp, {sanitize: false});
};
/**
* getTimedScore
* @returns {number}
*/
function getTimedScore() {
var timedScore = 0;
var action = vm.scoreConfig.action.seedTimed;
var slAction = vm.scoreConfig.action.seedSeederAndLife;
if (action.enable && vm.groupByStatus) {
angular.forEach(vm.groupByStatus.seeder, function (seed) {
if (seed.torrent.torrent_status === 'reviewed') {
var seedUnit = (60 * 60 * 1000) / action.additionTime;
var seedScore = seedUnit * action.timedValue;
if (seedScore > 0) {
//vip addition
if (action.vipRatio && vm.user.isVip) {
seedScore = seedScore * action.vipRatio;
}
if (slAction.enable) {
//torrent seeders count addition
if (seed.torrent.torrent_seeds <= slAction.seederCount) {
var seederUnit = slAction.seederBasicRatio + slAction.seederCoefficient * (slAction.seederCount - seed.torrent.torrent_seeds + 1);
seedScore = seedScore * seederUnit;
}
//torrent life addition
var life = moment(Date.now()) - moment(seed.torrent.createdat);
var days = life / (60 * 60 * 1000 * 24);
var lifeUnit = slAction.lifeBasicRatio + slAction.lifeCoefficientOfDay * days;
lifeUnit = lifeUnit > slAction.lifeMaxRatio ? slAction.lifeMaxRatio : lifeUnit;
seedScore = seedScore * lifeUnit;
}
}
timedScore = timedScore + seedScore;
}
});
}
return timedScore;
}
/**
* getSpeedScore
* @returns {number}
*/
function getSpeedScore() {
var totalScore = 0;
var action = vm.scoreConfig.action.seedUpDownload;
var slAction = vm.scoreConfig.action.seedSeederAndLife;
if (action.enable && vm.myPeers) {
angular.forEach(vm.myPeers, function (peer) {
var udScore = 0;
var upUnitScore = 1;
var downUnitScore = 1;
var seederUnit = 1;
var lifeUnit = 1;
var usize = peer.peer_cuspeed * 60 * 60;
var dsize = peer.peer_cdspeed * 60 * 60;
var uploadScore = 0;
var downloadScore = 0;
if (usize > 0 && action.uploadEnable) {
if (peer.torrent.torrent_size > action.additionSize) {
upUnitScore = Math.sqrt(peer.torrent.torrent_size / action.additionSize);
}
var upScore = usize / action.perlSize;
uploadScore = upUnitScore * action.uploadValue * upScore;
//uploader addition
if (vm.user._id == peer.torrent.user) {
uploadScore = uploadScore * action.uploaderRatio;
}
}
if (dsize > 0 && action.downloadEnable) {
if (peer.torrent.torrent_size > action.additionSize) {
downUnitScore = Math.sqrt(peer.torrent.torrent_size / action.additionSize);
}
var downScore = dsize / action.perlSize;
downloadScore = downUnitScore * action.downloadValue * downScore;
}
udScore = uploadScore + downloadScore;
if (udScore > 0) {
//vip addition
if (action.vipRatio && vm.user.isVip) {
udScore = udScore * action.vipRatio;
}
if (slAction.enable) {
//torrent seeders count addition
if (peer.torrent.torrent_seeds <= slAction.seederCount) {
seederUnit = slAction.seederBasicRatio + slAction.seederCoefficient * (slAction.seederCount - peer.torrent.torrent_seeds + 1);
udScore = udScore * seederUnit;
}
//torrent life addition
var life = moment(Date.now()) - moment(peer.torrent.createdat);
var days = life / (60 * 60 * 1000 * 24);
lifeUnit = slAction.lifeBasicRatio + slAction.lifeCoefficientOfDay * days;
lifeUnit = lifeUnit > slAction.lifeMaxRatio ? slAction.lifeMaxRatio : lifeUnit;
udScore = udScore * lifeUnit;
}
}
totalScore = totalScore + udScore;
});
}
return totalScore;
}
}
}());