cleaning up the modificationsLookup function

This commit is contained in:
Tyler Renelle 2012-10-11 12:41:31 -04:00
parent 476fb63a0d
commit eb9e385eb1
3 changed files with 29 additions and 32 deletions

View file

@ -76,14 +76,12 @@ Calculates Exp & GP modification based on weapon & lvl
expModifier = function(value, modifiers) {
var dmg, lvl, modified, weapon, _ref;
var dmg, lvl, modified, weapon;
if (modifiers == null) {
modifiers = null;
}
_ref = [user.get('items.weapon'), user.get('stats.lvl')], weapon = _ref[0], lvl = _ref[1];
if (modifiers) {
weapon = modifiers.weapon, lvl = modifiers.lvl;
modifiers = {};
}
weapon = modifiers.weapon || user.get('items.weapon');
lvl = modifiers.lvl || user.get('stats.lvl');
dmg = weapon * MODIFIER;
dmg += (lvl - 1) * MODIFIER;
modified = value + (value * dmg);
@ -98,14 +96,12 @@ Calculates HP-loss modification based on armor & lvl
hpModifier = function(value, modifiers) {
var ac, armor, lvl, modified, _ref;
var ac, armor, lvl, modified;
if (modifiers == null) {
modifiers = null;
}
_ref = [user.get('items.armor'), user.get('stats.lvl')], armor = _ref[0], lvl = _ref[1];
if (modifiers) {
armor = modifiers.armor, lvl = modifiers.lvl;
modifiers = {};
}
armor = modifiers.armor || user.get('items.armor');
lvl = modifiers.lvl || user.get('stats.lvl');
ac = armor * MODIFIER;
ac += (lvl - 1) * MODIFIER;
modified = value - (value * ac);

View file

@ -57,9 +57,9 @@ Calculates Exp & GP modification based on weapon & lvl
{value} task.value for gain
{modifiers} may manually pass in stats as {weapon, exp}. This is used for testing
###
expModifier = (value, modifiers=null) ->
[weapon, lvl] = [user.get('items.weapon'), user.get('stats.lvl')]
if modifiers then {weapon, lvl} = modifiers
expModifier = (value, modifiers = {}) ->
weapon = modifiers.weapon || user.get('items.weapon')
lvl = modifiers.lvl || user.get('stats.lvl')
dmg = weapon * MODIFIER # each new weapon increases exp gain
dmg += (lvl-1) * MODIFIER # same for lvls
modified = value + (value * dmg)
@ -70,9 +70,9 @@ Calculates HP-loss modification based on armor & lvl
{value} task.value which is hurting us
{modifiers} may manually pass in modifier as {armor, lvl}. This is used for testing
###
hpModifier = (value, modifiers=null) ->
[armor, lvl] = [user.get('items.armor'),user.get('stats.lvl')]
if modifiers then {armor, lvl} = modifiers
hpModifier = (value, modifiers = {}) ->
armor = modifiers.armor || user.get('items.armor')
lvl = modifiers.lvl || user.get('stats.lvl')
ac = armor * MODIFIER # each new armor decreases HP loss
ac += (lvl-1) * MODIFIER # same for lvls
modified = value - (value * ac)

View file

@ -43,23 +43,24 @@ freshTask = (taskObj) ->
model.at("_#{type}List").push taskObj
###
Helper function to determine if stats-updates are numerically correct based on scoring
{modifiers} The user stats modifiers as {lvl, armor, weapon}
{direction} 'up' or 'down'
Helper function to determine if stats updates are numerically correct based on scoring
@direction: 'up' or 'down'
@options: The user stats modifiers and times to run, defaults to {times:1, modifiers:{lvl:1, weapon:0, armor:0}}
###
modificationsLookup = (modifiers, direction, times=1) ->
modificationsLookup = (direction, options = {}) ->
merged = _.merge {times:1, lvl:1, weapon:0, armor:0}, options
{times, lvl, armor, weapon} = merged
userObj = cleanUserObj()
value = 0
{lvl, armor, weapon} = modifiers
_.times times, (n) ->
delta = scoring.taskDeltaFormula(value, direction)
value += delta
if direction=='up'
gain = scoring.expModifier(delta, modifiers)
gain = scoring.expModifier(delta, options)
userObj.stats.exp += gain
userObj.stats.money += gain
else
loss = scoring.hpModifier(delta, modifiers)
loss = scoring.hpModifier(delta, options)
userObj.stats.hp += loss
return {user:userObj, value:value}
@ -102,21 +103,21 @@ describe 'User', ->
expect(task.value).to.eql 0
it 'test a few scoring numbers (this will change if constants / formulae change)', ->
{user} = modificationsLookup({lvl:1,armor:0,weapon:0}, 'down', 1)
{user} = modificationsLookup('down')
expect(user.stats.hp).to.eql 49
{user} = modificationsLookup({lvl:1,armor:0,weapon:0}, 'down', 5)
{user} = modificationsLookup('down', {times:5})
expect(user.stats.hp).to.be.within(42,44)
{user} = modificationsLookup({lvl:1,armor:0,weapon:0}, 'up', 1)
{user} = modificationsLookup('up')
expect(user.stats.exp).to.eql 1
expect(user.stats.money).to.eql 1
{user} = modificationsLookup({lvl:1,armor:0,weapon:0}, 'up', 5)
{user} = modificationsLookup('up', {times:5})
expect(user.stats.exp).to.be.within(4,5)
it 'made proper modifications when down-scored', ->
## Trial 1
shouldBe = modificationsLookup({lvl:1,armor:0,weapon:0}, 'down', 1)
shouldBe = modificationsLookup('down')
scoring.score(uuid,'down')
[stats, task] = statsTask()
expect(stats.hp).to.be.eql shouldBe.user.stats.hp
@ -124,7 +125,7 @@ describe 'User', ->
## Trial 2
freshTask {type: 'habit', text: 'Habit', completed: false}
shouldBe = modificationsLookup({lvl:1,armor:0,weapon:0}, 'down', 10)
shouldBe = modificationsLookup('down', {times:10})
scoring.score(uuid,'down', {times:10})
[stats, task] = statsTask()
expect(stats.hp).to.be.eql shouldBe.user.stats.hp
@ -204,7 +205,7 @@ describe 'User', ->
lastCron = moment(model.get('_user.lastCron'))
expect(today.diff(lastCron, 'days')).to.eql 0
shouldBe = modificationsLookup({lvl:1,armor:0,weapon:0}, 'down', times)
shouldBe = modificationsLookup('down', {times:times})
# Should have updated points properly
expect(stats.hp).to.be.eql shouldBe.user.stats.hp
expect(task.value).to.eql shouldBe.value