const net = require('net'); const { randomInt } = require('crypto'); // For generating…
- User
const net = require('net'); const { randomInt } = require('crypto'); // For generating random integers const { promisify } = require('util'); const setTimeoutPromise = promisify(setTimeout);
const HOST = 'localhost'; const PORT = 25566;
const players = {}; const tileSize = 18;
const mapLayouts = [ [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] // Ground layer ], // Add additional predefined map layouts here [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 0, 0, 0, 0, 2, 0, 2, 0, 1, 1, 1, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] // Ground layer ] ];
const server = net.createServer(); const connections = []; let currentMapLayout = mapLayouts[0];
function serialize(data) { const json = JSON.stringify(data); const length = Buffer.byteLength(json); const buffer = Buffer.alloc(length + 4); buffer.writeUInt32BE(length, 0); buffer.write(json, 4); return buffer; }
function deserialize(buffer) { const length = buffer.readUInt32BE(0); return JSON.parse(buffer.toString('utf-8', 4, length + 4)); }
function getRandomSpawnPosition() { const x = randomInt(0, currentMapLayout[0].length); const y = randomInt(0, currentMapLayout.length); return { x: x * tileSize, y: y * tileSize }; }
function generateColor() { return { r: randomInt(0, 256), g: randomInt(0, 256), b: randomInt(0, 256) }; }
async function changeMapLayoutPeriodically() { let mapIndex = 0; while (true) { await setTimeoutPromise(60000); // Change map layout every 60 seconds currentMapLayout = mapLayouts[mapIndex]; mapIndex = (mapIndex + 1) % mapLayouts.length;
console.log("Map layout changed."); // Send the new map layout to all connected clients connections.forEach(playerConn => { try { playerConn.write(serialize({ players, map_layout: currentMapLayout })); } catch (e) { console.error(`Error sending new map layout: ${e.message}`); connections.splice(connections.indexOf(playerConn), 1); playerConn.end(); } }); } - Assistant