-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
163 lines (148 loc) · 4.96 KB
/
database.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
const mongoose = require('mongoose');
const clientOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'Zhonya-s-Stats'
};
mongoose.connect(process.env.MONGO_URL, clientOptions);
var connection = mongoose.connection;
connection.on('connected', function () {
console.log('Database is connected successfully');
});
connection.on('disconnected', function () {
console.log('Database is disconnected successfully');
})
connection.on('error', console.error.bind(console, 'connection error:'));
module.exports = connection;
async function checkSameChannel(guildId, channelId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return false;
if (guildData.channelId === channelId)
return true;
return false;
}
async function checkChannelSet(guildId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return false;
return true
}
async function setChannel(guildId, channelId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null) {
var newGuild = {
guildId: guildId,
channelId: channelId
};
await mongoRef.insertOne(newGuild);
}
else
await mongoRef.updateOne({ guildId: guildId }, { $set: { channelId: channelId } });
}
async function removeUser(guildId, summonerName) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return;
await mongoRef.updateOne({ guildId: guildId }, { $unset: { ["users." + summonerName]: "" } });
}
async function userExist(guildId, summonerName) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return false;
if (!guildData.users)
return false;
if (!(summonerName in guildData.users))
return false;
return true;
}
async function getStats(guildId, summonerName) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return;
return guildData.users[summonerName];
}
async function updateUser(guildId, summonerName, stats) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return;
await mongoRef.updateOne({ guildId: guildId }, { $set: { ["users." + summonerName]: stats } });
}
async function addUser(guildId, stats, queue) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
var formatted_stats = "";
if (queue === "solo/duo") {
formatted_stats = {
SOLO_hotStreak: stats.hotStreak,
SOLO_leaguePoints: stats.leaguePoints,
SOLO_wins: stats.wins,
SOLO_losses: stats.losses,
SOLO_rank: stats.tier + ' ' + stats.rank,
SOLO_winrate: stats.winrate
}
} else if (queue === "flex") {
formatted_stats = {
FLEX_hotStreak: stats.hotStreak,
FLEX_leaguePoints: stats.leaguePoints,
FLEX_wins: stats.wins,
FLEX_losses: stats.losses,
FLEX_rank: stats.tier + ' ' + stats.rank,
FLEX_winrate: stats.winrate
}
}
if (guildData === null) {
var newGuild = {
guildId: guildId,
users: { [stats.summonerName]: formatted_stats }
};
await mongoRef.insertOne(newGuild);
}
else
await mongoRef.updateOne({ guildId: guildId }, { $set: { ["users." + stats.summonerName]: formatted_stats } });
}
async function checkHasUsers(guildId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return false;
if (guildData.users[0] === null)
return false;
return true;
}
async function checkMaxUsers(guildId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return false;
if (!guildData.users)
return false;
if (Object.keys(guildData.users).length >= 9)
return true;
return false;
}
async function getLeaderboard(guildId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return;
return guildData;
}
async function getChannel(guildId) {
var mongoRef = connection.db.collection('Zhonya-s-Stats');
var guildData = await mongoRef.findOne({ guildId: guildId });
if (guildData === null)
return;
return guildData.channelId;
}
module.exports = {
checkSameChannel, checkChannelSet, userExist, setChannel, getStats, updateUser, addUser,
checkHasUsers, checkMaxUsers, removeUser, getLeaderboard, getChannel
};