-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
76 lines (66 loc) · 2.02 KB
/
player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Collection with some server-side specific functionality for players.
var _u = require('underscore'),
Backbone = require('backbone'),
PlayersCollection = Backbone.Collection.extend({
// Returns the ID of the current artist
getCurrentArtist: function () {
return this.artistOrder && this.pos >= 0 && this.artistOrder.length ? this.artistOrder[this.pos] : 0;
},
hasNextArtist: function () {
return this.artistOrder && this.pos < this.artistOrder.length - 1;
},
getNextArtist: function () {
var nextArtistId;
if (!this.artistOrder || !this.artistOrder.length) {
console.log('[err] something is wrong, no artistOrder.');
return 0;
}
if (this.pos >= this.artistOrder.length - 1) {
console.log('[err] getNextArtist: no artists left');
return 0;
}
while (true) {
nextArtistId = this.artistOrder[++this.pos];
if (this.get(nextArtistId)) {
// found artist
return nextArtistId;
}
else {
if (!this.hasNextArtist()) {
// no more artists
return 0;
}
// didn't find artist, advancing to next one.
nextArtistId = this.artistOrder[++this.pos];
}
}
},
decideArtistOrder: function () {
// TODO: Figure out if underscore 1.2.0 exists in NodeJS
/*
var tmpOrder = [];
this.each(function (player) {
tmpOrder.push(player.id);
});
this.artistOrder = _u.shuffle(tmpOrder);
*/
var len, idx, artistId,
playerIds = this.map(function (player) { return player.id; });
// Random insert into artistOrder
this.artistOrder = [];
len = playerIds.length;
while (playerIds.length > 0) {
idx = Math.floor(Math.random() * playerIds.length);
artistId = playerIds.splice(idx, 1);
this.artistOrder.push(artistId[0]);
len = playerIds.length;
}
this.pos = -1;
},
getLeader: function () {
return this.find(function(player) {
return player.get('isLeader');
});
}
});
module.exports = PlayersCollection;