Skip to content

Instantly share code, notes, and snippets.

@exiva
Last active August 29, 2015 14:27
Show Gist options
  • Save exiva/2287318cbcf6f1321a28 to your computer and use it in GitHub Desktop.
Save exiva/2287318cbcf6f1321a28 to your computer and use it in GitHub Desktop.
var getLeaderboard = function() {
var query = "users/"+settings.uid+"/scoreboard?oauth_token="+settings.oauth+"&v=20150724&m=swarm";
var xhr = new XMLHttpRequest();
xhr.open('GET', config.base + query, true);
xhr.onload = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
var items = {};
var scores = response.response.currentWeek.scores;
var pos = 100;
items.size = scores.length;
for (var i = 0; i < scores.length; i++) {
items[pos] = {};
items[pos].id = scores[i].user.id;
items[pos].name = scores[i].user.firstName+" "+scores[i].user.lastName;
items[pos].coins = scores[i].score+" coins";
pos++;
}
var appmsg = {'leaderboard': items};
console.log(JSON.stringify(appmsg));
Pebble.sendAppMessage(appmsg);
} else {
console.log(JSON.stringify(xhr.response));
}
}
};
xhr.send(null);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment