From 0dbf2257d757e7ae396299b1cecd5cff5016f766 Mon Sep 17 00:00:00 2001 From: Philip How Date: Wed, 6 Mar 2013 17:02:08 +0000 Subject: [PATCH 1/6] New Algorithms + fixes --- src/app/algos.coffee | 46 ++++++++++++++++++++++++------------------ src/app/browser.coffee | 22 +++++++++++--------- src/app/debug.coffee | 7 ++++++- src/app/helpers.coffee | 3 +++ src/app/scoring.coffee | 45 ++++++++++++++++++++++++----------------- views/app/footer.html | 3 ++- views/app/header.html | 2 +- 7 files changed, 78 insertions(+), 50 deletions(-) diff --git a/src/app/algos.coffee b/src/app/algos.coffee index 3a72add2cb..0ba65e1c45 100644 --- a/src/app/algos.coffee +++ b/src/app/algos.coffee @@ -1,5 +1,5 @@ - -MODIFIER = .02 +XP = 15 +HP = 2 priorityValue = (priority='!') -> switch priority @@ -9,7 +9,11 @@ priorityValue = (priority='!') -> else 1 module.exports.tnl = (level) -> - return (Math.pow(level,2)*10)+(level*10)+80 + if level >= 100 + value = 0 + else + value = Math.round(((Math.pow(level,2)*0.25)+(10 * level) + 139.75)/10)*10 # round to nearest 10 + return value ### Calculates Exp modificaiton based on level and weapon strength @@ -18,11 +22,12 @@ module.exports.tnl = (level) -> {level} current user level {priority} user-defined priority multiplier ### -module.exports.expModifier = (value, weaponStrength, level, priority='!') -> - levelModifier = (level-1) * MODIFIER - weaponModifier = weaponStrength / 100 - strength = 1 + weaponModifier + levelModifier - return value * strength * priorityValue(priority) +module.exports.expModifier = (value, weaponStr, level, priority='!') -> + str = (level-1) * 2 # ultimately get this from user + totalStr = (str + weaponStr) / 100 + strMod = 1 + totalStr + exp = value * XP * strMod * priorityValue(priority) + return Math.round(exp) ### Calculates HP modification based on level and armor defence @@ -32,11 +37,12 @@ module.exports.expModifier = (value, weaponStrength, level, priority='!') -> {level} current user level {priority} user-defined priority multiplier ### -module.exports.hpModifier = (value, armorDefense, helmDefense, shieldDefense, level, priority='!') -> - levelModifier = (level-1) * MODIFIER - armorModifier = (armorDefense + helmDefense + shieldDefense) / 100 - defense = 1 - levelModifier + armorModifier - return value * defense * priorityValue(priority) +module.exports.hpModifier = (value, armorDef, helmDef, shieldDef, level, priority='!') -> + def = (level-1) * 2 # ultimately get this from user? + totalDef = (def + armorDef + helmDef + shieldDef) / 100 #ultimate get this from user + defMod = 1 - totalDef + hp = value * HP * defMod * priorityValue(priority) + return Math.round(hp * 10)/10 # round to 1dp ### Future use @@ -52,10 +58,10 @@ module.exports.gpModifier = (value, modifier, priority='!') -> {direction} up or down ### module.exports.taskDeltaFormula = (currentValue, direction) -> - if direction is 'up' - delta = Math.max(Math.pow(0.95,currentValue),0.25) - else - delta = -Math.min(Math.pow(0.95,currentValue),5) - #console.log("CV = " + currentValue + " Dir = " + direction + " delta = " + delta) - delta = 20 if delta > 20 - return delta \ No newline at end of file + if currentValue < -47.27 then currentValue = -47.27 + else if currentValue > 21.27 then currentValue = 21.27 + delta = Math.pow(0.9747,currentValue) + return delta if direction is 'up' + return -delta + + diff --git a/src/app/browser.coffee b/src/app/browser.coffee index f4d5c8e31d..fcc32647ee 100644 --- a/src/app/browser.coffee +++ b/src/app/browser.coffee @@ -9,7 +9,10 @@ restoreRefs = module.exports.restoreRefs = (model) -> # see https://github.com/lefnire/habitrpg/issues/4 # also update in scoring.coffee. TODO create a function accessible in both locations #TODO find a method of calling algos.tnl() - 10*Math.pow(lvl,2)+(lvl*10)+80 + if lvl==100 + 0 + else + Math.round(((Math.pow(lvl,2)*0.25)+(10 * lvl) + 139.75)/10)*10 #refLists _.each ['habit', 'daily', 'todo', 'reward'], (type) -> @@ -171,13 +174,14 @@ setupGrowlNotifications = (model) -> else if num > 0 statsNotification " + #{rounded} HP", 'hp' # gained hp from potion/level? - user.on 'set', 'stats.exp', (captures, args, isLocal, silent) -> - num = captures - args - rounded = Math.abs(num.toFixed(1)) - if num < 0 and not silent - statsNotification " - #{rounded} XP", 'xp' - else if num > 0 - statsNotification " + #{rounded} XP", 'xp' + user.on 'set', 'stats.exp', (captures, args, isLocal, silent=false) -> + # unless silent + num = captures - args + rounded = Math.abs(num.toFixed(1)) + if num < 0 and num > -100 # TODO fix hackey negative notification supress + statsNotification " - #{rounded} XP", 'xp' + else if num > 0 + statsNotification " + #{rounded} XP", 'xp' user.on 'set', 'stats.gp', (captures, args) -> num = captures - args @@ -195,7 +199,7 @@ setupGrowlNotifications = (model) -> user.on 'set', 'stats.lvl', (captures, args) -> if captures > args if captures is 1 and args is 0 - statsNotification ' You died!', 'death' + statsNotification ' You died! Game over.', 'death' else statsNotification ' Level Up!', 'lvl' diff --git a/src/app/debug.coffee b/src/app/debug.coffee index 24195797bb..1f2172d272 100644 --- a/src/app/debug.coffee +++ b/src/app/debug.coffee @@ -8,8 +8,13 @@ module.exports.app = (appExports, model) -> user.set 'lastCron', yesterday window.location.reload() + appExports.emulateTenDays = -> + yesterday = +moment().subtract('days', 10).toDate() + user.set 'lastCron', yesterday + window.location.reload() + appExports.cheat = -> - user.incr 'stats.exp', 20 + user.incr 'stats.exp', model.get '_tnl' user.incr 'stats.gp', 1000 appExports.reset = -> diff --git a/src/app/helpers.coffee b/src/app/helpers.coffee index ff0c82459d..b6389bfba9 100644 --- a/src/app/helpers.coffee +++ b/src/app/helpers.coffee @@ -19,6 +19,9 @@ module.exports.viewHelpers = (view) -> view.fn "floor", (num) -> Math.floor num + + view.fn "ceil", (num) -> + Math.ceil num view.fn "lt", (a, b) -> a < b diff --git a/src/app/scoring.coffee b/src/app/scoring.coffee index a63a31a467..5cc5a8428c 100644 --- a/src/app/scoring.coffee +++ b/src/app/scoring.coffee @@ -46,14 +46,13 @@ score = (model, taskId, direction, times, batch, cron) -> # (aka, the total delta). This weirdness won't be necessary when calculating mathematically # rather than iteratively nextDelta = algos.taskDeltaFormula(value, direction) - value = Math.max(value + nextDelta, -31) if adjustvalue #cap values so we don't get silly values + value += nextDelta if adjustvalue delta += nextDelta addPoints = -> level = user.get('stats.lvl') weaponStrength = items.items.weapon[user.get('items.weapon')].strength - modified = algos.expModifier(delta,weaponStrength,level, priority) - exp += modified*10 + exp += algos.expModifier(delta,weaponStrength,level, priority) gp += algos.gpModifier(delta, 1, priority) subtractPoints = -> @@ -61,8 +60,7 @@ score = (model, taskId, direction, times, batch, cron) -> armorDefense = items.items.armor[user.get('items.armor')].defense helmDefense = items.items.head[user.get('items.head')].defense shieldDefense = items.items.shield[user.get('items.shield')].defense - modified = algos.hpModifier(delta,armorDefense,helmDefense,shieldDefense,level, priority) - hp += modified + hp += algos.hpModifier(delta,armorDefense,helmDefense,shieldDefense,level, priority) switch type when 'habit' @@ -81,7 +79,8 @@ score = (model, taskId, direction, times, batch, cron) -> subtractPoints() else calculateDelta(false) - addPoints() # obviously for delta>0, but also a trick to undo accidental checkboxes + if delta != 0 + addPoints() # obviously for delta>0, but also a trick to undo accidental checkboxes when 'todo' if cron? #cron @@ -137,20 +136,30 @@ updateStats = (model, newStats, batch) -> obj.stats.hp = newStats.hp if newStats.exp? - # level up & carry-over exp tnl = model.get '_tnl' - silent = false - if newStats.exp >= tnl - silent = true - user.set('stats.exp', newStats.exp) - while newStats.exp >= tnl # keep levelling up - newStats.exp -= tnl - obj.stats.lvl++ - tnl = algos.tnl(obj.stats.lvl) - obj.stats.hp = 50 + #silent = false + # if we're at level 100, turn xp to gold + if obj.stats.lvl >= 100 + newStats.gp += newStats.exp / 15 + newStats.exp = 0 + obj.stats.lvl = 100 + else + # level up & carry-over exp + if newStats.exp >= tnl + #silent = true # push through the negative xp silently + user.set('stats.exp', newStats.exp) # push normal + notification + while newStats.exp >= tnl and obj.stats.lvl < 100 # keep levelling up + newStats.exp -= tnl + obj.stats.lvl++ + tnl = algos.tnl(obj.stats.lvl) + if obj.stats.lvl== 100 + newStats.exp = 0 + obj.stats.hp = 50 obj.stats.exp = newStats.exp - user.pass(silent:true).set('stats.exp', obj.stats.exp) if silent + #if silent + #console.log("pushing silent :" + obj.stats.exp) + #user.pass(true).set('stats.exp', obj.stats.exp) # Set flags when they unlock features if !obj.flags.customizationsNotification and (obj.stats.exp > 10 or obj.stats.lvl > 1) @@ -218,7 +227,7 @@ cron = (model) -> value = obj.tasks[taskObj.id].value #get updated value absVal = if (completed) then Math.abs(value) else value todoTally += absVal - else if type is 'habit' #reset 'onlies' value to 0 + else if type is 'habit' # slowly reset 'onlies' value to 0 if taskObj.up==false or taskObj.down==false if Math.abs(taskObj.value) < 0.02 batch.set "tasks.#{taskObj.id}.value", 0 diff --git a/views/app/footer.html b/views/app/footer.html index 35be956150..bd7626f248 100644 --- a/views/app/footer.html +++ b/views/app/footer.html @@ -19,7 +19,8 @@ {else}
- + +
{/} diff --git a/views/app/header.html b/views/app/header.html index 2491090dae..d7bc716b3e 100644 --- a/views/app/header.html +++ b/views/app/header.html @@ -20,7 +20,7 @@
- {round(_user.stats.hp)} / 50 + {ceil(_user.stats.hp)} / 50
From 75aadf6eb118114f2ec6101e17392aee4293aac1 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 6 Mar 2013 13:32:10 -0500 Subject: [PATCH 2/6] add default numbers for restore tool if one isn't provided: #594 --- src/app/character.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/character.coffee b/src/app/character.coffee index ef2f36fadd..2fe875e563 100644 --- a/src/app/character.coffee +++ b/src/app/character.coffee @@ -82,7 +82,7 @@ module.exports.app = (appExports, model) -> batch = new BatchUpdate(model) batch.startTransaction() $('#restore-form input').each -> - batch.set $(this).attr('data-for'), parseInt($(this).val()) + batch.set $(this).attr('data-for'), parseInt($(this).val() || 1) batch.commit() user.on 'set', 'flags.customizationsNotification', (captures, args) -> From 5141aeda78a834cf55cda9ed9190227f0fdf3472 Mon Sep 17 00:00:00 2001 From: Daniel Saewitz Date: Wed, 6 Mar 2013 19:59:19 -0500 Subject: [PATCH 3/6] Fix broken tnl test --- test/api.mocha.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index 2b185afd96..f622810a40 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -100,8 +100,8 @@ describe 'API', -> expect(res.body.id).not.to.be.empty() self = _.clone(currentUser) delete self.apiToken - # To next level should be 100 - self.stats.tnl = 100 + # To next level + self.stats.tnl = 150 self.stats.maxHealth = 50 expect(res.body).to.eql self From 7cb291c4de7f604505276557ac7ca1a981695faf Mon Sep 17 00:00:00 2001 From: Daniel Saewitz Date: Wed, 6 Mar 2013 20:39:48 -0500 Subject: [PATCH 4/6] Rename user.stats.tnl to user.stats.toNextLevel --- src/server/api.coffee | 2 +- test/api.mocha.coffee | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/server/api.coffee b/src/server/api.coffee index 3fe4bd598a..6bdc0699c4 100644 --- a/src/server/api.coffee +++ b/src/server/api.coffee @@ -42,7 +42,7 @@ auth = (req, res, next) -> router.get '/user', auth, (req, res) -> user = req.userObj - user.stats.tnl = tnl user.stats.lvl + user.stats.toNextLevel = tnl user.stats.lvl user.stats.maxHealth = 50 delete user.apiToken diff --git a/test/api.mocha.coffee b/test/api.mocha.coffee index f622810a40..4b0adfebe5 100644 --- a/test/api.mocha.coffee +++ b/test/api.mocha.coffee @@ -100,8 +100,7 @@ describe 'API', -> expect(res.body.id).not.to.be.empty() self = _.clone(currentUser) delete self.apiToken - # To next level - self.stats.tnl = 150 + self.stats.toNextLevel = 150 self.stats.maxHealth = 50 expect(res.body).to.eql self From 63c00a726194011dbb92775131dc4d31cd9059d3 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 7 Mar 2013 12:53:45 -0500 Subject: [PATCH 5/6] add @MrConcepts normalization migration for busted busted values, and in preparation for another big algorithm upgrade --- migrations/20130307_normalize_algo_values.js | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 migrations/20130307_normalize_algo_values.js diff --git a/migrations/20130307_normalize_algo_values.js b/migrations/20130307_normalize_algo_values.js new file mode 100644 index 0000000000..116627251e --- /dev/null +++ b/migrations/20130307_normalize_algo_values.js @@ -0,0 +1,47 @@ +// mongo habitrpg ./node_modules/underscore/underscore.js ./migrations/20130307_normalize_algo_values.js + +/** + * Users were experiencing a lot of extreme Exp multiplication (https://github.com/lefnire/habitrpg/issues/594). + * This sets things straight, and in preparation for another algorithm overhaul + */ +db.users.find().forEach(function(user){ + if (user.stats.exp >= 3580) { + user.stats.exp = 0 + } + + if (user.stats.lvl > 100) { + user.stats.lvl = 100 + } + + _.each(user.tasks, function(task, key){ + // remove corrupt tasks + if (!task) { + delete user.tasks[key] + return + } + + // Fix busted values + if (task.value > 21.27) { + task.value = 21.27 + } + else if (task.value < -47.27) { + task.value = -47.27 + } + }); + + try { + db.users.update( + {_id:user._id}, + {$set: + { + 'stats.lvl': user.stats.lvl, + 'stats.exp': user.stats.exp, + 'tasks' : user.tasks + } + }, + {multi:true} + ); + } catch(e) { + print(e); + } +}) \ No newline at end of file From 05bd0fdd520a7a818069a622d6a4216d09a221d3 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Thu, 7 Mar 2013 13:16:47 -0500 Subject: [PATCH 6/6] semi-colons --- migrations/20130307_normalize_algo_values.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/migrations/20130307_normalize_algo_values.js b/migrations/20130307_normalize_algo_values.js index 116627251e..e42faded43 100644 --- a/migrations/20130307_normalize_algo_values.js +++ b/migrations/20130307_normalize_algo_values.js @@ -6,26 +6,26 @@ */ db.users.find().forEach(function(user){ if (user.stats.exp >= 3580) { - user.stats.exp = 0 + user.stats.exp = 0; } if (user.stats.lvl > 100) { - user.stats.lvl = 100 + user.stats.lvl = 100; } _.each(user.tasks, function(task, key){ // remove corrupt tasks if (!task) { - delete user.tasks[key] - return + delete user.tasks[key]; + return; } // Fix busted values if (task.value > 21.27) { - task.value = 21.27 + task.value = 21.27; } else if (task.value < -47.27) { - task.value = -47.27 + task.value = -47.27; } });