-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
135 lines (97 loc) · 3.46 KB
/
server.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
//{roomName: players[]}
var playersInRooms = {};
//{socketId: roomName}
var socketIdRoomNameMap = {};
var highestScoreInRoom = {};
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/game', function (req, res) {
res.sendFile(__dirname + '/game.html');
});
var previousY;
setTimeout(function prn(i){
var y = 0;
console.log('Creating enemy');
if(previousY === undefined) {
previousY = Math.floor(Math.random() * 450);
y = previousY;
}
else {
while(true) {
var newY = Math.floor(Math.random() * 450);
if(Math.abs(newY - previousY) >= 30) {
y = newY;
previousY = newY;
break;
}
}
}
console.log(y);
io.emit('enemyCreated', {x: 360, y: y});
setTimeout(prn, 2000, i);
}, 2000, 0);
io.on('connection', function (socket) {
console.log('a user connected', socket.handshake.query.room);
var roomName = socket.handshake.query.room;
// create a new player and add it to our players object
var playerType = null;
socket.join(roomName);
if(roomName in playersInRooms){
playersInRooms[roomName].push({
playerId: socket.id,
playerType: 'hero',
x:0,
y:0
})
}else{
playersInRooms[roomName] = [];
playersInRooms[roomName].push({
playerId: socket.id,
playerType: 'hero',
x:0,
y:0
});
}
socketIdRoomNameMap[socket.id] = roomName;
// send the players array to the new player
socket.emit('currentPlayers', playersInRooms[roomName]);
// update all other players of the new player
socket.to(roomName).emit('newPlayer', playersInRooms[roomName][playersInRooms[roomName].length - 1]);
// when a player disconnects, remove them from our players object
socket.on('disconnect', function () {
console.log('user disconnected');
// remove this player from our players array in a room
playersInRooms[socketIdRoomNameMap[socket.id]] = playersInRooms[socketIdRoomNameMap[socket.id]].filter((p) => p.playerId !== socket.id)
// emit a message to all players to remove this player
io.emit('disconnect', socket.id);
socket.in(socketIdRoomNameMap[socket.id]).emit('playerRemoved', socket.id);
delete socketIdRoomNameMap[socket.id];
});
socket.on('movementChanged', function (data) {
socket.to(socketIdRoomNameMap[data.playerId]).emit('playerMoved', data);
});
socket.on('enemyMoved', function (data) {
io.to(socketIdRoomNameMap[data.playerId])('enemyMoved', data);
});
socket.on('enemyCreated', function (data) {
io.to(socketIdRoomNameMap[data.playerId]).emit('enemyCreated', data);
});
socket.on('scoreUpdate', function (data) {
socket.to(socketIdRoomNameMap[data.playerId]).emit('scoreUpdate', data);
var highestScore = highestScoreInRoom[socketIdRoomNameMap[data.playerId]]
if(highestScore === undefined || data.score > highestScore.score) {
highestScore = {playerName: data.playerName, score:data.score}
}
highestScoreInRoom[socketIdRoomNameMap[data.playerId]] = highestScore;
io.to(socketIdRoomNameMap[data.playerId]).emit('highestScore', highestScore);
});
});
server.listen(process.env.PORT || 8080, function () {
console.log(`Listening on ${server.address().port}`);
});