mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Write functions to check if user has balance for challenge creation
This commit is contained in:
@@ -292,24 +292,35 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
//A helper function to dermine if the user has enough gems to create a Habitrpg challenge
|
function _checkIfUserHasEnoughGemsForTavernChallenge() {
|
||||||
$scope.hasEnoughGems = function(gid) {
|
var balance = User.user.balance || 0;
|
||||||
if (!gid) return;
|
var isForTavern = $scope.newChallenge.group == 'habitrpg';
|
||||||
var groupBalance = 0;
|
|
||||||
var group = _.find($scope.groups, {_id:gid});
|
if (isForTavern && balance <= 0) {
|
||||||
if (group) { groupBalance = group.balance; }
|
$scope.isTavernChallengeAndUserCannotProvidePrize = false;
|
||||||
var userBalance = User.user.balance || 0;
|
|
||||||
$scope.maxPrize = userBalance * 4 + ((group && groupBalance && group.leader==User.user._id) ? groupBalance*4 : 0);
|
|
||||||
if (gid == 'habitrpg') {
|
|
||||||
$scope.newChallenge.prize = 1;
|
|
||||||
//If the user does not have enough gems for the Habitrpg group, the set our enoughGems var to false
|
|
||||||
if ( $scope.maxPrize <= 0 ) $scope.enoughGems = false;
|
|
||||||
} else {
|
} else {
|
||||||
//Reset our enoughGems variable incase the user tried to create a challenge for habitrpg and was unable to
|
$scope.isTavernChallengeAndUserCannotProvidePrize = true;
|
||||||
$scope.enoughGems = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _calculateMaxPrize(gid) {
|
||||||
|
var userBalance = User.getBalanceInGems() || 0;
|
||||||
|
var availableGroupBalance = _calculateAvailableGroupBalance(gid);
|
||||||
|
|
||||||
|
return userBalance + availableGroupBalance;
|
||||||
|
}
|
||||||
|
|
||||||
|
function _calculateAvailableGroupBalance(gid) {
|
||||||
|
var groupBalance = 0;
|
||||||
|
var group = _.find($scope.groups, { _id: gid });
|
||||||
|
|
||||||
|
if (group && group.balance && group.leader === User.user._id) {
|
||||||
|
groupBalance = group.balance * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
return groupBalance;
|
||||||
|
}
|
||||||
|
|
||||||
function _shouldShowChallenge(chal) {
|
function _shouldShowChallenge(chal) {
|
||||||
// Have to check that the leader object exists first in the
|
// Have to check that the leader object exists first in the
|
||||||
// case where a challenge's leader deletes their account
|
// case where a challenge's leader deletes their account
|
||||||
|
|||||||
Reference in New Issue
Block a user