From e76c81b3d75ac95e78f7e0c7e6518e7e9220f408 Mon Sep 17 00:00:00 2001 From: Cole Gleason Date: Fri, 24 Jan 2014 15:31:08 -0600 Subject: [PATCH] refactor(api.percent): allow for round direction to be specified --- dist/habitrpg-shared.js | 25 ++++++++++++++++++++----- script/index.coffee | 8 ++++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/dist/habitrpg-shared.js b/dist/habitrpg-shared.js index 415688b2b2..61cbb52e42 100644 --- a/dist/habitrpg-shared.js +++ b/dist/habitrpg-shared.js @@ -27,7 +27,8 @@ process.nextTick = (function () { if (canPost) { var queue = []; window.addEventListener('message', function (ev) { - if (ev.source === window && ev.data === 'process-tick') { + var source = ev.source; + if ((source === window || source === null) && ev.data === 'process-tick') { ev.stopPropagation(); if (queue.length > 0) { var fn = queue.shift(); @@ -63,7 +64,7 @@ process.chdir = function (dir) { }; },{}],3:[function(require,module,exports){ -var global=self;/** +var global=typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};/** * @license * Lo-Dash 2.4.1 (Custom Build) * Build: `lodash modern -o ./dist/lodash.js` @@ -11208,11 +11209,23 @@ var process=require("__browserify_process");(function() { return task; }; - api.percent = function(x, y) { + api.percent = function(x, y, dir) { + var roundFn; + + switch (dir) { + case "up": + roundFn = Math.ceil; + break; + case "down": + roundFn = Math.floor; + break; + default: + roundFn = Math.round; + } if (x === 0) { x = 1; } - return Math.round(x / y * 100); + return roundFn(x / y * 100); }; /* @@ -12351,7 +12364,9 @@ var process=require("__browserify_process");(function() { drop.type = 'Food'; drop.dialog = "You've found " + drop.article + drop.text + "! " + drop.notes; } else if (rarity > .3) { - drop = user.fns.randomVal(content.eggs); + drop = user.fns.randomVal(_.where(content.eggs, { + canBuy: true + })); if ((_ref3 = (_base1 = user.items.eggs)[_name1 = drop.key]) == null) { _base1[_name1] = 0; } diff --git a/script/index.coffee b/script/index.coffee index c24b075d03..18501084ff 100644 --- a/script/index.coffee +++ b/script/index.coffee @@ -188,9 +188,13 @@ api.taskDefaults = (task={}) -> task.priority = 1 unless _.isNumber(task.priority) # hotfix for apiv1. once we're off apiv1, we can remove this task -api.percent = (x,y) -> +api.percent = (x,y, dir) -> + switch dir + when "up" then roundFn = Math.ceil + when "down" then roundFn = Math.floor + else roundFn = Math.round x=1 if x==0 - Math.round(x/y*100) + roundFn(x/y*100) ### Remove whitespace #FIXME are we using this anywwhere? Should we be?