Browse Source

Split SHUFFLE function to its intended use

Kristian Vos 4 years ago
parent
commit
e7cc54f815
2 changed files with 35 additions and 2 deletions
  1. 1 1
      backend/logic/stations.js
  2. 34 1
      backend/logic/utils.js

+ 1 - 1
backend/logic/stations.js

@@ -480,7 +480,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					(playlist, next) => {
-						UtilsModule.runJob("SHUFFLE", { array: playlist.songs }, this)
+						UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
 							.then(response => {
 								next(null, response.array);
 							})

+ 34 - 1
backend/logic/utils.js

@@ -228,13 +228,46 @@ class _UtilsModule extends CoreClass {
 	}
 
 	/**
-	 * Shuffles an array of songs by their position property
+	 * Shuffles an array
 	 *
 	 * @param {object} payload - object that contains the payload
 	 * @param {object} payload.array - an array of songs that should be shuffled
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	SHUFFLE(payload) {
+		// array
+		return new Promise(resolve => {
+			const { array } = payload;
+
+			// sort the positions array
+			let currentIndex = array.length;
+			let temporaryValue;
+			let randomIndex;
+
+			// While there remain elements to shuffle...
+			while (currentIndex !== 0) {
+				// Pick a remaining element...
+				randomIndex = Math.floor(Math.random() * currentIndex);
+				currentIndex -= 1;
+
+				// And swap it with the current element.
+				temporaryValue = array[currentIndex];
+				array[currentIndex] = array[randomIndex];
+				array[randomIndex] = temporaryValue;
+			}
+
+			resolve({ array });
+		});
+	}
+
+	/**
+	 * Shuffles an array of songs by their position property
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {object} payload.array - an array of songs that should be shuffled
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	SHUFFLE_SONG_POSITIONS(payload) {
 		// array
 		return new Promise(resolve => {
 			const { array } = payload;