diff --git a/server/scripts/modules/media.mjs b/server/scripts/modules/media.mjs index 2b09926..2e78fe0 100644 --- a/server/scripts/modules/media.mjs +++ b/server/scripts/modules/media.mjs @@ -29,6 +29,8 @@ const getMedia = async () => { const enableMediaPlayer = () => { // see if files are available if (playlist?.availableFiles?.length > 0) { + // randomize the list + randomizePlaylist(); // enable the icon const icon = document.getElementById('ToggleMedia'); icon.classList.add('available'); @@ -78,6 +80,20 @@ const stateChanged = () => { } }; +const randomizePlaylist = () => { + let availableFiles = [...playlist.availableFiles]; + const randomPlaylist = []; + while (availableFiles.length > 0) { + // get a randon item from the available files + const i = Math.floor(Math.random() * availableFiles.length); + // add it to the final list + randomPlaylist.push(availableFiles[i]); + // remove the file from the available files + availableFiles = availableFiles.filter((file, index) => index !== i); + } + playlist.availableFiles = randomPlaylist; +}; + export { // eslint-disable-next-line import/prefer-default-export toggleMedia,