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

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

app.currentUserKey = rdioUserKey;

app.roomUrl = 'https://rdiodj.firebaseio.com/room';

app.Player = Backbone.Firebase.Model.extend({

  isMaster: function () {
    return this.get('masterUserKey') === app.currentUserKey;
  },

  firebase: app.roomUrl + '/player'

});

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: app.roomUrl + '/queue',

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

});

app.queue = new app.TrackList();
app.playState = new app.Player();

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;

    if (app.playState.get('masterUserKey') === undefined) {
      app.playState.set({
        'masterUserKey': app.currentUserKey
      });
    }

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

    if (app.playState.isMaster()) {
      // Master listener should remove the next item from the queue
      self.playNext();

      R.player.on('change:playingTrack', function(newValue) {
        if (newValue === null) {
          self.playNext();
        }
      });

      R.player.on('change:playState', function(newValue) {
        app.playState.set({
          'playState': newValue
        });

      });
    } else {
      // Slave listener, should listen to app.playState changes
      if (app.playState.get('playState') == R.player.PLAYSTATE_PLAYING) {
        R.player.play({
          source: app.playState.get('playingTrack').trackKey
        });
      }

      app.playState.on('change:playingTrack', function(model, value, options) {
        console.log('change:playingTrack', model, value, options);
        R.player.play({
          source: value.trackKey
        });
      });
      app.playState.on('change:playState', function(model, value, options) {
        console.log('change:playState', model, value, options);
        switch (value) {
          case R.player.PLAYSTATE_PAUSED:
          case R.player.PLAYSTATE_STOPPED:
            R.player.pause();
            break;
        }
      });
    }

  },

  render: function() {
    if (this.rdioTrack) {
      var data = _.extend({
        'track': this.rdioTrack.attributes
      });
      this.$el.html(this.template(data));
      this.$el.show();<commit_msg>Keep player position in sync<commit_after>/*globals app, console, R, Backbone, Firebase, rdioUserKey, firebaseToken */

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

app.currentUserKey = rdioUserKey;

app.roomUrl = 'https://rdiodj.firebaseio.com/room';

app.Player = Backbone.Firebase.Model.extend({

  isMaster: function () {
    return this.get('masterUserKey') === app.currentUserKey;
  },

  firebase: app.roomUrl + '/player'

});

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: app.roomUrl + '/queue',

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

});

app.queue = new app.TrackList();
app.playState = new app.Player();

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;

    if (app.playState.get('masterUserKey') === undefined) {
      app.playState.set({
        'masterUserKey': app.currentUserKey
      });
    }

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

    if (app.playState.isMaster()) {
      // Master listener should remove the next item from the queue
      self.playNext();

      R.player.on('change:playingTrack', function(newValue) {
        if (newValue === null) {
          self.playNext();
        }
      });

      R.player.on('change:playState', function(newValue) {
        app.playState.set({
          'playState': newValue
        });
      });

      R.player.on('change:position', function(newValue) {
        app.playState.set({
          'position': newValue
        });
      });
    } else {
      // Slave listener, should listen to app.playState changes
      if (app.playState.get('playState') == R.player.PLAYSTATE_PLAYING) {
        R.player.play({
          source: app.playState.get('playingTrack').trackKey,
          initialPosition: app.playState.get('position')
        });
      }

      app.playState.on('change:playingTrack', function(model, value, options) {
        console.log('change:playingTrack', model, value, options);
        R.player.play({
          source: value.trackKey
        });
      });
      app.playState.on('change:playState', function(model, value, options) {
        console.log('change:playState', model, value, options);
        switch (value) {
          case R.player.PLAYSTATE_PAUSED:
          case R.player.PLAYSTATE_STOPPED:
            R.player.pause();
            break;
        }
      });
    }

  },

  render: function() {
    if (this.rdioTrack) {
      var data = _.extend({
        'track': this.rdioTrack.attributes
      });
      this.$el.html(this.template(data));
      this.$el.show();
{'fraction_non_alphanumeric': 0.16160493827160494, 'fraction_numerical': 0.0012345679012345679, 'mean_word_length': 2.62136790344211, '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': '5907833', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2641, 'n_words': 478}