mirror of
https://github.com/vkaelin/LeagueStats.git
synced 2026-03-26 05:17:27 +00:00
Refactor some code 🤟
This commit is contained in:
parent
8e05d6fe9e
commit
6289229573
2 changed files with 54 additions and 79 deletions
|
|
@ -71,9 +71,7 @@ function onLoad() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
document.querySelector('#refresh').style.display = 'block';
|
document.querySelector('#refresh').style.display = 'block';
|
||||||
const parsedEntireResponse = JSON.parse(response);
|
createObject(JSON.parse(response));
|
||||||
parsedEntireResponse[1] = JSON.parse(parsedEntireResponse[1]);
|
|
||||||
createObject(parsedEntireResponse);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onError() {
|
function onError() {
|
||||||
|
|
@ -90,8 +88,8 @@ function createObject(JSONData) {
|
||||||
|
|
||||||
const userStats = JSONData[0];
|
const userStats = JSONData[0];
|
||||||
const rankedStats = JSONData[1];
|
const rankedStats = JSONData[1];
|
||||||
const soloQStats = rankedStats.length !== 0 ? (rankedStats[0].queueType == 'RANKED_SOLO_5x5' ? rankedStats[0] : rankedStats[1]) : false;
|
const soloQStats = rankedStats !== null ? (rankedStats.queueType == 'RANKED_SOLO_5x5' ? rankedStats : JSONData[2]) : false;
|
||||||
const matches = JSONData[2].matches;
|
const matches = JSONData[3].matches;
|
||||||
|
|
||||||
const matchesInfos = [];
|
const matchesInfos = [];
|
||||||
// Loop on all matches
|
// Loop on all matches
|
||||||
|
|
@ -115,7 +113,7 @@ function createObject(JSONData) {
|
||||||
const map = maps[currentMatch.mapId];
|
const map = maps[currentMatch.mapId];
|
||||||
let mode = gameModes[currentMatch.queueId];
|
let mode = gameModes[currentMatch.queueId];
|
||||||
if (!mode)
|
if (!mode)
|
||||||
mode = 'Undefinded gamemode';
|
mode = 'Undefined gamemode';
|
||||||
const champion = currentMatch.participants[participantId - 1].championId;
|
const champion = currentMatch.participants[participantId - 1].championId;
|
||||||
const role = currentMatch.participants[participantId - 1].timeline.lane;
|
const role = currentMatch.participants[participantId - 1].timeline.lane;
|
||||||
const timeAgo = timeDifference(currentMatch.gameCreation);
|
const timeAgo = timeDifference(currentMatch.gameCreation);
|
||||||
|
|
@ -160,8 +158,8 @@ function createObject(JSONData) {
|
||||||
localInfos.level = userStats.summonerLevel;
|
localInfos.level = userStats.summonerLevel;
|
||||||
localInfos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé';
|
localInfos.rank = soloQStats ? soloQStats.tier + ' ' + soloQStats.rank : 'Joueur non classé';
|
||||||
localInfos.rankImgLink = getRankImg(soloQStats);
|
localInfos.rankImgLink = getRankImg(soloQStats);
|
||||||
localInfos.rankedWins = soloQStats.wins;
|
localInfos.rankedWins = soloQStats ? soloQStats.wins : undefined;
|
||||||
localInfos.rankedLosses = soloQStats.losses;
|
localInfos.rankedLosses = soloQStats ? soloQStats.losses : undefined;
|
||||||
|
|
||||||
nameChosen = userStats.name;
|
nameChosen = userStats.name;
|
||||||
|
|
||||||
|
|
@ -321,11 +319,5 @@ function createHTMLOneMatch(e) {
|
||||||
* @param text : String to display - error message
|
* @param text : String to display - error message
|
||||||
*/
|
*/
|
||||||
function displayPlayerNotFound(text) {
|
function displayPlayerNotFound(text) {
|
||||||
document.querySelector('.player__pp').style.background = 'url(https://cdn.valentinkaelin.ch/riot/profileicon/29.png) center/cover';
|
document.querySelector('.player').innerHTML = `<h3>${text}</h3>`
|
||||||
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 = '';
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
111
server.js
111
server.js
|
|
@ -13,6 +13,7 @@ let summonerID;
|
||||||
let accountID;
|
let accountID;
|
||||||
let username;
|
let username;
|
||||||
let JSONMatches;
|
let JSONMatches;
|
||||||
|
let finalJSON = [];
|
||||||
|
|
||||||
/* Set Port */
|
/* Set Port */
|
||||||
app.set('port', (process.env.PORT || 5000))
|
app.set('port', (process.env.PORT || 5000))
|
||||||
|
|
@ -37,103 +38,85 @@ app.get('/summoners', function (request, response) {
|
||||||
app.use('/public', express.static(__dirname + '/public'));
|
app.use('/public', express.static(__dirname + '/public'));
|
||||||
|
|
||||||
/* Launch app */
|
/* 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
|
// Send data of a summoner
|
||||||
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
|
|
||||||
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')
|
console.time('all')
|
||||||
username = req.body.playerName;
|
username = req.body.playerName;
|
||||||
|
finalJSON = [];
|
||||||
getAccountInfos(function (account) {
|
getAccountInfos(res);
|
||||||
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')
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Get account infos of an username
|
// 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) {
|
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) {
|
if (!error && response.statusCode == 200) {
|
||||||
let JSONBody = JSON.parse(body);
|
let JSONBody = JSON.parse(body);
|
||||||
//console.log(JSONBody);
|
|
||||||
summonerID = JSONBody.id;
|
summonerID = JSONBody.id;
|
||||||
accountID = JSONBody.accountId;
|
accountID = JSONBody.accountId;
|
||||||
callback(JSONBody);
|
finalJSON.push(JSONBody)
|
||||||
|
getRanked(res);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
console.log(response.statusCode);
|
console.log(response.statusCode);
|
||||||
console.log('username not found');
|
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
|
// Get 10 matches of an accountID
|
||||||
function getMatches(callback) {
|
const getMatches = function (res) {
|
||||||
console.time('getMatches');
|
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) {
|
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) {
|
if (!error && response.statusCode == 200) {
|
||||||
JSONMatches = JSON.parse(body);
|
JSONMatches = JSON.parse(body);
|
||||||
|
const matchsId = JSONMatches.matches.map(x => x.gameId)
|
||||||
|
|
||||||
let matchsId = [];
|
Promise.map(matchsId, function (id) {
|
||||||
for (let i = 0; i < JSONMatches.matches.length; i++) {
|
return getMatch('match/v4/matches/' + id);
|
||||||
matchsId[i] = JSONMatches.matches[i].gameId;
|
|
||||||
}
|
|
||||||
|
|
||||||
Promise.map(matchsId, function (item) { // old: .mapSeries
|
|
||||||
return apicall(item);
|
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
console.timeEnd('getMatches');
|
console.timeEnd('getMatches');
|
||||||
console.log('Finished');
|
console.log('Finished - Data sent to front');
|
||||||
callback(JSONMatches);
|
finalJSON.push(JSONMatches)
|
||||||
|
res.send(finalJSON);
|
||||||
|
console.timeEnd('all')
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log('Error');
|
console.log('Error Promise');
|
||||||
console.log(err.statusCode);
|
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
|
// Add match to global JSON
|
||||||
function addMatchToJSON(obj) {
|
const addMatchToJSON = function (obj) {
|
||||||
//console.log(obj.gameId);
|
JSONMatches.matches = JSONMatches.matches.map((match) => match.gameId === obj.gameId ? obj : match);
|
||||||
for (let i = 0; i < JSONMatches.matches.length; i++) {
|
}
|
||||||
if (JSONMatches.matches[i].gameId == obj.gameId) {
|
|
||||||
//console.log('yes');
|
|
||||||
JSONMatches.matches[i] = obj;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue