Try to make 10 request : 1 per match

This commit is contained in:
Kalaneee 2018-12-05 22:04:16 +01:00
parent 8f91674a95
commit 6a760869e8
3 changed files with 94 additions and 65 deletions

View file

@ -16,11 +16,40 @@ function onLoad() {
var userStats = parsedEntireResponse[0]; var userStats = parsedEntireResponse[0];
var rankedStats = JSON.parse(parsedEntireResponse[1]); var rankedStats = JSON.parse(parsedEntireResponse[1]);
var soloQStats = rankedStats.length !== 0 ? (rankedStats[0].queueType == 'RANKED_SOLO_5x5' ? rankedStats[0] : rankedStats[1]) : false; var soloQStats = rankedStats.length !== 0 ? (rankedStats[0].queueType == 'RANKED_SOLO_5x5' ? rankedStats[0] : rankedStats[1]) : false;
var matches = parsedEntireResponse[2]; var matches = parsedEntireResponse[2].matches;
//console.log(userStats); var accountId = userStats.accountId;
//console.log(rankedStats);
console.log(matches); var matchesInfos = [];
console.log(soloQStats); // Loop on all matches
for (let i = 0; i < matches.length; i++) {
var currentMatch = matches[i];
var participantId;
for (let i = 0; i < currentMatch.participantIdentities.length; i++) {
if (currentMatch.participantIdentities[i].player.accountId === accountId)
participantId = currentMatch.participantIdentities[i].participantId;
}
var teamId = currentMatch.participants[participantId - 1].teamId;
var win = false;
for (let i = 0; i < currentMatch.teams.length; i++) {
if (currentMatch.teams[i].teamId === teamId) {
if (currentMatch.teams[i].win === 'Win')
win = true;
}
}
var champion = currentMatch.participants[participantId - 1].championId;
var role = currentMatch.participants[participantId - 1].timeline.lane;
matchesInfos.push({
result: win,
champ: champion,
role: role
});
}
console.log(matchesInfos);
document.querySelector('.player__pp').style.background = 'url(https://cdn.valentinkaelin.ch/riot/profileicon/' + userStats.profileIconId + '.png) center/cover'; document.querySelector('.player__pp').style.background = 'url(https://cdn.valentinkaelin.ch/riot/profileicon/' + userStats.profileIconId + '.png) center/cover';
document.querySelector('.player__name').innerHTML = userStats.name; document.querySelector('.player__name').innerHTML = userStats.name;
@ -39,13 +68,14 @@ function onLoad() {
var matchesList = document.createElement('ul'); var matchesList = document.createElement('ul');
matchesList.className = 'list-matches'; matchesList.className = 'list-matches';
playerContainer.appendChild(matchesList); playerContainer.appendChild(matchesList);
matches.matches.forEach(e => { matchesInfos.forEach(e => {
var li = document.createElement('li'); var li = document.createElement('li');
li.className = e.result ? 'win' : 'lose';
var img = document.createElement('img'); var img = document.createElement('img');
var championName = championsId[e.champion]; var championName = championsId[e.champ];
img.setAttribute('src', '/public/img/champions/' + championName + '.png'); img.setAttribute('src', '/public/img/champions/' + championName + '.png');
img.className = 'champion-icon'; img.className = 'champion-icon';
li.innerHTML = championName + ' - ' + e.lane; li.innerHTML = championName + ' - ' + e.role;
li.appendChild(img); li.appendChild(img);
matchesList.appendChild(li); matchesList.appendChild(li);
}); });
@ -62,7 +92,6 @@ var nameToPick = document.querySelector('#name')
changeName.addEventListener('submit', function (e) { changeName.addEventListener('submit', function (e) {
e.preventDefault(); e.preventDefault();
//url = '/api?' + nameToPick.value; //url = '/api?' + nameToPick.value;
url = '/api';
req.open('POST', url, true); req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send('playerName=' + nameToPick.value); req.send('playerName=' + nameToPick.value);
@ -75,11 +104,11 @@ changeName.addEventListener('submit', function (e) {
function getRankImg(soloQStats) { function getRankImg(soloQStats) {
if (soloQStats) { if (soloQStats) {
if (soloQStats.tier != 'MASTER' && soloQStats.tier != 'CHALLENGER') { if (soloQStats.tier != 'MASTER' && soloQStats.tier != 'CHALLENGER') {
return '/public/img/tier-icons/' + soloQStats.tier.toLowerCase() + '_' + soloQStats.rank.toLowerCase() + '.png'; return 'https://cdn.valentinkaelin.ch/riot/tier-icons/' + soloQStats.tier.toLowerCase() + '_' + soloQStats.rank.toLowerCase() + '.png';
} else { } else {
return '/public/img/tier-icons/' + soloQStats.tier.toLowerCase() + '.png'; return 'https://cdn.valentinkaelin.ch/riot/tier-icons/' + soloQStats.tier.toLowerCase() + '.png';
} }
} else { } else {
return '/public/img/tier-icons/provisional.png'; return 'https://cdn.valentinkaelin.ch/riot/tier-icons/provisional.png';
} }
} }

View file

@ -39,15 +39,16 @@
.list-matches li { .list-matches li {
padding: 10px 0; padding: 10px 0;
background: #F1F5F8; background: #51D88A;
} }
.list-matches li:nth-child(2n) { .list-matches li.lose {
background: #DAE1E7; background: #EF5753;
} }
.champion-icon { .champion-icon {
width: 48px; width: 48px;
height: 48px; height: 48px;
margin-left: 5px; display: block;
margin: 0 auto;
} }

View file

@ -2,17 +2,20 @@ const express = require('express')
const request = require('request'); const request = require('request');
const path = require('path'); const path = require('path');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const rp = require('request-promise');
var Promise = require("bluebird");
const app = express() const app = express()
app.set('port', (process.env.PORT || 5000)) app.set('port', (process.env.PORT || 5000))
const key = 'RGAPI-5be8ff59-ab0b-48a8-a924-10984670a571'; const key = 'RGAPI-158e34a3-b8c2-4835-81bd-6bf0537e8c04';
var summonerID = 'HMOiIUvzYtfgPk5X53zWTeOZo52T-HYJQhwvhkPNh0BWxZ0'; var summonerID = 'HMOiIUvzYtfgPk5X53zWTeOZo52T-HYJQhwvhkPNh0BWxZ0';
var accountID = 'V1xNS14bjVeP54hg03JeMxkXJB29K4TfUMvijDB85nxbD4Y'; var accountID = 'V1xNS14bjVeP54hg03JeMxkXJB29K4TfUMvijDB85nxbD4Y';
var pseudo = 'Chil'; var pseudo = 'Chil';
var JSONMatches;
/* To retrieve data of post request */ /* To retrieve data of post request */
app.use( bodyParser.json() ); // to support JSON-encoded bodies app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true extended: true
})); }));
@ -29,6 +32,15 @@ app.use('/public', express.static(__dirname + '/public'));
app.listen(app.get('port'), () => console.log(`RiotAPI test app listening on port ${app.get('port')}!`)) app.listen(app.get('port'), () => console.log(`RiotAPI test app listening on port ${app.get('port')}!`))
function apicall(urlApi) {
//console.log(urlApi);
return rp({ url: 'https://euw1.api.riotgames.com/lol/match/v4/matches/' + urlApi + '?api_key=' + key, json: true }).then(function (obj) {
return addMatchToJSON(obj);
});
}
// Get data of rankeds // Get data of rankeds
function getRanked(callback) { function getRanked(callback) {
request('https://euw1.api.riotgames.com/lol/league/v4/positions/by-summoner/' + summonerID + '?api_key=' + key, function (error, response, body) { request('https://euw1.api.riotgames.com/lol/league/v4/positions/by-summoner/' + summonerID + '?api_key=' + key, function (error, response, body) {
@ -40,32 +52,13 @@ function getRanked(callback) {
// send data of rankeds and of username // send data of rankeds and of username
app.get('/api', function (req, res) { app.post('/api', function (req, res) {
var name = req.url.split('?')[1]; //console.log(req.body.playerName);
if (name != undefined) {
pseudo = name;
}
getAccountInfos(function (account) {
getRanked(function (ranked) {
getMatches(function(matches) {
var finalJSON = new Array();
finalJSON.push(account);
finalJSON.push(ranked);
finalJSON.push(matches);
res.send(finalJSON);
});
});
});
});
app.post('/api', function(req, res) {
console.log(req.body.playerName);
pseudo = req.body.playerName; pseudo = req.body.playerName;
getAccountInfos(function (account) { getAccountInfos(function (account) {
getRanked(function (ranked) { getRanked(function (ranked) {
getMatches(function(matches) { getMatches(function (matches) {
var finalJSON = new Array(); var finalJSON = new Array();
finalJSON.push(account); finalJSON.push(account);
finalJSON.push(ranked); finalJSON.push(ranked);
@ -78,31 +71,12 @@ app.post('/api', function(req, res) {
}); });
/*
app.get('/api', function (req, res) {
var name = req.url.split('?')[1];
if (name != undefined) {
pseudo = name;
}
getAccountInfos(function (JSONBody) {
getRanked(function (body) {
dataAPI = body;
var finalJSON = new Array();
finalJSON.push(JSONBody);
finalJSON.push(dataAPI);
res.send(finalJSON);
});
});
});*/
// Get account infos of an username // Get account infos of an username
function getAccountInfos(callback) { function getAccountInfos(callback) {
request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + pseudo + '?api_key=' + key, function (error, response, body) { request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + pseudo + '?api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
var JSONBody = JSON.parse(body); var JSONBody = JSON.parse(body);
console.log(JSONBody); //console.log(JSONBody);
summonerID = JSONBody.id; summonerID = JSONBody.id;
accountID = JSONBody.accountId; accountID = JSONBody.accountId;
callback(JSONBody); callback(JSONBody);
@ -115,9 +89,34 @@ function getAccountInfos(callback) {
function getMatches(callback) { function getMatches(callback) {
request('https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + accountID + '?endIndex=10&api_key=' + key, function (error, response, body) { request('https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + accountID + '?endIndex=10&api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
var JSONMatches = JSON.parse(body); JSONMatches = JSON.parse(body);
//console.log(JSONMatches);
callback(JSONMatches); let matchsId = new Array();
for (let i = 0; i < JSONMatches.matches.length; i++) {
matchsId[i] = JSONMatches.matches[i].gameId;
}
Promise.mapSeries(matchsId, function (item) {
return apicall(item);
}).then(() => {
console.log('Finished');
callback(JSONMatches);
}).catch(err => {
console.log('Error');
});
} }
}); });
} }
function addMatchToJSON(obj) {
//console.log(obj.gameId);
for (let i = 0; i < JSONMatches.matches.length; i++) {
if(JSONMatches.matches[i].gameId == obj.gameId) {
//console.log('yes');
JSONMatches.matches[i] = obj;
}
}
}