-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
339 lines (315 loc) · 10 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
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
var express = require('express');
var mysql = require('mysql');
var url = require('url');
var bodyParser = require('body-parser');
var jsdom = require("jsdom");
var urlencodedParser = bodyParser.urlencoded({extended:false});
var urlencodedParser_login = bodyParser.urlencoded({extended:false});
var { JSDOM } = jsdom;
var session = require('express-session');
var pug = require('pug');
var irc = require("irc");
var matchID = [];
var numberOfPlayers = [];
/*
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: ""
});
console.log("Connected!");
var sql = "CREATE DATABASE IF NOT EXISTS killEmAll";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Database created");
});
sql = "CREATE TABLE IF NOT EXISTS users (firstName VARCHAR(255), secondName VARCHAR(255), email VARCHAR(255), username VARCHAR(255), password VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Database created");
});
*/
con = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "killEmAll"
});
con.connect(function(err) {
if(err) throw err;
console.log('Connected!');
});
sql = "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, firstName VARCHAR(255), secondName VARCHAR(255), email VARCHAR(255), username VARCHAR(255), password VARCHAR(255))";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
});
var app = express();
var server = app.listen(3000);
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
app.use(session({secret: "ilovecristina"}));
app.get('/', function(req, res){
if(req.session.username)
{
// the user is logged, so he/she can play!
res.render('mainScreen.pug');
}
else
{
res.render('index.html');
}
});
app.get('/signUp', function(req, res){
//res.render('signUp');
res.render('signUp.pug', {
firstName: "",
secondName: "",
email: "",
username: "",
//error: "'" + req.body.username + "' is an invalid username...",
});
});
var howMany = 0;
app.post('/signUp', urlencodedParser, function(req, res){
console.log(req.body);
// check whether the inputs are valid or not
howMany = 0;
function makeSelect(callback)
{
sql = "SELECT * FROM users WHERE username='" + req.body.username + "';";
con.query(sql, function (err, rows) {
if (err) throw err;
rows.forEach(function(result) {
howMany++;
});
console.log(howMany);
callback();
});
}
function isUsernameValid()
{
console.log('waiting for the action of counting to finish...');
if(howMany == 0)
{
console.log("i am right in the if statement...");
// the inputs are valid, they must be inserted into the database
sql = "INSERT INTO users(firstName, secondName, email, username, password) VALUES('" + req.body.firstName + "', '" + req.body.secondName + "', '" + req.body.email + "', '" + req.body.username + "', '" + req.body.password + "');";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("username and password were inserted into the database!");
});
res.render('signUp-success');
}
else
{
console.log("sorry... this username is invalid!");
res.render('signUp.pug', {
firstName: req.body.firstName,
secondName: req.body.secondName,
email: req.body.email,
error: "'" + req.body.username + "' is an invalid username...",
});
}
}
makeSelect(isUsernameValid);
});
app.get('/login', function(req, res){
res.render('login.pug');
});
app.post('/login', urlencodedParser_login, function(req, res){
console.log(req.body);
sql = "SELECT COUNT(id) as Cnt, id as ID, email, firstName, secondName FROM users WHERE username='" + req.body.username + "' AND password='" + req.body.password + "';";
con.query(sql, function (err, result) {
if (err) throw err;
//console.log(result[0].Cnt);
if(result[0].Cnt == 0)
{
console.log("user or password are wrong");
//res.render('index');
res.render('login.pug', {
username: "",
error: "user or password are wrong",
password: ""
});
}
else
{
// the username and password match the database
req.session.username = req.body.username;
req.session.password = req.body.password;
req.session.ID = result[0].id;
req.session.email = result[0].email;
req.session.firstName = result[0].firstName;
req.session.secondName = result[0].secondName;
req.session.ID = result[0].ID;
//console.log(result[0]);
//console.log("logged in successfully: " + req.session.username + " " + req.session.password + " " + req.session.firstName + " " + req.session.secondName + " " + req.session.email + " " + req.session.ID);
res.render('mainScreen.pug');
}
});
});
app.get('/logout', function(req, res){
req.session.destroy();
res.redirect("/");
});
app.get('/whichUser', function(req, res){
res.send({username: req.session.username});
});
app.get('/checkIfChannelIsEmpty', function(req, res){
req.session.idForGame = req.param('id');
req.session.channelEntered = req.session.idForGame;
//req.session.numberOfPlayers = 0;
//req.session.listeningBot = new irc.Client("irc.accessirc.net", req.session.username, {
// channels: req.session.idForGame,
// autoConnect: false
//});
//req.session.listeningBot.connect();
//req.session.listeningBot.addListener("join", function(channel, who){
// count how many players are online
//req.session.numberOfPlayers++;
//req.session.listeningBot.say(channel, who + ", " + req.session.numberOfPlayers);
//});
if(typeof matchID[req.session.idForGame] === 'undefined')
{
var players = [
{
firstName: req.session.firstName,
secondName: req.session.secondName,
id: req.session.ID,
username: req.session.username,
email: req.session.email
},
{
firstName: "",
secondName: "",
id: "",
username: "",
email: ""
},
{
firstName: "",
secondName: "",
id: "",
username: "",
email: ""
},
{
firstName: "",
secondName: "",
id: "",
username: "",
email: ""
},
{
firstName: "",
secondName: "",
id: "",
username: "",
email: ""
},
{
firstName: "",
secondName: "",
id: "",
username: "",
email: ""
}
];
matchID[req.session.idForGame] = players;
numberOfPlayers[req.session.idForGame] = 1;
res.send({isEmpty: true});
}
else res.send({isEmpty: false});
});
/*app.get('/botIsListeningForEntrance', function(req, res){
//if(req.session.numberOfPlayers == 0) req.session.listeningBot.connect();
});
*/
app.get('/isChannelDefined', function(req, res){
var channelToVerify = req.param('id');
if(typeof numberOfPlayers[channelToVerify] === 'undefined')
{
res.send({isDefined: false, errormsg: "sorry, but this channel does not exist..."});
}
else
{
// check if the player who is trying to get into the match isn't the same with the previous ones
let playerCanEnter = true;
console.log(matchID[channelToVerify]);
function checkIfIdAlreadyExists(callback)
{
if(numberOfPlayers[channelToVerify] >= 5) playerCanEnter = false;
let lastCheck = true;
let iPlayers = numberOfPlayers[channelToVerify];
for(let i=1;i<=iPlayers || lastCheck;i++)
{
console.log("i : " + i);
if(i <= iPlayers)
{
if(matchID[channelToVerify][i].id == req.session.ID)
{
playerCanEnter = false;
console.log("this id already exists...");
}
}
if(i==iPlayers+1)
{
if(matchID[channelToVerify][i-1].id == req.session.ID)
{
playerCanEnter = false;
console.log("this id already exists...");
}
lastCheck = false;
i = numberOfPlayers[channelToVerify] + 100;
callback();
}
}
}
function buildTheResponse()
{
console.log("being in the callback...");
if(playerCanEnter)
{
let playerNr = ++numberOfPlayers[channelToVerify];
matchID[channelToVerify][playerNr].firstName = req.session.firstName;
matchID[channelToVerify][playerNr].secondName = req.session.secondName;
matchID[channelToVerify][playerNr].username = req.session.username;
matchID[channelToVerify][playerNr].email = req.session.email;
matchID[channelToVerify][playerNr].id = req.session.ID;
req.session.channelEntered = channelToVerify;
if(numberOfPlayers[channelToVerify] < 5) res.send({isDefined: true, currentNumberOfPLayers: playerNr});
else if(numberOfPlayers[channelToVerify] == 5) res.send({isDefined: true, currentNumberOfPLayers: "the match can begin"});
}
else res.send({isDefined: false, errormsg: "you've already entered a channel! please, wait for a response..."}); // it is one of the previous players
}
if(numberOfPlayers[channelToVerify] < 5)
{
checkIfIdAlreadyExists(buildTheResponse);
}
else res.send({isDefined: false, errormsg: "sorry, but the channel is full of players :("});
}
});
app.get('/shouldTheGameBegin', function(req, res){
if(req.session.channelEntered)
{
if(numberOfPlayers[req.session.channelEntered] == 5)
{
//console.log("the game begins");
res.send({msg: "the game begins!"});
}
else
{
let numberLeft = 5 - numberOfPlayers[req.session.channelEntered];
//console.log("only " + numberLeft + " players left to enter!");
res.send({msg: "only " + numberLeft + " players left to enter!"});
}
}
else
{
//console.log("the game has not been created yet...");
res.send({msg: "the game has not been created yet..."});
}
});