-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
634 lines (534 loc) · 22.6 KB
/
server.js
File metadata and controls
634 lines (534 loc) · 22.6 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
console.log("Starting server script...");
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cors = require('cors');
const path = require('path');
const app = express();
app.use(cors());
// Serve static files from the React app build directory
app.use(express.static(path.join(__dirname, 'dist')));
process.on('uncaughtException', (err) => {
console.error('UNCAUGHT EXCEPTION:', err);
});
process.on('unhandledRejection', (reason, p) => {
console.error('Unhandled Rejection at:', p, 'reason:', reason);
});
const server = http.createServer(app);
const io = new Server(server, {
cors: { origin: "*", methods: ["GET", "POST"] }
});
// --- GAME CONFIGURATION ---
const TEAMS = ['A', 'B', 'C'];
const MAX_HP = 100;
const GAME_DURATION = 300; // 5 mins
const SPAWN_RATE = 2500;
const REQUEST_LIFETIME = 12000;
console.log("Server configuration loaded. Bot logic enabled.");
// --- GAME STATE ---
let countdownTimer = null;
let gameState = {
status: 'LOBBY', // LOBBY, COUNTDOWN, PLAYING, GAMEOVER
timeLeft: GAME_DURATION,
countdown: null,
fillBots: false, // Default to false
teams: {
A: { hp: MAX_HP, cache: 20, score: 0, cooldowns: {}, targetPos: 100 },
B: { hp: MAX_HP, cache: 20, score: 0, cooldowns: {}, targetPos: 100 },
C: { hp: MAX_HP, cache: 20, score: 0, cooldowns: {}, targetPos: 100 }
},
requests: [], // Shared request pool
gameResult: null,
players: {} // socketId -> { name, team, role }
};
// Attack Costs & Cooldowns
const ATTACKS = {
SHUFFLE: { cost: 30, cooldown: 10000 },
FREEZE: { cost: 60, cooldown: 20000 },
GHOST: { cost: 45, cooldown: 30000 }
};
// --- GAME LOOP (Runs 10x per second) ---
setInterval(() => {
if (gameState.status !== 'PLAYING') return;
const now = Date.now();
// 1. Timer Logic
if (now % 1000 < 100) { // Approx once per second
gameState.timeLeft--;
if (gameState.timeLeft <= 0) endGame('TIMEOUT');
}
// 2. Request Aging & Explosions
gameState.requests.forEach(req => {
const age = now - req.birth;
// Status updates
if (age > REQUEST_LIFETIME * 0.8) req.status = 'critical';
else if (age > REQUEST_LIFETIME * 0.5) req.status = 'warning';
// Explosion
if (age >= REQUEST_LIFETIME) {
req.dead = true;
if (!req.isFake) {
// Find owner team (requests are assigned to specific teams or shared?
// In this logic, requests spawn for specific teams)
if (gameState.teams[req.team]) {
gameState.teams[req.team].hp -= 10;
io.to(req.team).emit('log', { text: `CRITICAL: Request exploded! -10 HP`, type: 'danger' });
if (gameState.teams[req.team].hp <= 0) {
checkElimination();
}
}
}
}
});
// Cleanup dead requests
gameState.requests = gameState.requests.filter(r => !r.dead);
// 3. Spawning Logic (Randomly every ~SPAWN_RATE)
if (Math.random() < (100 / SPAWN_RATE)) { // Rough probability based on tick rate
TEAMS.forEach(teamId => {
// Spawn independent requests for each team so they don't fight over the same blocks
gameState.requests.push({
id: `req-${Date.now()}-${Math.random()}`,
team: teamId,
sector: Math.floor(Math.random() * 200),
birth: Date.now(),
status: 'fresh',
isFake: false,
highlighted: false
});
});
io.emit('requests_update', gameState.requests);
}
// 4. Server-Side Bot Logic (Fill missing roles)
if (gameState.fillBots) {
TEAMS.forEach(teamId => {
const teamPlayers = Object.values(gameState.players).filter(p => p.team === teamId && p.connected);
const hasScheduler = teamPlayers.some(p => p.role === 'SCHEDULER');
const hasHacker = teamPlayers.some(p => p.role === 'HACKER');
// SCHEDULER BOT
if (!hasScheduler) {
gameState.requests.forEach(req => {
// Bot highlights requests that are warning or critical, or randomly highlights fresh ones
if (req.team === teamId && !req.highlighted && !req.isFake) {
if (req.status === 'critical' || req.status === 'warning') {
req.highlighted = true;
} else if (Math.random() < 0.05) { // 5% chance per tick to highlight fresh requests
req.highlighted = true;
}
}
});
}
// HACKER BOT
if (!hasHacker) {
const teamState = gameState.teams[teamId];
// 2% chance per tick to try an attack if cache is sufficient for at least one attack
if (teamState.cache >= 30 && Math.random() < 0.02) {
const atkKeys = Object.keys(ATTACKS);
// Filter attacks we can afford
const affordableAttacks = atkKeys.filter(key => teamState.cache >= ATTACKS[key].cost);
if (affordableAttacks.length > 0) {
const randomAtkKey = affordableAttacks[Math.floor(Math.random() * affordableAttacks.length)];
const attack = ATTACKS[randomAtkKey];
// Check Cooldown
const readyTime = teamState.cooldowns[randomAtkKey] || 0;
if (now >= readyTime) {
// Pick Target
const rivals = TEAMS.filter(t => t !== teamId && gameState.teams[t].hp > 0);
if (rivals.length > 0) {
const targetTeam = rivals[Math.floor(Math.random() * rivals.length)];
// Execute Attack
teamState.cache -= attack.cost;
teamState.cooldowns[randomAtkKey] = now + attack.cooldown;
// Apply Effects
io.to(targetTeam).emit('debuff_received', { type: randomAtkKey });
io.to(targetTeam).emit('log', { text: "WARNING: Unknown intrusion detected!", type: 'danger' });
io.to(teamId).emit('log', { text: `[BOT] Attack Successful: ${randomAtkKey} on Team ${targetTeam}`, type: 'success' });
if (randomAtkKey === 'GHOST') {
for(let i=0; i<5; i++) {
gameState.requests.push({
id: `fake-${Date.now()}-${Math.random()}`,
team: targetTeam,
sector: Math.floor(Math.random() * 200),
birth: Date.now(),
status: 'fresh',
isFake: true,
highlighted: true
});
}
}
}
}
}
}
}
});
}
// 5. Broadcast State (Optimized Split)
// A. Global State (Lightweight - HP, Score, Time) -> Broadcast to ALL
const globalTeams = {};
TEAMS.forEach(t => {
globalTeams[t] = {
hp: gameState.teams[t].hp,
score: gameState.teams[t].score
};
});
io.emit('game_tick', {
teams: globalTeams,
timeLeft: gameState.timeLeft
});
// B. Team Specific State (Heavy - Requests, Cache, Cooldowns) -> Send to Team Rooms
TEAMS.forEach(teamId => {
const teamRequests = gameState.requests.filter(r => r.team === teamId);
io.to(teamId).emit('team_data', {
cache: gameState.teams[teamId].cache,
cooldowns: gameState.teams[teamId].cooldowns,
targetPos: gameState.teams[teamId].targetPos,
requests: teamRequests
});
});
}, 100);
function resetGame() {
console.log("Resetting game state...");
if (countdownTimer) clearInterval(countdownTimer);
gameState.status = 'LOBBY';
gameState.countdown = null;
gameState.requests = [];
gameState.gameResult = null;
// Reset players ready state
Object.values(gameState.players).forEach(p => p.ready = false);
TEAMS.forEach(t => {
gameState.teams[t].hp = MAX_HP;
gameState.teams[t].score = 0;
gameState.teams[t].cache = 20;
gameState.teams[t].cooldowns = {};
gameState.teams[t].targetPos = 100;
});
io.emit('lobby_update', gameState.players);
io.emit('init_game', gameState);
}
function endGame(reason) {
gameState.status = 'GAMEOVER';
// 1. Filter for ALIVE teams first (Dead teams shouldn't win by score if others survived)
let candidates = Object.entries(gameState.teams).filter(([id, t]) => t.hp > 0);
// 2. If everyone is dead (Total System Failure), consider everyone for the "best of the worst"
if (candidates.length === 0) {
candidates = Object.entries(gameState.teams);
}
// 3. Sort by Score (Highest First)
const winnerEntry = candidates.sort((a,b) => b[1].score - a[1].score)[0];
const winner = winnerEntry ? winnerEntry[0] : 'None';
gameState.gameResult = { winner: `Team ${winner}`, reason };
io.emit('game_over', gameState.gameResult);
// Auto-reset after 10 seconds
setTimeout(() => {
if (gameState.status === 'GAMEOVER') {
resetGame();
}
}, 10000);
}
function checkElimination() {
const aliveTeams = TEAMS.filter(t => gameState.teams[t].hp > 0);
if (aliveTeams.length === 1) {
gameState.status = 'GAMEOVER';
gameState.gameResult = { winner: `Team ${aliveTeams[0]}`, reason: 'Last Team Standing' };
io.emit('game_over', gameState.gameResult);
// Auto-reset after 10 seconds
setTimeout(() => {
if (gameState.status === 'GAMEOVER') {
resetGame();
}
}, 10000);
} else if (aliveTeams.length === 0) {
endGame('Total System Failure');
}
}
// --- SOCKET HANDLERS ---
io.on('connection', (socket) => {
const playerId = socket.handshake.auth.playerId;
console.log('User connected:', socket.id, 'PlayerID:', playerId);
// Check for reconnection
let existingPlayer = Object.values(gameState.players).find(p => p.playerId === playerId);
if (existingPlayer) {
console.log(`Player ${existingPlayer.name} reconnected.`);
// Update socket ID mapping
// Remove old socket key
const oldSocketId = existingPlayer.id;
delete gameState.players[oldSocketId];
// Update player object
existingPlayer.id = socket.id;
existingPlayer.connected = true;
if (existingPlayer.disconnectTimeout) {
clearTimeout(existingPlayer.disconnectTimeout);
delete existingPlayer.disconnectTimeout;
}
// Add to new socket key
gameState.players[socket.id] = existingPlayer;
// Re-join team room
socket.join(existingPlayer.team);
// Send rejoin success
socket.emit('rejoin_success', {
team: existingPlayer.team,
role: existingPlayer.role,
name: existingPlayer.name,
state: gameState
});
// Broadcast update (to show they are online)
io.emit('lobby_update', gameState.players);
}
socket.on('join_lobby', ({ team, role, name }) => {
// 1. Check Team Size
const teamPlayers = Object.values(gameState.players).filter(p => p.team === team);
if (teamPlayers.length >= 3) {
socket.emit('log', { text: 'Team is full (Max 3)', type: 'danger' });
return;
}
// 2. Check Role Availability
const roleTaken = teamPlayers.find(p => p.role === role);
if (roleTaken) {
socket.emit('log', { text: `Role ${role} is already taken in Team ${team}`, type: 'danger' });
return;
}
// Leave previous team room if exists to prevent cross-talk
const previousPlayerState = gameState.players[socket.id];
if (previousPlayerState && previousPlayerState.team) {
socket.leave(previousPlayerState.team);
}
socket.join(team); // Join "Team A" room
// Store player info
gameState.players[socket.id] = {
id: socket.id,
playerId: playerId, // Store persistent ID
name: name || 'Unknown',
team,
role,
ready: false,
connected: true
};
// Broadcast updated player list to everyone
io.emit('lobby_update', gameState.players);
// Send initial game state to the new joiner
socket.emit('init_game', gameState);
});
socket.on('leave_team', () => {
const player = gameState.players[socket.id];
if (player) {
console.log(`Player ${player.name} left the team.`);
socket.leave(player.team);
delete gameState.players[socket.id];
io.emit('lobby_update', gameState.players);
}
});
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
const player = gameState.players[socket.id];
if (player) {
player.connected = false;
// Give them 30 seconds to reconnect before removing
player.disconnectTimeout = setTimeout(() => {
if (!player.connected) {
console.log(`Player ${player.name} timed out. Removing.`);
delete gameState.players[socket.id];
io.emit('lobby_update', gameState.players);
// Auto-reset if lobby is empty
if (Object.keys(gameState.players).length === 0) {
console.log("Lobby empty. Resetting game state.");
if (countdownTimer) clearInterval(countdownTimer);
gameState.status = 'LOBBY';
gameState.countdown = null;
gameState.requests = [];
gameState.gameResult = null;
TEAMS.forEach(t => {
gameState.teams[t].hp = MAX_HP;
gameState.teams[t].score = 0;
gameState.teams[t].cache = 20;
});
}
}
}, 30000);
}
});
socket.on('toggle_ready', () => {
const player = gameState.players[socket.id];
if (!player) return;
player.ready = !player.ready;
io.emit('lobby_update', gameState.players);
// Check Auto-Start
const players = Object.values(gameState.players);
if (players.length === 0) return;
// 1. Are ALL players ready?
const allReady = players.every(p => p.ready);
if (!allReady) {
if (gameState.status === 'COUNTDOWN') {
console.log("Countdown cancelled.");
clearInterval(countdownTimer);
gameState.status = 'LOBBY';
gameState.countdown = null;
io.emit('init_game', gameState);
}
return;
}
// 2. Validate Team Composition
const teamCounts = { A: 0, B: 0, C: 0 };
players.forEach(p => {
if (teamCounts[p.team] !== undefined) teamCounts[p.team]++;
});
const activeTeams = Object.entries(teamCounts).filter(([t, count]) => count > 0);
// Need at least one team
if (activeTeams.length === 0) return;
const invalidTeams = activeTeams.filter(([t, count]) => count < 3);
if (invalidTeams.length > 0 && !gameState.fillBots) {
const teamNames = invalidTeams.map(t => t[0]).join(', ');
// Only spam log if everyone is ready but teams are invalid
io.emit('log', { text: `Waiting for full teams: ${teamNames} need 3 players.`, type: 'warning' });
return;
}
if (gameState.status !== 'COUNTDOWN') {
console.log("All players ready. Starting countdown...");
gameState.status = 'COUNTDOWN';
gameState.countdown = 5;
io.emit('init_game', gameState);
if (countdownTimer) clearInterval(countdownTimer);
countdownTimer = setInterval(() => {
gameState.countdown--;
io.emit('countdown_tick', gameState.countdown);
if (gameState.countdown <= 0) {
clearInterval(countdownTimer);
console.log("Countdown finished. Starting game...");
gameState.status = 'PLAYING';
gameState.timeLeft = GAME_DURATION;
gameState.requests = [];
TEAMS.forEach(t => {
gameState.teams[t].hp = MAX_HP;
gameState.teams[t].score = 0;
gameState.teams[t].cache = 20;
});
io.emit('game_start');
}
}, 1000);
}
});
socket.on('reset_lobby', () => {
console.log("Reset lobby requested");
resetGame();
});
socket.on('toggle_bots', () => {
console.log(`[${socket.id}] Toggling bots. Old: ${gameState.fillBots}`);
gameState.fillBots = !gameState.fillBots;
console.log(`New Bot State: ${gameState.fillBots}`);
io.emit('init_game', gameState); // Broadcast new bot state
io.emit('log', { text: `Bots ${gameState.fillBots ? 'Enabled' : 'Disabled'}`, type: 'info' });
});
// Driver Movement (Relay to teammates only)
socket.on('driver_input', ({ team, targetPos }) => {
const player = gameState.players[socket.id];
// Security Check: Ensure player exists, is on the claimed team, and is a DRIVER
if (!player || player.team !== team || player.role !== 'DRIVER') {
// console.warn(`Unauthorized driver input from ${socket.id} (Role: ${player?.role}, Team: ${player?.team}) trying to control ${team}`);
return;
}
// Dead Check
if (gameState.teams[team].hp <= 0) return;
// Broadcast to everyone in Team A room so Scheduler/Hacker see the arm move
// console.log(`[${team}] Driver moved to ${targetPos}`);
if (gameState.teams[team]) {
gameState.teams[team].targetPos = targetPos;
}
socket.to(team).emit('arm_update', { targetPos, team });
});
// Scheduler Actions
socket.on('highlight_request', ({ reqId }) => {
const player = gameState.players[socket.id];
if (!player || player.role !== 'SCHEDULER') return;
if (gameState.teams[player.team].hp <= 0) return;
const req = gameState.requests.find(r => r.id === reqId);
// Only allow highlighting requests for their own team
if (req && req.team === player.team) {
req.highlighted = !req.highlighted;
}
});
socket.on('drop_request', ({ reqId, team }) => {
const player = gameState.players[socket.id];
// Validate player is Scheduler and on the correct team
if (!player || player.role !== 'SCHEDULER' || player.team !== team) return;
if (gameState.teams[team].hp <= 0) return;
const req = gameState.requests.find(r => r.id === reqId);
// Double check request belongs to team
if (req && req.team === team) {
gameState.requests = gameState.requests.filter(r => r.id !== reqId);
gameState.teams[team].hp = Math.max(0, gameState.teams[team].hp - 5);
io.to(team).emit('log', { text: "Request dropped manually. -5 HP", type: 'warning' });
}
});
const MAX_CACHE = 100;
// Service Success (Client claims they caught it)
socket.on('service_success', ({ reqId, team }) => {
const player = gameState.players[socket.id];
// Only allow team members (or specifically Driver) to claim success
if (!player || player.team !== team) return;
if (gameState.teams[team].hp <= 0) return;
const reqIndex = gameState.requests.findIndex(r => r.id === reqId);
if (reqIndex > -1) {
const req = gameState.requests[reqIndex];
// Verify request belongs to team
if (req.team !== team) return;
if (req.isFake) {
io.to(team).emit('service_feedback', { sector: req.sector, text: "GHOST! 0 PTS", color: "purple" });
} else {
gameState.teams[team].score += 100;
gameState.teams[team].cache = Math.min(MAX_CACHE, gameState.teams[team].cache + 10);
io.to(team).emit('service_feedback', { sector: req.sector, text: "+100 PTS", color: "green" });
}
gameState.requests.splice(reqIndex, 1);
}
});
// Hacker Attacks
socket.on('attack', ({ team, target, type }) => {
const player = gameState.players[socket.id];
// Validate Hacker Role and Team
if (!player || player.role !== 'HACKER' || player.team !== team) return;
if (gameState.teams[team].hp <= 0) return;
if (!target || (target !== 'ALL' && !TEAMS.includes(target))) {
console.log(`Attack failed: Invalid target '${target}' from ${player.name}`);
return;
}
const attack = ATTACKS[type];
const teamState = gameState.teams[team];
// Validate Cost
let cost = attack.cost;
if (target === 'ALL') cost *= 2;
if (teamState.cache >= cost) {
teamState.cache -= cost;
// Apply Effects
const targets = target === 'ALL' ? TEAMS.filter(t => t !== team) : [target];
targets.forEach(t => {
console.log(`Sending debuff ${type} to team ${t}`);
io.to(t).emit('debuff_received', { type });
io.to(t).emit('log', { text: "WARNING: Unknown intrusion detected!", type: 'danger' });
// Logic for specific attacks
if (type === 'GHOST') {
// Spawn fake requests for victim
for(let i=0; i<5; i++) {
gameState.requests.push({
id: `fake-${Date.now()}-${Math.random()}`,
team: t,
sector: Math.floor(Math.random() * 200),
birth: Date.now(),
status: 'fresh',
isFake: true,
highlighted: true // Auto-highlighted to trick Driver
});
}
} else if (type === 'SHUFFLE') {
// Since requests are just an array, shuffling logic would need to happen on client
// or we shuffle the array here.
// For simplicity, we just tell client to shuffle their view.
}
});
io.to(team).emit('log', { text: `Attack Successful: ${type}`, type: 'success' });
}
});
});
// Handle React routing, return all requests to React app
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
const PORT = process.env.PORT || 3000;
console.log("Attempting to listen on port " + PORT);
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));