-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (86 loc) · 2.3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
const { CustomError } = require("@mehra/ts");
const { appendFileSync } = require("fs");
const { Team, Players, Game } = require(".");
const { createPlayer, RecordedRandom } = require("./dist/script");
const parseOptions = () => {
let [
,
,
maxNumIterations = 1000,
progressInterval = Math.floor(+maxNumIterations / 100),
continueOnSuccess = false,
divideProgressLogByInterval = false,
] = process.argv;
console.log("Given options", {
maxNumIterations,
progressInterval,
continueOnSuccess,
divideProgressLogByInterval,
});
maxNumIterations = +maxNumIterations;
progressInterval = +progressInterval;
continueOnSuccess = !!continueOnSuccess;
divideProgressLogByInterval = !!divideProgressLogByInterval;
console.log("Using options", {
maxNumIterations,
progressInterval,
continueOnSuccess,
divideProgressLogByInterval,
});
return {
maxNumIterations,
progressInterval,
continueOnSuccess,
divideProgressLogByInterval,
};
};
const {
maxNumIterations,
progressInterval,
continueOnSuccess,
divideProgressLogByInterval,
} = parseOptions();
class NotCallableError extends CustomError {}
const APlayers = [Players.Weedle].map(
createPlayer(true, () => {
throw new NotCallableError();
})
);
const BPlayers = [
Players.Gengar1,
Players.Golbat,
Players.Haunter,
Players.Arbok,
Players.Gengar2,
].map(
createPlayer(false, () => {
throw new NotCallableError();
})
);
const recordedRandom = RecordedRandom();
const random = recordedRandom.random;
console.time();
for (let i = 0; i < maxNumIterations; i++) {
recordedRandom.reset();
if (i % progressInterval === 0)
console.timeLog(
undefined,
divideProgressLogByInterval ? i / progressInterval : i
);
APlayers.forEach((player) => player.reset(random));
BPlayers.forEach((player) => player.reset(random));
const A = new Team([...APlayers], random);
const B = new Team([...BPlayers], random);
if (new Game(A, B, random).play()) {
console.timeLog(undefined, "success", i, recordedRandom.getLog());
appendFileSync(
"avi.log",
`${new Date().toISOString()} success: ${i} randoms: ${JSON.stringify(
recordedRandom.getLog()
)}\n`
);
if (!continueOnSuccess) break;
}
}
console.timeEnd();