-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.js
46 lines (45 loc) · 2.14 KB
/
Game.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
// ReSharper disable UseOfImplicitGlobalInFunctionScope
// ReSharper disable AssignToImplicitGlobalInFunctionScope
shots = ['Ping', 'Pong'];
serverIndex = shotCount = pointCount = 0;
onCommandObject = async function (command) {
if (command.begin) {
players = [];
for (let index = 0; index < 2; ++index) {
players.push({ name: command.begin.playerNames[index], score: 0, worker: new Worker('Player.js') });
players[index].worker.onCommandObject = onPlayerCommandObject;
players[index].worker.postCommandObject({ setPlayerId: { playerId: index + 1 } });
}
Console.WriteLine(`First server: ${players[serverIndex = (Math.random() < 0.5) ? 0 : 1].name}.`);
players[serverIndex].worker.postCommandObject({ serve: {} });
}
}
onPlayerCommandObject = async function (command) {
if (command.serve) {
let index = command.serve.playerId - 1;
Console.WriteLine(`${players[index].name} serves: ${shots[shotCount = 0]}`);
players[index ^ 1].worker.postCommandObject({ handleShot: {} });
}
else if (command.makeShot) {
let index = command.makeShot.playerId - 1;
Console.WriteLine(`${players[index].name}: ${shots[++shotCount & 1]}`);
players[index ^ 1].worker.postCommandObject({ handleShot: {} });
}
else if (command.miss) {
let index = command.miss.playerId - 1;
Console.Write(`${players[index].name} misses.`);
const thisScore = players[index].score;
const otherScore = ++players[index ^ 1].score;
if ((otherScore >= 11) && ((otherScore - thisScore) >= 2)) {
Console.WriteLine(` ${players[index ^ 1].name} wins! Final score: ${players[index ^ 1].score}-${players[index].score}.`);
postExit();
} else {
if ((++pointCount % 2) === 0) {
Console.Write(` New server: ${players[serverIndex ^= 1].name}.`);
}
Console.WriteLine(` Score: ${players[serverIndex].score}-${players[serverIndex ^ 1].score}.`);
await delay(500);
players[serverIndex].worker.postCommandObject({ serve: {} });
}
}
};