Skip to content

Commit

Permalink
24th
Browse files Browse the repository at this point in the history
  • Loading branch information
enriquenov committed Mar 20, 2018
1 parent 55a9ee8 commit 3ad6e7c
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions app/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,44 @@ function getStarCount(repos) {
}, 0);
}

module.exports = {
battle = function(players) {
function calculateScore(profile, repos) {
var followers = profile.followers;
var totalStars = getStarCount(repos);

return followers * 3 + totalStars;
}

function handleError(error) {
console.warn(error);
return null;
}

function getUserData(player) {
return axios.all([
getProfile(player),
getRepos(player)
]).then(function(data){
var profile = data[0];
var repos = data[1];

return {
profile: profile,
score: calculateScore(profile, repos)
}
});
}

function sortPlayers (players) {
return players.sort(function(a, b) {
return b.score - a.score;
});
}

module.exports = {
battle: function(players) {
return axios.all(players.map(getUserData))
.then(sortPlayers)
.catch(handleError)
},
fetchPopularRepos: function (language) {
var encodedURI = window.encodeURI('https://api.github.com/search/repositories?q=stars:>1+language:' + language + '&sort=stars&order=desc&type=Repositories');
Expand Down

0 comments on commit 3ad6e7c

Please sign in to comment.