From 6a760869e8b08b2f6ea152feeae08911f66a1bee Mon Sep 17 00:00:00 2001 From: Kalaneee Date: Wed, 5 Dec 2018 22:04:16 +0100 Subject: [PATCH] Try to make 10 request : 1 per match --- public/client.js | 53 ++++++++++++++++++++------ public/style.css | 9 +++-- server.js | 97 ++++++++++++++++++++++++------------------------ 3 files changed, 94 insertions(+), 65 deletions(-) diff --git a/public/client.js b/public/client.js index ff969cf..7946439 100644 --- a/public/client.js +++ b/public/client.js @@ -16,11 +16,40 @@ function onLoad() { var userStats = parsedEntireResponse[0]; var rankedStats = JSON.parse(parsedEntireResponse[1]); var soloQStats = rankedStats.length !== 0 ? (rankedStats[0].queueType == 'RANKED_SOLO_5x5' ? rankedStats[0] : rankedStats[1]) : false; - var matches = parsedEntireResponse[2]; - //console.log(userStats); - //console.log(rankedStats); - console.log(matches); - console.log(soloQStats); + var matches = parsedEntireResponse[2].matches; + var accountId = userStats.accountId; + + var matchesInfos = []; + // 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__name').innerHTML = userStats.name; @@ -39,13 +68,14 @@ function onLoad() { var matchesList = document.createElement('ul'); matchesList.className = 'list-matches'; playerContainer.appendChild(matchesList); - matches.matches.forEach(e => { + matchesInfos.forEach(e => { var li = document.createElement('li'); + li.className = e.result ? 'win' : 'lose'; var img = document.createElement('img'); - var championName = championsId[e.champion]; + var championName = championsId[e.champ]; img.setAttribute('src', '/public/img/champions/' + championName + '.png'); img.className = 'champion-icon'; - li.innerHTML = championName + ' - ' + e.lane; + li.innerHTML = championName + ' - ' + e.role; li.appendChild(img); matchesList.appendChild(li); }); @@ -62,7 +92,6 @@ var nameToPick = document.querySelector('#name') changeName.addEventListener('submit', function (e) { e.preventDefault(); //url = '/api?' + nameToPick.value; - url = '/api'; req.open('POST', url, true); req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); req.send('playerName=' + nameToPick.value); @@ -75,11 +104,11 @@ changeName.addEventListener('submit', function (e) { function getRankImg(soloQStats) { if (soloQStats) { 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 { - return '/public/img/tier-icons/' + soloQStats.tier.toLowerCase() + '.png'; + return 'https://cdn.valentinkaelin.ch/riot/tier-icons/' + soloQStats.tier.toLowerCase() + '.png'; } } else { - return '/public/img/tier-icons/provisional.png'; + return 'https://cdn.valentinkaelin.ch/riot/tier-icons/provisional.png'; } } \ No newline at end of file diff --git a/public/style.css b/public/style.css index 1156225..4bcbd52 100644 --- a/public/style.css +++ b/public/style.css @@ -39,15 +39,16 @@ .list-matches li { padding: 10px 0; - background: #F1F5F8; + background: #51D88A; } -.list-matches li:nth-child(2n) { - background: #DAE1E7; +.list-matches li.lose { + background: #EF5753; } .champion-icon { width: 48px; height: 48px; - margin-left: 5px; + display: block; + margin: 0 auto; } \ No newline at end of file diff --git a/server.js b/server.js index 42e6b57..778dc6e 100644 --- a/server.js +++ b/server.js @@ -2,20 +2,23 @@ const express = require('express') const request = require('request'); const path = require('path'); const bodyParser = require('body-parser'); +const rp = require('request-promise'); +var Promise = require("bluebird"); const app = express() 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 accountID = 'V1xNS14bjVeP54hg03JeMxkXJB29K4TfUMvijDB85nxbD4Y'; var pseudo = 'Chil'; +var JSONMatches; /* 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 extended: true -})); +})); /* Homepage */ app.get('/', function (request, response) { @@ -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')}!`)) + +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) { @@ -40,32 +52,13 @@ function getRanked(callback) { // send data of rankeds and of username -app.get('/api', function (req, res) { - var name = req.url.split('?')[1]; - 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); +app.post('/api', function (req, res) { + //console.log(req.body.playerName); pseudo = req.body.playerName; getAccountInfos(function (account) { getRanked(function (ranked) { - getMatches(function(matches) { + getMatches(function (matches) { var finalJSON = new Array(); finalJSON.push(account); 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 function getAccountInfos(callback) { 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) { var JSONBody = JSON.parse(body); - console.log(JSONBody); + //console.log(JSONBody); summonerID = JSONBody.id; accountID = JSONBody.accountId; callback(JSONBody); @@ -113,11 +87,36 @@ function getAccountInfos(callback) { // Get matches of an accountID 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) { - var JSONMatches = JSON.parse(body); - //console.log(JSONMatches); - callback(JSONMatches); + JSONMatches = JSON.parse(body); + + 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; + } + } } \ No newline at end of file