From 306505ebab2ecd59dbc5f0f2de369b1f450554fc Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Fri, 30 Sep 2016 08:14:03 -0500 Subject: [PATCH] fix(api,client): Pass in predictable random to revive randomVal calls closes #8085 --- test/common/fns/randomVal.js | 12 ++---------- website/common/script/libs/randomVal.js | 2 +- website/common/script/ops/revive.js | 12 +++++++----- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/test/common/fns/randomVal.js b/test/common/fns/randomVal.js index 82469c0e7f..a604c63211 100644 --- a/test/common/fns/randomVal.js +++ b/test/common/fns/randomVal.js @@ -1,7 +1,4 @@ import randomVal from '../../../website/common/script/libs/randomVal'; -import { - generateUser, -} from '../../helpers/common.helper'; describe('randomVal', () => { let obj; @@ -32,19 +29,14 @@ describe('randomVal', () => { expect(Math.random).to.be.calledOnce; }); - it('can pass in a custom random function that takes in the user and a seed argument', () => { - let user = generateUser(); - let randomSpy = sandbox.stub().returns(0.3); + it('can pass in a predictable random value', () => { sandbox.spy(Math, 'random'); let result = randomVal(obj, { - user, - seed: 100, - predictableRandom: randomSpy, + predictableRandom: 0.3, }); expect(Math.random).to.not.be.called; - expect(randomSpy).to.be.calledOnce; expect(result).to.equal(2); }); diff --git a/website/common/script/libs/randomVal.js b/website/common/script/libs/randomVal.js index dea1c32784..ef692b7ba0 100644 --- a/website/common/script/libs/randomVal.js +++ b/website/common/script/libs/randomVal.js @@ -8,7 +8,7 @@ function trueRandom () { // returns random property (the value) module.exports = function randomVal (obj, options = {}) { let array = options.key ? _.keys(obj) : _.values(obj); - let random = (options.predictableRandom || trueRandom)(); + let random = options.predictableRandom || trueRandom(); array.sort(); diff --git a/website/common/script/ops/revive.js b/website/common/script/ops/revive.js index 51872c49fd..426159da0b 100644 --- a/website/common/script/ops/revive.js +++ b/website/common/script/ops/revive.js @@ -5,9 +5,7 @@ import { NotAuthorized, } from '../libs/errors'; import randomVal from '../libs/randomVal'; - -// TODO this is only used on the server -// move out of common? +import predictableRandom from '../fns/predictableRandom'; module.exports = function revive (user, req = {}, analytics) { if (user.stats.hp > 0) { @@ -29,7 +27,9 @@ module.exports = function revive (user, req = {}, analytics) { m[k] = k; } return m; - }, {})); + }, {}), { + predictableRandom: predictableRandom(user), + }); if (lostStat) { user.stats[lostStat]--; @@ -71,7 +71,9 @@ module.exports = function revive (user, req = {}, analytics) { } }); - let lostItem = randomVal(losableItems); + let lostItem = randomVal(losableItems, { + predictableRandom: predictableRandom(user), + }); let message = ''; let item = content.gear.flat[lostItem];