hi,
to create multiplayer, you need an multiplayer server.
example of multiplayer server in Node.js (JavaScript) using fastify and REST API:
(unfortunately, CopperCube doesn't support sending POST, DELETE or PATCH requests, so I'm used only GET)
const fastify = require('fastify')({ logger: false }); const PORT = 3000;
let players = Object.create(null);
// starting the server (async () => { try { await fastify.listen(PORT); console.log(`Server is running on port 3000`); } catch (err) { fastify.log.error(err); process.exit(1); } })();
// set position of player // example: ccbDoHTTPRequest('http://localhost:3000/player/set/position?playerName=Nick&posX=90&posY=40&posZ=10');
fastify.get('/player/set/position', async (request, reply) => { const playerName = request.query.playerName; const playerPosition = { x: parseInt(request.query.posX), y: parseInt(request.query.posY), z: parseInt(request.query.posZ) };
if (!playerName) return { ok: false, message: 'Please provide `playerName` key in query' }
if (!request.query.posX || !request.query.posY || !request.query.posZ) return { ok: false, message: 'Please provide `posX`, `posY` and `posZ` keys in query' }
// setting player position in `players` object players[playerName].position = playerPosition;
// returning data to client return { ok: true, position: playerPosition }; });
// get position of player // example: ccbDoHTTPRequest('http://localhost:3000/player/get/position?playerName=Nick'); fastify.get('/player/get/position', async (request, reply) => { const playerName = request.query.playerName;
if (!playerName) return { ok: false, message: 'Please provide `playerName` key in query' }
// returning data to client return { ok: true, position: players[playerName].position }; });
|