repl.it
Nodejs

No description

fork
loading
Files
  • index.js
  • client
  • Packager files
  • package-lock.json
  • package.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const players = [];
const world = [];

const express = require('express');
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);

server.listen(process.env.PORT || 8080);
app.use(express.static('client'));

io.on('connection', socket => {
	console.log(`${socket.id} joined`);
	players.push({
		id: socket.id,
		x: 0,
		y: 0
	});

	socket.on('build', block => {
		// world.push(data)
	});

	socket.on('clientstatechange', client => {
		const i = players.indexOf(players.find(p => p.id === client.id));
		if (i !== -1) {
			players[i].x += (client.d * 5) - (client.a * 5);
			players[i].y += (client.s * 5) - (client.w * 5);
			socket.emit('updatedstate', players);
		}
	});

	socket.on('disconnect', () => {
		console.log(`${socket.id} left`);
		players.splice(players.indexOf(players.find(p => p.id === socket.id)), 1);
	});
});