Skip to content

Instantly share code, notes, and snippets.

@superboum
Last active April 2, 2016 21:08
Show Gist options
  • Save superboum/03b4ed609669fd28a2759d45c47d3f88 to your computer and use it in GitHub Desktop.
Save superboum/03b4ed609669fd28a2759d45c47d3f88 to your computer and use it in GitHub Desktop.
Query mc.quarahtk.org API and parse data to extract nether brick used by selected players
const http = require('http');
/**********************
* L I B R A R Y
**********************/
/**
* Make a request on the API
* @param host hostname of the server, ex: mc.quarahtk.org
* @param path url path of the server, ex: /raw_data
* @param port port of the web server, ex: 80
* @param method HTTP method to use for the query, ex: GET
* @param callback function which be called when there is a result.
* cb(err,data) is err is not null, there is an error.
*/
requestAPI = function(host, path, port, method, cb) {
var options = {
port: port,
hostname: host,
method: method,
path: path
};
var req = http.request(options, function(res) {
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function() {
try {
cb(null, JSON.parse(body));
} catch(e) {
cb(e, null);
}
});
});
req.on('error', function (e) {
cb(e,null);
});
req.end();
};
getList = function(host, port, cb) {
requestAPI(host, "/raw_data/", port, "GET", cb);
};
getPlayer = function(host, port, id, cb) {
requestAPI(host, "/raw_data/"+id+".json", port, "GET", cb);
};
getPlayerList = function(host, port, list, cb) {
var todo = list.length;
list.forEach(function(elem) {
getPlayer(host,port,elem.uuid, function(err, data) {
if (!err) elem.data = data;
else elem.err = err;
if (--todo == 0) cb(list);
});
});
};
/***********************
* M A I N
***********************/
players = [
{name: "hamilcare", uuid: "e18b05dd-d692-4f54-a656-20a39ca4460b"},
{name: "cortayx", uuid: "42fd87ca-82bb-4d35-8d16-d06eb9d87a79"},
{name: "nekraus", uuid: "496de51f-8662-4a00-842b-9802eeb8cc9b"},
{name: "passion", uuid: "9d73f4a3-6bbe-4d93-98ff-59e31c523f25"}
];
var useItemNetherBrick = 0;
getPlayerList("mc.quarahtk.org", 80, players, function(list) {
list.forEach(function(player) {
console.log("----------\n"+player.name);
console.log("stat.useItem.minecraft.nether_brick: ", player.data["stat.useItem.minecraft.nether_brick"]);
useItemNetherBrick += player.data["stat.useItem.minecraft.nether_brick"];
});
console.log("-------------\nTOTAL");
console.log("stat.useItem.minecraft.nether_brick: ", useItemNetherBrick);
});
----------
hamilcare
stat.useItem.minecraft.nether_brick: 150
----------
cortayx
stat.useItem.minecraft.nether_brick: 9592
----------
nekraus
stat.useItem.minecraft.nether_brick: 2920
----------
passion
stat.useItem.minecraft.nether_brick: 3180
-------------
TOTAL
stat.useItem.minecraft.nether_brick: 15842
'stat.pickup.minecraft.nether_brick': 4782,
'stat.useItem.minecraft.nether_brick': 2920,
'stat.pickup.minecraft.nether_brick_fence': 30,
'stat.useItem.minecraft.netherrack': 26,
'stat.drop.minecraft.nether_brick': 59,
'stat.mineBlock.minecraft.nether_brick_fence': 43,
'stat.pickup.minecraft.netherrack': 4413,
'stat.mineBlock.minecraft.netherrack': 3385,
'stat.mineBlock.minecraft.nether_brick': 5254,
'stat.craftItem.minecraft.netherbrick': 481,
'stat.pickup.minecraft.netherbrick': 16,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment