-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
472 lines (416 loc) · 15.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DE-MOG
// Dictator Experiment through Multiplayer Online Game
//
// Date: 2014/06/18
// Version: 0.5
// Author: Olivier Allouard
// Website: http://running-panda.fr
// Contact: [email protected]
// Description:
// The Express server handles passing the content to the browser. It's the router
// The Socket.io server handles real time communications between clients and server
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright © Olivier Allouard
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
//to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
//and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability,
//fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability,
//whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
require('./player.js');
require('./experiment');
require('./sioserver.js');
//require('newrelic');
/*
lines to comment for the app to run locally on ubuntu:
require('newrelic');
nodeMailer = require('nodemailer'), //Used to send results by mail to the admin
smtpTransport = require('nodemailer-smtp-transport'),
sgTransport = require('nodemailer-sendgrid-transport'),
localTransport
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Variables Declaration
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var
gameport = process.env.PORT || 8099, // Listen port of the server (8099 or a specific port declared by the host machine)
io = require("socket.io"), //
express = require('express'), //
UUID = require('node-uuid'), //
routes = require('routes'), //
http = require('http'), //
app = express(), //
fs = require('fs'), //Used to write the result json file in the log folder of the server
/*
nodeMailer = require('nodemailer'), //Used to send results by mail to the admin
smtpTransport = require('nodemailer-smtp-transport'),
sgTransport = require('nodemailer-sendgrid-transport'), */
sendgrid = require('sendgrid')(process.env.SENDGRID_USERNAME, process.env.SENDGRID_PASSWORD),
sio = undefined,
wrap_server = undefined;
// to add a new variable to the result file:
// In core.js, record variable with this.variable and send variable to SetGameResult in function prototype.Share (4x)
// In player.js, add to function prototype.SetGameResult, prototype.addGameResult and var GameResult
// In sioserver.js, add to function addGameResults in prototype.checkEndedGames
// In experiment.js, add to prototype.addGameResults and var gameResult
var frame_time = 60;
var physic_time = 15;
var adminLogin = "demog"; //Login of the admin page
var adminPassw = "test"; //password of the admin page
var resultMailAdress = '[email protected]'; //mail adress where the results will be send
var mailSenderLogin = '[email protected]'; //login of the gmail account used to send results
var mailSenderPassw = 'wivyxuvo'; //password of the gmail account used to send results
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Experiment Functions and Variables
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var
current_experiment = CreateExperiment('space',"web",1,"space_coop","en"),
experimentsList = [current_experiment];
function CreateExperiment(name,type,iter,game,lang)
{
try{
return(new Experiment(name,type,iter,game,lang));
}catch(err)
{
console.log(err);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Mail sender set up
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
var LocalTransport = nodeMailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: mailSenderLogin,
pass: mailSenderPassw
}
});
*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Express server set up
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
app.configure(function(){
app.set('views', __dirname + '/views');
app.use(express.favicon());
//app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use("/public",express.static(__dirname+"/public"));
});
var server = http.createServer(app); // Creation of the Express server
server.listen(gameport);
app.get('/admin',function(req,res){
res.render('admin.ejs',{login: adminLogin, passw: adminPassw});
})
app.get('/dictateur',function(req,res){
res.render('dictateur.ejs', {exps: experimentsList, clientsInGameNum: wrap_server.clients.length, clientsInLobbyNum: wrap_server.clientsinLobby.length, clientsFinished: wrap_server.experiment.returnOldCLientsNum()});
});
app.post('/dictateur/add/', function(req,res){
var xpName = req.param('xpName');
var xpType = req.param('xpType');
var xpGame = req.param('xpGame');
var Iter = req.param('Iter');
var lang = req.param('lang');
if(xpName != '')
{
experimentsList.push(CreateExperiment(xpName,xpType,Iter,xpGame,lang));
}
res.redirect('/dictateur');
});
app.post('/dictateur/delete/:xpName', function(req, res) {
var xpName = req.param('xpName');
if (xpName != '') {
for(var i =0; i < experimentsList.length; i ++)
{
if(experimentsList[i].xpName == xpName)
{
experimentsList.splice(i,1);
}
}
}
res.redirect('/dictateur');
});
app.post('/dictateur/start/:xpName', function(req, res) {
var xpName = req.param('xpName');
if (xpName != '') {
for(var i =0; i < experimentsList.length; i ++)
{
if(experimentsList[i].xpName == xpName)
{
experimentsList[i].startXP();
current_experiment = experimentsList[i];
wrap_server.initServer(current_experiment, server);
}
else
{
experimentsList[i].stopXP();
}
}
}
res.redirect('/dictateur');
});
app.post('/dictateur/stop/:xpName', function(req, res) {
var xpName = req.param('xpName');
if (xpName != '') {
for(var i =0; i < experimentsList.length; i ++)
{
if(experimentsList[i].xpName == xpName)
{
wrap_server.stopServer();
experimentsList[i].stopXP();
}
}
}
res.redirect('/dictateur');
});
app.post('/dictateur/write/:xpName', function(req, res) {
var xpName = req.param('xpName');
if (xpName != '') {
for(var i =0; i < experimentsList.length; i ++)
{
if(experimentsList[i].xpName == xpName)
{
var mailOptions = {
from: 'DEMOG RESULTS SERVICE', // sender address
to: resultMailAdress, // list of receivers
subject: 'Results from exp', // Subject line
text: 'Results of the experiment : '+current_experiment.xpName, // plaintext body
html: 'Results of the experiment : '+current_experiment.xpName ,// html body
attachments :
[{ filename: 'result.json',
contents: JSON.stringify(experimentsList[i], null, 4)
}]
};
if(gameport == 8099)
{
LocalTransport.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}
});
}
else
{
var payload =
{
to : resultMailAdress,
from : mailSenderLogin,
subject : 'Experiment Result',
text : 'Results of the experiment : '+current_experiment.xpName,
files :
[
{
filename: 'result.json',
content : JSON.stringify(experimentsList[i], null, 4)
}
]
}
sendgrid.send(
payload,
function(error, json){
if(error){
console.log(error);
}
}
);
}
}
}
}
res.redirect('/dictateur');
});
app.get('/game', function(req, res){
res.render('client.ejs', {exp: current_experiment});
});
app.get('/end1', function(req,res){
res.render('end1.ejs');
});
app.get('/end3', function(req,res){
res.render('end3.ejs');
});
app.get('/end5', function(req,res){
res.render('end5.ejs');
});
app.get('/home1', function(req,res){
res.render('home1.ejs');
});
app.get('/home12', function(req,res){
res.render('home12.ejs');
});
app.get('/home13', function(req,res){
res.render('home13.ejs');
});
app.get('/home14', function(req,res){
res.render('home14.ejs');
});
app.get('/home15', function(req,res){
res.render('home15.ejs');
});
app.get('/home16', function(req,res){
res.render('home16.ejs');
});
app.get('/home17', function(req,res){
res.render('home17.ejs');
});
app.get('/home3', function(req,res){
res.render('home3.ejs');
});
app.get('/home32', function(req,res){
res.render('home32.ejs');
});
app.get('/home33', function(req,res){
res.render('home33.ejs');
});
app.get('/home34', function(req,res){
res.render('home34.ejs');
});
app.get('/home35', function(req,res){
res.render('home35.ejs');
});
app.get('/home36', function(req,res){
res.render('home36.ejs');
});
app.get('/home37', function(req,res){
res.render('home37.ejs');
});
app.get('/home5', function(req,res){
res.render('home5.ejs');
});
app.get('/home52', function(req,res){
res.render('home52.ejs');
});
app.get('/home53', function(req,res){
res.render('home53.ejs');
});
app.get('/home54', function(req,res){
res.render('home54.ejs');
});
app.get('/home55', function(req,res){
res.render('home55.ejs');
});
app.get('/home56', function(req,res){
res.render('home56.ejs');
});
app.get('/home57', function(req,res){
res.render('home57.ejs');
});
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Socket.IO server set up
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function CreateSIOServer()
{
sio = io.listen(server);
sio.configure(function (){
sio.set('log level', 0);
sio.set('authorization', function (handshakeData, callback){
callback(null , true);
});
});
wrap_server = new game_server();
wrap_server.initServer(current_experiment);
}
function StopSIOServer()
{
wrap_server = undefined;
sio = undefined;
}
current_experiment.isRunning = true;
CreateSIOServer();
if(sio != undefined)
{
/*
sio.on('connection', function(client){
var client_ip_address = sio.request.connection.remoteAddress;
console.log(client_ip_address);
} */
sio.sockets.on('connection', function (client){
//var socketId = client.id;
var clientIp = client.handshake.address;
clientIp = client.manager.handshaken[client.id].address;
console.log(clientIp)
var tmpClient = client;
var addr = client.handshake.address;
//console.log(sio);
//var sHeaders = client.handshake.headers;
//console.log(sHeaders.host);
client.userid = UUID();
client.player = new player();
client.player.InitResult(client.userid,undefined);
client.player.result.updateStatus("waiting for games");
//client.player.result.updateIP(addr.address);
//client.player.result.updateIP(sio.request.connection.remoteAddress);
wrap_server.addClient(client);
client.on('playerLogin', function (m){
client.player.result.amazonId = m;
});
client.on('partnerLost', function (){
client.player.result.lostPartner = 1;
});
client.on('message', function (m){
wrap_server.onMessage(client, m);
});
client.on('disconnect', function (){
if(wrap_server.isClientInLobby(client))
{
wrap_server.removeClientFromLobby(client);
wrap_server.removeClient(client);
}
else if(wrap_server.isClientInGame(client))
{
wrap_server.findPartner(wrap_server.findGame(client),client) // asks partner to emit message "partnerLost"
wrap_server.endGame(wrap_server.findGame(client));
wrap_server.removeClientFromLobby(client);
wrap_server.removeClient(client);
}
else
{
wrap_server.removeClient(client);
}
});
});
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Matching players routine
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Called every 5 seconds to match waiting players in the lobby
setInterval(function(){
if(wrap_server != undefined && wrap_server.experiment.isRunning)
{
wrap_server.matchClients();
}
else if(!wrap_server.experiment.isRunning)
{
}
}, 5000);
//Check ended games in wrap_server
setInterval(function(){
wrap_server.update(frame_time);
wrap_server.checkEndedGames();
}, frame_time);
//call update of the game server
setInterval(function(){
wrap_server.physic_update(physic_time);
}, physic_time);