-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbattle.js
109 lines (85 loc) · 3.19 KB
/
battle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"use strict"
const axios = require('axios');
exports.getData = (users) => {
const usersData = [];
return new Promise((resolve, reject) => {
const composeReposRequest = (user) => {
return `https://api.github.com/users/${user}/repos`;
}
const composeUserRequest = (user) => {
return `https://api.github.com/users/${user}`;
}
function getUserRepos(user) {
return axios.get(composeReposRequest(user));
}
function getUserInfo(user) {
return axios.get(composeUserRequest(user));
}
axios.all([getUserRepos(users[0]), getUserRepos(users[1]), getUserInfo(users[0]), getUserInfo(users[1])])
.then(axios.spread(function (repos_one, repos_two, info_one, info_two) {
const fetchedData = [repos_one.data,repos_two.data];
const firstUserData = info_one.data;
const secondUserData = info_two.data;
fetchedData.map(repos => {
let stars = 0,
forks = 0,
watches = 0;
repos.forEach(repo => {
stars += repo.stargazers_count;
forks += repo.forks;
watches += repo.watchers;
});
usersData.push({
stars: stars,
forks: forks,
watches: watches
});
});
usersData[0].username = firstUserData.login;
usersData[0].followers = firstUserData.followers;
usersData[1].username = secondUserData.login;
usersData[1].followers = secondUserData.followers;
return usersData;
})).then(output => {
console.log(usersData);
return resolve(usersData);
}).catch(err=>{return reject(err);});
});
}
exports.getWinner = obj =>{
let users = obj ,
starWinner = compare(users,"stars"),
forkWinner = compare(users, "forks"),
watchWinner = compare(users, "watches"),
ffWinner = compare(users,"followers"),
battleWinner = calculatePoints(users);
return {
star_winner:starWinner,
fork_winner:forkWinner,
watch_winner:watchWinner,
follows_winner: ffWinner,
battle_winner: battleWinner
}
};
function compare(users, prop){
var user1 = users[0], user2 = users[1];
if (user1[prop] > user2[prop]){
return user1.username;
}
else if (user1[prop] < user2[prop]){
return user2.username;
}
else{
return "tie";
}
}
function calculatePoints(users){
var points = {stars:20,forks:20, watches:10,follow:5};
var user1 = users[0], user2 = users[1];
var userPt1 = 0,userPt2 = 0;
userPt1 = (user1.stars * points.stars) + (user1.forks * points.forks) + (user1.watches * points.watches) + (user1.followers * points.follow);
userPt2 = (user2.stars * points.stars) + (user2.forks * points.forks) + (user2.watches * points.watches) + (user2.followers * points.follow);
if (userPt1 > userPt2){ return user1.username; }
else if (userPt1 < userPt2){ return user2.username; }
else { return "tie"; }
}