fix(api,client): Pass in predictable random to revive randomVal calls

closes #8085
This commit is contained in:
Blade Barringer
2016-09-30 08:14:03 -05:00
parent 2476cdd873
commit 306505ebab
3 changed files with 10 additions and 16 deletions

View File

@@ -1,7 +1,4 @@
import randomVal from '../../../website/common/script/libs/randomVal'; import randomVal from '../../../website/common/script/libs/randomVal';
import {
generateUser,
} from '../../helpers/common.helper';
describe('randomVal', () => { describe('randomVal', () => {
let obj; let obj;
@@ -32,19 +29,14 @@ describe('randomVal', () => {
expect(Math.random).to.be.calledOnce; expect(Math.random).to.be.calledOnce;
}); });
it('can pass in a custom random function that takes in the user and a seed argument', () => { it('can pass in a predictable random value', () => {
let user = generateUser();
let randomSpy = sandbox.stub().returns(0.3);
sandbox.spy(Math, 'random'); sandbox.spy(Math, 'random');
let result = randomVal(obj, { let result = randomVal(obj, {
user, predictableRandom: 0.3,
seed: 100,
predictableRandom: randomSpy,
}); });
expect(Math.random).to.not.be.called; expect(Math.random).to.not.be.called;
expect(randomSpy).to.be.calledOnce;
expect(result).to.equal(2); expect(result).to.equal(2);
}); });

View File

@@ -8,7 +8,7 @@ function trueRandom () {
// returns random property (the value) // returns random property (the value)
module.exports = function randomVal (obj, options = {}) { module.exports = function randomVal (obj, options = {}) {
let array = options.key ? _.keys(obj) : _.values(obj); let array = options.key ? _.keys(obj) : _.values(obj);
let random = (options.predictableRandom || trueRandom)(); let random = options.predictableRandom || trueRandom();
array.sort(); array.sort();

View File

@@ -5,9 +5,7 @@ import {
NotAuthorized, NotAuthorized,
} from '../libs/errors'; } from '../libs/errors';
import randomVal from '../libs/randomVal'; import randomVal from '../libs/randomVal';
import predictableRandom from '../fns/predictableRandom';
// TODO this is only used on the server
// move out of common?
module.exports = function revive (user, req = {}, analytics) { module.exports = function revive (user, req = {}, analytics) {
if (user.stats.hp > 0) { if (user.stats.hp > 0) {
@@ -29,7 +27,9 @@ module.exports = function revive (user, req = {}, analytics) {
m[k] = k; m[k] = k;
} }
return m; return m;
}, {})); }, {}), {
predictableRandom: predictableRandom(user),
});
if (lostStat) { if (lostStat) {
user.stats[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 message = '';
let item = content.gear.flat[lostItem]; let item = content.gear.flat[lostItem];