repl.it
@rshetty/

project-pokeworld

Nodejs

No description

fork
loading
Files
  • index.js
  • public
  • LICENSE
  • manifest.json
  • package-lock.json
  • README.md
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);

players = {}; // Player list
queue = [] ;
// Use correct file directory
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});
 
server.listen(8081, function () {
  console.log(`Listening on ${server.address().port}`);
});

io.on('connection', function (socket) {
  console.log('a user connected');

	// Define a new player in the player list
	players[socket.id] = {
		rotation: 0,
		x: 300,
		y: 200,
		playerId: socket.id
	};

	// Send object to local player
	socket.emit('currentPlayers', players);
	// Send object to other players
	socket.broadcast.emit('newPlayer', players[socket.id]);

  socket.on('disconnect', function () {
    console.log('user disconnected');

		// Remove this player from our players object
		
		// Emit a message to all players to remove this player
		tempPlayer = players[socket.id];
		io.emit('disconnect', tempPlayer);
		delete players[socket.id];
  });

	// Update player positions on server
	socket.on('playerMovement', function (movementData) {
		players[socket.id].x = movementData.x;
		players[socket.id].y = movementData.y;
		players[socket.id].anim = movementData.anim;
		// Emit new position to all players
		socket.broadcast.emit('playerMoved', players[socket.id]);
	});

	// Handle the cessation of animations on server
	socket.on('stopAnim', function(){
		players[socket.id].playingAnim = false;

		//Emit message to all players
		socket.broadcast.emit('stopPAnim', players[socket.id]);
	});

	socket.on('new message', function(messageData){
		socket.broadcast.emit('new message', {message: messageData.message, username: messageData.username});
	});

	socket.on('add username', function(username){
		players[socket.id].username = username;
	});

	socket.on('matchmake', function(id){
		if (queue.includes(id)){
			socket.emit('already in queue', id);
			return;
		}
		queue.push(id);
		if (queue.length >= 2){
			player1 = queue.shift();
			player2 = queue.shift();
			io.emit('join match', {player1: player1, player2: player2});
		}
	});
});
node v10.16.0