-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataCollector.js
235 lines (192 loc) · 6.07 KB
/
dataCollector.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
const https = require('https');
const sqlite3 = require('sqlite3').verbose();
const riotAPI = require('./riotapi.js');
db = null;
class Database {
constructor(){
// open the database
this.db = new sqlite3.Database('./db/opgg.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
console.error(err.message);
}
//console.log('[DB] Connected to the opgg database.');
});
}
disconnect() {
this.db.close((err) => {
if (err) {
console.error(err.message);
}
//console.log('[DB] Close the database connection.');
this.db = null;
});
}
}
module.exports = {
getUserData: function(name, oriRes){
console.log(name);
getSummonerPipe(name, oriRes);
},
updateUserData: function(name, oriRes){
updateUser(name, oriRes);
},
updateMatchesData: function(puuid, oriRes) {
matchListPipe(puuid, oriRes);
},
getMatchesData: function(puuid, oriRes){
getMatches(puuid, oriRes);
},
getRequestsCount: function(oriRes){
oriRes.write(riotAPI.getRequestsCount().toString());
oriRes.end();
}
}
async function saveUserDataToDB(data) {
return new Promise(function(resolve,reject){
db.run('INSERT or REPLACE INTO summoner(id, accountId, puuid, name, profileIconId, revisionDate, summonerLevel, leagueEntries) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', [data.id, data.accountId, data.puuid, data.name, data.profileIconId, data.revisionDate, data.summonerLevel, data.leagueEntries], (err) => {
if(err) {
return reject("[saveUserDataToDB] " + err.message);
}
console.log('Row was added to the table: ${this.lastID}');
resolve(true);
});
});
};
async function getUserDataFromDB(name){
return new Promise(function(resolve,reject){
db.all('SELECT *, COUNT(name) as res FROM summoner WHERE name=?', [name], (err, rows) => {
if(err){console.log(name); return reject("[getUserDataFromDB] " + err.message);}
resolve(rows[0]);
});
});
}
/* GET SUMMONER INFO / ADD NEW USER */
async function getSummonerPipe(name, oriRes) {
try {
dbInst = new Database(); db = dbInst.db;
console.log("Pulling data from local db");
var user = null;
user = await getUserDataFromDB(name);
console.log("USER return: " + user.res);
if(user.res == 0) {
//download
const d = await riotAPI.getSummonerByName(name);
//console.log(d);
if(!d.hasOwnProperty('status')){
const l = await riotAPI.getLeagueEntriesInAllQueues(d.id);
d.leagueEntries = JSON.stringify(l);
await saveUserDataToDB(d);
oriRes.write(JSON.stringify(d));
oriRes.end();
} else {
oriRes.end("USER_DEOS_NOT_EXISTS");
}
} else if (user.res == 1) {
console.log("Username found in local db!");
oriRes.write(JSON.stringify(user));
oriRes.end();
} else {
console.log("Error");
oriRes.end("Error");
}
db = null; dbInst.disconnect();
} catch (error) {
console.log(error);
}
}
/* UPDATE USER */
async function updateUser(name, oriRes){
try {
dbInst = new Database(); db = dbInst.db;
const d = await riotAPI.getSummonerByName(name);
const l = await riotAPI.getLeagueEntriesInAllQueues(d.id);
d.leagueEntries = JSON.stringify(l);
if(d.name){
await saveUserDataToDB(d);
oriRes.end();
} else {
oriRes.end("User does not exists in Riot Database, or API is broken :)");
}
db = null; dbInst.disconnect();
} catch (error) {
console.log(error);
}
}
/* GET MATCHES */
async function getMatchesFromDB(puuid){
return new Promise(function(resolve,reject){
db.all('SELECT match.matchId, match.data FROM match INNER JOIN matches on matches.matchId = match.matchId WHERE matches.puuid=?', [puuid], (err, rows) => {
if(err){console.log(name); return reject("[getMatchesFromDB] " + err.message);}
var allData = [];
rows.forEach( row => {
allData.push({matchId: row.matchId, data: JSON.parse(row.data)});
});
resolve(allData);
});
});
}
async function getMatches(puuid, oriRes){
try {
dbInst = new Database(); db = dbInst.db;
var allData = [];
allData = await getMatchesFromDB(puuid);
console.log(allData.length);
if(allData.length == 0){
oriRes.write("NO_MATCHES");
console.log("no games lol");
} else {
oriRes.write(JSON.stringify(allData));
}
oriRes.end();
db = null; dbInst.disconnect();
} catch (error) {
console.log(error);
}
}
/* ---------------UPDATE MATCHES-----------*/
function chunkString(str, length) {
return new Promise ((resolve) => {
const chunked = str.match(new RegExp('.{1,' + length + '}', 'g'));
resolve(chunked);
});
}
async function matchListPipe(puuid, oriRes){
try {
dbInst = new Database(); db = dbInst.db;
console.log("[Pipe1][1]Downloading list of match ids for "+puuid+"...");
const obj = await riotAPI.getListOfMatchIds(puuid);
console.log("[Pipe1][1]Done");
console.log(obj);
if(!obj.hasOwnProperty('status')){
for (const element of obj) {
console.log("[Pipe1]["+element+"]Adding match id to DB");
await db.run('INSERT INTO matches(puuid, matchId) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM matches WHERE puuid=? AND matchId=?)', [puuid, element,puuid,element]);
console.log("[Pipe1]["+element+"]Done");
console.log("[Pipe1]["+element+"] starting download Pipe");
await matchDownloadPipe(element);
console.log("[Pipe1]["+element+"] pipe completed");
};
} else {
console.log("[MATCH_LIST_PIPE] Error");
}
console.log("[MATCH_LIST_PIPE] All tasks done!");
oriRes.end();
db = null; dbInst.disconnect();
} catch (error) {
console.log(error);
}
}
async function matchDownloadPipe(matchId) {
try {
//dbInst = new Database(); db = dbInst.db;
console.log("[MATCH_DATA_PIPE] Downloading game " + matchId + "...");
const d = await riotAPI.getMatchDetails(matchId);
console.log("[MATCH_DATA_PIPE] [" + matchId + "] Done");
console.log("[MATCH_DATA_PIPE] ["+matchId+"] Saving match data to db");
await db.run('INSERT or REPLACE INTO match(matchId, data) VALUES(?, ?)', [matchId, d]);
console.log("[MATCH_DATA_PIPE] [" + matchId + "] Saved");
//db = null; dbInst.disconnect();
} catch (error) {
console.log(error);
}
}