Created
January 11, 2017 00:44
-
-
Save gamrxist/ae1c945fccdda5b32496d0939a0994b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A temporary solution to the feature request here: | |
// http://support.screeps.com/hc/en-us/community/posts/115000032045-Request-Ranking-by-GCL | |
var https = require('https'); | |
var _ = require('lodash'); | |
var min_gcl = 0; | |
var max_gcl = 3; | |
var interval = 1440; // 8 => 1hour, 180 => 24hour, 1440 => 7days | |
var results_per_page = 10; // Must be 10? API gives error with other values | |
var max_ranking = 500; // players with rank higher than this won't be included | |
var result = []; | |
var active_responses = 0; | |
var pages = []; | |
function onResponseBegin() { | |
active_responses++; | |
//console.log('>> response '+active_responses); | |
process.stdout.write('+'); | |
} | |
function onResponseEnd() { | |
active_responses--; | |
//console.log('<< response '+active_responses); | |
process.stdout.write('-'); | |
if (active_responses == 0) { | |
if (pages.length) { | |
//console.log('-- callback '+pages.length); | |
process.stdout.write('X'); | |
let callback = pages.shift(); | |
callback(); | |
} | |
else { | |
endReport(); | |
} | |
} | |
} | |
function endReport() { | |
process.stdout.write('\n'); | |
let sorted = _.orderBy(result, ['rank']); | |
_.forEach(sorted, row => console.log(JSON.stringify(row))); | |
} | |
function getUserInfo(userId, stats) { | |
//console.log(`getUserInfo(${userId})`); | |
var query = '/api/user/find?id=' + userId; | |
var options = { | |
hostname: 'screeps.com', | |
port: 443, | |
path: query, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
}; | |
var data = ''; | |
onResponseBegin(); | |
https.request(options, function(response) { | |
response.on('data', function (chunk) { | |
data += chunk; | |
}); | |
response.on('end', function() { | |
//console.log(JSON.stringify(JSON.parse(data),null,4)); | |
var info = JSON.parse(data), | |
gcl = Math.floor(Math.pow(info.user.gcl/1000000,1/2.4))+1; | |
if (gcl >= min_gcl && gcl <= max_gcl) { | |
result.push(_.merge({ | |
username: info.user.username, | |
gclLevel: gcl, | |
gcl: info.user.gcl, | |
}, stats)); | |
} | |
onResponseEnd(); | |
}); | |
}).end(); | |
} | |
function getUser(userId, interval, extra) { | |
//console.log(`getUser(${userId}) `+JSON.stringify(extra)); | |
var query = '/api/user/stats?id=' + userId + '&interval=' + interval; | |
var options = { | |
hostname: 'screeps.com', | |
port: 443, | |
path: query, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
}; | |
var data = ''; | |
onResponseBegin(); | |
https.request(options, function(response) { | |
response.on('data', function (chunk) { | |
data += chunk; | |
}); | |
response.on('end', function() { | |
//console.log(JSON.stringify(JSON.parse(data),null,4)); | |
var stats = JSON.parse(data).stats; | |
getUserInfo(userId, _.merge(extra, stats)); | |
onResponseEnd(); | |
}) | |
}).end(); | |
} | |
function getBoard(offset, interval, maxOffset) { | |
var query = '/api/leaderboard/list?limit='+ results_per_page +'&mode=world&offset=' + offset + '&season=2016-12'; | |
var options = { | |
hostname: 'screeps.com', | |
port: 443, | |
path: query, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json; charset=utf-8', | |
}, | |
}; | |
var data = ''; | |
onResponseBegin(); | |
if (offset < maxOffset) { | |
pages.push(_.bind(getBoard, null, offset + results_per_page, interval, maxOffset)); | |
} | |
https.request(options, function(response) { | |
response.on('data', function (chunk) { | |
data += chunk; | |
}); | |
response.on('end', function() { | |
//console.log(JSON.stringify(JSON.parse(data),null,4)); | |
var users = JSON.parse(data).users, | |
userIds = _.keys(users); | |
for (let i = 0, n = userIds.length; i < n; i++) { | |
getUser(users[userIds[i]]._id, interval, {rank: offset+i}); | |
} | |
onResponseEnd(); | |
}); | |
}).end(); | |
} | |
getBoard(0, interval, max_ranking); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment