LeagueStats/public/summoner.js

332 lines
11 KiB
JavaScript
Raw Normal View History

2019-03-24 19:25:45 +00:00
const req = new XMLHttpRequest();
const url = '/api';
const localInfos = {};
let nameChosen = '';
let itemsJSON;
2019-03-18 19:55:33 +00:00
// Get username from param
2019-03-24 19:25:45 +00:00
const fullUrl = new URL(window.location.href);
const searchParams = new URLSearchParams(fullUrl.search);
2019-03-18 19:55:33 +00:00
nameChosen = searchParams.get('username');
/**
* Get the JSON file of all the items
*/
async function getItemsJSON() {
return fetch('public/item.json').then(resp => resp.json()).then(json => { return json });
}
/**
* Main function of the program
*/
async function main() {
req.addEventListener('load', onLoad);
req.addEventListener('error', onError);
itemsJSON = await getItemsJSON();
//console.log(itemsJSON);
/* Check on the load of the page if the content is already cached */
if (localStorage[nameChosen]) {
console.log('cached on page load');
document.querySelector('#refresh').style.display = 'block';
displayContent(localStorage[nameChosen]);
} else {
console.log('not cached on page load');
req.open('POST', url, true);
document.querySelector('.loader--overlay').style.display = 'block';
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send('playerName=' + nameChosen);
}
/* Debug button to reset localstorage */
document.querySelector('.debug').addEventListener('click', () => {
console.log('CLEAR LOCALSTORAGE');
localStorage.clear();
});
/* Refresh button */
2019-03-24 19:25:45 +00:00
const refreshBtn = document.querySelector('#refresh');
2019-03-18 19:55:33 +00:00
refreshBtn.addEventListener('click', (e) => {
e.preventDefault();
document.querySelector('.loader--overlay').style.display = 'block';
req.open('POST', url, true);
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send('playerName=' + nameChosen);
})
}
// Call main function
main();
function onLoad() {
document.querySelector('.loader--overlay').style.display = 'none';
2019-03-24 19:25:45 +00:00
const response = this.responseText;
2019-03-18 19:55:33 +00:00
// If username isn't found
if (!response) {
document.querySelector('#refresh').style.display = 'none';
displayPlayerNotFound('Joueur introuvable');
return;
}
document.querySelector('#refresh').style.display = 'block';
2019-03-24 19:25:45 +00:00
const parsedEntireResponse = JSON.parse(response);
2019-03-18 19:55:33 +00:00
parsedEntireResponse[1] = JSON.parse(parsedEntireResponse[1]);
createObject(parsedEntireResponse);
}
function onError() {
console.log('error receiving async AJAX call');
}
/**
* Create an object in LocalStorage with the data we need to display
* @param JSONData : big JSON with all the data of the request to the server
*/
function createObject(JSONData) {
console.log('--- ALL INFOS ---')
console.log(JSONData);
2019-03-24 19:25:45 +00:00
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;
2019-03-18 19:55:33 +00:00
2019-03-24 19:25:45 +00:00
const matchesInfos = [];
2019-03-18 19:55:33 +00:00
// Loop on all matches
for (let i = 0; i < matches.length; i++) {
2019-03-24 19:25:45 +00:00
const currentMatch = matches[i];
let participantId;
2019-03-18 19:55:33 +00:00
for (let i = 0; i < currentMatch.participantIdentities.length; i++) {
if (currentMatch.participantIdentities[i].player.accountId === userStats.accountId)
participantId = currentMatch.participantIdentities[i].participantId;
}
2019-03-24 19:25:45 +00:00
const teamId = currentMatch.participants[participantId - 1].teamId;
let win = false;
2019-03-18 19:55:33 +00:00
for (let i = 0; i < currentMatch.teams.length; i++) {
if (currentMatch.teams[i].teamId === teamId) {
if (currentMatch.teams[i].win === 'Win')
win = true;
}
}
2019-03-24 19:25:45 +00:00
const map = maps[currentMatch.mapId];
const mode = gameModes[currentMatch.queueId];
2019-03-18 19:55:33 +00:00
if (!mode)
mode = 'Undefinded gamemode';
2019-03-24 19:25:45 +00:00
const champion = currentMatch.participants[participantId - 1].championId;
const role = currentMatch.participants[participantId - 1].timeline.lane;
const timeAgo = timeDifference(currentMatch.gameCreation);
const time = secToTime(currentMatch.gameDuration);
const kills = currentMatch.participants[participantId - 1].stats.kills;
const deaths = currentMatch.participants[participantId - 1].stats.deaths;
const assists = currentMatch.participants[participantId - 1].stats.assists;
const level = currentMatch.participants[participantId - 1].stats.champLevel;
const items = [];
2019-03-18 19:55:33 +00:00
for (let i = 0; i < 6; i++) {
2019-03-24 19:25:45 +00:00
const currentItem = 'item' + i;
2019-03-18 19:55:33 +00:00
items.push(currentMatch.participants[participantId - 1].stats[currentItem]);
}
2019-03-24 19:25:45 +00:00
const gold = (currentMatch.participants[participantId - 1].stats.goldEarned / 1000).toFixed(1) + 'k';
const minions = currentMatch.participants[participantId - 1].stats.totalMinionsKilled + currentMatch.participants[participantId - 1].stats.neutralMinionsKilled;
2019-03-18 19:55:33 +00:00
matchesInfos.push({
result: win,
map: map,
gamemode: mode,
champ: champion,
role: role,
date: timeAgo,
time: time,
kills: kills,
deaths: deaths,
assists: assists,
level: level,
items: items,
gold: gold,
minions: minions
});
}
console.log(matchesInfos);
2019-03-24 18:47:13 +00:00
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;
2019-03-18 19:55:33 +00:00
nameChosen = userStats.name;
console.log('====== Saved infos ======');
2019-03-24 18:47:13 +00:00
console.log(localInfos);
2019-03-18 19:55:33 +00:00
2019-03-24 18:47:13 +00:00
localStorage[nameChosen] = JSON.stringify(localInfos);
2019-03-18 19:55:33 +00:00
displayContent(localStorage[nameChosen]);
}
/**
* Display on the page all the dynamic content
* @param stringData : Stringify object with all datas
*/
function displayContent(stringData) {
2019-03-24 19:25:45 +00:00
const data = JSON.parse(stringData);
2019-03-18 19:55:33 +00:00
document.querySelector('.player__pp').style.background = 'url(https://cdn.valentinkaelin.ch/riot/profileicon/' + data.profileIconId + '.png) center/cover';
document.querySelector('.player__name').innerHTML = data.name;
document.querySelector('.player__level').innerHTML = data.level;
document.querySelector('.player__rank').innerHTML = data.rank;
document.querySelector('.player__rank-img').style.background = 'url(' + data.rankImgLink + ') center/cover';
document.querySelector('.player__ratio').innerHTML = data.rankedWins ? data.rankedWins + ' wins / ' + data.rankedLosses + ' losses' : "Joueur non classé";
2019-03-24 19:25:45 +00:00
const playerContainer = document.querySelector('.player');
const oldList = document.querySelector('.list-matches');
2019-03-18 19:55:33 +00:00
if (oldList)
oldList.parentNode.removeChild(oldList);
2019-03-24 19:25:45 +00:00
const matchesList = document.createElement('ul');
2019-03-18 19:55:33 +00:00
matchesList.className = 'list-matches';
playerContainer.appendChild(matchesList);
/* Loop on all matches */
data.matches.forEach(e => {
2019-03-24 19:25:45 +00:00
const li = createHTMLOneMatch(e);
2019-03-18 19:55:33 +00:00
matchesList.appendChild(li);
});
}
/**
* Return the HTML of one match
* @param e : Infos in JSON of the match
*/
function createHTMLOneMatch(e) {
2019-03-24 19:25:45 +00:00
const li = document.createElement('li');
2019-03-18 19:55:33 +00:00
li.className = e.result ? 'match win' : 'match lose';
2019-03-24 19:25:45 +00:00
const container = document.createElement('div');
2019-03-18 19:55:33 +00:00
container.className = 'content-container';
/* First col (champion/summoners) */
2019-03-24 19:25:45 +00:00
const first = document.createElement('div');
2019-03-18 19:55:33 +00:00
first.className = 'first';
2019-03-24 19:25:45 +00:00
const imgChamp = document.createElement('img');
const championName = championsId[e.champ];
2019-03-18 19:55:33 +00:00
imgChamp.setAttribute('src', 'https://cdn.valentinkaelin.ch/riot/champions/' + championName + '.png');
imgChamp.className = 'champion-icon';
2019-03-24 19:25:45 +00:00
const level = document.createElement('span');
2019-03-18 19:55:33 +00:00
level.className = 'level';
level.innerText = e.level;
2019-03-24 19:25:45 +00:00
const summonerSpells = document.createElement('div');
2019-03-18 19:55:33 +00:00
summonerSpells.className = 'summonerSpells';
2019-03-24 19:25:45 +00:00
const firstSpell = document.createElement('img');
2019-03-18 19:55:33 +00:00
firstSpell.setAttribute('src', 'https://cdn.valentinkaelin.ch/riot/spells/SummonerFlash.png');
firstSpell.className = 'spell-icon';
summonerSpells.appendChild(firstSpell);
2019-03-24 19:25:45 +00:00
const secondSpell = document.createElement('img');
2019-03-18 19:55:33 +00:00
secondSpell.setAttribute('src', 'https://cdn.valentinkaelin.ch/riot/spells/SummonerDot.png');
secondSpell.className = 'spell-icon';
summonerSpells.appendChild(secondSpell);
2019-03-24 19:25:45 +00:00
const name = document.createElement('span');
2019-03-18 19:55:33 +00:00
name.className = 'champion-name';
name.innerText = championName;
first.appendChild(imgChamp);
first.appendChild(level);
first.appendChild(summonerSpells);
first.appendChild(name);
/* Second col (gamemode) */
2019-03-24 19:25:45 +00:00
const second = document.createElement('div');
2019-03-18 19:55:33 +00:00
second.className = 'second';
2019-03-24 19:25:45 +00:00
const map = document.createElement('div');
2019-03-18 19:55:33 +00:00
map.className = 'map';
map.innerText = e.map;
2019-03-24 19:25:45 +00:00
const gamemode = document.createElement('div');
2019-03-18 19:55:33 +00:00
gamemode.className = 'gamemode';
gamemode.innerText = e.gamemode;
second.appendChild(map);
second.appendChild(gamemode);
/* Third col (items) */
2019-03-24 19:25:45 +00:00
const third = document.createElement('div');
2019-03-18 19:55:33 +00:00
third.className = 'third';
e.items.forEach(e => {
2019-03-24 19:25:45 +00:00
const img = document.createElement('div');
2019-03-18 19:55:33 +00:00
img.className = 'item ' + e;
if(e !== 0) {
2019-03-24 19:25:45 +00:00
const itemImage = itemsJSON.data[e].image;
img.style.background = `url('https://cdn.valentinkaelin.ch/riot/${itemImage.sprite}') -${itemImage.x}px -${itemImage.y}px`;
2019-03-18 19:55:33 +00:00
} else {
img.style.background = "url('https://cdn.valentinkaelin.ch/riot/items/0.png') 0% 0% / cover";
}
third.appendChild(img);
});
/* Fourth col (stats) */
2019-03-24 19:25:45 +00:00
const fourth = document.createElement('div');
2019-03-18 19:55:33 +00:00
fourth.className = 'fourth';
2019-03-24 19:25:45 +00:00
const score = document.createElement('div');
2019-03-18 19:55:33 +00:00
score.className = 'score';
score.innerText = e.kills + '/' + e.deaths + '/' + e.assists;
2019-03-24 19:25:45 +00:00
const goldFarm = document.createElement('div');
2019-03-18 19:55:33 +00:00
goldFarm.className = 'gold-farm';
2019-03-24 19:25:45 +00:00
const gold = document.createElement('div');
2019-03-18 19:55:33 +00:00
gold.className = 'gold';
gold.innerText = e.gold;
2019-03-24 19:25:45 +00:00
const farm = document.createElement('div');
2019-03-18 19:55:33 +00:00
farm.className = 'farm';
farm.innerText = e.minions;
goldFarm.appendChild(gold);
goldFarm.appendChild(farm);
2019-03-24 19:25:45 +00:00
const durationDate = document.createElement('div');
2019-03-18 19:55:33 +00:00
durationDate.className = 'duration-date';
2019-03-24 19:25:45 +00:00
const duration = document.createElement('div');
2019-03-18 19:55:33 +00:00
duration.className = 'duration';
duration.innerText = e.time;
2019-03-24 19:25:45 +00:00
const date = document.createElement('div');
2019-03-18 19:55:33 +00:00
date.className = 'date';
date.innerText = e.date;
durationDate.appendChild(duration);
durationDate.appendChild(date);
fourth.appendChild(score);
fourth.appendChild(goldFarm);
fourth.appendChild(durationDate);
/* End */
container.appendChild(first);
container.appendChild(second);
container.appendChild(third);
container.appendChild(fourth);
li.appendChild(container);
return li;
}
/**
* Display the information that the player has not be found
* @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 = '';
2019-03-24 19:25:45 +00:00
}