-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathhost.js
More file actions
350 lines (300 loc) · 9.28 KB
/
host.js
File metadata and controls
350 lines (300 loc) · 9.28 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
var decks = {
'standard': [
{ value: NaN, estimate: '?' },
{ value: 0, estimate: '0' },
{ value: 0.5, estimate: '½' },
{ value: 1, estimate: '1' },
{ value: 2, estimate: '2' },
{ value: 3, estimate: '3' },
{ value: 5, estimate: '5' },
{ value: 8, estimate: '8' },
{ value: 13, estimate: '13' },
{ value: 20, estimate: '20' },
{ value: 40, estimate: '40' },
{ value: 100, estimate: '100' },
{ value: Infinity, estimate: '∞' },
{ value: NaN, estimate: '<i class="fa fa-coffee"></i>' },
{ value: NaN, estimate: 'ಠ_ಠ'}
],
'fibonacci': [
{ value: NaN, estimate: '?' },
{ value: 0, estimate: '0' },
{ value: 1, estimate: '1' },
{ value: 2, estimate: '2' },
{ value: 3, estimate: '3' },
{ value: 5, estimate: '5' },
{ value: 8, estimate: '8' },
{ value: 13, estimate: '13' },
{ value: 21, estimate: '21' },
{ value: 34, estimate: '34' },
{ value: 55, estimate: '55' },
{ value: 89, estimate: '89' },
{ value: Infinity, estimate: '∞' },
{ value: NaN, estimate: '<i class="fa fa-coffee"></i>' },
{ value: NaN, estimate: 'ಠ_ಠ'}
],
'letters': [
{ value: 0, estimate: 'A' },
{ value: 1, estimate: 'B' },
{ value: 2, estimate: 'C' },
{ value: 3, estimate: 'D' },
{ value: 4, estimate: 'E' },
{ value: 5, estimate: 'F' },
{ value: NaN, estimate: '?' },
{ value: NaN, estimate: '<i class="fa fa-coffee"></i>' }
],
'tshirt': [
{ value: 0, estimate: 'XS' },
{ value: 1, estimate: 'S' },
{ value: 2, estimate: 'M' },
{ value: 3, estimate: 'L' },
{ value: 4, estimate: 'XL' },
{ value: NaN, estimate: '?' },
{ value: Infinity, estimate: '∞' },
{ value: NaN, estimate: '<i class="fa fa-coffee"></i>' }
]
};
var socket = io.connect('http://'+window.location.host);
var roomId = BP.room.roomId;
var title = BP.room.title;
var votes = {};
var voters = {};
var voteData = {};
var voting = false;
// 0 - start, 1 - betting open, 2 - reveal
var roundStatus = 0;
var userTemp = '<li data-name="{{name}}" data-uid="{{uid}}">'+
'<div title="Don\'t count this voter\'s estimates" class="ಠ_ಠ"><i class="fa fa-eye"></i></div>'+
'<div title="Remove this voter from room" class="kickVoter">×</div>'+
'<img class="voterImage" src="{{avatar}}" />'+
'<h3 class="voterName">{{name}}</h3>'+
'<div class="card">'+
'<div class="cardBack"></div>'+
'<div class="cardInner">'+
'<div class="cardValue"></div>'+
'<div class="cornerValue topleft"></div>'+
'<div class="cornerValue bottomright"></div>'+
'</div>'+
'</div>'+
'</li>';
var ticketTemp = '<a href="{{url}}" class="key" target="_blank">{{key}}</a>: <span class="title">{{title}}</span>';
var getDeck = function() {
return decks[$('input[name=deckType]:checked').val() || 'standard'];
};
var getNumVotes = function() {
var count = 0;
BP.each(votes,function() {
count++;
});
return count;
};
var processVotes = function() {
voteData = {
votes: votes,
average: -1,
trueAverage: -1,
min: -1,
max: -1,
allVotesEqual: true,
lastVote: -1,
total: 0,
numVotes: 0,
spread: 0
};
var deck = getDeck();
var minCardIdx = 0, maxCardIdx = 0;
BP.each(votes, function(vote) {
if(!isNaN(vote)) {
voteData.total += vote;
if(voteData.lastVote === -1){
voteData.lastVote = vote;
voteData.min = vote;
voteData.max = vote;
}
if(voteData.lastVote !== vote){ voteData.allVotesEqual = false; }
if(voteData.max < vote){ voteData.max = vote; }
if(voteData.min > vote){ voteData.min = vote; }
voteData.numVotes++;
}
});
BP.each(deck, function(card, i) {
if(card.value === voteData.min) { minCardIdx = i; }
if(card.value === voteData.max) { maxCardIdx = i; }
});
voteData.spread = maxCardIdx - minCardIdx;
voteData.average = voteData.numVotes === 0 ? 0 : voteData.total/voteData.numVotes;
if(voteData.average > 0.5) {
voteData.trueAverage = voteData.average;
voteData.average = Math.round(voteData.average);
}
};
var page = new BP.Page({
socket: socket,
domRoot: '#room',
socketEvents: {
'newVoter': 'addVoter',
'voterLeave': 'removeVoter',
'updateTicket': 'updateTicket',
'newVote': 'acceptVote'
},
domEvents: {
'change input[name=deckType]': 'updateVoterDecks',
'click .ಠ_ಠ': 'ಠ_ಠ',
'click .kickVoter': 'kickVoter',
'click .setting': 'toggleSettingMenu',
'click #showLink': 'showShareLink',
'click #toggleRound': 'toggleRound'
},
DOM: {
users: '#users',
ticket: '#ticket',
average: '#average',
largeSpread: '#largeSpread'
},
initialize: function() {
socket.emit('createRoom', {roomId: roomId, title: title});
this.useNotifications = BP.localStorage.get('useNotifications');
var inviteId = $('#showLink').attr('data-link');
var inviteLinkMarkup = document.location.host + '/<strong>' + inviteId + '</strong>';
$('#showLink').attr('data-link', inviteLinkMarkup);
$('#inviteUrl').html(inviteLinkMarkup);
},
addVoter: function(data) {
voters[data.uid] = data;
data.name = $('<span></span>').html(data.name).text();
var html = BP.template(userTemp, data);
$(html).appendTo(this.$users);
this.updateVoterDecks();
socket.emit('updateVoters', {roomId: roomId, voters: voters});
// emit the appropriate voting state for the new voter to pick up on
if (voting) {
socket.emit('newRound', {roomId: roomId, ticket: BP.currentTicket});
} else {
socket.emit('roundEnd',{roomId: roomId});
}
},
removeVoter: function(data) {
delete votes[data.uid];
delete voters[data.uid];
this.$('li[data-uid="' + data.uid + '"]').remove();
socket.emit('updateVoters', {roomId: roomId, voters: voters});
},
updateTicket: function(data) {
BP.currentTicket = data;
this.$ticket.html(BP.template(ticketTemp, data));
},
acceptVote: function(data) {
if(roundStatus === 1){
var
$voter = this.$('li[data-uid="'+data.uid+'"]'),
$card = $voter.find('.card'),
$mainValue = $card.find('.cardValue'),
$cornerValues = $card.find('.cornerValue'),
$cardBack = $card.find('.cardBack');
$mainValue.html(data.cardValue);
$cornerValues.html(data.cardValue);
if(data.cardValue === 'coffee') {
$mainValue.addClass('coffee');
$cornerValues.addClass('coffee');
}
$cardBack.css('background-color', data.color).attr('class', 'cardBack').addClass(data.pattern);
$cardBack.css('background-image', 'url(/images/cards/' + data.pattern + '.png)');
$card.addClass('visible');
if(!$voter.data('observer'))
votes[data.uid] = data.value;
if(this.useNotifications && getNumVotes() === this.$users.find('li:not([data-observer=true])').length) {
BP.Notification.send({
title: 'BitPoints - '+BP.room.title,
body: 'All votes are in!'
});
}
}
},
updateVoterDecks: function() {
socket.emit('deckChange',getDeck());
},
ಠ_ಠ: function(e, $el) {
$el.parent().attr('data-observer', $el.parent().attr('data-observer') !== 'true');
},
kickVoter: function(e, $el) {
socket.emit('kickVoter',{roomId:roomId,uid:$el.parent().data('uid')});
},
toggleSettingMenu: function(e, $el) {
$el.siblings().find('.drop').removeClass('active');
$el.next('.drop').toggleClass('active');
},
showShareLink: function(e, $el) {
var link = $el.data('link'),
modal = new BP.Modal({
id: 'inviteLink',
content: link,
size: 'large'
});
modal.show();
},
startNewRound: function(e, $el) {
voting = true;
this.$average.hide().find('.val').empty();
this.$largeSpread.hide();
$el.text('Stop Estimating');
this.$('.card').removeClass('visible showValue spin');
$('html').addClass('voting');
// Clear out all votes
votes = {};
// wait until cards are fully hidden to remove classes and emit events
window.setTimeout(function(){
this.$('.cardValue').removeClass('coffee min max');
this.$('.cornerValue').removeClass('coffee');
socket.emit('newRound', {roomId: roomId, ticket: BP.currentTicket});
}.bind(this), 1500);
},
endCurrentRound: function(e, $el) {
voting = false;
$el.text('Begin Estimating');
this.$('.card').addClass('showValue');
$('html').removeClass('voting');
processVotes();
// If there's only one person, vote data is useless
if(voteData.numVotes > 1) {
// Log info for advanced users
console.log(votes);
console.log(voteData);
var averageText;
if (voteData.trueAverage > -1 && voteData.average !== voteData.trueAverage) {
averageText = voteData.trueAverage.toFixed(2);
if (voteData.average > voteData.trueAverage) {
averageText += ', rounded up';
} else if (voteData.average < voteData.trueAverage) {
averageText += ', rounded down';
}
} else {
averageText = voteData.average + ' on the money';
}
var outcomeText = voteData.average;
if (voteData.spread > 3) {
outcomeText += '*';
averageText += ' - The vote spread is large';
}
this.$average.show().find('.val').text(outcomeText);
this.$average.show().find('.val').attr('title', averageText);
// Animate fun-times if everyone votes the same
if(voteData.numVotes > 3 && voteData.allVotesEqual){
this.$('.card').addClass('spin');
}
}
socket.emit('roundEnd',{
roomId: roomId,
roundData: voteData,
outcome: outcomeText
});
},
toggleRound: function(e, $el){
roundStatus = (roundStatus % 2) + 1;
if(roundStatus === 1) {
this.startNewRound(e, $el);
} else if(roundStatus === 2) {
this.endCurrentRound(e, $el);
}
}
});
page.init();