Nafiul Huda commited on
Update server.js
Browse files
server.js
CHANGED
|
@@ -2,433 +2,140 @@ const express = require('express');
|
|
| 2 |
const http = require('http');
|
| 3 |
const { Server } = require('socket.io');
|
| 4 |
const fs = require('fs');
|
| 5 |
-
const path = require('path');
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const server = http.createServer(app);
|
| 9 |
-
const io = new Server(server, {
|
| 10 |
-
cors: { origin: "*" },
|
| 11 |
-
pingInterval: 2000,
|
| 12 |
-
pingTimeout: 5000
|
| 13 |
-
});
|
| 14 |
|
| 15 |
app.use(express.static('public'));
|
| 16 |
|
| 17 |
-
//
|
| 18 |
-
const
|
| 19 |
-
const TICK_INTERVAL = 1000 / TICK_RATE;
|
| 20 |
-
const GAME_WIDTH = 540;
|
| 21 |
-
const GAME_HEIGHT = 960;
|
| 22 |
-
const GRAVITY = 0.6;
|
| 23 |
-
const JUMP_FORCE = -17;
|
| 24 |
-
const SPRING_FORCE = -26;
|
| 25 |
-
const PLAYER_WIDTH = 40;
|
| 26 |
-
const PLAYER_HEIGHT = 50;
|
| 27 |
-
const PLATFORM_WIDTH = 70;
|
| 28 |
-
const PLATFORM_HEIGHT = 15;
|
| 29 |
-
const MOVE_SPEED = 8;
|
| 30 |
-
const MAX_FALL_SPEED = 20;
|
| 31 |
-
|
| 32 |
-
// Leaderboard file path
|
| 33 |
-
const LEADERBOARD_PATH = process.env.NODE_ENV === 'production'
|
| 34 |
-
? '/app/data/leaderboard.json'
|
| 35 |
-
: './leaderboard.json';
|
| 36 |
-
|
| 37 |
-
// Seeded random number generator for deterministic platform generation
|
| 38 |
-
class SeededRandom {
|
| 39 |
-
constructor(seed) {
|
| 40 |
-
this.seed = seed % 2147483647;
|
| 41 |
-
if (this.seed <= 0) this.seed += 2147483646;
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
next() {
|
| 45 |
-
this.seed = (this.seed * 16807) % 2147483647;
|
| 46 |
-
return (this.seed - 1) / 2147483646;
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
nextRange(min, max) {
|
| 50 |
-
return min + this.next() * (max - min);
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
nextInt(min, max) {
|
| 54 |
-
return Math.floor(this.nextRange(min, max));
|
| 55 |
-
}
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
// Global game seed
|
| 59 |
-
const WORLD_SEED = 12345;
|
| 60 |
-
const CHUNK_SIZE = 500;
|
| 61 |
-
|
| 62 |
-
// Platform types
|
| 63 |
-
const PLATFORM_NORMAL = 0;
|
| 64 |
-
const PLATFORM_MOVING = 1;
|
| 65 |
-
const PLATFORM_BREAKABLE = 2;
|
| 66 |
-
const PLATFORM_SPRING = 3;
|
| 67 |
-
|
| 68 |
-
// Generate platforms for a chunk
|
| 69 |
-
function generateChunk(chunkIndex) {
|
| 70 |
-
const rng = new SeededRandom(WORLD_SEED + chunkIndex * 7919);
|
| 71 |
-
const platforms = [];
|
| 72 |
-
const chunkTop = -chunkIndex * CHUNK_SIZE;
|
| 73 |
-
const chunkBottom = chunkTop + CHUNK_SIZE;
|
| 74 |
-
|
| 75 |
-
// Difficulty scaling
|
| 76 |
-
const difficulty = Math.min(chunkIndex / 20, 1);
|
| 77 |
-
const platformCount = Math.max(4, Math.floor(8 - difficulty * 3));
|
| 78 |
-
const verticalSpacing = CHUNK_SIZE / platformCount;
|
| 79 |
-
|
| 80 |
-
for (let i = 0; i < platformCount; i++) {
|
| 81 |
-
const y = chunkTop + i * verticalSpacing + rng.nextRange(10, verticalSpacing - 30);
|
| 82 |
-
const x = rng.nextRange(20, GAME_WIDTH - PLATFORM_WIDTH - 20);
|
| 83 |
-
|
| 84 |
-
// Determine platform type
|
| 85 |
-
let type = PLATFORM_NORMAL;
|
| 86 |
-
const typeRoll = rng.next();
|
| 87 |
-
|
| 88 |
-
if (chunkIndex > 2) {
|
| 89 |
-
if (typeRoll < 0.15 * difficulty) {
|
| 90 |
-
type = PLATFORM_BREAKABLE;
|
| 91 |
-
} else if (typeRoll < 0.3 * difficulty) {
|
| 92 |
-
type = PLATFORM_MOVING;
|
| 93 |
-
} else if (typeRoll < 0.1 + 0.1 * difficulty) {
|
| 94 |
-
type = PLATFORM_SPRING;
|
| 95 |
-
}
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
platforms.push({
|
| 99 |
-
id: `${chunkIndex}_${i}`,
|
| 100 |
-
x: x,
|
| 101 |
-
y: y,
|
| 102 |
-
width: PLATFORM_WIDTH,
|
| 103 |
-
height: PLATFORM_HEIGHT,
|
| 104 |
-
type: type,
|
| 105 |
-
broken: false,
|
| 106 |
-
moveDir: type === PLATFORM_MOVING ? (rng.next() > 0.5 ? 1 : -1) : 0,
|
| 107 |
-
moveSpeed: type === PLATFORM_MOVING ? rng.nextRange(1.5, 3) : 0,
|
| 108 |
-
originalX: x
|
| 109 |
-
});
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
return platforms;
|
| 113 |
-
}
|
| 114 |
|
| 115 |
-
//
|
| 116 |
-
|
| 117 |
-
const platformCache = new Map();
|
| 118 |
-
const brokenPlatforms = new Set();
|
| 119 |
-
|
| 120 |
-
// Get platforms for a range
|
| 121 |
-
function getPlatformsInRange(minY, maxY) {
|
| 122 |
-
const minChunk = Math.floor(-maxY / CHUNK_SIZE);
|
| 123 |
-
const maxChunk = Math.floor(-minY / CHUNK_SIZE) + 1;
|
| 124 |
-
const platforms = [];
|
| 125 |
-
|
| 126 |
-
for (let i = Math.max(0, minChunk); i <= maxChunk; i++) {
|
| 127 |
-
if (!platformCache.has(i)) {
|
| 128 |
-
platformCache.set(i, generateChunk(i));
|
| 129 |
-
}
|
| 130 |
-
platforms.push(...platformCache.get(i));
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
return platforms.filter(p => !brokenPlatforms.has(p.id));
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
// Load leaderboard
|
| 137 |
-
function loadLeaderboard() {
|
| 138 |
try {
|
| 139 |
-
if (fs.existsSync(
|
| 140 |
-
|
| 141 |
-
return JSON.parse(data);
|
| 142 |
}
|
| 143 |
-
} catch (e) {
|
| 144 |
-
|
| 145 |
-
}
|
| 146 |
-
return [];
|
| 147 |
}
|
| 148 |
|
| 149 |
-
// Save
|
| 150 |
-
function
|
| 151 |
try {
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
fs.mkdirSync(dir, { recursive: true });
|
| 155 |
-
}
|
| 156 |
-
fs.writeFileSync(LEADERBOARD_PATH, JSON.stringify(leaderboard, null, 2));
|
| 157 |
-
} catch (e) {
|
| 158 |
-
console.error('Failed to save leaderboard:', e);
|
| 159 |
-
}
|
| 160 |
}
|
| 161 |
|
| 162 |
-
let
|
|
|
|
| 163 |
|
| 164 |
-
//
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
this.x = GAME_WIDTH / 2 - PLAYER_WIDTH / 2;
|
| 170 |
-
this.y = 0;
|
| 171 |
-
this.vx = 0;
|
| 172 |
-
this.vy = 0;
|
| 173 |
-
this.score = 0;
|
| 174 |
-
this.highestY = 0;
|
| 175 |
-
this.input = { left: false, right: false };
|
| 176 |
-
this.lastProcessedInput = 0;
|
| 177 |
-
this.alive = true;
|
| 178 |
-
this.color = this.generateColor();
|
| 179 |
-
this.facingRight = true;
|
| 180 |
-
}
|
| 181 |
-
|
| 182 |
-
generateColor() {
|
| 183 |
-
const hue = Math.random() * 360;
|
| 184 |
-
return `hsl(${hue}, 70%, 60%)`;
|
| 185 |
-
}
|
| 186 |
-
|
| 187 |
-
update(platforms) {
|
| 188 |
-
if (!this.alive) return;
|
| 189 |
-
|
| 190 |
-
// Apply input
|
| 191 |
-
if (this.input.left) {
|
| 192 |
-
this.vx = -MOVE_SPEED;
|
| 193 |
-
this.facingRight = false;
|
| 194 |
-
} else if (this.input.right) {
|
| 195 |
-
this.vx = MOVE_SPEED;
|
| 196 |
-
this.facingRight = true;
|
| 197 |
-
} else {
|
| 198 |
-
this.vx *= 0.85;
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
// Apply gravity
|
| 202 |
-
this.vy += GRAVITY;
|
| 203 |
-
this.vy = Math.min(this.vy, MAX_FALL_SPEED);
|
| 204 |
-
|
| 205 |
-
// Update position
|
| 206 |
-
this.x += this.vx;
|
| 207 |
-
this.y += this.vy;
|
| 208 |
-
|
| 209 |
-
// Screen wrap
|
| 210 |
-
if (this.x < -PLAYER_WIDTH) {
|
| 211 |
-
this.x = GAME_WIDTH;
|
| 212 |
-
} else if (this.x > GAME_WIDTH) {
|
| 213 |
-
this.x = -PLAYER_WIDTH;
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
// Platform collision (only when falling)
|
| 217 |
-
if (this.vy > 0) {
|
| 218 |
-
for (const platform of platforms) {
|
| 219 |
-
if (platform.broken) continue;
|
| 220 |
-
|
| 221 |
-
if (this.checkPlatformCollision(platform)) {
|
| 222 |
-
if (platform.type === PLATFORM_BREAKABLE) {
|
| 223 |
-
platform.broken = true;
|
| 224 |
-
brokenPlatforms.add(platform.id);
|
| 225 |
-
}
|
| 226 |
-
|
| 227 |
-
this.y = platform.y - PLAYER_HEIGHT;
|
| 228 |
-
this.vy = platform.type === PLATFORM_SPRING ? SPRING_FORCE : JUMP_FORCE;
|
| 229 |
-
break;
|
| 230 |
-
}
|
| 231 |
-
}
|
| 232 |
-
}
|
| 233 |
-
|
| 234 |
-
// Update score
|
| 235 |
-
const newScore = Math.floor(-this.y / 10);
|
| 236 |
-
if (newScore > this.score) {
|
| 237 |
-
this.score = newScore;
|
| 238 |
-
if (this.y < this.highestY) {
|
| 239 |
-
this.highestY = this.y;
|
| 240 |
-
}
|
| 241 |
-
}
|
| 242 |
-
|
| 243 |
-
// Check death (fell below screen)
|
| 244 |
-
const cameraY = this.highestY - GAME_HEIGHT * 0.4;
|
| 245 |
-
if (this.y > cameraY + GAME_HEIGHT + 100) {
|
| 246 |
-
this.alive = false;
|
| 247 |
-
}
|
| 248 |
-
}
|
| 249 |
-
|
| 250 |
-
checkPlatformCollision(platform) {
|
| 251 |
-
const playerBottom = this.y + PLAYER_HEIGHT;
|
| 252 |
-
const playerPrevBottom = playerBottom - this.vy;
|
| 253 |
-
|
| 254 |
-
return this.x + PLAYER_WIDTH > platform.x &&
|
| 255 |
-
this.x < platform.x + platform.width &&
|
| 256 |
-
playerPrevBottom <= platform.y &&
|
| 257 |
-
playerBottom >= platform.y &&
|
| 258 |
-
playerBottom <= platform.y + platform.height + 10;
|
| 259 |
-
}
|
| 260 |
-
|
| 261 |
-
reset() {
|
| 262 |
-
this.x = GAME_WIDTH / 2 - PLAYER_WIDTH / 2;
|
| 263 |
-
this.y = 0;
|
| 264 |
-
this.vx = 0;
|
| 265 |
-
this.vy = 0;
|
| 266 |
-
this.score = 0;
|
| 267 |
-
this.highestY = 0;
|
| 268 |
-
this.alive = true;
|
| 269 |
-
}
|
| 270 |
-
|
| 271 |
-
getState() {
|
| 272 |
-
return {
|
| 273 |
-
id: this.id,
|
| 274 |
-
username: this.username,
|
| 275 |
-
x: this.x,
|
| 276 |
-
y: this.y,
|
| 277 |
-
vx: this.vx,
|
| 278 |
-
vy: this.vy,
|
| 279 |
-
score: this.score,
|
| 280 |
-
alive: this.alive,
|
| 281 |
-
color: this.color,
|
| 282 |
-
facingRight: this.facingRight,
|
| 283 |
-
lastProcessedInput: this.lastProcessedInput
|
| 284 |
-
};
|
| 285 |
-
}
|
| 286 |
}
|
| 287 |
|
| 288 |
-
// Socket.io connection handling
|
| 289 |
io.on('connection', (socket) => {
|
| 290 |
-
|
|
|
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
// Send initial state
|
| 298 |
-
socket.emit('init', {
|
| 299 |
-
playerId: socket.id,
|
| 300 |
-
worldSeed: WORLD_SEED,
|
| 301 |
-
chunkSize: CHUNK_SIZE,
|
| 302 |
-
leaderboard: leaderboard.slice(0, 10)
|
| 303 |
-
});
|
| 304 |
-
|
| 305 |
-
console.log(`${username} joined the game`);
|
| 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 |
socket.on('disconnect', () => {
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
if (player.score > 0) {
|
| 347 |
-
const entry = {
|
| 348 |
-
username: player.username,
|
| 349 |
-
score: player.score,
|
| 350 |
-
date: Date.now()
|
| 351 |
-
};
|
| 352 |
-
|
| 353 |
-
leaderboard.push(entry);
|
| 354 |
-
leaderboard.sort((a, b) => b.score - a.score);
|
| 355 |
-
leaderboard = leaderboard.slice(0, 100);
|
| 356 |
-
saveLeaderboard(leaderboard);
|
| 357 |
-
}
|
| 358 |
-
}
|
| 359 |
-
players.delete(socket.id);
|
| 360 |
-
console.log(`Player disconnected: ${socket.id}`);
|
| 361 |
});
|
| 362 |
});
|
| 363 |
|
| 364 |
-
//
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
platform.x += platform.moveSpeed * platform.moveDir;
|
| 370 |
-
|
| 371 |
-
if (platform.x <= 20 || platform.x >= GAME_WIDTH - PLATFORM_WIDTH - 20) {
|
| 372 |
-
platform.moveDir *= -1;
|
| 373 |
-
}
|
| 374 |
-
}
|
| 375 |
-
}
|
| 376 |
-
}
|
| 377 |
-
}
|
| 378 |
-
|
| 379 |
-
// Game loop
|
| 380 |
-
let lastTick = Date.now();
|
| 381 |
-
|
| 382 |
-
function gameLoop() {
|
| 383 |
-
const now = Date.now();
|
| 384 |
-
const delta = now - lastTick;
|
| 385 |
-
|
| 386 |
-
if (delta >= TICK_INTERVAL) {
|
| 387 |
-
lastTick = now - (delta % TICK_INTERVAL);
|
| 388 |
-
|
| 389 |
-
// Update platforms
|
| 390 |
-
updatePlatforms();
|
| 391 |
-
|
| 392 |
-
// Update all players
|
| 393 |
-
for (const [, player] of players) {
|
| 394 |
-
if (player.alive) {
|
| 395 |
-
const platforms = getPlatformsInRange(
|
| 396 |
-
player.y - GAME_HEIGHT,
|
| 397 |
-
player.y + GAME_HEIGHT
|
| 398 |
-
);
|
| 399 |
-
player.update(platforms);
|
| 400 |
-
}
|
| 401 |
-
}
|
| 402 |
-
|
| 403 |
-
// Broadcast game state
|
| 404 |
-
const gameState = {
|
| 405 |
-
timestamp: now,
|
| 406 |
-
players: Array.from(players.values()).map(p => p.getState()),
|
| 407 |
-
brokenPlatforms: Array.from(brokenPlatforms)
|
| 408 |
-
};
|
| 409 |
-
|
| 410 |
-
io.emit('state', gameState);
|
| 411 |
}
|
| 412 |
-
|
| 413 |
-
setImmediate(gameLoop);
|
| 414 |
-
}
|
| 415 |
|
| 416 |
-
// Cleanup old
|
| 417 |
setInterval(() => {
|
| 418 |
-
const
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
for (const [chunk] of platformCache) {
|
| 423 |
-
if (chunk < activeMinChunk - 5) {
|
| 424 |
-
platformCache.delete(chunk);
|
| 425 |
}
|
| 426 |
}
|
| 427 |
-
|
|
|
|
| 428 |
|
| 429 |
-
// Start server
|
| 430 |
const PORT = process.env.PORT || 7860;
|
| 431 |
-
server.listen(PORT, '0.0.0.0', () => {
|
| 432 |
-
console.log(`Server running on port ${PORT}`);
|
| 433 |
-
gameLoop();
|
| 434 |
-
});
|
|
|
|
| 2 |
const http = require('http');
|
| 3 |
const { Server } = require('socket.io');
|
| 4 |
const fs = require('fs');
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const server = http.createServer(app);
|
| 8 |
+
const io = new Server(server, { cors: { origin: "*" } });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
app.use(express.static('public'));
|
| 11 |
|
| 12 |
+
// Data files
|
| 13 |
+
const DATA_FILE = './data.json';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
// Load data
|
| 16 |
+
function loadData() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try {
|
| 18 |
+
if (fs.existsSync(DATA_FILE)) {
|
| 19 |
+
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
|
|
|
| 20 |
}
|
| 21 |
+
} catch (e) {}
|
| 22 |
+
return { leaderboard: [], players: {} };
|
|
|
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
+
// Save data
|
| 26 |
+
function saveData() {
|
| 27 |
try {
|
| 28 |
+
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
| 29 |
+
} catch (e) {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
+
let data = loadData();
|
| 33 |
+
const activePlayers = new Map();
|
| 34 |
|
| 35 |
+
// Get client IP
|
| 36 |
+
function getIP(socket) {
|
| 37 |
+
return socket.handshake.headers['x-forwarded-for']?.split(',')[0].trim()
|
| 38 |
+
|| socket.handshake.address
|
| 39 |
+
|| socket.id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
}
|
| 41 |
|
|
|
|
| 42 |
io.on('connection', (socket) => {
|
| 43 |
+
const ip = getIP(socket);
|
| 44 |
+
console.log(`Connected: ${ip}`);
|
| 45 |
|
| 46 |
+
// Send current state
|
| 47 |
+
socket.emit('init', {
|
| 48 |
+
savedPlayer: data.players[ip] || null,
|
| 49 |
+
leaderboard: data.leaderboard.slice(0, 10)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
});
|
| 51 |
|
| 52 |
+
// Player joins/updates
|
| 53 |
+
socket.on('update', (playerData) => {
|
| 54 |
+
const player = {
|
| 55 |
+
id: socket.id,
|
| 56 |
+
ip: ip,
|
| 57 |
+
name: (playerData.name || 'Anon').substring(0, 12),
|
| 58 |
+
x: playerData.x,
|
| 59 |
+
y: playerData.y,
|
| 60 |
+
vy: playerData.vy,
|
| 61 |
+
score: playerData.score,
|
| 62 |
+
color: playerData.color,
|
| 63 |
+
alive: playerData.alive,
|
| 64 |
+
facingRight: playerData.facingRight
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
activePlayers.set(socket.id, player);
|
| 68 |
+
|
| 69 |
+
// Save progress for this IP
|
| 70 |
+
data.players[ip] = {
|
| 71 |
+
name: player.name,
|
| 72 |
+
score: player.score,
|
| 73 |
+
highScore: Math.max(player.score, data.players[ip]?.highScore || 0),
|
| 74 |
+
y: player.y,
|
| 75 |
+
x: player.x,
|
| 76 |
+
color: player.color,
|
| 77 |
+
lastSeen: Date.now()
|
| 78 |
+
};
|
| 79 |
});
|
| 80 |
|
| 81 |
+
// Game over - save to leaderboard
|
| 82 |
+
socket.on('gameover', (finalData) => {
|
| 83 |
+
const score = finalData.score || 0;
|
| 84 |
+
const name = (finalData.name || 'Anon').substring(0, 12);
|
| 85 |
+
|
| 86 |
+
if (score > 0) {
|
| 87 |
+
// Update leaderboard
|
| 88 |
+
const existing = data.leaderboard.find(e => e.ip === ip);
|
| 89 |
+
if (existing) {
|
| 90 |
+
if (score > existing.score) {
|
| 91 |
+
existing.score = score;
|
| 92 |
+
existing.name = name;
|
| 93 |
+
existing.date = Date.now();
|
| 94 |
+
}
|
| 95 |
+
} else {
|
| 96 |
+
data.leaderboard.push({ ip, name, score, date: Date.now() });
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
data.leaderboard.sort((a, b) => b.score - a.score);
|
| 100 |
+
data.leaderboard = data.leaderboard.slice(0, 50);
|
| 101 |
+
|
| 102 |
+
// Clear saved progress
|
| 103 |
+
if (data.players[ip]) {
|
| 104 |
+
data.players[ip].score = 0;
|
| 105 |
+
data.players[ip].y = 0;
|
| 106 |
+
data.players[ip].x = 270;
|
| 107 |
}
|
| 108 |
|
| 109 |
+
saveData();
|
| 110 |
+
io.emit('leaderboard', data.leaderboard.slice(0, 10));
|
| 111 |
}
|
| 112 |
});
|
| 113 |
|
| 114 |
socket.on('disconnect', () => {
|
| 115 |
+
activePlayers.delete(socket.id);
|
| 116 |
+
saveData();
|
| 117 |
+
console.log(`Disconnected: ${ip}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
});
|
| 119 |
});
|
| 120 |
|
| 121 |
+
// Broadcast all players at 30fps (enough for others)
|
| 122 |
+
setInterval(() => {
|
| 123 |
+
const players = Array.from(activePlayers.values());
|
| 124 |
+
if (players.length > 0) {
|
| 125 |
+
io.emit('players', players);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
+
}, 33);
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
// Cleanup old saved players (7 days)
|
| 130 |
setInterval(() => {
|
| 131 |
+
const week = Date.now() - 7 * 24 * 60 * 60 * 1000;
|
| 132 |
+
for (const ip in data.players) {
|
| 133 |
+
if (data.players[ip].lastSeen < week) {
|
| 134 |
+
delete data.players[ip];
|
|
|
|
|
|
|
|
|
|
| 135 |
}
|
| 136 |
}
|
| 137 |
+
saveData();
|
| 138 |
+
}, 3600000);
|
| 139 |
|
|
|
|
| 140 |
const PORT = process.env.PORT || 7860;
|
| 141 |
+
server.listen(PORT, '0.0.0.0', () => console.log(`Server on :${PORT}`));
|
|
|
|
|
|
|
|
|