-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
69 lines (61 loc) · 2.6 KB
/
server.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
require("dotenv").config(); // Load environment variables
const io = require("socket.io")(process.env.PORT || 3000, {
cors: {
origin: process.env.URL || "*", // Allow configured URL or all origins as fallback
methods: ["GET", "POST"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"]
},
});
let lightStates = {}; // Track light state for each slot
let slotParagraphs = {}; // Map slot IDs to assigned paragraphs
// Predefined paragraphs
const paragraphs = [
"In Squid Game, players must remain perfectly still when the giant doll turns around. Any movement detected means instant elimination.",
"The rules are simple: move during red light and you're eliminated. Only move when the light is green. Stay focused and control your movements.",
"456 players entered the game seeking the ultimate prize. Many failed at this first challenge, unable to control their trembling bodies.",
"The giant doll's song echoes through the field: 'Red light, green light, 1-2-3!' Each note could be your last if you're not careful.",
"The stakes are life and death. One wrong move, one slight tremor, and it's game over. Keep your eyes on the prize and stay absolutely still.",
];
// Utility function to randomly assign a paragraph to a slot
function assignRandomParagraph(slotId) {
if (!slotParagraphs[slotId]) {
const randomParagraph = paragraphs[Math.floor(Math.random() * paragraphs.length)];
slotParagraphs[slotId] = randomParagraph;
}
return slotParagraphs[slotId];
}
io.on("connection", (socket) => {
console.log("A user connected");
// Emit the current light state and assigned paragraph to the connected client
socket.on("request-light-state", (data) => {
const { slotId } = data;
const assignedParagraph = assignRandomParagraph(slotId); // Ensure paragraph is assigned
socket.emit("light-state", {
slotId,
isGreen: lightStates[slotId] || false,
paragraph: assignedParagraph,
});
});
// Admin toggles the light
socket.on("toggle-light", (data) => {
const { slotId } = data;
lightStates[slotId] = !lightStates[slotId];
io.emit("light-toggle", {
slotId,
isGreen: lightStates[slotId],
}); // Notify all clients
console.log(`Light for slot ${slotId} is now ${lightStates[slotId] ? "green" : "red"}`);
});
socket.on("score-update", (data) => {
// Broadcast the score update to all connected clients
io.emit("leaderboard-update", {
slotId: data.slotId,
username: data.username,
score: data.score
});
});
socket.on("disconnect", () => {
console.log("A user disconnected");
});
});