diff --git a/common/script/index.coffee b/common/script/index.coffee index e8ca6096ae..88402eeaf4 100644 --- a/common/script/index.coffee +++ b/common/script/index.coffee @@ -33,8 +33,6 @@ api.planGemLimits = convRate: 20 #how much does a gem cost? convCap: 25 #how many gems can be converted / month? -api.statCalc = require('./methods/statCalculations') - ### ------------------------------------------------------ Time / Day diff --git a/common/script/methods/statCalculations.js b/common/script/methods/statCalculations.js deleted file mode 100644 index 51a554f9cf..0000000000 --- a/common/script/methods/statCalculations.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -var Content = require('../content.coffee'); - -function levelBonus(level) { - // Level bonus is derived by taking the level, subtracting one, - // taking the smaller of it or maxLevel (100), - // dividing that by two and then raising it to a whole number - - // TODO: 100 is a magic number, extract from script.index into own module and call here - var levelOrMaxLevel = Math.min((level - 1), 100) - var levelDividedByTwo = levelOrMaxLevel / 2 - var statBonus = Math.ceil(levelDividedByTwo ) - - return statBonus; -} - -function equipmentStatBonus(stat, equipped) { - var gear = Content.gear.flat; - var total = 0; - - var equipmentTypes = ['weapon', 'armor', 'head', 'shield']; - - _(equipmentTypes).each(function(type) { - var equippedItem = equipped[type] - if(gear[equippedItem]) { - var equipmentStat = gear[equippedItem][stat]; - - total += equipmentStat; - } - }); - - return total; -} - -function classBonus(user, stat) { - var computedStats = user._statsComputed; - if(computedStats) { - var bonus = computedStats[stat] - - user.stats.buffs[stat] - - levelBonus(user.stats.lvl) - - equipmentStatBonus(stat, user.items.gear.equipped) - - user.stats[stat] - - return bonus; - } -} - -module.exports = { - classBonus: classBonus, - equipmentStatBonus: equipmentStatBonus, - levelBonus: levelBonus -} diff --git a/karma.conf.js b/karma.conf.js index d1d576e53b..bbb3bb45a8 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -55,6 +55,7 @@ module.exports = function(config) { "website/public/js/services/taskServices.js", "website/public/js/services/paymentServices.js", "website/public/js/services/questServices.js", + "website/public/js/services/statServices.js", "website/public/js/filters/money.js", "website/public/js/filters/roundLargeNumbers.js", diff --git a/test/common/statCalculations.js b/test/spec/services/statServicesSpec.js similarity index 90% rename from test/common/statCalculations.js rename to test/spec/services/statServicesSpec.js index f0ffd38ce6..4593038171 100644 --- a/test/common/statCalculations.js +++ b/test/spec/services/statServicesSpec.js @@ -1,12 +1,20 @@ 'use strict'; -var sinon = require('sinon'); -var chai = require("chai"); -chai.use(require("sinon-chai")); -var expect = chai.expect; -var statCalc = require('../../common/script/methods/statCalculations'); +describe('Stats Service', function() { + var scope, statCalc, user; + + beforeEach(function() { + user = specHelper.newUser(); + + module(function($provide) { + $provide.value('User', {user: user}); + }); + + inject(function($rootScope, $controller, Stats) { + statCalc = Stats; + }); + }); -describe('stat calculation functions', function() { describe('levelBonus', function() { it('calculates bonus as half of level for even numbered level under 100', function() { var level = 50; diff --git a/website/public/js/services/statServices.js b/website/public/js/services/statServices.js new file mode 100644 index 0000000000..593031526f --- /dev/null +++ b/website/public/js/services/statServices.js @@ -0,0 +1,65 @@ +'use strict'; + +(function(){ + angular + .module('habitrpg') + .factory('Stats', statsFactory); + + statsFactory.$inject = [ + 'Content', + 'Shared' + ]; + + function statsFactory(Content, Shared) { + + function levelBonus(level) { + // Level bonus is derived by taking the level, subtracting one, + // taking the smaller of it or maxLevel (100), + // dividing that by two and then raising it to a whole number + + var levelOrMaxLevel = Math.min((level - 1), Shared.maxLevel); + var levelDividedByTwo = levelOrMaxLevel / 2; + var bonus = Math.ceil(levelDividedByTwo ); + + return bonus; + } + + function equipmentStatBonus(stat, equipped) { + var gear = Content.gear.flat; + var total = 0; + + var equipmentTypes = ['weapon', 'armor', 'head', 'shield']; + + _(equipmentTypes).each(function(type) { + var equippedItem = equipped[type]; + if(gear[equippedItem]) { + var equipmentStat = gear[equippedItem][stat]; + + total += equipmentStat; + } + }); + + return total; + } + + function classBonus(user, stat) { + var computedStats = user._statsComputed; + + if(computedStats) { + var bonus = computedStats[stat] + - user.stats.buffs[stat] + - levelBonus(user.stats.lvl) + - equipmentStatBonus(stat, user.items.gear.equipped) + - user.stats[stat]; + + return bonus; + } + } + + return { + classBonus: classBonus, + equipmentStatBonus: equipmentStatBonus, + levelBonus: levelBonus + } + } +}()); diff --git a/website/public/manifest.json b/website/public/manifest.json index 029b618a94..7d4189887a 100644 --- a/website/public/manifest.json +++ b/website/public/manifest.json @@ -52,6 +52,7 @@ "js/services/challengeServices.js", "js/services/paymentServices.js", "js/services/questServices.js", + "js/services/statServices.js", "js/filters/money.js", "js/filters/roundLargeNumbers.js",