Guild Wars 2 API v2 – War Score Lookup under JavaScript

Over the last few weeks had quite a bit of time away from SourceMod development and doing more stuff in Web based languages.

My last project was a small script for www.Vabbi.eu for looking up war scores with the Guild Wars 2 v2 API, utilising Axios for the HTTP Requests. That everyone is welcome to use – Licensed under the GPLv3.

/*
* Guild Wars 2 War Score Lookup Script
*
* Coded by dubbeh
*
* Licensed under the GPLv3
*
*/
var RED_WORLD_INDEX = 1;
var BLUE_WORLD_INDEX = 2;
var GREEN_WORLD_INDEX = 3;
var gw2_worlds;
var gw2_world_name;
var gw2_world_obj;
var gw2_matchup_id;
var gw2_matchup_info;
function gw2api_get_matchup_scores(world_name) {
gw2_world_name = world_name;
axios.get("https://api.guildwars2.com/v2/worlds?ids=all")
.then(gw2api_got_worlds)
.catch(function (error) {
console.log("GW2API: " + error);
});
}
function gw2api_got_worlds(response) {
if (response.status == 200 && response.statusText == 'OK') {
gw2_worlds = response.data;
gw2_world_obj = gw2api_get_world_object_from_name(gw2_world_name);
if (gw2_world_obj) {
axios.get("https://api.guildwars2.com/v2/wvw/matches/overview?world=" + gw2_world_obj.id)
.then(gw2api_got_machup_overview)
.catch(function (error) {
console.log("GW2API: " + error);
});
} else {
console.log("GW2API : Unable to find world with name '" + gw2_world_name + "'.");
}
}
}
function gw2api_got_machup_overview(response) {
if (response.status == 200 && response.statusText == 'OK') {
gw2_matchup_overview = response.data;
axios.get("https://api.guildwars2.com/v2/wvw/matches/" + gw2_matchup_overview.id)
.then(gw2api_got_matchup_info)
.catch(function (error) {
console.log("GW2API: " + error);
});
}
}
function gw2api_got_matchup_info(response) {
if (response.status == 200 && response.statusText == 'OK') {
gw2_matchup_info = response.data;
gw2api_write_matchup_stats();
}
}
function gw2api_write_matchup_stats() {
document.getElementById("gw2results").innerHTML =
"<font color=\"red\">" +
gw2api_get_matchup_world_name_list(RED_WORLD_INDEX) +
" (VP:" + gw2api_get_world_victory_points(RED_WORLD_INDEX) +
" K/D:" + gw2api_get_world_kd(RED_WORLD_INDEX) + ") " +
"</font>" +
" – " +
"<font color=\"blue\">" +
gw2api_get_matchup_world_name_list(BLUE_WORLD_INDEX) +
" (VP:" + gw2api_get_world_victory_points(BLUE_WORLD_INDEX) +
" K/D:" + gw2api_get_world_kd(BLUE_WORLD_INDEX) + ") " +
"</font>" +
" – " +
"<font color=\"green\">" +
gw2api_get_matchup_world_name_list(GREEN_WORLD_INDEX) +
" (VP:" + gw2api_get_world_victory_points(GREEN_WORLD_INDEX) +
" K/D:" + gw2api_get_world_kd(GREEN_WORLD_INDEX) + ")" +
"</font>";
}
function gw2api_get_world_object_from_id(world_id) {
for (var i in gw2_worlds) {
if (gw2_worlds[i].id == world_id) {
return gw2_worlds[i];
}
}
return 0;
}
function gw2api_get_world_object_from_name(world_name) {
for (var i in gw2_worlds) {
if (gw2_worlds[i].name == world_name) {
return gw2_worlds[i];
}
}
return 0;
}
function gw2api_get_matchup_world_name_list(world_colour_index) {
var primary_world;
var size;
var world_list = "";
if (world_colour_index == RED_WORLD_INDEX) {
primary_world = gw2api_get_world_object_from_id(gw2_matchup_overview.worlds.red);
size = gw2_matchup_info.all_worlds.red.length;
} else if (world_colour_index == BLUE_WORLD_INDEX) {
primary_world = gw2api_get_world_object_from_id(gw2_matchup_overview.worlds.blue);
size = gw2_matchup_info.all_worlds.blue.length;
} else if (world_colour_index == GREEN_WORLD_INDEX) {
primary_world = gw2api_get_world_object_from_id(gw2_matchup_overview.worlds.green);
size = gw2_matchup_info.all_worlds.green.length;
} else {
console.log("GW2 API: Unknown world_colour_index");
return world_list;
}
if (size > 1) {
world_list += primary_world.name + " + ";
for (var i = 0; i < size; i++) {
if ((gw2_matchup_info.all_worlds.red[i] == primary_world.id) ||
(gw2_matchup_info.all_worlds.blue[i] == primary_world.id) ||
(gw2_matchup_info.all_worlds.green[i] == primary_world.id)) {
continue;
} else if (i == size 2) {
if (world_colour_index == RED_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.red[i]).name;
} else if (world_colour_index == BLUE_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.blue[i]).name;
} else if (world_colour_index == GREEN_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.green[i]).name;
}
} else {
if (world_colour_index == RED_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.red[i]).name + " + ";
} else if (world_colour_index == BLUE_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.blue[i]).name + " + ";
} else if (world_colour_index == GREEN_WORLD_INDEX) {
world_list += gw2api_get_world_object_from_id(gw2_matchup_info.all_worlds.green[i]).name + " + ";
}
}
}
return world_list;
}
return primary_world.name;
}
function gw2api_get_world_total_kills(world_colour_index) {
if (world_colour_index == RED_WORLD_INDEX) {
return gw2_matchup_info.kills.red;
} else if (world_colour_index == BLUE_WORLD_INDEX) {
return gw2_matchup_info.kills.blue;
} else if (world_colour_index == GREEN_WORLD_INDEX) {
return gw2_matchup_info.kills.green;
}
return 0;
}
function gw2api_get_world_total_deaths(world_colour_index) {
if (world_colour_index == RED_WORLD_INDEX) {
return gw2_matchup_info.deaths.red;
} else if (world_colour_index == BLUE_WORLD_INDEX) {
return gw2_matchup_info.deaths.blue;
} else if (world_colour_index == GREEN_WORLD_INDEX) {
return gw2_matchup_info.deaths.green;
}
return 0;
}
function gw2api_get_world_victory_points(world_colour_index) {
if (world_colour_index == RED_WORLD_INDEX) {
return gw2_matchup_info.victory_points.red;
} else if (world_colour_index == BLUE_WORLD_INDEX) {
return gw2_matchup_info.victory_points.blue;
} else if (world_colour_index == GREEN_WORLD_INDEX) {
return gw2_matchup_info.victory_points.green;
}
}
function gw2api_get_world_kd(world_colour_index) {
if (world_colour_index == RED_WORLD_INDEX) {
return (gw2api_get_world_total_kills(RED_WORLD_INDEX) / gw2api_get_world_total_deaths(RED_WORLD_INDEX)).toFixed(2);
} else if (world_colour_index == BLUE_WORLD_INDEX) {
return (gw2api_get_world_total_kills(BLUE_WORLD_INDEX) / gw2api_get_world_total_deaths(BLUE_WORLD_INDEX)).toFixed(2);
} else if (world_colour_index == GREEN_WORLD_INDEX) {
return (gw2api_get_world_total_kills(GREEN_WORLD_INDEX) / gw2api_get_world_total_deaths(GREEN_WORLD_INDEX)).toFixed(2);
}
}

view raw
gw2api.js
hosted with ❤ by GitHub

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="js/gw2api.js"></script>
<title>GW2 API World Scores</title>
</head>
<body>
<script>
gw2api_get_matchup_scores ("Vabbi");
</script>
<p id="gw2results">Syncing…</p>
</body>
</html>

view raw
index.html
hosted with ❤ by GitHub

All you need to do is change line 12 in index.html to your own server name (Make sure to follow the standard server names returned by the API – Which can be found here).

<script>gw2api_get_matchup_scores ("My Server Name");</script>

Currently in the process over moving the code over to TypeScript and using some OOP, which is my next step and get a lot more data displayed in a pretty way.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.