From 74e99c500fe29f0c1501dae643697fefd4522883 Mon Sep 17 00:00:00 2001 From: Galexion Date: Sat, 18 Feb 2023 08:33:40 -0500 Subject: [PATCH] Increased ttr for UserPlayLog --- public/javascript/user.js | 154 ++++++++++++++++++++++---------------- views/user.ejs | 2 +- 2 files changed, 91 insertions(+), 65 deletions(-) diff --git a/public/javascript/user.js b/public/javascript/user.js index 36ecba2..13a7343 100644 --- a/public/javascript/user.js +++ b/public/javascript/user.js @@ -73,15 +73,14 @@ function padNumber(num, size) { return s; } -async function getSongInformation(id) { - const musicData = await musicMetadata(); +async function getSongInformation(musicData,id) { const songMatch = musicData[id]; if (!songMatch) { - return null; + return null; } return songMatch; - } - +} + // Ban Check lmao @@ -144,53 +143,80 @@ request.send(); // User Play Log -async function userPlayLogFormatter(div) { +let scoreIncrement = 0; +let errorIncrement = 0; +let displayedScoreCount = 20; + +async function userPlayLogFormatter(div, loadMoreButton) { const UserPlayLogData = await UserPlayLog(aime_card_id); - const musicIds = UserPlayLogData.map(score => score.music_id); - const songs = await Promise.all(musicIds.map(getSongInformation)); - - let myDiv = document.getElementById("scoreWrapper"); - if (myDiv === null) { - const scoresWrapperDiv = document.createElement('div'); + let scoresWrapperDiv = document.getElementById("scoreWrapper"); + + // If the scores wrapper div doesn't exist, create it. + if (scoresWrapperDiv === null) { + scoresWrapperDiv = document.createElement('div'); scoresWrapperDiv.id = "scoreWrapper"; - - for (let i = UserPlayLogData.length - 1; i >= 0; i--) { - const newDiv = document.createElement('div'); - let score = JSON.parse(JSON.stringify(UserPlayLogData[i])); - let song = JSON.parse(JSON.stringify(songs[i])); - - if (!song || !song.name) { - console.log(song) - console.log(score.music_id) - console.log("Song or title is undefined"); - continue; - } - - newDiv.innerHTML = ` -

${song.name.str || ""} ||

- `; - - let jacket = document.createElement('img'); - jacket.addEventListener("error", function () { - this.src = "assets/icon/UI_Icon_000000.png"; - }); - jacket.classList.add('score-jacket'); - jacket.src = "assets/jacket/UI_Jacket_" + padNumber(Number(score.music_id), 6) + '.png'; - newDiv.append(jacket) - - newDiv.setAttribute("id", "score-grid"); - newDiv.setAttribute("class", "item"); - - scoresWrapperDiv.appendChild(newDiv); - } - div.appendChild(scoresWrapperDiv); } + + // Get the user's play log data and music IDs. + const musicIds = UserPlayLogData.map(score => score.music_id); + const musicData = await musicMetadata(); + + let errorIncrement = 0; + let scoreIncrement = 0; + const startIndex = scoresWrapperDiv.children.length; + const maxScores = startIndex + 20; + for (let i = startIndex; i < UserPlayLogData.length && scoreIncrement < maxScores; i++) { + const newDiv = document.createElement('div'); // Create a Div + let score = JSON.parse(JSON.stringify(UserPlayLogData[i])); // get score data + let song = await getSongInformation(musicData, score.music_id) // get song data + + if (!song || !song.name) { // if a song can't be found, skip it and increment the error counter by 1. + console.log(song) + console.log(score.music_id) + console.log("Song or title is undefined"); + errorIncrement++ + continue; + } + + newDiv.innerHTML = ` +

${song.name.str || ""} ||

+ `; + + let jacket = document.createElement('img'); + jacket.addEventListener("error", function () { + this.src = "assets/icon/UI_Icon_000000.png"; + }); + jacket.classList.add('score-jacket'); + jacket.src = "assets/jacket/UI_Jacket_" + padNumber(Number(score.music_id), 6) + '.png'; + newDiv.append(jacket) + + newDiv.setAttribute("id", "score-grid"); + newDiv.setAttribute("class", "item"); + scoreIncrement++ + + scoresWrapperDiv.appendChild(newDiv); + + } + + // Edit the Score Info Header with the relevant information. + const header = document.querySelector('#score-info-header'); + header.textContent = `Showing ${startIndex + scoreIncrement - errorIncrement} out of ${UserPlayLogData.length} scores.`; + if (errorIncrement > 0) { + header.insertAdjacentHTML('afterend', `

${errorIncrement} Songs Failed to load properly, most likely due to missing information.

`); + } + + // If there are no more scores to load, hide the "Load More" button. + if (startIndex + scoreIncrement >= UserPlayLogData.length) { + loadMoreButton.style.display = "none"; + } } + + //===================================================================== // Now Exiting: User Data ////////////// Now Entering: Nested Div Hell //===================================================================== @@ -238,27 +264,27 @@ function UserPlayLog(aime_card_id) { async function musicMetadata() { const url = "/assets/metadata/musicMetadata.json"; - + try { - const response = await fetch(url, { - method: "GET", - headers: { - "Content-Type": "application/json", - }, - }); - - const data = await response.json(); - - const mapping = {}; - data.forEach((song) => { - mapping[song.name.id] = song; - }); - - return mapping; + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + + const data = await response.json(); + + const mapping = {}; + data.forEach((song) => { + mapping[song.name.id] = song; + }); + + return mapping; } catch (error) { - console.error(error); + console.error(error); } - } - - - +} + + + diff --git a/views/user.ejs b/views/user.ejs index e66e19f..0e1124e 100644 --- a/views/user.ejs +++ b/views/user.ejs @@ -37,7 +37,7 @@