Nafiul Huda commited on
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,154 +1,94 @@
|
|
| 1 |
const express = require('express');
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
try {
|
| 17 |
-
if (fs.existsSync(DATA_FILE)) {
|
| 18 |
-
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
| 19 |
-
}
|
| 20 |
-
} catch (e) {}
|
| 21 |
-
return { leaderboard: [], players: {} };
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
// Save data
|
| 25 |
-
function saveData() {
|
| 26 |
-
try {
|
| 27 |
-
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
| 28 |
-
} catch (e) {}
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
let data = loadData();
|
| 32 |
-
const activePlayers = new Map();
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|| socket.handshake.address
|
| 37 |
-
|| socket.id;
|
| 38 |
-
}
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
// Send current state
|
| 44 |
-
socket.emit('init', {
|
| 45 |
-
savedPlayer: data.players[ip] || null,
|
| 46 |
-
leaderboard: data.leaderboard.slice(0, 10)
|
| 47 |
-
});
|
| 48 |
-
|
| 49 |
-
socket.on('update', (p) => {
|
| 50 |
-
// We now store velocity for smoother client-side interpolation
|
| 51 |
-
const player = {
|
| 52 |
id: socket.id,
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
x:
|
| 56 |
-
y:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
score: p.score,
|
| 60 |
-
color: p.color,
|
| 61 |
-
alive: p.alive,
|
| 62 |
-
facingRight: p.facingRight
|
| 63 |
};
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
// Save progress occasionally or on specific events to reduce disk I/O
|
| 68 |
-
if (Math.random() < 0.05) {
|
| 69 |
-
data.players[ip] = {
|
| 70 |
-
name: player.name,
|
| 71 |
-
score: player.score,
|
| 72 |
-
highScore: Math.max(player.score, data.players[ip]?.highScore || 0),
|
| 73 |
-
y: player.y,
|
| 74 |
-
x: player.x,
|
| 75 |
-
color: player.color,
|
| 76 |
-
lastSeen: Date.now()
|
| 77 |
-
};
|
| 78 |
-
}
|
| 79 |
});
|
| 80 |
-
|
| 81 |
-
socket.on('gameover', (finalData) => {
|
| 82 |
-
const score = finalData.score || 0;
|
| 83 |
-
const name = (finalData.name || 'Anon').substring(0, 12);
|
| 84 |
-
|
| 85 |
-
// Always save high score on death
|
| 86 |
-
if (data.players[ip]) {
|
| 87 |
-
data.players[ip].score = 0;
|
| 88 |
-
data.players[ip].y = 0;
|
| 89 |
-
data.players[ip].lastSeen = Date.now();
|
| 90 |
-
}
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
data.leaderboard.sort((a, b) => b.score - a.score);
|
| 105 |
-
data.leaderboard = data.leaderboard.slice(0, 50);
|
| 106 |
-
|
| 107 |
-
saveData();
|
| 108 |
-
io.emit('leaderboard', data.leaderboard.slice(0, 10));
|
| 109 |
}
|
| 110 |
});
|
| 111 |
-
|
|
|
|
| 112 |
socket.on('disconnect', () => {
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
});
|
| 115 |
});
|
| 116 |
|
| 117 |
-
//
|
|
|
|
| 118 |
setInterval(() => {
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
y: Math.round(p.y),
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
c: p.color,
|
| 130 |
-
f: p.facingRight ? 1 : 0
|
| 131 |
});
|
| 132 |
-
}
|
| 133 |
-
|
| 134 |
-
if (players.length > 0) {
|
| 135 |
-
// volatile emit: if client is lagging, drop packet (prevents TCP holdup)
|
| 136 |
-
io.volatile.emit('players', players);
|
| 137 |
-
}
|
| 138 |
-
}, 15);
|
| 139 |
|
| 140 |
-
|
| 141 |
-
setInterval(() => {
|
| 142 |
-
const week = Date.now() - 7 * 24 * 60 * 60 * 1000;
|
| 143 |
-
let changed = false;
|
| 144 |
-
for (const ip in data.players) {
|
| 145 |
-
if (data.players[ip].lastSeen < week) {
|
| 146 |
-
delete data.players[ip];
|
| 147 |
-
changed = true;
|
| 148 |
-
}
|
| 149 |
}
|
| 150 |
-
if (changed) saveData();
|
| 151 |
-
}, 3600000);
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const http = require('http');
|
| 3 |
const { Server } = require('socket.io');
|
|
|
|
| 4 |
|
| 5 |
const app = express();
|
| 6 |
const server = http.createServer(app);
|
| 7 |
const io = new Server(server, { cors: { origin: "*" } });
|
| 8 |
|
| 9 |
+
app.use(express.static('public')); // Serve index.html from a 'public' folder or root
|
| 10 |
|
| 11 |
+
// GLOBAL STATE
|
| 12 |
+
const players = {};
|
| 13 |
+
// Generate a new level seed every time the server restarts (or could be daily)
|
| 14 |
+
const GLOBAL_SEED = Math.floor(Math.random() * 1000000);
|
| 15 |
|
| 16 |
+
io.on('connection', (socket) => {
|
| 17 |
+
console.log(`Player connected: ${socket.id}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
// 1. Send configuration on connect
|
| 20 |
+
socket.emit('config', { seed: GLOBAL_SEED });
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
// 2. Player joins the game
|
| 23 |
+
socket.on('join', (data) => {
|
| 24 |
+
players[socket.id] = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
id: socket.id,
|
| 26 |
+
name: data.name.substring(0, 12) || "Guest",
|
| 27 |
+
skin: data.skin || "square",
|
| 28 |
+
x: 0,
|
| 29 |
+
y: 0,
|
| 30 |
+
score: 0,
|
| 31 |
+
isDead: false
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
};
|
| 33 |
|
| 34 |
+
// Broadcast new player to others (optional, mostly handled by update loop)
|
| 35 |
+
io.emit('playerList', Object.values(players));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
// 3. Receive player updates (High frequency)
|
| 39 |
+
socket.on('update', (data) => {
|
| 40 |
+
const p = players[socket.id];
|
| 41 |
+
if (p) {
|
| 42 |
+
p.x = data.x;
|
| 43 |
+
p.y = data.y;
|
| 44 |
+
p.vx = data.vx;
|
| 45 |
+
p.vy = data.vy;
|
| 46 |
+
p.skin = data.skin; // Update skin in case they changed it
|
| 47 |
+
p.score = data.score;
|
| 48 |
+
p.isDead = data.isDead;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
});
|
| 51 |
+
|
| 52 |
+
// 4. Handle Disconnect
|
| 53 |
socket.on('disconnect', () => {
|
| 54 |
+
console.log(`Player disconnected: ${socket.id}`);
|
| 55 |
+
delete players[socket.id];
|
| 56 |
+
io.emit('playerDisconnect', socket.id);
|
| 57 |
});
|
| 58 |
});
|
| 59 |
|
| 60 |
+
// BROADCAST LOOP (30 FPS)
|
| 61 |
+
// We send the state of all players to all clients
|
| 62 |
setInterval(() => {
|
| 63 |
+
// 1. Pack data to save bandwidth (array of arrays is smaller than array of objects)
|
| 64 |
+
// Format: [id, x, y, skin, isDead, score, name]
|
| 65 |
+
const pack = [];
|
| 66 |
+
const leaderboard = [];
|
| 67 |
+
|
| 68 |
+
for (const id in players) {
|
| 69 |
+
const p = players[id];
|
| 70 |
+
// Only send active players or recently dead ones
|
| 71 |
+
pack.push({
|
| 72 |
+
id: p.id,
|
| 73 |
+
x: Math.round(p.x), // Round to reduce float size
|
| 74 |
y: Math.round(p.y),
|
| 75 |
+
s: p.skin,
|
| 76 |
+
d: p.isDead
|
|
|
|
|
|
|
| 77 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
leaderboard.push({ name: p.name, score: p.score });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
// Sort leaderboard top 3
|
| 83 |
+
leaderboard.sort((a, b) => b.score - a.score);
|
| 84 |
+
const top3 = leaderboard.slice(0, 3);
|
| 85 |
+
|
| 86 |
+
io.emit('worldUpdate', { players: pack, lb: top3 });
|
| 87 |
+
|
| 88 |
+
}, 33);
|
| 89 |
+
|
| 90 |
+
const PORT = process.env.PORT || 3000;
|
| 91 |
+
server.listen(PORT, () => {
|
| 92 |
+
console.log(`Server running on port ${PORT}`);
|
| 93 |
+
console.log(`Level Seed: ${GLOBAL_SEED}`);
|
| 94 |
+
});
|