-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
361 lines (317 loc) · 12.3 KB
/
Copy pathserver.js
File metadata and controls
361 lines (317 loc) · 12.3 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// ============================================================
// server.js — Express + Socket.IO game server (Render-ready)
// ============================================================
const express = require('express');
const http = require('http');
const fs = require('fs');
const { Server } = require('socket.io');
const path = require('path');
const GameRoom = require('./game/GameRoom');
const RoomStore = require('./game/RoomStore');
const app = express();
const server = http.createServer(app);
// ---- Render / reverse-proxy support ----
// Trust the proxy so req.ip, req.protocol etc. are correct behind Render's load balancer
app.set('trust proxy', 1);
// Socket.IO — configured for Render:
// • cors: wildcard so any origin (Render URL or custom domain) can connect
// • transports: prefer WebSocket, fall back to polling (Render supports both)
// • pingTimeout / pingInterval tuned for Render's 30s idle timeout
const io = new Server(server, {
cors: { origin: '*', methods: ['GET', 'POST'] },
transports: ['websocket', 'polling'],
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 30000,
allowEIO3: true,
});
// Configure Redis adapter if REDIS_URL is provided, or try local Redis in development
const adapterRedisUrl = process.env.REDIS_URL || (process.env.NODE_ENV !== 'production' ? 'redis://127.0.0.1:6379' : null);
if (adapterRedisUrl) {
try {
const { createAdapter } = require('@socket.io/redis-adapter');
const { createClient } = require('redis');
const pubClient = createClient({ url: adapterRedisUrl });
const subClient = pubClient.duplicate();
Promise.all([pubClient.connect(), subClient.connect()])
.then(() => {
io.adapter(createAdapter(pubClient, subClient));
console.log(`[Socket.IO] Connected to Redis Pub/Sub adapter at ${adapterRedisUrl}.`);
})
.catch((err) => {
if (process.env.REDIS_URL) {
console.error('[Socket.IO] Redis adapter connection failed:', err.message);
} else {
console.log('[Socket.IO] Local Redis not detected, using in-memory Socket.IO adapter.');
}
});
} catch (err) {
console.error('[Socket.IO] Failed to load/configure Redis adapter:', err.message);
}
}
// Initialize RoomStore
const roomStore = new RoomStore();
roomStore.init().catch(err => {
console.error('[RoomStore] Initialization failed:', err.message);
});
// Use PORT from environment (Render injects this) or 3000 for local dev
const PORT = process.env.PORT || 3000;
const rooms = {}; // roomCode → GameRoom local cache
async function getRoom(roomCode) {
if (!roomCode) return null;
const code = roomCode.toUpperCase();
if (rooms[code]) {
return rooms[code];
}
const data = await roomStore.get(code);
if (data) {
const room = GameRoom.fromJSON(data, io, roomStore);
rooms[code] = room;
return room;
}
return null;
}
// Intercept root requests with join code and dynamic host name to serve index.html with Open Graph meta tags
app.get(['/', '/index.html'], async (req, res, next) => {
const host = req.query.host;
const join = req.query.join;
if (join) {
try {
const filePath = path.join(__dirname, 'public', 'index.html');
let html = await fs.promises.readFile(filePath, 'utf8');
const hostName = host ? decodeURIComponent(host) : 'a friend';
const origin = `${req.protocol}://${req.get('host')}`;
const ogTitle = `<meta property="og:title" content="Ludo Raja 🇳🇵 — Play with ${hostName}" />`;
const ogDesc = `<meta property="og:description" content="Join Room ${join} to play Ludo Raja with ${hostName} in real-time!" />`;
const ogImage = `<meta property="og:image" content="${origin}/favicon.png" />`;
const ogUrl = `<meta property="og:url" content="${origin}/?join=${join}&host=${encodeURIComponent(hostName)}" />`;
const ogType = `<meta property="og:type" content="website" />`;
const tags = `\n ${ogTitle}\n ${ogDesc}\n ${ogImage}\n ${ogUrl}\n ${ogType}`;
html = html.replace('</head>', `${tags}\n</head>`);
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
return res.send(html);
} catch (err) {
console.error('[OG Tag Injection Error]', err);
}
}
next();
});
// Serve static files
app.use(express.static(path.join(__dirname, 'public'), {
setHeaders: (res, path) => {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate');
}
}));
app.use(express.json());
// ---- Health check (required for Render) ----
app.get('/health', (_req, res) => res.status(200).json({ status: 'ok', uptime: process.uptime() }));
// ---- REST endpoints ----
// Create a room
app.post('/api/rooms', async (req, res) => {
const { maxPlayers } = req.body;
const roomCode = _generateCode();
const room = new GameRoom(roomCode, io, roomStore);
room.maxPlayers = maxPlayers || 4;
rooms[roomCode] = room;
await roomStore.saveRoom(room);
res.json({ roomCode });
});
// Check room exists
app.get('/api/rooms/:code', async (req, res) => {
const room = await getRoom(req.params.code);
if (!room) return res.status(404).json({ error: 'Room not found' });
res.json(room.getRoomInfo());
});
// Get game history for a finished room (1-day TTL)
app.get('/api/history/:code', async (req, res) => {
const history = await roomStore.getHistory(req.params.code);
if (!history) return res.status(404).json({ error: 'No history found for this room' });
res.json(history);
});
// ---- Socket.IO events ----
io.on('connection', (socket) => {
console.log(`[+] Socket connected: ${socket.id}`);
// Join a room as a player
socket.on('join-room', async ({ roomCode, name, color, userId }) => {
roomCode = roomCode.toUpperCase();
const room = await getRoom(roomCode);
if (!room) {
socket.emit('error', { message: 'Room not found' });
return;
}
// Determine player color based on userId or next free slot
const allowedColors = room.getAllowedColors();
let assignedColor = color;
if (assignedColor && !allowedColors.includes(assignedColor)) {
assignedColor = null;
}
if (room.userIdMap && room.userIdMap[userId]) {
assignedColor = room.userIdMap[userId];
}
if (!assignedColor || !allowedColors.includes(assignedColor)) {
const takenColors = Object.values(room.players).map(p => p.color);
const freeColor = allowedColors.find(c => !takenColors.includes(c));
if (!freeColor) {
socket.emit('error', { message: 'Room is full' });
return;
}
assignedColor = freeColor;
}
if (room.status !== 'waiting') {
// Allow re-joining any non-bot slot in a live or paused game.
const isRejoining = (room.status === 'playing' || room.status === 'paused') &&
room.slotConfig.some(s => s.color === assignedColor && !s.isBot);
if (!isRejoining) {
socket.emit('error', { message: 'Game already started' });
return;
}
} else {
// If lobby is waiting, make sure color isn't already taken by another human player
const existingSocketId = room.colorMap[assignedColor];
if (existingSocketId && room.players[existingSocketId] && existingSocketId !== socket.id) {
socket.emit('error', { message: `Color ${assignedColor.toUpperCase()} is already taken!` });
return;
}
}
socket.join(roomCode);
socket.roomCode = roomCode;
socket.playerColor = assignedColor;
await room.joinPlayer(socket.id, name, assignedColor, userId);
if (room.status === 'playing' || room.status === 'paused') {
const isPaused = room.status === 'paused';
// Send the current game state immediately to the rejoining player
socket.emit('game-started', {
slotConfig: room.slotConfig,
hostColor: room.hostColor,
theme: room.theme,
state: room.game.getState(),
isPaused,
});
// Send remaining timer only if game is actively running
if (!isPaused && room.timerStart) {
const elapsed = Date.now() - room.timerStart;
const remaining = Math.max(0, 30000 - elapsed);
socket.emit('timer-start', { duration: remaining, color: room.game.currentColor });
}
} else {
io.to(roomCode).emit('room-updated', room.getRoomInfo());
}
socket.emit('joined', { color: assignedColor, roomCode });
console.log(`[+] ${name} (${assignedColor}) joined room ${roomCode}`);
});
// Start the game
socket.on('start-game', async ({ roomCode, theme }) => {
roomCode = roomCode?.toUpperCase() || socket.roomCode;
const room = await getRoom(roomCode);
if (!room || room.hostSocketId !== socket.id) return;
// Automatically build slotConfig on start (empty slots become bots)
const slotConfig = room.buildStartSlotConfig();
// Register bot slots (colorMap = null)
for (const slot of slotConfig) {
if (slot.isBot && !room.colorMap[slot.color]) {
room.colorMap[slot.color] = null;
}
}
const state = await room.startGame(slotConfig, theme);
io.to(roomCode).emit('game-started', {
slotConfig: room.slotConfig, // enriched with real player names
hostColor: room.hostColor,
theme,
state,
});
// If first turn is a bot, handle it
room._handleBotTurnIfNeeded();
});
// Host pauses / resumes the game
socket.on('pause-game', async () => {
const room = await getRoom(socket.roomCode);
if (room) await room.pause(socket.id);
});
socket.on('resume-game', async () => {
const room = await getRoom(socket.roomCode);
if (room) await room.resume(socket.id);
});
// Host force-ends the game
socket.on('force-end-game', async () => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
await room.forceEnd(socket.id);
});
// Roll dice
socket.on('roll-dice', async () => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
await room.handleRoll(socket.id);
});
// Move token
socket.on('move-token', async ({ tokenId }) => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
await room.handleMove(socket.id, tokenId);
});
// Emoji reaction
socket.on('reaction', async ({ emoji }) => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
room.handleReaction(socket.id, emoji);
});
// Chat message
socket.on('chat', async ({ message }) => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
const player = room.players[socket.id];
if (!player) return;
io.to(roomCode).emit('chat', {
name: player.name,
color: player.color,
message: message.substring(0, 100),
ts: Date.now(),
});
});
// Disconnect
socket.on('disconnect', async () => {
const roomCode = socket.roomCode;
const room = await getRoom(roomCode);
if (!room) return;
await room.removePlayer(socket.id);
if (room.status === 'waiting') {
io.to(roomCode).emit('room-updated', room.getRoomInfo());
}
console.log(`[-] Socket disconnected: ${socket.id}`);
});
});
// ---- Start server ----
server.listen(PORT, '0.0.0.0', () => {
const env = process.env.NODE_ENV || 'development';
console.log('\n🎲 ==========================================');
console.log(' Premium Ludo Game Server Running!');
console.log(` Environment: ${env}`);
console.log('==========================================');
console.log(` Local: http://localhost:${PORT}`);
if (env !== 'production') {
console.log(` Network: Run 'ipconfig getifaddr en0' to get your WiFi IP`);
console.log(' Share the network URL with friends on same WiFi!');
} else {
console.log(' Deployed on Render — use your Render URL to play!');
}
console.log('==========================================\n');
});
// ---- Graceful shutdown (Render sends SIGTERM on deploy/scale) ----
process.on('SIGTERM', () => {
console.log('SIGTERM received — shutting down gracefully');
server.close(() => {
console.log('Server closed');
process.exit(0);
});
});
function _generateCode() {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
let code = '';
for (let i = 0; i < 6; i++) code += chars[Math.floor(Math.random() * chars.length)];
return code;
}