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();
            }
        });
    }
}

server.on('connection', (conn) => {
    console.log('Client connected:', conn.remoteAddress);
    connections.push(conn);

    conn.on('data', (data) => {
        try {
            const message = deserialize(data);
            const playerName = message.name;

            // Generate a unique spawn position
            const spawnPosition = getRandomSpawnPosition();

            // Initialize new player with a unique color and position
            players[conn.remoteAddress] = {
                name: playerName,
                position: spawnPosition,
                color: generateColor()
            };

            // Send initial positions and map layout to the new client
            conn.write(serialize({ players, map_layout: currentMapLayout }));

            conn.on('data', (data) => {
                const update = deserialize(data);
                players[conn.remoteAddress].position = update.position;

                // Send updated positions and map layout to all clients
                connections.forEach(playerConn => {
                    if (playerConn !== conn) {
                        playerConn.write(serialize({ players, map_layout: currentMapLayout }));
                    }
                });
            });

        } catch (e) {
            console.error(`Error processing data: ${e.message}`);
        }
    });

    conn.on('end', () => {
        console.log('Client disconnected:', conn.remoteAddress);
        delete players[conn.remoteAddress];
        connections.splice(connections.indexOf(conn), 1);

        // Inform remaining clients about the disconnected player
        connections.forEach(playerConn => {
            playerConn.write(serialize({ players }));
        });
    });
});

server.listen(PORT, HOST, () => {
    console.log(`Server listening on ${HOST}:${PORT}`);
    changeMapLayoutPeriodically();
});


give me client for that server use kaplayjs(fork of kaboomjs) and javascript 
import kaplay from "kaplay";
import "kaplay/global";

const k = kaplay();