forked from odota/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.js
99 lines (97 loc) · 2.87 KB
/
queries.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
var db = require('./db');
var async = require('async');
var redis = require('./redis').client;
function fillPlayerNames(players, cb) {
if (!players) {
return cb();
}
//make hash of account_ids to players
//use $in query to get these players from db
//loop through results and join with players by hash
//iterate back through original array to get back players in order
var player_hash = {};
players.forEach(function(p) {
player_hash[p.account_id] = p;
});
var player_ids = players.map(function(p) {
return Number(p.account_id);
});
db.players.find({
account_id: {
$in: player_ids
}
}, {
fields: {
"cache": 0
}
}, function(err, docs) {
if (err) {
return cb(err);
}
docs.forEach(function(d) {
var player = player_hash[d.account_id];
if (player && d) {
for (var prop in d) {
player[prop] = d[prop];
}
}
});
players = players.map(function(p) {
return player_hash[p.account_id];
});
cb(err);
});
}
function getSets(cb) {
async.parallel({
"bots": function(cb) {
redis.get("bots", function(err, bots) {
bots = JSON.parse(bots || "[]");
//sort list of bots descending, but full bots go to end (concentrates load)
/*
bots.sort(function(a, b) {
var threshold = 50;
if (a.friends > threshold) {
return 1;
}
if (b.friends > threshold) {
return -1;
}
return (b.friends - a.friends);
});
*/
//sort ascending (distributes load)
bots.sort(function(a,b){
return a.friends-b.friends;
});
cb(err, bots);
});
},
"ratingPlayers": function(cb) {
redis.get("ratingPlayers", function(err, rps) {
cb(err, JSON.parse(rps || "{}"));
});
},
"trackedPlayers": function(cb) {
redis.get("trackedPlayers", function(err, tps) {
cb(err, JSON.parse(tps || "{}"));
});
},
"userPlayers": function(cb) {
redis.get("userPlayers", function(err, ups) {
cb(err, JSON.parse(ups || "{}"));
});
},
"donators": function(cb) {
redis.get("donators", function(err, ds) {
cb(err, JSON.parse(ds || "{}"));
});
}
}, function(err, results) {
cb(err, results);
});
}
module.exports = {
getSets: getSets,
fillPlayerNames: fillPlayerNames
};