<commit_before>var App = require('./App');
// var Spotify = require('./Spotify');
var clone = require('clone');
var uuid = require('node-uuid');
var shuffle = require('shuffle-array');
var fs = require('fs');

var Queue = {
    queue: [],
    oldQueue: [],
    history: [],
    saveInterval: -1,
    shuffled: true,
    getCurrent: function () {
        return Queue.queue.length > 0 ? Queue.queue[0] : null;
    },
    skip: function (direction) {
        if (direction > 0) {
            //Forward
            if (Queue.queue.length > 0) {
                Queue.history.push(Queue.queue.shift())
            }
        } else if (direction < 0) {
            //Back
            if (Queue.history.length > 0) {
                Queue.queue.unshift(Queue.history.pop())
            }
        }
        Queue.update();
    },
    addTrack: function (track, source, noUpdate) {
        track.uuid = uuid.v4();
        track.source = source;

        Queue.queue.push(track);

        if (Queue.isShuffled()) {
            Queue.shuffle();
        }
        noUpdate || Queue.update();

    },
    addTracks: function (tracks, source) {
        console.log(source);
        tracks.forEach(function (track) {
            Queue.addTrack(track, source, true);
        });
        Queue.update();
    },
    clear: function (noUpdate) {
        Queue.queue = [];

        noUpdate || Queue.update();
    },
    update: function () {
        var queue = Queue.serialise();
        // var diff = [];
        // if (diff.length == 0)return;
        //
        // if (queue.length == 0 || diff.length / queue.length > 0.5) {
        //     More than 50% of the elements have changed. Just send the whole queue
        if (queue != Queue.oldQueue) App.getIo().emit('queue', queue);
        // } else {
        //     App.getIo().emit('queueDiff', diff)
        // }

        Queue.oldQueue = queue;

        if (Queue.getCurrent() !== require('./Spotify').currentTrack) {
            require('./Spotify').playTrack(Queue.getCurrent()).then(() => App.updatePlayState());
        }
    },
    serialise: function (big) {
        if(big){
            return Queue.queue.map(Queue.serialiseTrack);
        } else {
            return Queue.queue.map(function (track) {
                return [track.link.split(":")[2], track.uuid, track.source]
            });
        }
    },
    serialiseTrack: function (track) {
        if (track === null) return null;
        return {
            id: track.link.split(":")[2],
            uuid: track.uuid,
            source: track.source
        };
    },
    isShuffled: function () {
        return Queue.shuffled;
    },
    setShuffled: function (shuffled, noUpdate) {
        if (shuffled == Queue.shuffled) return;<commit_msg>Don't attempt to play tracks that aren't available<commit_after>var App = require('./App');
// var Spotify = require('./Spotify');
var clone = require('clone');
var uuid = require('node-uuid');
var shuffle = require('shuffle-array');
var fs = require('fs');

var Queue = {
    queue: [],
    oldQueue: [],
    history: [],
    saveInterval: -1,
    shuffled: true,
    getCurrent: function () {
        return Queue.queue.length > 0 ? Queue.queue[0] : null;
    },
    skip: function (direction) {
        if (direction > 0) {
            //Forward
            if (Queue.queue.length > 0) {
                Queue.history.push(Queue.queue.shift())
            }
        } else if (direction < 0) {
            //Back
            if (Queue.history.length > 0) {
                Queue.queue.unshift(Queue.history.pop())
            } else {
                // No history, so just seek to the start of the track
                require("./Spotify").seek(0);
            }
        }
        Queue.update();
    },
    addTrack: function (track, source, noUpdate) {
        if (track.availability !== 1) {
            console.log("Track unavailable, %s - %s", track.artists[0].name, track.name)
            //TODO send notification to connected clients
            return;
        }
        console.log("Added track %s - %s", track.artists[0].name, track.name);
        track.uuid = uuid.v4();
        track.source = source;

        Queue.queue.push(track);

        if (Queue.isShuffled()) {
            Queue.shuffle();
        }
        noUpdate || Queue.update();

    },
    addTracks: function (tracks, source) {
        tracks.forEach(function (track) {
            Queue.addTrack(track, source, true);
        });
        Queue.update();
    },
    clear: function (noUpdate) {
        Queue.queue = [];

        noUpdate || Queue.update();
    },
    update: function () {
        var queue = Queue.serialise();
        // var diff = [];
        // if (diff.length == 0)return;
        //
        // if (queue.length == 0 || diff.length / queue.length > 0.5) {
        //     More than 50% of the elements have changed. Just send the whole queue
        if (queue != Queue.oldQueue) App.getIo().emit('queue', queue);
        // } else {
        //     App.getIo().emit('queueDiff', diff)
        // }

        Queue.oldQueue = queue;

        if (Queue.getCurrent() !== require('./Spotify').currentTrack) {
            require('./Spotify').playTrack(Queue.getCurrent()).then(() => App.updatePlayState());
        }
    },
    serialise: function (big) {
        if (big) {
            return Queue.queue.map(Queue.serialiseTrack);
        } else {
            return Queue.queue.map(function (track) {
                return [track.link.split(":")[2], track.uuid, track.source]
            });
        }
    },
    serialiseTrack: function (track) {
        if (track === null) return null;
        return {
            id: track.link.split(":")[2],
            uuid: track.uuid,
            source: track.source
        };
    },
    isShuffled: function () {
        return Queue.shuffled;
    },
    setShuffled: function (shuffled, noUpdate) {
        if (shuffled == Queue.shuffled) return;
{'fraction_non_alphanumeric': 0.15516372795969774, 'fraction_numerical': 0.006045340050377834, 'mean_word_length': 1.7358750574184658, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '447389', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1714, 'n_words': 385}