/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.js :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: edbernar { if (userMeInfo.id > 0) { sendRequest("get_user_info", {username: user}) clearInterval(interval); } }, 100); } else sendRequest("get_user_info", {id: user}); crossProfil.addEventListener('click', () => { if (typeof(user) == 'string') pageRenderer.changePage('homePage'); else pageRenderer.changePage('lobbyPage'); }); waitForUserInfo().then((userInfo) => { console.log(userInfo); if (userInfo == null) { pageRenderer.changePage('homePage'); return ; } if (typeof(user) != 'string') history.replaceState({}, document.title, window.location.pathname + '/' + userInfo.username); username.innerHTML = userInfo.username + ''; if (!userInfo.online) document.getElementsByClassName('online-status')[0].style.backgroundColor = '#E74040'; pfp.style.backgroundImage = `url("${userInfo.pfp}")` pfp.style.backgroundSize = "cover"; pfp.style.backgroundRepeat = "no-repeat"; banner.style.backgroundImage = `url("${userInfo.banner}")` banner.style.backgroundSize = "cover"; banner.style.backgroundRepeat = "no-repeat"; externButtons(userInfo); createGraph(ctx, {win: userInfo.nbWin, lose: userInfo.nbLoss}); showHistory(userInfo); if (userInfo.id == userMeInfo.id) { inviteButton.remove(); pfp.innerHTML = `
` banner.innerHTML = `` editPenPfpBg = document.getElementById('editPenPfpBg'); inputPfp = document.getElementById('inputPfp'); editPenPfpBg.addEventListener('click', () => { inputPfp.setAttribute('accept', '.png, .jpeg, .jpg, .gif'); inputPfp.click(); }); inputPfp.setAttribute('accept', '.png, .jpeg, .jpg, .gif'); inputPfp.addEventListener('change', (event) => inputChange(true, event)); editPenBanner = document.getElementsByClassName('editPen')[0]; inputBanner = document.getElementById('inputBanner'); editPenBanner.addEventListener('click', () => { inputBanner.setAttribute('accept', '.png, .jpeg, .jpg, .gif'); inputBanner.click(); }); inputBanner.setAttribute('accept', '.png, .jpeg, .jpg, .gif'); inputBanner.addEventListener('change', (event) => inputChange(false, event)); } if (userInfo.id != userMeInfo.id) { convButton.addEventListener('click', () => { showChatMenu(); showPrivateChat({id: userInfo.id, name: userInfo.username}); }); inviteButton.addEventListener('click', () => { if (!userInfo.online) CN.new("Invitation", `Can't invite ${userInfo.username} (offline)`) else { pageRenderer.changePage("waitingGamePage", false, {username: userInfo.username, id: userInfo.id, isTournament: false}); } }); } else convButton.remove(); }); } static dispose() { LiveChat.dispose(); } } function externButtons(userInfo) { const githubButton = document.getElementById('github'); const discordButton = document.getElementById('discord'); if (userInfo.github) githubButton.setAttribute('href', userInfo.github); else githubButton.remove(); if (userInfo.discord) discordButton.setAttribute('name', userInfo.discord); else discordButton.remove(); } function inputChange(isPfp, event) { const reader = new FileReader(); const validTypes = ['image/png', 'image/jpeg', 'image/webp', 'image/gif']; const file = event.target.files[0]; function arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); } if (validTypes.includes(file.type)) { reader.onload = (e) => { const arrayBuffer = e.target.result; if (isPfp) sendRequest('change_pfp', {img: arrayBufferToBase64(arrayBuffer), type: file.type}) else sendRequest('change_banner', {img: arrayBufferToBase64(arrayBuffer), type: file.type}) waitForchangePfp().then((content) => { if (isPfp) document.getElementsByClassName('profile-image')[0].style.backgroundImage = `url("${content.pfp}")` else document.getElementsByClassName('background-card')[0].style.backgroundImage = `url("${content.banner}")` }); } reader.readAsArrayBuffer(file); } } function createGraph(ctx, data) { new Chart(ctx, { type: 'pie', data: { labels: ['Win', 'Lose'], datasets: [{ label: 'Couleurs', data: [data.win, data.lose], backgroundColor: ['#11ad11', '#E74040'], hoverOffset: 4 }] }, options: { responsive: true, plugins: { legend: { position: 'bottom', }, tooltip: { enabled: true } } } }); } function showHistory(userInfo) { const divHistory = document.getElementById('scroll-match'); const history = userInfo.history; history.forEach(element => { const div = document.createElement('div'); div.setAttribute('class', 'history-card'); if (element.forfeit) div.style.backgroundColor = '#c45f0c'; else if (element.won) div.style.backgroundColor = '#11ad11'; div.innerHTML = `

${userInfo.username}

${element.p1.score} - ${element.p2.score}

${element.p2.username}

` divHistory.appendChild(div); }); } export { ProfilPage };