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 url = '/api';
var params = 'playerName=Kalane';
var nameChosen = '';
var infos = {};
var localInfos = {};
var itemsJSON;
// Get username from param
@ -37,38 +36,15 @@ async function main() {
req.open('POST', url, true);
document.querySelector('.loader--overlay').style.display = 'block';
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// req.send(params);
req.send('playerName=' + nameChosen);
}
/* Debug button to reset localstorage */
document.querySelector('.debug').addEventListener('click', () => {
console.log('CLEAR LOCALSTORAGE');
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 */
var refreshBtn = document.querySelector('#refresh');
refreshBtn.addEventListener('click', (e) => {
@ -177,22 +153,22 @@ function createObject(JSONData) {
}
console.log(matchesInfos);
infos.accountId = userStats.accountId;
infos.matches = matchesInfos;
infos.profileIconId = userStats.profileIconId;
infos.name = userStats.name;
infos.level = userStats.summonerLevel;
infos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé';
infos.rankImgLink = getRankImg(soloQStats);
infos.rankedWins = soloQStats.wins;
infos.rankedLosses = soloQStats.losses;
localInfos.accountId = userStats.accountId;
localInfos.matches = matchesInfos;
localInfos.profileIconId = userStats.profileIconId;
localInfos.name = userStats.name;
localInfos.level = userStats.summonerLevel;
localInfos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé';
localInfos.rankImgLink = getRankImg(soloQStats);
localInfos.rankedWins = soloQStats.wins;
localInfos.rankedLosses = soloQStats.losses;
nameChosen = userStats.name;
console.log('====== Saved infos ======');
console.log(infos);
console.log(localInfos);
localStorage[nameChosen] = JSON.stringify(infos);
localStorage[nameChosen] = JSON.stringify(localInfos);
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) {
//console.log(urlApi);
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
app.post('/api', function (req, res) {
//console.log(req.body.playerName);
console.time('all')
pseudo = req.body.playerName;
getAccountInfos(function (account) {
@ -72,6 +74,7 @@ app.post('/api', function (req, res) {
finalJSON.push(matches);
console.log('Data sent to front');
res.send(finalJSON);
console.timeEnd('all')
});
});
});
@ -90,6 +93,7 @@ function getAccountInfos(callback) {
callback(JSONBody);
}
else {
console.log(response.statusCode);
console.log('username not found');
callback(null);
}
@ -109,7 +113,7 @@ function getMatches(callback) {
matchsId[i] = JSONMatches.matches[i].gameId;
}
Promise.mapSeries(matchsId, function (item) {
Promise.map(matchsId, function (item) { // old: .mapSeries
return apicall(item);
}).then(() => {
console.timeEnd('getMatches');
@ -117,6 +121,7 @@ function getMatches(callback) {
callback(JSONMatches);
}).catch(err => {
console.log('Error');
console.log(err.statusCode);
});
}
@ -126,7 +131,6 @@ function getMatches(callback) {
function addMatchToJSON(obj) {
//console.log(obj.gameId);
for (var i = 0; i < JSONMatches.matches.length; i++) {
if (JSONMatches.matches[i].gameId == obj.gameId) {
//console.log('yes');

View file

@ -28,8 +28,8 @@
<div class="search">
<div class="container mx-auto">
<form class="flex items-center mb-6" id="changeName" method="post" action="api">
<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">
<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="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">
Chercher
</button>