-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop.js
173 lines (156 loc) · 6.43 KB
/
stop.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
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require("socket.io").listen(server),
nicknames = {},
mysql = require('mysql'),
nUsers = 0,
usuarios = [];
server.listen(8888);
//server.listen(process.env.PORT, process.env.IP);
///MYSQL///////////////////////////////////////////////////
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'stop',
//port: 3306
});
connection.connect(function(error){
if(error){
console.log('ERROR Conexion');
}else{
console.log('Conexion correcta.');
}
});
//connection.end();
/////////////////////////////////////////////////////////
app.get('/', function(req, res) {
res.sendfile(__dirname + '/jeuStop.html');
});
io.sockets.on('connection', function(socket) {
socket.on('send message', function(data) {
io.sockets.emit('new message', {msg: data, nick: socket.nickname});
});
socket.on('new user', function(data, callback) {
if (data in nicknames) {
callback(false);
} else {
callback(true);
socket.nickname = data;
nicknames[socket.nickname] = 1;
updateNickNames();
usuarios.push(socket.nickname);
io.sockets.emit('USERS', usuarios);
nUsers++;
}
});
socket.on('disconnect', function(data) {
if(!socket.nickname) return;
delete nicknames[socket.nickname];
usuarios.splice(nUsers-1,1);
nUsers--;
updateNickNames();
});
socket.on('BLUR', function(data1,data2,data3,data4,data5,data6) {
/////Base de données////////////////////////////////////////////////////////////////////////////////////////////////////
var result1 = 'Nulle';
var result2 = 'Nulle';
var result3 = 'Nulle';
var result4 = 'Nulle';
var result5 = 'Nulle';
var result6 = 'Nulle';
var queryString1 = 'SELECT * FROM Animaux';
var queryString2 = 'SELECT * FROM pays';
var queryString3 = 'SELECT * FROM films';
var queryString4 = 'SELECT * FROM fleuves';
var queryString5 = 'SELECT * FROM langages';
var queryString6 = 'SELECT * FROM villes';
var query = connection.query(queryString1, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data1) {
console.log(rows[i].descrip);
result1 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
var query = connection.query(queryString2, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data2) {
console.log(rows[i].descrip);
result2 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
var query = connection.query(queryString3, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data3) {
console.log(rows[i].descrip);
result3 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
var query = connection.query(queryString4, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data4) {
console.log(rows[i].descrip);
result4 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
var query = connection.query(queryString5, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data5) {
console.log(rows[i].descrip);
result5 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
var query = connection.query(queryString6, function(error,rows,fields){
if(error){
console.log('ERROR database');
}else{
for(var i in rows){
if(rows[i].resp == data6) {
console.log(rows[i].descrip);
result6 = rows[i].descrip;
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
}
}
}
});
///////////////////////////////////////////////////////////
io.sockets.emit('BLUR2',socket.nickname,data1,data2,data3,data4,data5,data6,result1,result2,result3,result4,result5,result6);
});
socket.on('STOP', function(){
io.sockets.emit('STOP2');
});
function updateNickNames() {
io.sockets.emit('usernames', nicknames);
}
});