-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoreHandling.js
272 lines (236 loc) · 8.57 KB
/
scoreHandling.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
var express = require('express');
var router = express.Router();
//to connect with db
var url = "mongodb://perS0nADm1N:"+encodeURIComponent("*geo@P0w3r3d*")+"@ds155097.mlab.com:55097/web_geography";
var local_url = "mongodb://127.0.0.1:27017/web_geography";
//to connect with the mongo client
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
//for the scoreEntry objects
const ScoreEntry = require('./models/scoreEntry');
const FlagScoreEntry = require('./models/flagScoreEntry');
//test
router.get('/sendScore.html', ensureAuthenticated, function(req, res){
//res.sendFile(__dirname+'/sendScore.html');
res.render('sendScore');
});
router.get('/leaderboard.html', function(req, res){
var scoreArray = new Array(0);
var usernameArray = new Array(0);
var continentChoiceArray = new Array(0);
//var url = "mongodb://perS0nADm1N:"+encodeURIComponent("*geo@P0w3r3d*")+"@ds155097.mlab.com:55097/web_geography";
//grab the scores entries before with a list for users and list for scores
MongoClient.connect(url,
function(err, db) {
if (err) throw err;
console.log("DB reached!");
var dbo = db.db("web_geography");
//sorts the list before giving the result set
dbo.collection("scoreentries").find().sort({score: -1})
.toArray(function(err, result) {
if (err) throw err;
console.log(result);
//counting scores for each type. If it surpasses ten, the other scores are deleted
var africaCount = 0;
var asiaCount = 0;
var europeCount = 0;
var NACount = 0;
var oceaniaCount = 0;
var SACount = 0;
var allCount = 0;
for(var i=0;i<result.length;i++){
console.log(result[i].score);
//checking if timestamp is expired. if it is, delete the score
if(new Date().getTime() - result[i].timeStamp >= 604800000){
//delete the object
dbo.collection("scoreentries").deleteMany({timeStamp: result[i].timeStamp});
continue;
}
if(result[i].continentChoice == 1 && africaCount < 10){
africaCount+=1;
}
else if(result[i].continentChoice == 2 && asiaCount < 10){
asiaCount+=1;
}
else if(result[i].continentChoice == 3 && europeCount < 10){
europeCount+=1;
}
else if(result[i].continentChoice == 4 && NACount < 10){
NACount+=1;
}
else if(result[i].continentChoice == 5 && oceaniaCount < 10){
oceaniaCount+=1;
}
else if(result[i].continentChoice == 6 && SACount < 10){
SACount+=1;
}
else if((result[i].continentChoice == 0
|| result[i].continentChoice == undefined) && allCount < 10){
allCount+=1;
}
else{
//deletes by timestamp as timestamp is very unique for each score
dbo.collection("scoreentries").deleteMany({timeStamp: result[i].timeStamp});
}
usernameArray[i]=result[i].username;
scoreArray[i]=result[i].score;
continentChoiceArray[i]=result[i].continentChoice;
}
console.log(usernameArray[0]);
res.render('leaderboard', {userList: usernameArray,
scoreList: scoreArray, continentChoiceList: continentChoiceArray});
db.close();
});
});
});
router.get('/leaderboardFlags.html', function(req, res){
var scoreArray = new Array(0);
var usernameArray = new Array(0);
var continentChoiceArray = new Array(0);
//var url = "mongodb://perS0nADm1N:"+encodeURIComponent("*geo@P0w3r3d*")+"@ds155097.mlab.com:55097/web_geography";
//grab the scores entries before with a list for users and list for scores
MongoClient.connect(url,
function(err, db) {
if (err) throw err;
console.log("DB reached!");
var dbo = db.db("web_geography");
//sorts the list before giving the result set
dbo.collection("flagscoreentries").find().sort({score: -1})
.toArray(function(err, result) {
if (err) throw err;
console.log(result);
//counting scores for each type. If it surpasses ten, the other scores are deleted
var africaCount = 0;
var asiaCount = 0;
var europeCount = 0;
var NACount = 0;
var oceaniaCount = 0;
var SACount = 0;
var allCount = 0;
for(var i=0;i<result.length;i++){
console.log(result[i].score);
//checking if timestamp is expired. if it is, delete the score
if(new Date().getTime() - result[i].timeStamp >= 604800000){
//delete the object
dbo.collection("flagscoreentries").deleteMany({timeStamp: result[i].timeStamp});
continue;
}
if(result[i].continentChoice == 1 && africaCount < 10){
africaCount+=1;
}
else if(result[i].continentChoice == 2 && asiaCount < 10){
asiaCount+=1;
}
else if(result[i].continentChoice == 3 && europeCount < 10){
europeCount+=1;
}
else if(result[i].continentChoice == 4 && NACount < 10){
NACount+=1;
}
else if(result[i].continentChoice == 5 && oceaniaCount < 10){
oceaniaCount+=1;
}
else if(result[i].continentChoice == 6 && SACount < 10){
SACount+=1;
}
else if((result[i].continentChoice == 0
|| result[i].continentChoice == undefined) && allCount < 10){
allCount+=1;
}
else{
//deletes by timestamp as timestamp is very unique for each score
dbo.collection("flagscoreentries").deleteMany({timeStamp: result[i].timeStamp});
}
usernameArray[i]=result[i].username;
scoreArray[i]=result[i].score;
continentChoiceArray[i]=result[i].continentChoice;
}
console.log(usernameArray[0]);
res.render('leaderboardFlags', {userList: usernameArray,
scoreList: scoreArray, continentChoiceList: continentChoiceArray});
db.close();
});
});
});
//To send the score of the user to the backend for handling
router.post('/sendScore.html', async(req, res) =>{
console.log(req.body.scoreText);
console.log(req.user.username+" My Username");
scoreEntry = new ScoreEntry({
username: req.user.username,
score: req.body.scoreText,
continentChoice: req.body.continentChoiceText,
timeStamp: new Date().getTime(),
});
var typeGame = req.body.gameChoiceText;
//choosing the appropriate type of game
var object=null;
var collection="";
if(typeGame == 1){
collection = "scoreentries";
object = ScoreEntry;
}
else{
collection = "flagscoreentries";
object = FlagScoreEntry;
}
//there must be a condition to see if there is an already existing score for
//the user, as username cannot be replicated
let user = await object.findOne({ username: req.user.username,
continentChoice: req.body.continentChoiceText});
//if the user already exist in the leaderboard of the specific continent choice
if (user) {
//we need to update the user's score if the new score is greater
if(req.body.scoreText > user.score){
MongoClient.connect(url,
function(err, db) {
console.log(url);
var dbo = db.db("web_geography");
//query to set the new values
var newvalues = { $set: {score: req.body.scoreText} };
dbo.collection(collection).updateOne(user, newvalues, function(err, res) {
if (err)
throw err;
console.log(user.username);
console.log("1 document updated");
db.close();
});
});
}
//if the new score isnt greater, then do nothing.
}
else{
//create a new score entry for the user. Verifies on leaderboard page if it merits to stay in DB.
//first connect to outside DB
//Connects to the online DB.
MongoClient.connect(url, { useNewUrlParser: true }, function(err,db){
object.create(scoreEntry, function (err, user) {
console.log(url);
if (err) {
console.log(err);
throw err;
} else {
//should indicate here that the user was successfully created
//returns to the hompage if the user was created
console.log("New score entry");
console.log(scoreEntry.username);
}
});
});
}
console.log("Has finished: "+req.body.hasFinished);
if(req.body.hasFinished == 1){
res.redirect('/examMaxScore.html');
}
else{
res.redirect('/gameOver.html');
}
});
//checks if the user is authenticated
function ensureAuthenticated(req, res, next){
if (req.isAuthenticated()){
next();
}
res.redirect("/login.html");
}
module.exports = router;