From 4d5b6992be71b06c469e5d65be211842cb62ead3 Mon Sep 17 00:00:00 2001 From: Thomas Gamble Date: Sat, 10 Sep 2016 10:37:15 -0700 Subject: [PATCH] drops are randomly selected, not based on user values fixes #7929 --- test/common/fns/randomVal.js | 134 +++++++----------------- website/common/script/fns/randomDrop.js | 3 +- website/common/script/fns/randomVal.js | 13 ++- 3 files changed, 46 insertions(+), 104 deletions(-) diff --git a/test/common/fns/randomVal.js b/test/common/fns/randomVal.js index f2e22cae61..631c71ab71 100644 --- a/test/common/fns/randomVal.js +++ b/test/common/fns/randomVal.js @@ -4,116 +4,52 @@ import { } from '../../helpers/common.helper'; describe('shared.fns.randomVal', () => { - let user; - let obj = { - a: 1, - b: 2, - c: 3, - d: 4, - }; + let user, obj; beforeEach(() => { user = generateUser(); + obj = { + a: 1, + b: 2, + c: 3, + d: 4, + }; }); - describe('returns a random property value from an object', () => { - it('returns the same value when the seed is the same', () => { - let val1 = randomVal(user, obj, { - seed: 222, - }); - - let val2 = randomVal(user, obj, { - seed: 222, - }); - - expect(val2).to.equal(val1); - }); - - it('returns the same value when user.stats is the same', () => { - user.stats.gp = 34; - let val1 = randomVal(user, obj); - let val2 = randomVal(user, obj); - - expect(val2).to.equal(val1); - }); - - it('returns a different value when the seed is different', () => { - let val1 = randomVal(user, obj, { - seed: 222, - }); - - let val2 = randomVal(user, obj, { - seed: 333, - }); - - expect(val2).to.not.equal(val1); - }); - - it('returns a different value when user.stats is different', () => { - user.stats.gp = 34; - let val1 = randomVal(user, obj); - user.stats.gp = 343; - let val2 = randomVal(user, obj); - - expect(val2).to.not.equal(val1); - }); + afterEach(() => { + sandbox.restore(); }); - describe('returns a random key from an object', () => { - it('returns the same key when the seed is the same', () => { - let key1 = randomVal(user, obj, { - key: true, - seed: 222, - }); + it('returns a random value from an object', () => { + let result = randomVal(user, obj); + expect(result).to.be.oneOf([1, 2, 3, 4]); + }); - let key2 = randomVal(user, obj, { - key: true, - seed: 222, - }); + it('uses Math.random to determine the property', () => { + sandbox.spy(Math, 'random'); - expect(key2).to.equal(key1); + randomVal(user, obj); + + expect(Math.random).to.be.calledOnce; + }); + + it('can pass in a custom random function that takes in the user and a seed argument', () => { + let randomSpy = sandbox.stub().returns(0.3); + sandbox.spy(Math, 'random'); + + let result = randomVal(user, obj, { + seed: 100, + randomFunc: randomSpy, }); - it('returns the same key when user.stats is the same', () => { - user.stats.gp = 45; - let key1 = randomVal(user, obj, { - key: true, - }); + expect(Math.random).to.not.be.called; + expect(randomSpy).to.be.calledOnce; + expect(randomSpy).to.be.calledWith(user, 100); + expect(result).to.equal(2); + }); - let key2 = randomVal(user, obj, { - key: true, - }); - - expect(key2).to.equal(key1); - }); - - it('returns a different key when the seed is different', () => { - let key1 = randomVal(user, obj, { - key: true, - seed: 222, - }); - - let key2 = randomVal(user, obj, { - key: true, - seed: 333, - }); - - expect(key2).to.not.equal(key1); - }); - - it('returns a different key when user.stats is different', () => { - user.stats.gp = 45; - let key1 = randomVal(user, obj, { - key: true, - }); - - user.stats.gp = 43; - - let key2 = randomVal(user, obj, { - key: true, - }); - - expect(key2).to.not.equal(key1); - }); + it('returns a random key when the key option is passed in', () => { + let result = randomVal(user, obj, { key: true }); + expect(result).to.be.oneOf(['a', 'b', 'c', 'd']); }); }); diff --git a/website/common/script/fns/randomDrop.js b/website/common/script/fns/randomDrop.js index 83bb585668..56f5ba893d 100644 --- a/website/common/script/fns/randomDrop.js +++ b/website/common/script/fns/randomDrop.js @@ -3,7 +3,6 @@ import content from '../content/index'; import i18n from '../i18n'; import { daysSince } from '../cron'; import { diminishingReturns } from '../statHelpers'; -import _predictableRandom from './predictableRandom'; import randomVal from './randomVal'; // Clone a drop object maintaining its functions so that we can change it without affecting the original item @@ -21,7 +20,7 @@ module.exports = function randomDrop (user, options, req = {}) { let rarity; let task; - let predictableRandom = options.predictableRandom || _predictableRandom; + let predictableRandom = options.predictableRandom || Math.random; task = options.task; chance = _.min([Math.abs(task.value - 21.27), 37.5]) / 150 + 0.02; diff --git a/website/common/script/fns/randomVal.js b/website/common/script/fns/randomVal.js index 2244d04558..bf40bcc8cf 100644 --- a/website/common/script/fns/randomVal.js +++ b/website/common/script/fns/randomVal.js @@ -1,12 +1,19 @@ import _ from 'lodash'; -import predictableRandom from './predictableRandom'; // Get a random property from an object // returns random property (the value) +function randomGenerator (user, seed, providedRandom) { + return providedRandom ? providedRandom(user, seed) : Math.random(); +} + module.exports = function randomVal (user, obj, options = {}) { let array = options.key ? _.keys(obj) : _.values(obj); - let rand = predictableRandom(user, options.seed); + let rand = randomGenerator(user, options.seed, options.randomFunc); + array.sort(); - return array[Math.floor(rand * array.length)]; + + let randomIndex = Math.floor(rand * array.length); + + return array[randomIndex]; };