refactor: move randomVal to a lib
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import randomVal from '../../../website/common/script/fns/randomVal';
|
||||
import randomVal from '../../../website/common/script/libs/randomVal';
|
||||
import {
|
||||
generateUser,
|
||||
} from '../../helpers/common.helper';
|
||||
|
||||
describe('shared.fns.randomVal', () => {
|
||||
describe('randomVal', () => {
|
||||
let obj;
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
@@ -29,12 +29,12 @@ describe('shared.ops.buyGear', () => {
|
||||
stats: { gp: 200 },
|
||||
});
|
||||
|
||||
sinon.stub(shared.fns, 'randomVal');
|
||||
sinon.stub(shared, 'randomVal');
|
||||
sinon.stub(shared.fns, 'predictableRandom');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
shared.fns.randomVal.restore();
|
||||
shared.randomVal.restore();
|
||||
shared.fns.predictableRandom.restore();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import handleTwoHanded from './handleTwoHanded';
|
||||
import predictableRandom from './predictableRandom';
|
||||
import crit from './crit';
|
||||
import randomVal from './randomVal';
|
||||
import randomDrop from './randomDrop';
|
||||
import autoAllocate from './autoAllocate';
|
||||
import updateStats from './updateStats';
|
||||
@@ -11,7 +10,6 @@ module.exports = {
|
||||
handleTwoHanded,
|
||||
predictableRandom,
|
||||
crit,
|
||||
randomVal,
|
||||
randomDrop,
|
||||
autoAllocate,
|
||||
updateStats,
|
||||
|
||||
@@ -3,7 +3,7 @@ import content from '../content/index';
|
||||
import i18n from '../i18n';
|
||||
import { daysSince } from '../cron';
|
||||
import { diminishingReturns } from '../statHelpers';
|
||||
import randomVal from './randomVal';
|
||||
import randomVal from '../libs/randomVal';
|
||||
|
||||
// TODO This is only used on the server
|
||||
// move to user model as an instance method?
|
||||
|
||||
@@ -91,12 +91,14 @@ api.statsComputed = statsComputed;
|
||||
import shops from './libs/shops';
|
||||
api.shops = shops;
|
||||
|
||||
import randomVal from './libs/randomVal';
|
||||
api.randomVal = randomVal;
|
||||
|
||||
import autoAllocate from './fns/autoAllocate';
|
||||
import crit from './fns/crit';
|
||||
import handleTwoHanded from './fns/handleTwoHanded';
|
||||
import predictableRandom from './fns/predictableRandom';
|
||||
import randomDrop from './fns/randomDrop';
|
||||
import randomVal from './fns/randomVal';
|
||||
import resetGear from './fns/resetGear';
|
||||
import ultimateGear from './fns/ultimateGear';
|
||||
import updateStats from './fns/updateStats';
|
||||
@@ -107,7 +109,6 @@ api.fns = {
|
||||
handleTwoHanded,
|
||||
predictableRandom,
|
||||
randomDrop,
|
||||
randomVal,
|
||||
resetGear,
|
||||
ultimateGear,
|
||||
updateStats,
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import _ from 'lodash';
|
||||
|
||||
// Get a random property from an object
|
||||
// returns random property (the value)
|
||||
|
||||
function trueRandom () {
|
||||
return Math.random();
|
||||
}
|
||||
|
||||
// Get a random property from an object
|
||||
// returns random property (the value)
|
||||
module.exports = function randomVal (obj, options = {}) {
|
||||
let array = options.key ? _.keys(obj) : _.values(obj);
|
||||
let random = (options.predictableRandom || trueRandom)();
|
||||
@@ -6,7 +6,7 @@ import splitWhitespace from '../libs/splitWhitespace';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
import randomVal from '../fns/randomVal';
|
||||
import randomVal from '../libs/randomVal';
|
||||
|
||||
// TODO this is only used on the server
|
||||
// move out of common?
|
||||
|
||||
@@ -4,7 +4,7 @@ import _ from 'lodash';
|
||||
import {
|
||||
NotAuthorized,
|
||||
} from '../libs/errors';
|
||||
import randomVal from '../fns/randomVal';
|
||||
import randomVal from '../libs/randomVal';
|
||||
|
||||
// TODO this is only used on the server
|
||||
// move out of common?
|
||||
|
||||
@@ -609,7 +609,7 @@ schema.methods._processCollectionQuest = async function processCollectionQuest (
|
||||
let itemsFound = {};
|
||||
|
||||
_.times(progress.collectedItems, () => {
|
||||
let item = shared.fns.randomVal(quest.collect, {key: true});
|
||||
let item = shared.randomVal(quest.collect, {key: true});
|
||||
|
||||
if (!itemsFound[item]) {
|
||||
itemsFound[item] = 0;
|
||||
|
||||
Reference in New Issue
Block a user