Use map instead of mapSeries

This commit is contained in:
Valentin Kaelin 2019-03-24 19:47:13 +01:00
parent 34a99fd799
commit 79856538bf
3 changed files with 20 additions and 40 deletions

View file

@ -1,8 +1,7 @@
var req = new XMLHttpRequest(); var req = new XMLHttpRequest();
var url = '/api'; var url = '/api';
var params = 'playerName=Kalane';
var nameChosen = ''; var nameChosen = '';
var infos = {}; var localInfos = {};
var itemsJSON; var itemsJSON;
// Get username from param // Get username from param
@ -37,38 +36,15 @@ async function main() {
req.open('POST', url, true); req.open('POST', url, true);
document.querySelector('.loader--overlay').style.display = 'block'; document.querySelector('.loader--overlay').style.display = 'block';
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// req.send(params);
req.send('playerName=' + nameChosen); req.send('playerName=' + nameChosen);
} }
/* Debug button to reset localstorage */ /* Debug button to reset localstorage */
document.querySelector('.debug').addEventListener('click', () => { document.querySelector('.debug').addEventListener('click', () => {
console.log('CLEAR LOCALSTORAGE'); console.log('CLEAR LOCALSTORAGE');
localStorage.clear(); localStorage.clear();
}); });
/* Form to change username */
var changeName = document.querySelector('#changeName');
var nameToPick = document.querySelector('#name')
changeName.addEventListener('submit', function (e) {
e.preventDefault();
console.log('here')
nameChosen = nameToPick.value;
if (localStorage[nameChosen]) {
console.log('cached on search');
document.querySelector('#refresh').style.display = 'block';
displayContent(localStorage[nameChosen]);
} else {
document.querySelector('.loader--overlay').style.display = 'block';
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send('playerName=' + nameToPick.value);
}
});
/* Refresh button */ /* Refresh button */
var refreshBtn = document.querySelector('#refresh'); var refreshBtn = document.querySelector('#refresh');
refreshBtn.addEventListener('click', (e) => { refreshBtn.addEventListener('click', (e) => {
@ -177,22 +153,22 @@ function createObject(JSONData) {
} }
console.log(matchesInfos); console.log(matchesInfos);
infos.accountId = userStats.accountId; localInfos.accountId = userStats.accountId;
infos.matches = matchesInfos; localInfos.matches = matchesInfos;
infos.profileIconId = userStats.profileIconId; localInfos.profileIconId = userStats.profileIconId;
infos.name = userStats.name; localInfos.name = userStats.name;
infos.level = userStats.summonerLevel; localInfos.level = userStats.summonerLevel;
infos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé'; localInfos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé';
infos.rankImgLink = getRankImg(soloQStats); localInfos.rankImgLink = getRankImg(soloQStats);
infos.rankedWins = soloQStats.wins; localInfos.rankedWins = soloQStats.wins;
infos.rankedLosses = soloQStats.losses; localInfos.rankedLosses = soloQStats.losses;
nameChosen = userStats.name; nameChosen = userStats.name;
console.log('====== Saved infos ======'); console.log('====== Saved infos ======');
console.log(infos); console.log(localInfos);
localStorage[nameChosen] = JSON.stringify(infos); localStorage[nameChosen] = JSON.stringify(localInfos);
displayContent(localStorage[nameChosen]); displayContent(localStorage[nameChosen]);
} }

View file

@ -38,6 +38,7 @@ app.listen(app.get('port'), () => console.log(`RiotAPI test app listening on por
async function apicall(urlApi) { async function apicall(urlApi) {
//console.log(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 rp({ url: 'https://euw1.api.riotgames.com/lol/match/v4/matches/' + urlApi + '?api_key=' + key, json: true }).then(function (obj) {
@ -59,6 +60,7 @@ function getRanked(callback) {
// send data of rankeds and of username // send data of rankeds and of username
app.post('/api', function (req, res) { app.post('/api', function (req, res) {
//console.log(req.body.playerName); //console.log(req.body.playerName);
console.time('all')
pseudo = req.body.playerName; pseudo = req.body.playerName;
getAccountInfos(function (account) { getAccountInfos(function (account) {
@ -72,6 +74,7 @@ app.post('/api', function (req, res) {
finalJSON.push(matches); finalJSON.push(matches);
console.log('Data sent to front'); console.log('Data sent to front');
res.send(finalJSON); res.send(finalJSON);
console.timeEnd('all')
}); });
}); });
}); });
@ -90,6 +93,7 @@ function getAccountInfos(callback) {
callback(JSONBody); callback(JSONBody);
} }
else { else {
console.log(response.statusCode);
console.log('username not found'); console.log('username not found');
callback(null); callback(null);
} }
@ -109,7 +113,7 @@ function getMatches(callback) {
matchsId[i] = JSONMatches.matches[i].gameId; matchsId[i] = JSONMatches.matches[i].gameId;
} }
Promise.mapSeries(matchsId, function (item) { Promise.map(matchsId, function (item) { // old: .mapSeries
return apicall(item); return apicall(item);
}).then(() => { }).then(() => {
console.timeEnd('getMatches'); console.timeEnd('getMatches');
@ -117,6 +121,7 @@ function getMatches(callback) {
callback(JSONMatches); callback(JSONMatches);
}).catch(err => { }).catch(err => {
console.log('Error'); console.log('Error');
console.log(err.statusCode);
}); });
} }
@ -126,7 +131,6 @@ function getMatches(callback) {
function addMatchToJSON(obj) { function addMatchToJSON(obj) {
//console.log(obj.gameId); //console.log(obj.gameId);
for (var i = 0; i < JSONMatches.matches.length; i++) { for (var i = 0; i < JSONMatches.matches.length; i++) {
if (JSONMatches.matches[i].gameId == obj.gameId) { if (JSONMatches.matches[i].gameId == obj.gameId) {
//console.log('yes'); //console.log('yes');

View file

@ -28,8 +28,8 @@
<div class="search"> <div class="search">
<div class="container mx-auto"> <div class="container mx-auto">
<form class="flex items-center mb-6" id="changeName" method="post" action="api"> <form class="flex items-center mb-6" id="changeName" method="get" action="summoners">
<input type="search" class="hadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline mr-1" id="name" name="playerName" placeholder="Pseudo du Joueur"> <input type="search" class="hadow appearance-none border rounded w-full py-2 px-3 text-grey-darker leading-tight focus:outline-none focus:shadow-outline mr-1" id="name" name="username" placeholder="Pseudo du Joueur">
<button type="submit" class="bg-white hover:bg-grey-lightest text-grey-darkest font-semibold py-2 px-4 mr-1 border border-grey-light rounded shadow"> <button type="submit" class="bg-white hover:bg-grey-lightest text-grey-darkest font-semibold py-2 px-4 mr-1 border border-grey-light rounded shadow">
Chercher Chercher
</button> </button>