-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
352 lines (263 loc) · 7.59 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
var express = require('express');
var socket_io = require('socket.io');
var path = require('path');
var util = require('util');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
const ngrok = require('ngrok');
const opn = require('opn');
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
var io = socket_io();
app.io = io;
var Player = require('./Player');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'node_modules')));
// app.use(express.static(path.join(__dirname, 'bower_components')));
app.use('/', routes);
app.use('/users', users);
if(process.env.CONFIGHTTPS) {
ngrok.connect(3000, (err, url) => {
console.log(`HTTPS URL : ${url}`);
opn(url, {app: ['google chrome']});
});
}else {
console.log('navigate to localhost:3000');
opn("http://localhost:3000", {app: ['google chrome']});
}
//Send this user to the new user to know whose already in the chat
var users = [];
var players = [];
io.on("connection", function( socket ){
//LISTENING TO 'new player'
socket.on('new player', function(data){
console.log(data);
//Create a new player
var newPlayer = new Player(data.x, data.y);
//this.id === socket.id (same thing)
newPlayer.id = this.id;
newPlayer.name = data.username;
newPlayer.setHealth(100);
//Broadcast new player to connected socket clients
this.broadcast.emit('new player', {
id: newPlayer.id,
health: newPlayer.getHealth(),
name: newPlayer.name,
x: newPlayer.getX(),
y: newPlayer.getY()
});
//send existing players to the new player
var i, existingPlayer;
for(i = 0; i < players.length;i++){
existingPlayer = players[i];
this.emit('new player', {
name: existingPlayer.name,
id: existingPlayer.id,
x: existingPlayer.getX(),
y: existingPlayer.getY(),
health: existingPlayer.getHealth()
});
};
//add new player to the players array
players.push(newPlayer);
// console.log(players);
});
//LISTENING TO 'move player'
socket.on('move player', function(data){
//Find the player in array
var movePlayer = playerById(this.id);
//Player not found
if(!movePlayer){
util.log('Player not found ' + this.id);
return;
};
//update the player[id] new position
//its specific depending on the new user
movePlayer.setX(data.x);
movePlayer.setY(data.y);
//Broadcast updated position to connected socket clients
this.broadcast.emit('move player', {
id: movePlayer.id,
x: movePlayer.getX(),
y: movePlayer.getY()
});
});
//LISTENING TO HEALING SPELL
socket.on('heal player', function(data){
var healPlayer = playerById(this.id);
if(!healPlayer){
util.log('Player not found ' + this.id);
return;
}
healPlayer.setHealth(data.health);
this.broadcast.emit('heal player',{
id: healPlayer.id,
health: healPlayer.getHealth()
});
});
//DECREASE HEALTH
socket.on('decrease health', function(data){
var decreasePlayerHealth = playerById(this.id);
if(!decreasePlayerHealth){
util.log('Player not found ' + this.id);
return;
};
decreasePlayerHealth.setHealth(data.health);
this.broadcast.emit("decrease health", {
health: decreasePlayerHealth.getHealth(),
id: decreasePlayerHealth.id
});
})
//SEND MAGICWALL INFO
socket.on('send magicwall', function(data){
var magicWall = playerById(this.id);
if(!magicWall){
util.log('Player not found ' + this.id);
return;
};
this.broadcast.emit("send magicwall",{
id: magicWall.id,
x: data.x,
y: data.y,
angle: data.angle
});
});
//SEND SD
socket.on('send sd', function(data){
var suddenDeath = playerById(this.id);
if(!suddenDeath){
util.log("Player not found " + this.id);
return;
};
this.broadcast.emit("send sd", {
id: suddenDeath.id,
x: data.x,
y: data.y,
angle: data.angle
});
});
//DEAD
socket.on('I Died', function(data){
console.log(data.msg);
});
//Killed Someone
socket.on('Killed Someone', function(data){
var killer = playerById(this.id);
if(!killer){
util.log("Player not found " + this.id);
};
killer.setKillCount();
console.log(data.killer + " has killed " + killer.getKillCount() + " in total");
io.emit('Killed Someone', {
id: this.id,
killer: data.killer,
killCount: killer.getKillCount(),
victim: data.victim
});
})
//CHATROOM vvvvv
// console.log(socket);
//This is unique for every user to makes a connection.
//This variable becomes different for every user
//Pretty much this function is for that specific user. Its not shared
//Check if user enter the page
console.log( "A user connected with the id of "+ socket.id);
var username = "";
//Client is asking for all users. Send back the array that we made
socket.on('request-users', function(){
socket.emit('users', {users: users});
});
//Adding New Users
socket.on('add-user', function(data){
//The expression means that theres no connected with the same name
if(users.indexOf(data.username) == -1){
username = data.username;
io.emit('add-user',{username: username});
io.emit('get name', {username: username});
users.push(data.username);
console.log(data.username + " has joined the chat");
}else{
console.log("naaaah son");
socket.emit('prompt-username', {message: 'User already exists!'});
};
});
//Send your message to everybody who is connected with io.emit
socket.on('message', function(data){
io.emit('phaser message',{
id: this.id,
message: data.message
});
io.emit('message', {
username: username, //Grabbing the variable at the top that was set when user signed up
message: data.message
});
});
//Take out user from array and from the global io
socket.on('disconnect', function(){
console.log(username + " has disconnected");
var removePlayer = playerById(this.id);
//player not found
if(!removePlayer){
util.log('Player not found '+ this.id);
return;
};
//Remove player from players array
io.emit('remove-user', {username: username});
this.broadcast.emit('remove player',{id: this.id});
players.splice(players.indexOf(removePlayer), 1);
users.splice(users.indexOf(username),1);
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
/* ************************************************
** GAME HELPER FUNCTIONS
************************************************ */
// Find player by ID
function playerById (id) {
var i
for (i = 0; i < players.length; i++) {
if (players[i].id === id) {
return players[i]
}
}
return false
}
module.exports = app;