forked from odota/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetReplayUrl.js
55 lines (55 loc) · 1.96 KB
/
getReplayUrl.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
var db = require('./db');
var r = require('./redis');
var redis = r.client;
var utility = require('./utility');
var getData = utility.getData;
module.exports = function getReplayUrl(match, cb) {
db.matches.findOne({
match_id: match.match_id
}, function(err, doc) {
if (err){
return cb(err);
}
if (match.fileName || match.url) {
//if there's already a filename or url, we don't to retrieve
//this is for custom jobs!
return cb(err);
}
if (doc && doc.url) {
console.log("replay url in db");
match.url = doc.url;
return cb(err);
}
else {
redis.get("retrievers", function(err, result) {
if (err || !result) {
console.log("failed to get retrievers from redis");
return cb(err);
}
result = JSON.parse(result);
//make array of retriever urls and use a random one on each retry
var urls = result.map(function(r) {
return r + "&match_id=" + match.match_id;
});
getData(urls, function(err, body) {
if (err || !body || !body.match) {
//non-retryable error
return cb("invalid body or error");
}
var url = "http://replay" + body.match.cluster + ".valve.net/570/" + match.match_id + "_" + body.match.replaySalt + ".dem.bz2";
match.url = url;
//save replay url in db
db.matches.update({
match_id: match.match_id
}, {
$set: match
}, {
upsert: true
}, function(err) {
cb(err);
});
});
});
}
});
}