forked from odota/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
82 lines (81 loc) · 2.89 KB
/
filter.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
var constants = require('./constants.json');
var utility = require('./utility');
var isSignificant = utility.isSignificant;
var isRadiant = utility.isRadiant;
module.exports = function filter(matches, filters) {
//accept a hash of filters, run all the filters in the hash in series
//console.log(filters);
var conditions = {
//filter: significant, remove unbalanced game modes/lobbies
significant: function(m, key) {
return Number(isSignificant(constants, m)) === key;
},
//filter: player won
win: function(m, key) {
return Number(m.player_win) === key;
},
patch: function(m, key) {
return m.patch === key;
},
game_mode: function(m, key) {
return m.game_mode === key;
},
lobby_type: function(m, key) {
return m.lobby_type === key;
},
hero_id: function(m, key) {
return m.players[0].hero_id === key;
},
isRadiant: function(m, key) {
return Number(m.players[0].isRadiant) === key;
},
lane_role: function(m, key) {
return m.players[0].parsedPlayer.lane_role === key;
},
//GETFULLPLAYERDATA: we need to iterate over match.all_players
//ensure all array elements fit the condition
included_account_id: function(m, key, arr) {
return arr.every(function(k) {
return m.all_players.some(function(p) {
return p.account_id === k;
});
});
},
with_hero_id: function(m, key, arr) {
return arr.every(function(k) {
return m.all_players.some(function(p) {
return (p.hero_id === k && isRadiant(p) === isRadiant(m.players[0]));
});
});
},
against_hero_id: function(m, key, arr) {
return arr.every(function(k) {
return m.all_players.some(function(p) {
return (p.hero_id === k && isRadiant(p) !== isRadiant(m.players[0]));
});
});
}
};
//TODO implement more filters, including from parse data
//filter: specific regions
//filter: item was built
//filter: max gold/xp advantage
var filtered = [];
for (var i = 0; i < matches.length; i++) {
var include = true;
//verify the match passes each filter test
for (var key in filters) {
if (conditions[key]) {
//earlier, we arrayified everything
//pass the first element, as well as the full array
//check that it passes all filters
include = include && conditions[key](matches[i], filters[key][0], filters[key]);
}
}
//if we passed, push it
if (include) {
filtered.push(matches[i]);
}
}
return filtered;
}