-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
194 lines (169 loc) · 5.71 KB
/
app.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 8000;
const cors = require("cors");
const ROOM_SIZE = 3;
const rooms = new Map();
let roomId = 1;
app.use(express.static(__dirname + '/client'));
app.use(cors());
function onConnection(socket){
// console.log("connected");
socket.isassigned = false;
rooms.forEach((roomInfo, room) => {
let players = roomInfo.players;
if (!socket.isassigned && players.length < ROOM_SIZE
&& !roomInfo.gameStarted) {
socket.isassigned = true;
socket.room = room;
socket.join(room);
players.push({playerID: socket.id});
//console.log(rooms);
}
});
if (!socket.isassigned) {
let room = "room" + roomId;
let angle = Math.floor(Math.random() * 360); // returns a random integer from 0 to 360
rooms.set(room,
{
gameStarted: false,
ballPosition:
{
angle: angle
},
players: [{playerID: socket.id}]
}
);
socket.room = room;
socket.isassigned = true;
socket.join(room);
roomId++;
//console.log(rooms);
}
// console.log(io.sockets.adapter.rooms);
console.log(1);
emitAssignment(socket);
socket.on("disconnecting", _ => {
console.log(2);
console.log(rooms.get(socket.room).players);
let players = rooms.get(socket.room).players
// rooms.get(socket.room).players.remove(socket.id);
for (let i = 0; i < players.length; i++) {
if (socket.id == players[i].playerID) {
players.splice(i--, 1);
}
}
console.log(rooms.get(socket.room).players);
if (!rooms.get(socket.room).gameStarted) {
emitAssignment(socket);
}
if (rooms.get(socket.room).players <= 0) {
rooms.delete(socket.room);
}
});
socket.on('playerMovement', function(movementData)
{
rooms.get(socket.room).players.forEach(function(player)
{
if(player.playerID === socket.id)
{
player.x = movementData.x;
player.y = movementData.y;
}
else
{
io.to(player.playerID).emit("playerMoved", {playerID: socket.id, x: movementData.x, y: movementData.y, velocity: movementData.velocity});
}
});
});
socket.on('create npcs', data => {
setTimeout(_=> {
io.in(socket.room).emit('create npcs', data);
}, 3000);
// console.log(data);
});
socket.on('create keys', data => {
setTimeout(_=> {
io.in(socket.room).emit('create keys', data);
}, 3000);
// console.log(data);
});
socket.on('update npcs', data => {
io.in(socket.room).emit('update npcs', data);
});
socket.on('fireball', data => {
io.in(socket.room).emit('fireball', data);
});
socket.on('game end', data => {
console.log("game end");
console.log(data);
io.in(socket.room).emit('game end', data);
});
socket.on('update keys', data => {
io.in(socket.room).emit('update keys', data);
});
socket.on('scene created', _=>{
//Sends information
io.to(socket.id).emit('currentPlayers', rooms.get(socket.room));
});
socket.on('pop up', data => {
io.in(socket.room).emit('pop up', data);
});
socket.on('consumed', data => {
rooms.get(socket.room).players.forEach(function(player) {
if(player.playerID !== socket.id) {
io.to(player.playerID).emit('consumed', data);
}
});
});
socket.on('freeze', playerType => {
rooms.get(socket.room).players.forEach(function(player) {
if(player.type !== playerType) {
io.to(player.playerID).emit('freeze');
}
});
});
socket.on('keyTouched', data => {
io.in(socket.room).emit('keyTaken', data);
})
}
function emitAssignment(socket) {
console.log(rooms.get(socket.room).players);
let roomSize = rooms.get(socket.room).players.length;
if (roomSize >= ROOM_SIZE) {
for (let i = 0; i < roomSize; i++) {
let id = rooms.get(socket.room).players[i].playerID;
if (i == 0) {
rooms.get(socket.room).players[i].x = 960;
rooms.get(socket.room).players[i].y = 300;
rooms.get(socket.room).players[i].type = 'hider';
} else if (i == 2) {
rooms.get(socket.room).players[i].x = 632;
rooms.get(socket.room).players[i].y = 228;
rooms.get(socket.room).players[i].type = 'finder';
} else {
rooms.get(socket.room).players[i].x = 1228;
rooms.get(socket.room).players[i].y = 228;
rooms.get(socket.room).players[i].type = 'finder';
}
io.to(id).emit("assign", roomSize, rooms.get(socket.room).players[i].type);
// io.to(id).emit("currentPlayers", rooms.get(socket.room));
}
rooms.get(socket.room).gameStarted = true;
}
io.in(socket.room).emit("assign", roomSize);
}
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
io.on('connection', onConnection);
http.listen(port, () => console.log('listening on port ' + port));