From 091600d7fc2e348246886af548c7bd5381d97bc8 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Mon, 2 Dec 2013 19:14:29 -0700 Subject: [PATCH] classes: add _statsComputed property, and sample `user.stats.buffs.stealth` handling (@SabreCat @danielthebard) --- dist/habitrpg-shared.js | 33 +++++++++++++++++++++++++-------- script/algos.coffee | 34 +++++++++++++++++++++++++--------- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 837c9a1d24..a81c28d068 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -8751,6 +8751,25 @@ var global=self;/** obj = module.exports = {}; + obj.defineComputed = function(user) { + if (user._statsComputed != null) { + return; + } + return Object.defineProperty(user, '_statsComputed', { + get: function() { + var _this = this; + return _.reduce(['per', 'con', 'str', 'int'], function(m, stat) { + m[stat] = _.reduce('stats stats.buffs items.gear.current.weapon items.gear.current.armor items.gear.current.head items.gear.current.shield'.split(' '), function(m2, path) { + var val, _ref1; + val = helpers.dotGet(path, _this); + return m2 + (~path.indexOf('items.gear') ? (+((_ref1 = items.items.gear.flat[val]) != null ? _ref1[stat] : void 0) || 0) * (~(val != null ? val.indexOf(_this.stats["class"]) : void 0) ? 1.2 : 1) : +val[stat] || 0); + }, 0); + return m; + }, {}); + } + }); + }; + obj.revive = function(user, options) { var candidate, loseThisItem, owned, paths; if (options == null) { @@ -8957,14 +8976,9 @@ var global=self;/** options = {}; } user._tmp = {}; - user.stats._computed = _.reduce(['per', 'con', 'str', 'int'], function(m, stat) { - m[stat] = _.reduce('stats stats.buffs items.weapon items.armor items.head items.shield'.split(' '), function(m2, path) { - return m2 + (+helpers.dotGet("" + path + "." + stat, user) || 0); - }, 0); - return m; - }, {}); + obj.defineComputed(user); console.log({ - computed: user.stats._computed + computed: user._statsComputed }); _ref1 = [+user.stats.gp, +user.stats.hp, +user.stats.exp, ~~user.stats.lvl], gp = _ref1[0], hp = _ref1[1], exp = _ref1[2], lvl = _ref1[3]; _ref2 = [task.type, +task.value, ~~task.streak, task.priority || '!'], type = _ref2[0], value = _ref2[1], streak = _ref2[2], priority = _ref2[3]; @@ -9197,10 +9211,13 @@ var global=self;/** } todoTally = 0; user.todos.concat(user.dailys).forEach(function(task) { - var absVal, completed, id, repeat, scheduleMisses, type; + var absVal, completed, id, repeat, scheduleMisses, type, _ref2; if (!task) { return; } + if ((_ref2 = user.stats.buffs) != null ? _ref2.stealth-- : void 0) { + return; + } id = task.id, type = task.type, completed = task.completed, repeat = task.repeat; if (!completed) { scheduleMisses = daysMissed; diff --git a/script/algos.coffee b/script/algos.coffee index 0991cdd9a7..73349aa799 100644 --- a/script/algos.coffee +++ b/script/algos.coffee @@ -8,6 +8,26 @@ XP = 15 HP = 2 obj = module.exports = {} +obj.defineComputed = (user) -> + #FIXME we should put this function in a sane place instead of this check, somewhere (both on client & server) where + # it will only define the property once on user initialization + return if user._statsComputed? + + # Aggregate all intrinsic stats, buffs, weapon, & armor into computed stats + Object.defineProperty user, '_statsComputed', + get: -> + _.reduce(['per','con','str','int'], (m,stat) => + m[stat] = _.reduce('stats stats.buffs items.gear.current.weapon items.gear.current.armor items.gear.current.head items.gear.current.shield'.split(' '), (m2,path) => + val = helpers.dotGet(path, @) + m2 + + if ~path.indexOf('items.gear') + # get the gear stat, and multiply it by 1.2 if it's class-gear + (+items.items.gear.flat[val]?[stat] or 0) * (if ~val?.indexOf(@stats.class) then 1.2 else 1) + else + +val[stat] or 0 + , 0); m + , {}) + obj.revive = (user, options={})-> paths = options.paths || {} @@ -186,15 +206,8 @@ obj.score = (user, task, direction, options={}) -> # the API consumer, then cleared afterwards user._tmp = {} - # Aggregate all intrinsic stats, buffs, weapon, & armor into computed stats - user.stats._computed = _.reduce(['per','con','str','int'], (m,stat) -> - m[stat] = _.reduce('stats stats.buffs items.weapon items.armor items.head items.shield'.split(' '), (m2,path) -> - m2 + (+helpers.dotGet("#{path}.#{stat}", user) or 0) - , 0) - #m[stat] = user.stats[stat] + user.stats.buffs?[stat] + user.items.weapon[stat] + user.items.armor[stat] + user.items.head[stat] + user.items.shield[stat] - m - , {}) - console.log computed: user.stats._computed + obj.defineComputed user # put this somewhere else? + console.log computed: user._statsComputed [gp, hp, exp, lvl] = [+user.stats.gp, +user.stats.hp, +user.stats.exp, ~~user.stats.lvl] [type, value, streak, priority] = [task.type, +task.value, ~~task.streak, task.priority or '!'] @@ -399,6 +412,9 @@ obj.cron = (user, options={}) -> todoTally = 0 user.todos.concat(user.dailys).forEach (task) -> return unless task + + return if user.stats.buffs?.stealth-- # User "evades" a certain number of tasks + {id, type, completed, repeat} = task # Deduct experience for missed Daily tasks, but not for Todos (just increase todo's value) unless completed