-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (67 loc) · 1.81 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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var favicon = require('serve-favicon');
var users = 0;
var nPlayersPerGame = [0,0,0,0,0,0];
var pcs = 0;
app.set('port', (process.env.PORT || 5000));
app.use('/', express.static(__dirname + '/public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(favicon(__dirname + '/public/images/favicon.ico'));
app.get('/', function(req, res){
res.sendFile('public/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.emit('number players', nPlayersPerGame);
socket.on('enlist', function(data){
var email, hasPC, com, drinks, games
email = data[0][0];
hasPC = data[1];
com = data[0][1];
drinks = data[0][2];
games = data[2];
console.log(email);
console.log(hasPC);
console.log(com);
console.log(drinks);
console.log(games);
for (var i = 0; i<6; i++){
if(games[i] == true){
nPlayersPerGame[i]++;
}
}
users++;
var user = 'User number '+users+'\n';
user += 'email: '+email+'\n';
if(hasPC == true){
user += 'Has a PC\n';
pcs++;
console.log(''+pcs+' PCs available');
}else{
user += 'No PC\n';
}
if (com !== ''){user += 'Says: '+com+'\n';}
user += 'Drinks '+drinks+'\n';
user += 'Games will play: '+games+'\n-------------------\n\n';
fs.appendFile('enlisted.txt', user, function (err) {
if (err) {
throw err;
socket.emit('error', err);
}else{
console.log('New user enlisted!');
socket.emit('enlisted');
}
});
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(app.get('port'), function(){
console.log('listening on port', app.get('port'));
});