/
index.js
67 lines (56 loc) · 2.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Chance from 'chance';
import clicker from './species/clicker';
import toucher from './species/toucher';
import formFiller from './species/formFiller';
import scroller from './species/scroller';
import typer from './species/typer';
import alert from './mogwais/alert';
import fps from './mogwais/fps';
import gizmo from './mogwais/gizmo';
import distribution from './strategies/distribution';
import bySpecies from './strategies/bySpecies';
import allTogether from './strategies/allTogether';
import executeInSeries from './utils/executeInSeries';
const defaultConfig = {
species: [clicker(), formFiller(), toucher(), scroller(), typer()],
mogwais: [fps(), alert(), gizmo()],
strategies: [distribution()],
logger: console,
randomizer: new Chance(),
window: window,
};
export const createHorde = (userConfig) => {
const config = { ...defaultConfig, ...userConfig };
const { logger, randomizer, window } = config;
const speciesConfig = {
logger,
randomizer,
window,
};
const species = config.species.map((specie) => specie(speciesConfig));
const strategies = config.strategies.map((strat) => strat(randomizer));
const stop = () => strategies.forEach((strat) => strat.stop());
const mogwaisConfig = {
...speciesConfig,
stop,
};
const mogwais = config.mogwais.map((mogwai) => mogwai(mogwaisConfig));
const unleash = async () => {
const beforeHorde = [...mogwais];
const cleansUps = mogwais.map((mogwai) => mogwai.cleanUp).filter((cleanUp) => typeof cleanUp === 'function');
await executeInSeries(beforeHorde, []);
const unleashedStrategies = strategies.map((strat) => strat(species));
await Promise.all(unleashedStrategies);
await executeInSeries(cleansUps, []);
};
return {
unleash,
stop,
};
};
export const species = { clicker, toucher, formFiller, scroller, typer };
export const allSpecies = Object.values(species).map((specie) => specie());
export const mogwais = { alert, fps, gizmo };
export const allMogwais = Object.values(mogwais).map((mogwai) => mogwai());
export const strategies = { distribution, bySpecies, allTogether };
export { default as Chance } from 'chance';