forked from ably-labs/realtime-multiplayer-space-invaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-worker.js
336 lines (309 loc) · 8.66 KB
/
server-worker.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
const {
Worker,
isMainThread,
parentPort,
threadId,
workerData,
} = require("worker_threads");
const envConfig = require("dotenv").config();
const Ably = require("ably");
const p2 = require("p2");
const ABLY_API_KEY = process.env.ABLY_API_KEY;
const CANVAS_HEIGHT = 700;
const CANVAS_WIDTH = 1400;
const SHIP_PLATFORM = CANVAS_HEIGHT - 12;
const BULLET_PLATFORM = SHIP_PLATFORM - 32;
const PLAYER_VERTICAL_INCREMENT = 20;
const PLAYER_VERTICAL_MOVEMENT_UPDATE_INTERVAL = 1000;
const PLAYER_SCORE_INCREMENT = 5;
const P2_WORLD_TIME_STEP = 1 / 16;
const MIN_PLAYERS_TO_START_GAME = 6;
const GAME_TICKER_MS = 100;
let players = {};
let playerChannels = {};
let shipX = between(20, CANVAS_WIDTH - 20);
let shipY = SHIP_PLATFORM;
let colorIndex = 0;
let avatarTypes = ["A", "B", "C"];
let gameOn = false;
let alivePlayers = 0;
let totalPlayers = 0;
let gameRoomName = workerData.hostRoomCode + ":primary";
let roomCode = workerData.hostRoomCode;
let hostClientId = workerData.hostClientId;
let hostNickname = workerData.hostNickname;
let gameRoom;
let gameTickerOn = false;
let bulletTimer = 0;
let shipBody;
let world;
let shipVelocityTimer = 0;
let killerBulletId = "";
let copyOfShipBody = {
position: "",
velocity: "",
};
// instantiate to Ably
const realtime = new Ably.Realtime({
key: ABLY_API_KEY,
echoMessages: false,
});
// wait until connection with Ably is established
realtime.connection.once("connected", () => {
gameRoom = realtime.channels.get(gameRoomName);
// subscribe to new players entering the game
gameRoom.presence.subscribe("enter", (player) => {
console.log("new player");
let newPlayerId;
alivePlayers++;
totalPlayers++;
parentPort.postMessage({
roomName: roomCode,
totalPlayers: totalPlayers,
gameOn: gameOn,
});
newPlayerId = player.clientId;
playerChannels[newPlayerId] = realtime.channels.get(
workerData.hostRoomCode + ":clientChannel-" + player.clientId
);
if (++colorIndex == 6) {
colorIndex = 0;
}
if (totalPlayers == 1) {
gameTickerOn = true;
startGameDataTicker();
}
newPlayerObject = {
id: newPlayerId,
invaderAvatarType: avatarTypes[between(0, 3)],
invaderAvatarColor: randomColorGenerator(),
x: playerXposition(colorIndex),
y: 20,
score: 0,
nickname: player.data.nickname,
isAlive: true,
};
players[newPlayerId] = newPlayerObject;
subscribeToPlayerInput(playerChannels[newPlayerId], newPlayerId);
});
// subscribe to players leaving the game
gameRoom.presence.subscribe("leave", (player) => {
let leavingPlayer = player.clientId;
alivePlayers--;
totalPlayers--;
parentPort.postMessage({
roomName: roomCode,
totalPlayers: totalPlayers,
});
delete players[leavingPlayer];
if (totalPlayers <= 0) {
killWorkerThread();
}
});
gameRoom.publish("thread-ready", {
start: true,
});
});
// start the ship and bullets
function startShipAndBullets() {
gameOn = true;
world = new p2.World({
gravity: [0, -9.82],
});
shipBody = new p2.Body({
position: [shipX, shipY],
velocity: [calcRandomVelocity(), 0],
});
world.addBody(shipBody);
startMovingPhysicsWorld();
for (let playerId in players) {
startDownwardMovement(playerId);
}
}
// start the game tick
function startGameDataTicker() {
let tickInterval = setInterval(() => {
if (!gameTickerOn) {
clearInterval(tickInterval);
} else {
bulletOrBlank = "";
bulletTimer += GAME_TICKER_MS;
if (bulletTimer >= GAME_TICKER_MS * 5) {
bulletTimer = 0;
bulletOrBlank = {
y: BULLET_PLATFORM,
id: "bulletId-" + generateRandomId(),
};
}
if (shipBody) {
copyOfShipBody = shipBody;
}
// fan out the latest game state
gameRoom.publish("game-state", {
players: players,
playerCount: totalPlayers,
shipBody: copyOfShipBody.position,
bulletOrBlank: bulletOrBlank,
gameOn: gameOn,
killerBullet: killerBulletId,
});
}
}, GAME_TICKER_MS);
}
// subscribe to each player's input events
function subscribeToPlayerInput(channelInstance, playerId) {
channelInstance.subscribe("pos", (msg) => {
if (msg.data.keyPressed == "left") {
if (players[playerId].x - 20 < 20) {
players[playerId].x = 25;
} else {
players[playerId].x -= 20;
}
} else if (msg.data.keyPressed == "right") {
if (players[playerId].x + 20 > 1380) {
players[playerId].x = 1375;
} else {
players[playerId].x += 20;
}
}
});
channelInstance.subscribe("start-game", (msg) => {
startShipAndBullets();
});
// subscribe to players being shot
channelInstance.subscribe("dead-notif", (msg) => {
players[msg.data.deadPlayerId].isAlive = false;
killerBulletId = msg.data.killerBulletId;
alivePlayers--;
if (alivePlayers == 0) {
setTimeout(() => {
finishGame("");
}, 1000);
}
});
}
// update the y position of each player when the game starts
function startDownwardMovement(playerId) {
let interval = setInterval(() => {
if (players[playerId] && players[playerId].isAlive) {
players[playerId].y += PLAYER_VERTICAL_INCREMENT;
players[playerId].score += PLAYER_SCORE_INCREMENT;
if (players[playerId].y > SHIP_PLATFORM) {
finishGame(playerId);
clearInterval(interval);
}
} else {
clearInterval(interval);
}
}, PLAYER_VERTICAL_MOVEMENT_UPDATE_INTERVAL);
}
// finish the game
function finishGame(playerId) {
console.log("finished");
let firstRunnerUpName = "";
let secondRunnerUpName = "";
let winnerName = "Nobody";
let leftoverPlayers = new Array();
for (let item in players) {
leftoverPlayers.push({
nickname: players[item].nickname,
score: players[item].score,
});
}
leftoverPlayers.sort((a, b) => {
return b.score - a.score;
});
if (playerId == "") {
if (leftoverPlayers.length >= 3) {
firstRunnerUpName = leftoverPlayers[0].nickname;
secondRunnerUpName = leftoverPlayers[1].nickname;
} else if (leftoverPlayers == 2) {
firstRunnerUp = leftoverPlayers[0].nickname;
}
} else {
winnerName = players[playerId].nickname;
if (leftoverPlayers.length >= 3) {
firstRunnerUpName = leftoverPlayers[1].nickname;
secondRunnerUpName = leftoverPlayers[2].nickname;
} else if (leftoverPlayers.length == 2) {
firstRunnerUpName = leftoverPlayers[1].nickname;
}
}
// fan out leaderboard info when the game has finished
gameRoom.publish("game-over", {
winner: winnerName,
firstRunnerUp: firstRunnerUpName,
secondRunnerUp: secondRunnerUpName,
totalPlayers: totalPlayers,
});
killWorkerThread();
}
// reset all variables in the server
function killWorkerThread() {
parentPort.postMessage({
resetEntry: true,
roomName: roomCode,
});
for (let item in playerChannels) {
playerChannels[item].detach();
}
process.exit(0);
}
// assign player position per color to form pride rainbow
function playerXposition(index) {
switch (index) {
case 0:
return between(22, 246);
case 1:
return between(247, 474);
case 2:
return between(475, 702);
case 3:
return between(703, 930);
case 4:
return between(931, 1158);
case 5:
return between(1159, 1378);
default:
return between(22, 246);
}
}
function between(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function generateRandomId() {
return Math.random().toString(36).substr(2, 8);
}
// start the server-side physics world
function startMovingPhysicsWorld() {
let p2WorldInterval = setInterval(function () {
if (!gameOn) {
clearInterval(p2WorldInterval);
} else {
// updates velocity every 5 seconds
if (++shipVelocityTimer >= 80) {
shipVelocityTimer = 0;
shipBody.velocity[0] = calcRandomVelocity();
}
world.step(P2_WORLD_TIME_STEP);
if (shipBody.position[0] > 1400 && shipBody.velocity[0] > 0) {
shipBody.position[0] = 0;
} else if (shipBody.position[0] < 0 && shipBody.velocity[0] < 0) {
shipBody.position[0] = 1400;
}
}
}, 1000 * P2_WORLD_TIME_STEP);
}
// calculate a random horizontal velocity for the ship
function calcRandomVelocity() {
let randomShipXVelocity = between(20, 200);
randomShipXVelocity *= Math.floor(Math.random() * 2) == 1 ? 1 : -1;
return randomShipXVelocity;
}
// method to randomly generate a color for the player
function randomColorGenerator() {
const randomColor = "000000".replace(/0/g, function () {
return (~~(Math.random() * 16)).toString(16);
});
return "0x" + randomColor;
}