<commit_before>/*globals app, console, R, Backbone, Firebase, rdioUserKey, firebaseToken */

window.app = window.app || {};

app.currentUserKey = rdioUserKey;

app.Track = Backbone.Model.extend({
  LIKE: 'like',
  DISLIKE: 'dislike',

  defaults: {
    "trackKey": null,
    "userKey": null,
    "votes": {},
    "upVotes": 0,
    "downVotes": 0
  },

  getVoteRef: function() {
    return this.collection.firebase.child(this.get('id')).child('votes');
  },

  vote: function(newVote) {
    console.info('Voting', newVote, 'for', this.get('trackKey'));
    this.getVoteRef().child(app.currentUserKey).set(newVote);
    this.updateVoteCounts();
  },

  upVote: function() {
    this.vote(this.LIKE);
  },

  downVote: function() {
    this.vote(this.DISLIKE);
  },

  getVoteCount: function(votes, voteType) {
    var self = this;
    var count = _.reduce(votes, function(num, vote) {
      if (vote === voteType) {
        return num + 1;
      } else {
        return num;
      }
    }, 0);
    return count;
  },

  updateVoteCounts: function() {
    var votes = _.values(this.get('votes'));
    var likeCount = this.getVoteCount(votes, this.LIKE);
    var dislikeCount = this.getVoteCount(votes, this.DISLIKE);

    console.log('Updated vote count', 'like', likeCount, 'dislike', dislikeCount);

    this.set({
      upVotes: likeCount,
      downVotes: dislikeCount
    });
  }

});


app.TrackList = Backbone.Firebase.Collection.extend({
  model: app.Track,

  firebase: 'https://rdiodj.firebaseio.com/room/queue',

  comparator: function(track) {
    return (track.get('upVotes') - track.get('downVotes')) * -1;
  }

});

app.queue = new app.TrackList();

app.NowPlayingView = Backbone.View.extend({
  el: '#now-playing',

  template: _.template($('#now-playing-template').html()),

  events: {
    'click #player-play': 'playNext'
  },

  initialize: function() {
    var self = this;
    this.rdioTrack = null;
    this.rdioUser = null;

    R.player.on("change:playingTrack", function(newValue) {
      if (newValue !== null) {
        self.rdioTrack = newValue;
        self.render();
      }
    });

    R.player.on("change:playState", function(newValue) {
      if (newValue === R.player.PLAYSTATE_STOPPED) {
        self.playNext();
      }
    });

  },

  render: function() {
    if (this.rdioTrack) {
      var data = _.extend({
        'track': this.rdioTrack.attributes
      });
      this.$el.html(this.template(data));
      this.$el.show();
    } else {
      this.$el.hide();
    }
    return this;
  },

  playNext: function() {
    var queueItem = app.queue.shift();<commit_msg>Don't play when there is nothing in the queue<commit_after>/*globals app, console, R, Backbone, Firebase, rdioUserKey, firebaseToken */

window.app = window.app || {};

app.currentUserKey = rdioUserKey;

app.Track = Backbone.Model.extend({
  LIKE: 'like',
  DISLIKE: 'dislike',

  defaults: {
    "trackKey": null,
    "userKey": null,
    "votes": {},
    "upVotes": 0,
    "downVotes": 0
  },

  getVoteRef: function() {
    return this.collection.firebase.child(this.get('id')).child('votes');
  },

  vote: function(newVote) {
    console.info('Voting', newVote, 'for', this.get('trackKey'));
    this.getVoteRef().child(app.currentUserKey).set(newVote);
    this.updateVoteCounts();
  },

  upVote: function() {
    this.vote(this.LIKE);
  },

  downVote: function() {
    this.vote(this.DISLIKE);
  },

  getVoteCount: function(votes, voteType) {
    var self = this;
    var count = _.reduce(votes, function(num, vote) {
      if (vote === voteType) {
        return num + 1;
      } else {
        return num;
      }
    }, 0);
    return count;
  },

  updateVoteCounts: function() {
    var votes = _.values(this.get('votes'));
    var likeCount = this.getVoteCount(votes, this.LIKE);
    var dislikeCount = this.getVoteCount(votes, this.DISLIKE);

    console.log('Updated vote count', 'like', likeCount, 'dislike', dislikeCount);

    this.set({
      upVotes: likeCount,
      downVotes: dislikeCount
    });
  }

});


app.TrackList = Backbone.Firebase.Collection.extend({
  model: app.Track,

  firebase: 'https://rdiodj.firebaseio.com/room/queue',

  comparator: function(track) {
    return (track.get('upVotes') - track.get('downVotes')) * -1;
  }

});

app.queue = new app.TrackList();

app.NowPlayingView = Backbone.View.extend({
  el: '#now-playing',

  template: _.template($('#now-playing-template').html()),

  events: {
    'click #player-play': 'playNext'
  },

  initialize: function() {
    var self = this;
    this.rdioTrack = null;
    this.rdioUser = null;

    R.player.on("change:playingTrack", function(newValue) {
      if (newValue !== null) {
        self.rdioTrack = newValue;
        self.render();
      }
    });

    R.player.on("change:playState", function(newValue) {
      if (newValue === R.player.PLAYSTATE_STOPPED) {
        self.playNext();
      }
    });

  },

  render: function() {
    if (this.rdioTrack) {
      var data = _.extend({
        'track': this.rdioTrack.attributes
      });
      this.$el.html(this.template(data));
      this.$el.show();
    } else {
      this.$el.hide();
    }
    return this;
  },

  playNext: function() {
    if (!app.queue.length) {
      return;
    }

    var queueItem = app.queue.shift();
{'fraction_non_alphanumeric': 0.17057045712126936, 'fraction_numerical': 0.0018889308651303363, 'mean_word_length': 2.8397389412617837, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2330265', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1837, 'n_words': 334}