-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom.ts
More file actions
48 lines (45 loc) · 1.33 KB
/
random.ts
File metadata and controls
48 lines (45 loc) · 1.33 KB
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
import { createGame } from "./server/game.js";
import Random from "./server/solo/Random.js";
import { isMainThread, Worker, parentPort } from "worker_threads";
if (process.argv.includes("--sync")) {
let index = 0;
(async () => {
while (true) {
try {
const players: [Random, Random] = [new Random(0, 1), new Random(1, 1)];
players[0].timeout = false;
players[1].timeout = false;
await createGame(players, (winner) => {
console.log(`Game ${index++} winner: ${winner}`);
});
} catch (e) {
throw e;
}
}
})()
} else if (!isMainThread) {
parentPort?.on("message", async (index: string) => {
const players: [Random, Random] = [new Random(0, 1), new Random(1, 1)];
players[0].timeout = false;
players[1].timeout = false;
await createGame(players, (winner) => {
parentPort?.postMessage({ index, winner });
});
});
} else {
let index = 0;
for (let i = 0; i < 16; i++) {
new Promise(async () => {
while (true) {
const worker = new Worker("./random.js");
worker.postMessage(index++);
await new Promise((resolve) => {
worker.on("message", (res) => {
console.log(`Game ${res.index} winner: ${res.winner}`);
resolve(undefined);
});
});
}
});
}
}