This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
getPlayers.js
191 lines (172 loc) · 5.73 KB
/
getPlayers.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
// Dependencies
var Promise = require('bluebird');
var parser = require('cheerio');
// Includes
var http = require('./util/http.js').func;
var getRoles = require('./util/getRoles.js').func;
var getRole = require('./util/getRole.js').func;
var promise = require('./util/promise.js');
var getVerificationInputs = require('./util/getVerificationInputs.js').func;
var settings = require('../settings.json').getPlayers;
// Args
exports.required = ['group'];
exports.optional = ['rank', 'limit', 'online'];
// Define
var interval = settings.interval; // Players for every set of requests
var delay = settings.delay; // Time (in milliseconds) to wait after every interval
var debug = false; // Prints out information about what the function is doing, good for status updates or debugging
// Because of the complexity of this function, it's easier to have shared variables initiated every time a call is made.
exports.func = function (args) {
var players = {};
var added = 0;
var scanned = 0;
var rank;
var limit;
var online;
var group;
var post;
var pages;
var total;
var httpOpt;
function addPlayer (name, id) {
if (!players[name]) {
added++;
players[name] = id;
}
}
function echo (str) {
if (debug) {
console.log(str);
}
}
function getPlayersOnPage (html) {
var $ = parser.load(html);
var currentCount = parseInt($('[name="ctl00$cphRoblox$rbxGroupRoleSetMembersPane$RolesetCountHidden"]').attr('value'), 10);
if (rank && rank !== -1) {
if (total) {
total = Math.min(total, currentCount);
} else {
total = currentCount;
}
}
$('div[class=Avatar]').each(function (index, element) {
if ((limit > -1) && added >= limit) {
return;
}
scanned++;
element = $(element);
var link = element.find('a').first();
var img = element.find('.OnlineStatus').find('img').first();
if (!online || img.attr('src') === '../images/online.png') {
var id = parseInt(link.attr('href').substring(29), 10);
addPlayer(link.attr('title'), id);
}
element = null;
});
$ = null;
html = null;
}
function runInterval (pass) {
return promise(function (resolve, reject) {
var toDo = [];
var max = (pass + 1) * interval;
echo('Current: ' + (pass * interval + 1 + (pass === 0 ? 1 : 0)));
echo('To: ' + max + ' out of ' + pages + ' under pass ' + pass);
var last = Math.min(pages, max);
for (var i = pass * interval + 1 + (pass === 0 ? 1 : 0); i <= last; i++) {
if (limit >= -1) {
toDo.push(i);
} else {
httpOpt.options.form.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlUsers_Footer$ctl01$PageTextBox = i;
toDo.push(http(httpOpt).then(getPlayersOnPage));
}
}
function completed () {
toDo = null;
setTimeout(resolve, delay);
}
if (limit >= -1) {
Promise.each(toDo, function (i) {
httpOpt.options.form.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlUsers_Footer$ctl01$PageTextBox = i;
return http(httpOpt).then(getPlayersOnPage);
}).then(completed);
} else {
Promise.all(toDo).then(completed);
}
});
}
function getPlayersInRole (role) {
var url = '//www.roblox.com/Groups/group.aspx?gid=' + group;
return http({url: url})
.then(function (body) {
var $ = parser.load(body);
if (!rank || rank === -1 && !total) {
total = parseInt($('#MemberCount').text().substring(9), 10);
}
post = getVerificationInputs({html: body});
post.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlRolesetList = role;
post.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$currentRoleSetID = role;
post.__EVENTTARGET = 'ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlRolesetList';
post.__ASYNCPOST = 'true';
delete post.__RequestVerificationToken;
httpOpt = {
url: url,
options: {
method: 'POST',
form: post,
headers: {
'User-Agent': 'Mozilla'
}
}
};
return http(httpOpt)
.then(function (body) {
var $ = parser.load(body);
pages = parseInt($('div[id=ctl00_cphRoblox_rbxGroupRoleSetMembersPane_dlUsers_Footer_ctl01_Div1]').find('div[class=paging_pagenums_container]').text(), 10) || 1;
if (limit > -1) {
pages = Math.min(pages, Math.ceil(limit / 12));
total = (total != null) ? total : limit;
}
getPlayersOnPage(body);
if (pages === 1) {
return;
}
delete post.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlRolesetList;
post.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlUsers_Footer$ctl01$PageTextBox = 1;
post.ctl00$cphRoblox$rbxGroupRoleSetMembersPane$dlUsers_Footer$ctl01$HiddenInputButton = '';
var toDo = [];
for (var pass = 0; pass < pages / 1000; pass++) {
toDo.push(pass);
}
return Promise.each(toDo, runInterval);
}); });
}
return {
promise: promise(function (resolve, reject) {
group = args.group;
rank = args.rank;
online = args.online || false;
limit = args.limit || -2;
getRoles({group: group}).then(function (roles) {
var toDo = [];
if (rank && rank !== -1) {
toDo.push(getRole({roles: roles, rank: rank}).ID);
} else {
for (var i = 0; i < roles.length; i++) {
toDo.push(roles[i].ID);
}
}
Promise.each(toDo, getPlayersInRole).then(function () {
total = added;
resolve({
total: added,
players: players
});
});
});
}),
getStatus: function () {
return Math.round((total ? (scanned / total) : 0) * 10000) / 100;
}
};
};