refactor(stats): Address PR comments

This commit is contained in:
Sabe Jones
2015-11-18 15:06:55 -05:00
parent 7d2643063f
commit 31b68219d1
3 changed files with 57 additions and 62 deletions

View File

@@ -13,12 +13,12 @@ api = module.exports = {};
api.i18n = i18n; api.i18n = i18n;
import statHelpers from './statHelpers.js' import * as statHelpers from './statHelpers';
api.maxLevel = statHelpers.maxLevel; api.maxLevel = statHelpers.maxLevel;
api.capByLevel = statHelpers.capByLevel; api.capByLevel = statHelpers.capByLevel;
api.maxHealth = statHelpers.maxHealth; api.maxHealth = statHelpers.MAX_HEALTH;
api.tnl = statHelpers.tnl; api.tnl = statHelpers.toNextLevel;
api.diminishingReturns = statHelpers.diminishingReturns; api.diminishingReturns = statHelpers.diminishingReturns;
$w = api.$w = function(s) { $w = api.$w = function(s) {

View File

@@ -4,9 +4,9 @@
------------------------------------------------------ ------------------------------------------------------
*/ */
const maxLevel = 100; export const maxLevel = 100;
function capByLevel (lvl) { export function capByLevel (lvl) {
if (lvl > maxLevel) { if (lvl > maxLevel) {
return maxLevel; return maxLevel;
} else { } else {
@@ -20,7 +20,7 @@ function capByLevel (lvl) {
------------------------------------------------------ ------------------------------------------------------
*/ */
const maxHealth = 50; export const MAX_HEALTH = 50;
/* /*
------------------------------------------------------ ------------------------------------------------------
@@ -28,7 +28,7 @@ const maxHealth = 50;
------------------------------------------------------ ------------------------------------------------------
*/ */
function tnl (lvl) { export function toNextLevel (lvl) {
return Math.round((Math.pow(lvl, 2) * 0.25 + 10 * lvl + 139.75) / 10) * 10; return Math.round((Math.pow(lvl, 2) * 0.25 + 10 * lvl + 139.75) / 10) * 10;
} }
@@ -39,17 +39,6 @@ function tnl (lvl) {
{halfway} (optional) the point at which the graph starts bending {halfway} (optional) the point at which the graph starts bending
*/ */
function diminishingReturns (bonus, max, halfway) { export function diminishingReturns (bonus, max, halfway = max/2) {
if (!halfway) {
halfway = max / 2;
}
return max * (bonus / (bonus + halfway)); return max * (bonus / (bonus + halfway));
} }
export default {
maxLevel,
capByLevel,
maxHealth,
tnl,
diminishingReturns,
};

View File

@@ -1,58 +1,64 @@
var sinon = require('sinon'); import {
var chai = require("chai") maxHealth,
chai.use(require("sinon-chai")) maxLevel,
var expect = chai.expect capByLevel,
tnl,
diminishingReturns,
} from '../../common/script/index';
var shared = require('../../common/script/index.js'); describe('helper functions used in stat calculations', () => {
describe('helper functions used in stat calculations', function() { describe('maxHealth', () => {
it('provides a maximum Health value', () => {
var HEALTH_CAP = 50; const HEALTH_CAP = 50;
var LEVEL_CAP = 100; expect(maxHealth).to.eql(HEALTH_CAP);
var LEVEL = 57;
var BONUS = 600;
var MAXIMUM = 200;
var HALFWAY = 75;
it('provides a maximum Health value', function() {
expect(shared.maxHealth).to.eql(HEALTH_CAP);
});
describe('maximum level cap', function() {
it('returns a maximum level for attribute gain', function() {
expect(shared.maxLevel).to.eql(LEVEL_CAP);
});
it('returns level given if below cap', function() {
expect(shared.capByLevel(LEVEL)).to.eql(LEVEL);
});
it('returns level given if equal to cap', function() {
expect(shared.capByLevel(LEVEL_CAP)).to.eql(LEVEL_CAP);
});
it('returns level cap if above cap', function() {
expect(shared.capByLevel(LEVEL_CAP + LEVEL)).to.eql(LEVEL_CAP);
}); });
}); });
describe('Experience to next level', function() { const LEVEL_CAP = 100;
it('increases Experience target from one level to the next', function() { const LEVEL = 57;
expect(shared.tnl(LEVEL + 1)).to.be.greaterThan(shared.tnl(LEVEL));
describe('maxLevel', () => {
it('returns a maximum level for attribute gain', () => {
expect(maxLevel).to.eql(LEVEL_CAP);
}); });
}); });
describe('diminishing returns', function() { describe('capByLevel', () => {
it('provides a value under the maximum, given a bonus and maximum', function() { it('returns level given if below cap', () => {
expect(shared.diminishingReturns(BONUS,MAXIMUM)).to.be.lessThan(MAXIMUM); expect(capByLevel(LEVEL)).to.eql(LEVEL);
}); });
it('provides a value under the maximum, given a bonus, maximum, and halfway point', function() { it('returns level given if equal to cap', () => {
expect(shared.diminishingReturns(BONUS,MAXIMUM,HALFWAY)).to.be.lessThan(MAXIMUM); expect(capByLevel(LEVEL_CAP)).to.eql(LEVEL_CAP);
}); });
it('provides a different curve if a halfway point is defined', function() { it('returns level cap if above cap', () => {
expect(shared.diminishingReturns(BONUS,MAXIMUM,HALFWAY)).to.not.eql(shared.diminishingReturns(BONUS,MAXIMUM)); expect(capByLevel(LEVEL_CAP + LEVEL)).to.eql(LEVEL_CAP);
});
});
describe('toNextLevel', () => {
it('increases Experience target from one level to the next', () => {
expect(tnl(LEVEL + 1)).to.be.greaterThan(shared.tnl(LEVEL));
});
});
describe('diminishingReturns', () => {
const BONUS = 600;
const MAXIMUM = 200;
const HALFWAY = 75;
it('provides a value under the maximum, given a bonus and maximum', () => {
expect(diminishingReturns(BONUS, MAXIMUM)).to.be.lessThan(MAXIMUM);
});
it('provides a value under the maximum, given a bonus, maximum, and halfway point', () => {
expect(diminishingReturns(BONUS, MAXIMUM, HALFWAY)).to.be.lessThan(MAXIMUM);
});
it('provides a different curve if a halfway point is defined', () => {
expect(diminishingReturns(BONUS, MAXIMUM, HALFWAY)).to.not.eql(shared.diminishingReturns(BONUS, MAXIMUM));
}); });
}); });
}); });