Search code examples
javascriptnode.jsapiriot-games-api

NODE JS API RIOT for list rank friends


Good morning, everyone,

I'm a young developer and I really like nodejs, I try to learn by myself by creating small personal applications. So I created an application that uses the RIOT API, to display the nickname, the league with its points, the points won or lost. It works very well for one person, but when I decide to make it work for several people it gets stuck. On the net we talk about asynchronous but being a novice I'm lost, here is my code, first of all thank you for reading, and in advance for the help.

Ps: Sorry for me bad english :D

var https = require('https');
var http = require('http');


const EU = 'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const NA1 = 'https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const URLKEY = '?api_key=';
    const Thomas = 'ZHd2VXBU5vuGumSIty1WXq5OqWu0XpAfXG8TyjIgDSwAGlU';
    const John = 'DkcxPRTDj1WDUMiOn_RN16AtGqNvKKig6SItWxoc3FhmIfQ';
    const MaximeG = 'LzIGCODKziH7E4QvOJ0cFHL2MJ3B00pukIShOH8qYemKlXs';

var server = http.createServer(function(req, res){
    let data = '';
    const key = '';

    // Doesn't work when you put multiple players :(
    const PLAYERID= [Thomas,John]; 

    PLAYERID.forEach(id => {
        console.log('Here the different id');
        console.log(id);
        console.log('continuation');

    https.get(EU + id + URLKEY + key,
    (rr)=>{
        rr.on('data', (chunk) => {
            console.log(chunk.toString());
            data += chunk;
});
        rr.on('end', () => { 
            let result = JSON.parse(data);
            console.log('result before json');
            console.log(result);
            console.log('result after parse');
            console.log(data);

            let output = [''];
            for(let i=0;i<result.length;i++){
                output += "Name : "+result[i].summonerName+ " League : "+result[i].queueType+"\n\tRank : "+result[i].tier+" "+result[i].rank+" "+result[i].leaguePoints+" LP Nombre de victoires : "+result[i].wins+" Nombre de defaites : "+result[i].losses+" \n";
        console.log(output);
            }
            res.end(output);
        });
    });
});
}).listen(8080);

Solution

  • Try this

    Replace const key with your key

    
    var https = require('https');
    var http = require('http');
    
    
    const EU = 'https://euw1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const NA1 = 'https://na1.api.riotgames.com/lol/league/v4/entries/by-summoner/';
    const URLKEY = '?api_key=';
    const Thomas = 'ZHd2VXBU5vuGumSIty1WXq5OqWu0XpAfXG8TyjIgDSwAGlU';
    const John = 'DkcxPRTDj1WDUMiOn_RN16AtGqNvKKig6SItWxoc3FhmIfQ';
    const MaximeG = 'LzIGCODKziH7E4QvOJ0cFHL2MJ3B00pukIShOH8qYemKlXs';
    
    
    const key = 'RGAPI-f5c263a7-1137-4c1c-b9a7-4cdef0738fcd';
    
    // Doesn't work when you put multiple players :(
    const PLAYERID = [Thomas, John];
    PLAYERID.forEach(id => {
        console.log('Here the different id');
        console.log(id);
        console.log('continuation');
        const responses = []
        https.get(EU + id + URLKEY + key, (rr) => {
            rr.on('data', (chunk) => {
                responses.push(JSON.parse(chunk));
            });
            rr.on('end', () => {
                let output = ['']
                responses.forEach((item) => {
                    for (let i = 0; i < item.length; i++) {
                        output += "Name : " + item[i].summonerName + " League : " + item[i].queueType + "\n\tRank : " + item[i].tier + " " + item[i].rank + " " + item[i].leaguePoints + " LP Nombre de victoires : " + item[i].wins + " Nombre de defaites : " + item[i].losses + " \n";
                        console.log(output);
                    }
                })
    
    
            });
        });
    });
    
    var server = http.createServer(function (req, res) {
    
    }).listen(8080);