<commit_before>var Song = require('../models/song.js')
var mpd = require('mpd'),
    cmd = mpd.cmd
var locks = require('locks');
var mutex = locks.createMutex();
var client = mpd.connect({
    port: 6600,
    host: 'localhost',
});
client.on('ready', function() {
    console.log("ready");
    playerCallback();
});
client.on('system', function(name) {
    console.log("update", name);
});
function playerCallback() {
    mutex.lock(function() {
        client.sendCommand(cmd("status", []), function(err, msg) {
            if (err) throw err;
            var msgPairs = mpd.parseKeyValueMessage(msg)
            console.log(JSON.stringify(msgPairs))
            if (msgPairs.state == 'stop') {
                Song.nextSong(function(err, doc) {
                    if (err) {
                        mutex.unlock();
                        throw(err)
                    }
                    client.sendCommand(cmd("add", [doc.file_path]), function(err, msg) {
                        if (err) {
                            mutex.unlock();
                            throw(err)
                        }
                        console.log("New song added to MPD");
                        Song.songCompleted(doc._id, function(err, doc) {
                            console.log("Song started" + doc)
                            client.sendCommand(cmd("play", []), function(err, msg) {
                                mutex.unlock();
                            })
                        })
                    })
                })
            } else if (msgPairs.elapsed.startsWith('0.0') && msgPairs.state == 'pause') {
                Song.nextSong(function(err, doc) {
                    if (err) {
                        mutex.unlock();
                        throw(err)
                    }
                    console.log(JSON.stringify(doc));
                    console.log('FILE!!!:' + doc.file_path)
                    client.sendCommand(cmd("add", [doc.file_path]), function(err, msg) {
                        if (err) {
                            mutex.unlock();
                            throw(err)
                        }
                        console.log("New song added to MPD");
                        Song.songCompleted(doc._id, function(err, doc) {
                            console.log("Song started" + doc)
                            client.sendCommand(cmd("play", []), function(err, msg) {
                                mutex.unlock();
                            })
                        })<commit_msg>Refactored code<commit_after>var Song = require('../models/song.js')
var mpd = require('mpd'),
    cmd = mpd.cmd
var locks = require('locks');
var mutex = locks.createMutex();
var client = mpd.connect({
    port: 6600,
    host: 'localhost',
});
client.on('ready', function() {
    console.log("ready");
    playerCallback();
});
client.on('system', function(name) {
    console.log("update", name);
});
function playerCallback() {
    mutex.lock(function() {
        client.sendCommand(cmd("status", []), function(err, msg) {
            if (err) throw err;
            var msgPairs = mpd.parseKeyValueMessage(msg)
            console.log(JSON.stringify(msgPairs))
            if (msgPairs.state == 'stop' || msgPairs.elapsed.startsWith('0.0') && msgPairs.state == 'pause') {
                Song.nextSong(function(err, doc) {
                    if (err) {
                        mutex.unlock();
                        throw(err)
                    }
                    console.log(JSON.stringify(doc));
                    console.log('FILE!!!:' + doc.file_path)
                    client.sendCommand(cmd("add", [doc.file_path]), function(err, msg) {
                        if (err) {
                            mutex.unlock();
                            throw(err)
                        }
                        console.log("New song added to MPD");
                        Song.songCompleted(doc._id, function(err, doc) {
                            console.log("Song started" + doc)
                            client.sendCommand(cmd("play", []), function(err, msg) {
                                mutex.unlock();
                            })
                        })
{'fraction_non_alphanumeric': 0.13551732264906904, 'fraction_numerical': 0.0028281876031110063, 'mean_word_length': 1.2278215223097113, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7436144', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1055, 'n_words': 194}