Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Presac/4209cb7712188a50fe483a6ad820e521 to your computer and use it in GitHub Desktop.
Save Presac/4209cb7712188a50fe483a6ad820e521 to your computer and use it in GitHub Desktop.
Get the heropoints from all continents in Guild Wars 2
<html>
<head>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
// This variable will hold data about all continents
//var json_obj = [];
var json_obj = [{
"name": "Tyria",
"floors": [],
"id": 1
},
{
"name": "Mists",
"floors": [],
"id": 2
}
];
// Get data from continents id=1. Corresponds to Tyria
$.getJSON("https://api.guildwars2.com/v2/continents/1/floors", { page: "0" },
function(data1) {})
.done(function(data1) {
json_obj[0].floors = data1;
// Get data from continents id=2. Corresponds to Mists
$.getJSON("https://api.guildwars2.com/v2/continents/2/floors", { page: "0" },
function(data2) {})
.done(function(data2) {
json_obj[1].floors = data2;
// Iterate through both continents
for (var k = 0; k < json_obj.length; k++) {
// Iterate through all floors in the continent
// The iteration is backwards to handle deletion through splice
for (var i = json_obj[k].floors.length - 1; i >= 0; i--) {
// Save the current floor in a temporary variable
var floor = json_obj[k].floors[i];
// Delete the values we don't want
if (floor.hasOwnProperty("texture_dims")) {
delete floor.texture_dims;
}
if (floor.hasOwnProperty("clamped_view")) {
delete floor.clamped_view;
}
// Check whether the "region" key is present and save it as
// a temporary value
if (floor.hasOwnProperty("regions")) {
var allRegions = floor.regions;
// Iterate through all objects in "region"
for (var regionKey in allRegions) {
// Delete all the values we don't want
if (allRegions[regionKey].hasOwnProperty("continent_rect")) {
delete allRegions[regionKey].continent_rect;
}
if (allRegions[regionKey].hasOwnProperty("label_coord")) {
delete allRegions[regionKey].label_coord;
}
// Check whether the "map" key is present and save it as
// a temporary value
if (allRegions[regionKey].hasOwnProperty("maps")) {
var map = allRegions[regionKey].maps;
// Iterate through all keys in "map"
for (var keyKey in map) {
// Check for the presence of a key and whether it's array
// is empty
if (map[keyKey].skill_challenges.length != 0 && map[keyKey].hasOwnProperty("skill_challenges")) {
// Remove all keys on each "map" we don't want
for (var key3 in map[keyKey]) {
if (!(key3 == "skill_challenges" ||
key3 == "name")) {
delete map[keyKey][key3];
}
}
} else {
// Removes the entry in the "map" object if skill_challenges is empty or isn't there
delete map[keyKey];
}
}
// Removes the "map" entry if it is empty
if ($.isEmptyObject(map)) {
delete allRegions[regionKey].maps;
}
}
// Removes the entry in the "regions" object if "map" has been deleted from it
if (!allRegions[regionKey].hasOwnProperty("maps")) {
delete allRegions[regionKey];
}
}
// If "regions" don't hold any values, delete it.
if ($.isEmptyObject(floor.regions)) {
json_obj[k].floors.splice(i, 1);
}
}
}
}
console.log(json_obj);
var jS = JSON.stringify(json_obj, undefined, 2);
$("#json").html(jS);
});
});
</script>
<pre id="json"></pre>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment