From 1b118d86b26f07643e490ff01f0ca8f38379dc12 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sat, 26 Dec 2015 19:40:36 -0600 Subject: [PATCH] refactor: Extract constants into constants file --- common/script/constants.js | 3 +++ common/script/index.js | 13 +++++++++---- common/script/statHelpers.js | 22 ++++------------------ test/common/user.fns.updateStats.test.js | 23 ++++++++++------------- 4 files changed, 26 insertions(+), 35 deletions(-) create mode 100644 common/script/constants.js diff --git a/common/script/constants.js b/common/script/constants.js new file mode 100644 index 0000000000..d6e0aa9384 --- /dev/null +++ b/common/script/constants.js @@ -0,0 +1,3 @@ +export const MAX_HEALTH = 50; +export const MAX_LEVEL = 100; +export const MAX_STAT_POINTS = MAX_LEVEL; diff --git a/common/script/index.js b/common/script/index.js index 65ec55706f..e23fe47ab9 100644 --- a/common/script/index.js +++ b/common/script/index.js @@ -2,6 +2,11 @@ import { daysSince, shouldDo, } from '../../common/script/cron'; +import { + MAX_HEALTH, + MAX_LEVEL, + MAX_STAT_POINTS, +} from './constants'; import * as statHelpers from './statHelpers'; var $w, _, api, content, i18n, moment, preenHistory, sortOrder, @@ -20,10 +25,9 @@ api = module.exports = {}; api.i18n = i18n; api.shouldDo = shouldDo; -api.maxLevel = statHelpers.MAX_LEVEL; -api.maxStatPoints = statHelpers.MAX_STAT_POINTS; +api.maxLevel = MAX_LEVEL; api.capByLevel = statHelpers.capByLevel; -api.maxHealth = statHelpers.MAX_HEALTH; +api.maxHealth = MAX_HEALTH; api.tnl = statHelpers.toNextLevel; api.diminishingReturns = statHelpers.diminishingReturns; @@ -2283,7 +2287,8 @@ api.wrap = function(user, main) { tnl = api.tnl(user.stats.lvl); user.stats.hp = 50; var userTotalStatPoints = user.stats.str + user.stats.int + user.stats.con + user.stats.per; - if (userTotalStatPoints >= api.maxStatPoints) { + + if (userTotalStatPoints >= MAX_STAT_POINTS) { continue; } if (user.preferences.automaticAllocation) { diff --git a/common/script/statHelpers.js b/common/script/statHelpers.js index 1794d7f0b5..ff52662d52 100644 --- a/common/script/statHelpers.js +++ b/common/script/statHelpers.js @@ -1,11 +1,13 @@ +import { + MAX_LEVEL, +} from './constants'; + /* ------------------------------------------------------ Level cap ------------------------------------------------------ */ -export const MAX_LEVEL = 100; - export function capByLevel (lvl) { if (lvl > MAX_LEVEL) { return MAX_LEVEL; @@ -14,22 +16,6 @@ export function capByLevel (lvl) { } } -/* - ------------------------------------------------------ - Stats cap - ------------------------------------------------------ - */ - -export const MAX_STAT_POINTS = MAX_LEVEL; - -/* - ------------------------------------------------------ - Health cap - ------------------------------------------------------ - */ - -export const MAX_HEALTH = 50; - /* ------------------------------------------------------ Scoring diff --git a/test/common/user.fns.updateStats.test.js b/test/common/user.fns.updateStats.test.js index 8f641ea3a4..633078714b 100644 --- a/test/common/user.fns.updateStats.test.js +++ b/test/common/user.fns.updateStats.test.js @@ -1,4 +1,3 @@ -/* eslint-disable camelcase */ import { generateUser, } from '../helpers/common.helper'; @@ -19,31 +18,29 @@ describe('user.fns.updateStats', () => { }); context('Stat Allocation', () => { - it('Adds an attibute point when user\'s stat points are less than max level', () => { - let stats = { - exp: 3581, - }; - + it('adds an attibute point when user\'s stat points are less than max level', () => { + user.stats.exp = 3581; user.stats.lvl = 99; user.stats.str = 25; user.stats.int = 25; user.stats.con = 25; user.stats.per = 24; - user.fns.updateStats(stats); + + user.fns.updateStats(user.stats); + expect(user.stats.points).to.eql(1); }); - it('Does not add an attibute point when user\'s stat points are equal to max level', () => { - let stats = { - exp: 3581, - }; - + it('does not add an attibute point when user\'s stat points are equal to max level', () => { + user.stats.exp = 3581; user.stats.lvl = 99; user.stats.str = 25; user.stats.int = 25; user.stats.con = 25; user.stats.per = 25; - user.fns.updateStats(stats); + + user.fns.updateStats(user.stats); + expect(user.stats.points).to.eql(0); }); });