mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
fix(api,client): Pass in predictable random to revive randomVal calls
closes #8085
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user