diff --git a/public/summoner.js b/public/summoner.js
index f341d81..fb2448f 100644
--- a/public/summoner.js
+++ b/public/summoner.js
@@ -71,9 +71,7 @@ function onLoad() {
return;
}
document.querySelector('#refresh').style.display = 'block';
- const parsedEntireResponse = JSON.parse(response);
- parsedEntireResponse[1] = JSON.parse(parsedEntireResponse[1]);
- createObject(parsedEntireResponse);
+ createObject(JSON.parse(response));
}
function onError() {
@@ -90,8 +88,8 @@ function createObject(JSONData) {
const userStats = JSONData[0];
const rankedStats = JSONData[1];
- const soloQStats = rankedStats.length !== 0 ? (rankedStats[0].queueType == 'RANKED_SOLO_5x5' ? rankedStats[0] : rankedStats[1]) : false;
- const matches = JSONData[2].matches;
+ const soloQStats = rankedStats !== null ? (rankedStats.queueType == 'RANKED_SOLO_5x5' ? rankedStats : JSONData[2]) : false;
+ const matches = JSONData[3].matches;
const matchesInfos = [];
// Loop on all matches
@@ -115,7 +113,7 @@ function createObject(JSONData) {
const map = maps[currentMatch.mapId];
let mode = gameModes[currentMatch.queueId];
if (!mode)
- mode = 'Undefinded gamemode';
+ mode = 'Undefined gamemode';
const champion = currentMatch.participants[participantId - 1].championId;
const role = currentMatch.participants[participantId - 1].timeline.lane;
const timeAgo = timeDifference(currentMatch.gameCreation);
@@ -160,8 +158,8 @@ function createObject(JSONData) {
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;
+ localInfos.rankedWins = soloQStats ? soloQStats.wins : undefined;
+ localInfos.rankedLosses = soloQStats ? soloQStats.losses : undefined;
nameChosen = userStats.name;
@@ -321,11 +319,5 @@ function createHTMLOneMatch(e) {
* @param text : String to display - error message
*/
function displayPlayerNotFound(text) {
- document.querySelector('.player__pp').style.background = 'url(https://cdn.valentinkaelin.ch/riot/profileicon/29.png) center/cover';
- document.querySelector('.player__name').innerHTML = '';
- document.querySelector('.player__level').innerHTML = '';
- document.querySelector('.player__rank').innerHTML = '';
- document.querySelector('.player__rank-img').style.background = 'https://cdn.valentinkaelin.ch/riot/tier-icons/provisional.png';
- document.querySelector('.player__ratio').innerHTML = text;
- document.querySelector('.list-matches').innerHTML = '';
+ document.querySelector('.player').innerHTML = `
${text}
`
}
diff --git a/server.js b/server.js
index 358f29a..4c0d582 100644
--- a/server.js
+++ b/server.js
@@ -13,6 +13,7 @@ let summonerID;
let accountID;
let username;
let JSONMatches;
+let finalJSON = [];
/* Set Port */
app.set('port', (process.env.PORT || 5000))
@@ -37,103 +38,85 @@ app.get('/summoners', function (request, response) {
app.use('/public', express.static(__dirname + '/public'));
/* Launch app */
-app.listen(app.get('port'), () => console.log(`RiotAPI test app listening on port ${app.get('port')}!`))
+app.listen(app.get('port'), () => console.log(`RiotAPI app listening on port ${app.get('port')}!`))
-// Get data of one match
-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) {
- return addMatchToJSON(obj);
- });
-}
-
-// Get data of rankeds
-function getRanked(callback) {
- request('https://euw1.api.riotgames.com/lol/league/v4/positions/by-summoner/' + summonerID + '?api_key=' + key, function (error, response, body) {
- if (!error && response.statusCode == 200) {
- callback(body);
- }
- })
-}
-
-// send data of rankeds and of username
+// Send data of a summoner
app.post('/api', function (req, res) {
//console.log(req.body.playerName);
console.time('all')
username = req.body.playerName;
-
- getAccountInfos(function (account) {
- if (!account)
- res.send(null)
- getRanked(function (ranked) {
- getMatches(function (matches) {
- let finalJSON = [];
- finalJSON.push(account);
- finalJSON.push(ranked);
- finalJSON.push(matches);
- console.log('Data sent to front');
- res.send(finalJSON);
- console.timeEnd('all')
- });
- });
- });
-
+ finalJSON = [];
+ getAccountInfos(res);
});
-
// Get account infos of an username
-function getAccountInfos(callback) {
+const getAccountInfos = function (res) {
request('https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + encodeURIComponent(username) + '?api_key=' + key, function (error, response, body) {
if (!error && response.statusCode == 200) {
let JSONBody = JSON.parse(body);
- //console.log(JSONBody);
summonerID = JSONBody.id;
accountID = JSONBody.accountId;
- callback(JSONBody);
+ finalJSON.push(JSONBody)
+ getRanked(res);
}
else {
console.log(response.statusCode);
console.log('username not found');
- callback(null);
+ res.send(null);
}
});
}
+// Get data of rankeds stats
+const getRanked = function (res) {
+ request('https://euw1.api.riotgames.com/lol/league/v4/positions/by-summoner/' + summonerID + '?api_key=' + key, function (error, response, body) {
+ if (!error && response.statusCode == 200) {
+ let JSONBody = JSON.parse(body);
+ if (JSONBody.length > 0) {
+ finalJSON.push(...JSONBody);
+ } else {
+ console.log('empty rank stats')
+ finalJSON.push(null, null);
+ }
+ getMatches(res);
+ }
+ })
+}
-// Get matches of an accountID
-function getMatches(callback) {
+// Get 10 matches of an accountID
+const getMatches = function (res) {
console.time('getMatches');
+
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) {
JSONMatches = JSON.parse(body);
+ const matchsId = JSONMatches.matches.map(x => x.gameId)
- let matchsId = [];
- for (let i = 0; i < JSONMatches.matches.length; i++) {
- matchsId[i] = JSONMatches.matches[i].gameId;
- }
-
- Promise.map(matchsId, function (item) { // old: .mapSeries
- return apicall(item);
+ Promise.map(matchsId, function (id) {
+ return getMatch('match/v4/matches/' + id);
}).then(() => {
console.timeEnd('getMatches');
- console.log('Finished');
- callback(JSONMatches);
+ console.log('Finished - Data sent to front');
+ finalJSON.push(JSONMatches)
+ res.send(finalJSON);
+ console.timeEnd('all')
}).catch(err => {
- console.log('Error');
- console.log(err.statusCode);
+ console.log('Error Promise');
+ console.log(err);
});
-
}
});
}
+// Get data of one match
+const getMatch = async function (urlApi) {
+ //console.log(urlApi);
+ return rp({ url: 'https://euw1.api.riotgames.com/lol/' + urlApi + '?api_key=' + key, json: true }).then(function (obj) {
+ return addMatchToJSON(obj);
+ });
+}
+
// Add match to global JSON
-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;
- }
- }
-}
\ No newline at end of file
+const addMatchToJSON = function (obj) {
+ JSONMatches.matches = JSONMatches.matches.map((match) => match.gameId === obj.gameId ? obj : match);
+}