-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathleaderboard.js
90 lines (81 loc) · 2.5 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
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
const { Client } = require('pg');
if(SERVER === 'localhost'){
require("./DATABASE_URL.js");
}
else{
connectionString = connectionString = process.env.DATABASE_URL;
}
const client = new Client({
connectionString:connectionString,
ssl:{
rejectUnauthorized: false
}
});
client.connect();
var leaderboardXP = [];
var compareXP = function(currentRow){
if(currentRow.xp === undefined){
leaderboardXP.splice(i,0,currentRow);
return;
}
for(var i in leaderboardXP){
if(leaderboardXP[i].level > currentRow.level){
leaderboardXP.splice(i,0,currentRow);
return;
}
else if(leaderboardXP[i].level === currentRow.level){
if(leaderboardXP[i].xp > currentRow.xp){
leaderboardXP.splice(i,0,currentRow);
return;
}
else if(leaderboardXP[i].xp < currentRow.xp){
}
else if(leaderboardXP[i].xp === currentRow.xp){
leaderboardXP.splice(i,0,currentRow);
return;
}
}
}
leaderboardXP.push(currentRow);
}
updateLeaderboard = function(){
leaderboardXP = [];
client.query('SELECT * FROM progress;', (err, res) => {
for(var i in res.rows){
var currentRow = {
xp:JSON.parse(res.rows[i].qprogress).xp,
level:JSON.parse(res.rows[i].qprogress).level,
inventory:JSON.parse(res.rows[i].qprogress).inventory,
username:res.rows[i].qusername,
}
if(leaderboardXP.length === 0){
leaderboardXP.push(currentRow);
}
else{
compareXP(currentRow);
}
}
leaderboardXP = leaderboardXP.reverse();
for(var i in SOCKET_LIST){
SOCKET_LIST[i].emit('updateLeaderboard',leaderboardXP);
}
});
}
var account = undefined;
client.query('SELECT * FROM account;', (err, res) => {
account = res.rows;
client.query('SELECT * FROM progress;', (err1, res1) => {
for(var i in res1.rows){
var illegalAccount = true;
for(var j in res.rows){
if(res1.rows[i].qusername === res.rows[j].qusername){
illegalAccount = false;
}
}
if(illegalAccount){
//client.query('DELETE FROM progress WHERE qusername=\'' + res1.rows[i].qusername + '\';', (err, res) => {
//});
}
}
});
});