Nafiul Huda commited on
Update server.js
Browse files3 unmodified lines456789101112131415161718192021222324252627282930311 unmodified line3334353637383940414243444546474849505152535455565758596061626364656667686970714 unmodified lines76777879808182838485863 unmodified linesconst app = express();const server = http.createServer(app);const io = new Server(server, { cors: { origin: "*" } });app.use(express.static('public'));// GLOBAL STATEconst players = {};const GLOBAL_SEED = Math.floor(Math.random() * 1000000);io.on('connection', (socket) => {// 1. Send configuration on connectsocket.emit('config', { seed: GLOBAL_SEED });// 2. Player joinssocket.on('join', (data) => {players[socket.id] = {id: socket.id,name: data.name.substring(0, 12) || "Guest",skin: data.skin || "square",x: 0,y: 0,score: 0,isDead: false};});1 unmodified linesocket.on('update', (data) => {const p = players[socket.id];if (p) {p.x = data.x;p.y = data.y;p.vx = data.vx;p.vy = data.vy;p.skin = data.skin;p.score = data.score;p.isDead = data.isDead;}});// 4. Handle Disconnectsocket.on('disconnect', () => {delete players[socket.id];});});// BROADCAST LOOP (60 TPS for smoothness)setInterval(() => {const pack = [];const leaderboard = [];for (const id in players) {const p = players[id];// Pack data strictly needed for renderingpack.push({id: p.id,x: Math.round(p.x),y: Math.round(p.y),s: p.skin,d: p.isDead,n: p.name // Explicitly send name every tick to ensure it loads});if (!p.isDead) {leaderboard.push({ name: p.name, score: p.score });4 unmodified linesleaderboard.sort((a, b) => b.score - a.score);const top3 = leaderboard.slice(0, 3);io.emit('worldUpdate', { players: pack, lb: top3 });}, 16); // ~60msconst PORT = process.env.PORT || 3000;server.listen(PORT, () => {console.log(`Server running on port ${PORT}`);});3 unmodified lines456789101112131415161718192021222324252627282930313233343536371 unmodified line394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586874 unmodified lines92939495969798991001011021033 unmodified linesconst app = express();const server = http.createServer(app);const io = new Server(server, {cors: { origin: "*" },pingInterval: 2000,pingTimeout: 5000});app.use(express.static('public'));// GLOBAL STATEconst players = {};// A static seed ensuring everyone gets the exact same map logicconst GLOBAL_SEED = Math.floor(Math.random() * 1000000);io.on('connection', (socket) => {// 1. Send configuration immediatelysocket.emit('config', { seed: GLOBAL_SEED });// 2. Player joinssocket.on('join', (data) => {players[socket.id] = {id: socket.id,name: (data.name || "Guest").substring(0, 12),skin: data.skin || "square",x: 0,y: 0,score: 0,isDead: false,lastSeen: Date.now()};});1 unmodified linesocket.on('update', (data) => {const p = players[socket.id];if (p) {// Data comes in array: [x, y, vx, vy, score, isDead]p.x = data[0];p.y = data[1];p.vx = data[2];p.vy = data[3];p.score = data[4];p.isDead = data[5];p.lastSeen = Date.now();}});socket.on('disconnect', () => {delete players[socket.id];});});// BROADCAST LOOP (30 TPS)// 30 times a second is the sweet spot for browser games.// 60 TPS over internet often causes packet choking (the 0.1fps issue).setInterval(() => {const pack = [];const leaderboard = [];const now = Date.now();for (const id in players) {const p = players[id];// Timeout check (remove stale players)if (now - p.lastSeen > 10000) {delete players[id];continue;}// COMPRESSION: Send Array instead of Object// [id, x, y, skin, isDead, name]pack.push([p.id,Math.round(p.x),Math.round(p.y),p.skin,p.isDead ? 1 : 0,p.name]);if (!p.isDead) {leaderboard.push({ name: p.name, score: p.score });4 unmodified linesleaderboard.sort((a, b) => b.score - a.score);const top3 = leaderboard.slice(0, 3);// Send world stateio.emit('w', { p: pack, l: top3 });}, 33);const PORT = process.env.PORT || 3000;server.listen(PORT, () => {console.log(`Server running on port ${PORT} | Seed: ${GLOBAL_SEED}`);});
server.js+42-25