Skip to content

Commit

Permalink
feat: migrate strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre Haller committed Feb 26, 2020
1 parent 50eaa3b commit b300076
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 122 deletions.
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import distribution from './strategies/distribution';

import executeInSeries from './utils/executeInSeries';

const species = [typer()];
const species = [clicker(), formFiller()];

const mogwais = [fps()];

const strategies = [bySpecies()];
const strategies = [distribution()];

const defaultConfig = {
species,
Expand All @@ -36,7 +36,7 @@ export default userConfig => {
const { logger, randomizer } = config;
const species = config.species.map(specie => specie(logger, randomizer));
const mogwais = config.mogwais.map(mogwai => mogwai(logger));
const strategies = config.strategies.map(strat => strat(species));
const strategies = config.strategies.map(strat => strat(randomizer));

const unleash = async () => {
const gremlinsAndMogwais = [...species, ...mogwais];
Expand All @@ -47,7 +47,8 @@ export default userConfig => {

// const horde = config.strategies.map(strat => strat.apply(null, [species].concat(params)));
await executeInSeries(beforeHorde, []);
await Promise.all(strategies);
const unleashedStrategies = strategies.map(strat => strat(species));
await Promise.all(unleashedStrategies);
// await executeInSeries(afterHorde, []);
};

Expand Down
3 changes: 2 additions & 1 deletion src/species/clicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const getDefaultConfig = randomizer => {
showAction: defaultShowAction,
canClick: defaultCanClick,
maxNbTries: 10,
log: false,
};
};

Expand Down Expand Up @@ -97,7 +98,7 @@ export default userConfig => (logger, randomizer) => {
config.showAction(posX, posY, clickType);
}

if (logger && typeof logger.log === 'function') {
if (logger && config.log) {
logger.log('gremlin', 'clicker ', clickType, 'at', posX, posY);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/species/formFiller.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ const getDefaultConfig = randomizer => {
showAction: defaultShowAction,
canFillElement: defaultCanFillElement,
maxNbTries: 10,
log: false,
};
};

Expand Down Expand Up @@ -141,7 +142,7 @@ export default userConfig => (logger, randomizer) => {
config.showAction(element);
}

if (logger && typeof logger.log === 'function') {
if (logger && config.log) {
logger.log('gremlin', 'formFiller', 'input', value, 'in', element);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/species/scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const getDefaultConfig = randomizer => {
return {
positionSelector: defaultPositionSelector,
showAction: defaultShowAction,
log: false,
};
};

Expand All @@ -69,7 +70,7 @@ export default userConfig => (logger, randomizer) => {
config.showAction(scrollX, scrollY);
}

if (logger && typeof logger.log === 'function') {
if (logger && config.log) {
logger.log('gremlin', 'scroller ', 'scroll to', scrollX, scrollY);
}
};
Expand Down
25 changes: 7 additions & 18 deletions src/strategies/allTogether.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
import executeInSeries from '../utils/executeInSeries';
import configurable from '../utils/configurable';
import wait from '../utils/wait';

/**
* Execute all Gremlins species at once ; repeat 10ms after for 100 times
*
* const allTogetherStrategy = gremlins.strategies.allTogether();
* horde.strategy(allTogetherStrategy);
*
* The actual attack duration depends on the number of species in the horde.
*/
export default () => {
const config = {
export default userConfig => () => {
const defaultConfig = {
delay: 10, // delay in milliseconds between each wave
nb: 100, // number of waves to execute (can be overridden in params)
};

const config = { ...defaultConfig, ...userConfig };

let stopped;

const allTogetherStrategy = async (gremlins, params) => {
const nb = params && params.nb ? params.nb : config.nb;
const delay = params && params.delay ? params.delay : config.delay;
const horde = this;
const allTogetherStrategy = async gremlins => {
const { nb, delay } = config;

stopped = false;
for (let i = 0; i < nb; i++) {
await wait(delay);
if (stopped) {
return Promise.resolve();
}
await executeInSeries(gremlins, [], horde);
await executeInSeries(gremlins, []);
}
return Promise.resolve();
};
allTogetherStrategy.stop = () => {
stopped = true;
};

configurable(allTogetherStrategy, config);

return allTogetherStrategy;
};
56 changes: 9 additions & 47 deletions src/strategies/bySpecies.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,20 @@
import executeInSeries from '../utils/executeInSeries';
import configurable from '../utils/configurable';
import wait from '../utils/wait';

/**
* For each species, execute the gremlin 200 times, separated by a 10ms delay
*
* const bySpeciesStrategy = gremlins.strategies.bySpecies();
* horde.strategy(bySpeciesStrategy);
*
* The actual attack duration depends on the number of species in the horde.
*
* The bySpecies strategy can be customized as follows:
*
* bySpeciesStrategy.delay(10); // delay in milliseconds between each gremlin action
* bySpeciesStrategy.nb(200); // number times to execute each gremlin
*
* Example usage:
*
* horde
* .gremlin(gremlins.species.clicker())
* .gremlin(gremlins.species.formFiller())
* .strategy(gremlins.strategies.bySpecies()
* .delay(1000) // one action per second
* .nb(10) // each gremlin will act 10 times
* )
* .unleash();
*
* // t clickerGremlin clicked
* // t+1s clickerGremlin clicked
* // ...
* // t+9s clickerGremlin clicked
* // t+10s formFillerGremlin filled
* // t+11s formFillerGremlin filled
* // ...
* // t+19s formFillerGremlin filled
* // t+20s, end of the attack
*/
export default () => {
const config = {
delay: 10, // delay in milliseconds between each attack
nb: 200, // number of attacks to execute (can be overridden in params)
export default userConfig => () => {
const defaultConfig = {
delay: 10, // delay in milliseconds between each wave
nb: 100, // number of waves to execute (can be overridden in params)
};

const config = { ...defaultConfig, ...userConfig };

let stopped;

const bySpeciesStrategy = async (newGremlins, params) => {
console.log('by species called');
const nb = params && params.nb ? params.nb : config.nb;
const delay = params && params.delay ? params.delay : config.delay;
const bySpeciesStrategy = async newGremlins => {
const { nb, delay } = config;

const gremlins = [...newGremlins]; // clone the array to avoid modifying the original
const horde = this;

stopped = false;

Expand All @@ -61,7 +25,7 @@ export default () => {
if (stopped) {
return Promise.resolve();
}
await executeInSeries([gremlin], [], horde, delay);
await executeInSeries([gremlin], []);
}
}
return Promise.resolve();
Expand All @@ -71,7 +35,5 @@ export default () => {
stopped = true;
};

configurable(bySpeciesStrategy, config);

return bySpeciesStrategy;
};
60 changes: 10 additions & 50 deletions src/strategies/distribution.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
import Chance from 'chance';

import executeInSeries from '../utils/executeInSeries';
import configurable from '../utils/configurable';
import wait from '../utils/wait';

/**
* Execute all Gremlins randomly following a distribution, separated by a 10ms
* delay, for 100 times
*
* This is the default attack strategy, so selecting no strategy is equivalent to
*
* const distributionStrategy = gremlins.strategies.distribution();
* horde.strategy(distributionStrategy);
*
* The attack duration is roughly equivalent to delay * nb, although setTimeout
* may make it longer when delay is small.
*
* By default, this strategy uses a uniform distribution, i.e. all gremlins
* have an equal chance to be selected for the next action.
*
* The distribution strategy can be customized as follows:
*
* distributionStrategy.distribution([0.25, 0.25, 0.25, 0.25]); // chances for each gremlin to be selected ; total must equal 1
* distributionStrategy.delay(10); // delay in milliseconds between each wave
* distributionStrategy.nb(100); // number of waves to execute (can be overridden in params)
* distributionStrategy.randomizer(randomizerObject); // inject a randomizer
*
* Example usage:
*
* horde.strategy(gremlins.strategies.distribution()
* .delay(50)
* .distribution([
* 0.3, // first gremlin
* 0.3, // second gremlin
* 0.3, // third gremlin
* 0.1, // fourth gremlin
* ])
* )
*/
export default () => {
const config = {
export default userConfig => randomizer => {
const defaultConfig = {
distribution: [], // percentage of each gremlin species ; the sum of all values should equal to 1
delay: 10, // delay in milliseconds between each wave
nb: 1000, // number of waves to execute (can be overridden in params)
randomizer: new Chance(),
delay: 10,
nb: 1000,
};

const config = { ...defaultConfig, ...userConfig };

let stopped;

const distributionStrategy = async (newGremlins, params) => {
const nb = params && params.nb ? params.nb : config.nb;
const delay = params && params.delay ? params.delay : config.delay;
const horde = params.horde;
const distributionStrategy = async newGremlins => {
const { nb, delay } = config;

const gremlins = [...newGremlins]; // clone the array to avoid modifying the original
const distribution = config.distribution.length === 0 ? getUniformDistribution(gremlins) : config.distribution;
Expand All @@ -65,7 +27,7 @@ export default () => {
if (stopped) {
return Promise.resolve();
}
await executeInSeries([gremlin], [], horde);
await executeInSeries([gremlin], []);
}
return Promise.resolve();
};
Expand All @@ -83,7 +45,7 @@ export default () => {

const pickGremlin = (newGremlins, distribution) => {
let chance = 0;
const random = config.randomizer.floating({ min: 0, max: 1 });
const random = randomizer.floating({ min: 0, max: 1 });
for (let i = 0, count = newGremlins.length; i < count; i++) {
chance += distribution[i];
if (random <= chance) return newGremlins[i];
Expand All @@ -96,7 +58,5 @@ export default () => {
stopped = true;
};

configurable(distributionStrategy, config);

return distributionStrategy;
};

0 comments on commit b300076

Please sign in to comment.