-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackLogManager.js
More file actions
293 lines (216 loc) · 6.77 KB
/
backLogManager.js
File metadata and controls
293 lines (216 loc) · 6.77 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/****
* The purpose of the backLogManager is to contain all the scripts
* associated with grabbing a users old ranked games. We do this
* because only ranked games can be looked back at. Normal games
* are only seeable on the last 10 games a user has played.
*
* The process of getting a users old ranked games will go
* (1) Check that the user isn't already backlogged
* (2) Get the users earliest recorded match id
* (3) Iterate through users ranked games, add games that
- Match_Id < earliestRecordedMatchId
* (4) Update users backLogged status
****/
// Models to interface with Mongo DataBase
var userModel = require("./models/userModel.js").userModel;
var gameModel = require("./models/gameModel.js").gameModel;
// Config file for apiKey ect
var config = require('./config/config');
// Request for API Calls
var request = require('request');
// Converting to UTC
var moment = require('moment-timezone');
// Keeps a tally of how many users we are currently updating
var updateNumber = 0;
var checkBackloggedStatus = function checkBackloggedStatus(user, server, res){
// 1 Find user in db (if not find, res(error))
userModel.findOne({"summonerName": user, "server": server})
.exec(function (err, userData) {
if (err) console.log("Error finding backlogged status - " + err);
if(userData){
if(userData.backLogged === true){
res.send(true);
} else if(userData.backLogged === false) {
checkBackLoggedCompletion(userData,res);
}
}
});
}
function checkBackLoggedCompletion(userData, res){
// 3 Get earliest known game and send back to user
gameModel.find({userId : userData._id})
.sort({matchId : 1})
.limit(1)
.exec(function(err,gameData){
if(err) console.log(err);
res.send(gameData);
});
}
// Update User Games Functions
var updateUser = function getUserToUpdate(){
// Only update if last update is completed
if(updateNumber <= 0){
// Grab Users to update
userModel.find({$or : [{backLogged: false},{backLogged: null}], lastMatchId:{$ne:25}})
.limit(20)
.exec(function (err, userData) {
if (err) return console.error(err);
if(userData.length > 0){
updateNumber = userData.length;
while(userData.length > 0){
var user = userData[0];
userData.splice(0, 1);
backLogUser(user._id);
}
userData = null;
user = null;
} else {
//console.log("Everyone is updated");
}
});
} else {
console.log("Can't update yet still - " + updateNumber);
}
}
// Check given user isn't updated
var backLogUser = function verifyUser(userId){
userModel.findOne({"_id":userId}).exec(function (err, user) {
if (err) console.log("Error in backLogManager - " + err);
if(user && user.lastMatchId[0] !== 25 && user.backLogged === false){
// Send valid user to get earliestRecordedMatchId
getEarliestGame(user);
}
});
}
function getEarliestGame(user){
gameModel.find({userId : user._id})
.sort({matchId : 1})
.limit(1)
.exec(function(err,gameData){
if(err) console.log(err);
var oldestRecordedGame = gameData[0].matchId;
var summonerId = user.summonerId;
var server = user.server;
var indexFrom = 0;
var indexTo = 15;
var errorCount = 0;
getOldGames(user, oldestRecordedGame, summonerId, server, indexFrom,
indexTo, errorCount)
});
}
function getOldGames(user, oldestRecordedGame, summonerId, server, indexFrom, indexTo, errorCount){
// Iterate until we get 200 res + empty response
var matchHistoryUrl = 'https://' + server
+ '.api.pvp.net/api/lol/' + server
+ '/v2.2/matchhistory/' + summonerId
+ '?beginIndex=' + indexFrom
+ '&endIndex=' + indexTo
+ '&api_key=' + config.apikey;
console.log(matchHistoryUrl);
// Request up to 15 games
request( matchHistoryUrl,
function (error, response, body) {
if(error) console.log(error)
if (!error && response.statusCode == 200) {
var gamesData = JSON.parse(response.body);
var numberOfKeys = Object.keys(gamesData).length;
indexFrom += 15;
indexTo += 15;
if(numberOfKeys == 0){
// Scan complete
console.log("indexFrom =" + indexFrom + "err =" + errorCount);
// Update users backLogged status
userBackLogged(user);
} else {
// Pass to analysis function
analyzeGameSet(user, gamesData, oldestRecordedGame);
// Keep scanning
getOldGames(user, oldestRecordedGame, summonerId, server, indexFrom, indexTo, errorCount);
}
} else {
// Has there been too many errors to keep going?
if(errorCount >= 150){
// Don't Rerequest url
updateNumber -= 1;
} else if(!response || response.statusCode == 404){
// If the user can't be found, no point trying again
userBackLogged(user);
} else {
// Rerequest given url
getOldGames(user, oldestRecordedGame, summonerId, server, indexFrom, indexTo, errorCount+1);
}
}
});
}
function analyzeGameSet(user, gamesData, oldestRecordedGame){
// Get the array of games
var gamesData = gamesData["matches"];
// Iterate through each game
gamesData.forEach(function(game){
if(game.matchId < oldestRecordedGame){
var participants = game.participants[0];
var stats = participants["stats"];
var timeline = participants["timeline"];
var matchId = game.matchId;
var timePlayed = game.matchDuration;
var champion = participants.championId;
var dateCreated = game.matchCreation;
if(timeline){
var position = timeline.lane;
} else {
var position = 0;
}
var isWin = stats.winner;
if(position == "TOP"){
position = 1;
} else if(position == "MIDDLE"){
position = 2;
} else if(position == "JUNGLE"){
position = 3;
} else if(position == "BOTTOM"){
position = 4;
} else {
position = 0;
}
addGame(user, matchId, timePlayed, champion, position,
dateCreated, isWin)
} else {
// This game has already been recorded
}
});
}
function addGame(user, newMatchId, newDuration, newChampion, newPosition, dateCreated, isWin){
/*
Given data will add game to db
*/
// Format date so that it is UTC timezone
var dateCreatedUtc = moment.tz(dateCreated, "UTC").format();
console.log("DateCreated - " + dateCreatedUtc);
var newGame = new gameModel({
matchId: newMatchId,
dateTime : dateCreatedUtc,
duration : newDuration,
champion : newChampion,
position : newPosition,
server : user.server,
userId : user._id,
isWin : isWin
});
newGame.save(function (err, newGame) {
if (err) return console.error(err);
});
user = null;
}
function userBackLogged(user){
userModel.update({"_id": user._id}, { backLogged: true}, function(err,res){
if(err) return handleError(err);
console.log(user.summonerName + " backlogged");
updateNumber -= 1;
});
}
// Functions that can be called outside this module
module.exports = {
backLogUser: backLogUser,
updateUser: updateUser,
checkBackloggedStatus: checkBackloggedStatus
}