-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaderboard.js
65 lines (61 loc) · 2.17 KB
/
leaderboard.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
// Function topTen
// Obtain the ten users with the highest win streak from database
const topTen = (db, callback) => {
// Get all username and win streaks, sorted in descending order of win streak
db.all(`SELECT username,winstreak FROM users ORDER BY winstreak DESC;`, (err,rows) => {
if (err) { // SQL error
return callback(Error("SQL-related error occured"));
}
else { // No error
// Get first ten entries
let topTen = [];
for (let i = 0; i < 10; i++) {
if (rows[i]) { // If entry exists, push it to array
topTen.push(rows[i]);
}
else { break; } // Otherwise, break
}
return callback(false,topTen);
}
});
}
// Function increment
// Increase the win streak of specified username by 1
const increment = (db, username, callback) => {
// Get current win streak of username
db.get(`SELECT winstreak FROM users WHERE username='${username}';`, (err, row) => {
if (row) {
let streak = row['winstreak'];
streak++; // Increment by 1
// Update win streak of username to new value
db.run(`UPDATE users SET winstreak=${streak} WHERE username='${username}';`, (err) => {
if (err) {
return callback(Error("SQL-related error occured"));
}
else {
return callback(false);
}
});
}
else if (err) { // SQL error
return callback(Error("SQL-related error occured"));
}
else { // No error but no entry found
return callback(Error("User not found"));
}
});
}
// Function reset
// Set win streak of specified username to 0
const reset = (db, username, callback) => {
// Update win streak of username to 0
db.run(`UPDATE users SET winstreak=0 WHERE username='${username}';`, (err) => {
if (err) { // SQL error
return callback(Error("SQL-related error occured"));
}
else {
return callback(false);
}
});
}
module.exports = {topTen, increment, reset}