-
Notifications
You must be signed in to change notification settings - Fork 1
/
rumble.js
376 lines (356 loc) · 11.1 KB
/
rumble.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
$(function() {
var config = {
heat: {
max: 10,
reduceFactor: 0.91
}
};
_.mixin({
// Split a large collection in smaller ones of predefined size
split: function(col, size) {
var arr = col.toArray();
var tokens = Math.ceil(col.size() / size);
var res = new Array(tokens);
for (var i = 0; i < tokens; i++) {
res[i] = _(arr.slice(i * size, (i + 1) * size));
}
return _(res);
}
});
// id(value[0..size[), heat, reservedBy
var Cell = Backbone.Model.extend({
initialize: function() {
this.set('heat', config.heat.max / 2);
},
clean: function() {
this.set('heat', this.get('heat') * config.heat.reduceFactor);
this.unset('reservedBy');
},
reserve: function(by) {
this.set('heat', Math.min(config.heat.max, this.get('heat') + 1));
this.set('reservedBy', by);
}
});
var CellView = Backbone.View.extend({
tagName: 'td',
initialize: function() {
this.model.bind('change', this.render, this);
},
render: function() {
this.$el.on('click', _.bind(function() {
window.rumbleApp.select(this.model.id);
}, this));
if (this.model.has('reservedBy')) {
this.$el.text(this.model.get('reservedBy').id);
} else {
this.$el.text(this.model.id + 1);
}
var heat = Math.floor(this.model.get('heat') * 255 / config.heat.max);
this.$el.css('background-color', 'rgb(' + heat + ',' + (255 - heat) + ', 0)');
return this;
}
});
var Grid = Backbone.Collection.extend({
model: Cell,
clean: function() {
this.each(function(cell) { cell.clean(); });
}
});
var GridView = Backbone.View.extend({
tagName: 'table',
id: 'cells',
initialize: function() {
this.collection.bind('add', this.render, this);
this.collection.bind('remove', this.render, this);
this.collection.bind('reset', this.render, this);
},
render: function() {
this.$el.empty();
var c = Math.ceil(Math.sqrt(this.collection.size()));
if (c > 0) {
_.split(this.collection, c).each(function(row) {
var r = $('<tr>');
row.each(function(cell) {
r.append(new CellView({ model: cell }).render().el);
});
this.$el.append(r);
}, this);
}
return this;
}
});
// name, comment, image
var Badge = Backbone.Model.extend({
});
var BadgeList = Backbone.Collection.extend({
model: Badge
});
var BadgeView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
this.model.bind('change', this.render, this);
},
render: function() {
this.$el.empty();
var image = $('<img />').attr({
'src': 'data:image/png;base64,' + this.model.get('image'),
'alt': this.model.get('name'),
'title': this.model.get('name') + ': ' + this.model.get('comment')
});
this.$el.append(image);
return this;
}
});
var BadgeListView = Backbone.View.extend({
tagName: 'ul',
id: 'badges',
initialize: function() {
this.collection.bind('add', this.render, this);
this.collection.bind('remove', this.render, this);
this.collection.bind('reset', this.render, this);
},
render: function() {
this.$el.empty();
this.collection.each(function(badge) {
this.$el.append(new BadgeView({ model: badge }).render().el);
}, this);
return this;
}
});
// id(name), score
var Player = Backbone.Model.extend({
urlRoot: '/players',
winRound: function() {
this.set('score', this.get('score') + 1);
}
});
var PlayerMiniView = Backbone.View.extend({
tagName: 'li',
initialize: function() {
this.model.bind('change', this.render, this);
},
render: function() {
this.$el.text(this.model.id + ': ' + this.model.get('score'));
return this;
}
});
var PlayerList = Backbone.Collection.extend({
model: Player,
comparator: function(player) {
return (- player.get('score'));
},
onName: function(name, callback) {
var player = this.get(name);
if (player) {
callback(player);
} else {
player = new Player({ id: name });
player.fetch({ success: _.bind(function(model) {
this.add(model);
callback(model);
}, this) });
}
}
});
var PlayerListView = Backbone.View.extend({
tagName: 'ul',
id: 'players',
initialize: function() {
this.collection.bind('add', this.render, this);
this.collection.bind('remove', this.render, this);
this.collection.bind('reset', this.render, this);
// TODO handle ordering change
},
render: function() {
this.$el.empty();
this.collection.each(function(player) {
this.$el.append(new PlayerMiniView({ model: player }).render().el);
}, this);
return this;
}
});
// cells, players, playing, message
var App = Backbone.Model.extend({
defaults: {
playing: false
},
initialize: function() {
this.set('cells', new Grid());
this.set('badges', new BadgeList());
},
setSize: function(size) {
var c = this.get('cells');
c.reset();
for (var i = 0; i < size; i++) {
c.add(new Cell({ id: i }));
}
},
listen: function(socket) {
this.socket = socket;
this.socket.on('playerchosenumber', _.bind(function(data) {
//TODO mesure ping
this.get('players').onName(data.player, _.bind(function(player) {
this.get('cells').get(data.value).reserve(player);
}, this));
}, this));
this.socket.on('newplayer', _.bind(function(player) {
this.get('players').add(player);
}, this));
this.socket.on('playerleave', _.bind(function(data) {
this.get('players').remove(this.get('players').get(data.player));
}, this));
this.socket.on('newbadge', _.bind(function(badge) {
this.get('badges').add(badge);
}, this));
this.socket.on('closedbeforeresult', _.bind(function() {
this.set('playing', false);
}, this));
this.socket.on('terminateround', _.bind(function(data) {
var cells = this.get('cells');
var winner = cells.get(data.winner);
if (winner.has('reservedBy')) {
var player = winner.get('reservedBy');
player.winRound();
this.set('message', player.id + ' wins with number ' + (data.winner + 1));
} else {
this.set('message', 'Nobody had chosen number ' + (data.winner + 1));
}
this.get('cells').clean();
this.set('playing', true);
this.get('chronometer').start();
}, this));
},
select: function(value) {
if (this.get('playing') === true) {
//TODO validate
this.socket.emit('choosenumber', { chosen: value });
this.set('playing', false);
}
}
});
var Chronometer = Backbone.Model.extend({
start: function() {
this.set('end', (new Date((new Date()).getTime() + this.get('duration'))).getTime());
window.setTimeout(_.bind(function() { this.unset('end'); this.trigger('finish'); }, this), this.get('duration'));
this.trigger('start');
},
running: function() {
return this.has('end');
},
remaining: function() {
if (this.has('end')) {
return (this.get('end') - new Date().getTime());
} else {
return this.get('duration');
}
}
});
var ChronometerView = Backbone.View.extend({
id: 'chronometer',
initialize: function() {
this.model.on('start', this.startRendering, this);
this.model.on('finish', this.stopRendering, this);
},
render: function() {
if (this.model.running()) {
var remaining = this.model.remaining();
var seconds = Math.floor(remaining / 1000);
var milliseconds = remaining - (seconds * 1000);
this.$el.text('Time left: ' + seconds + 's ' + milliseconds + 'ms');
} else {
this.$el.text('Waiting...');
}
return this;
},
startRendering: function() {
var callback = _.bind(this.render, this);
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame;
if (raf) {
var call = _.bind(function() {
callback();
this.rafId = raf(call);
}, this);
call();
} else {
// Fallback at 30fps
this.rafId = window.setInterval(callback, 1000 / 30);
}
},
stopRendering: function() {
if (this.rafId) {
var caf = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
if (caf) {
caf(this.rafId);
} else {
window.clearInterval(this.rafId);
}
this.rafId = null;
}
this.render();
}
});
var AppView = Backbone.View.extend({
initialize: function() {
this.model.on('change:playing', this.updateButton, this);
this.model.on('change:message', this.updateMessage, this);
},
updateButton: function() {
if (this.button) {
this.button.prop('disabled', !this.model.get('playing'));
}
},
updateMessage: function() {
if (this.message) {
this.message.text(this.model.get('message'));
}
},
render: function() {
this.$el.empty();
var choosevalue = $('<div />').attr('id', 'choosevalue');
this.$el.append(choosevalue);
var input = $('<input />').attr({
placeholder: 'Enter your choice...',
required: true
});
choosevalue.append(input);
this.button = $('<button />').text('Select').on('click', _.bind(function() { this.select(input.val() - 1); }, this.model));
choosevalue.append(this.button);
this.message = $('<div />').attr('id', 'message');
this.$el.append(this.message);
this.$el.append(new GridView({ collection: this.model.get('cells') }).render().el);
this.$el.append(new PlayerListView({ collection: this.model.get('players') }).render().el);
this.$el.append(new ChronometerView({ model: this.model.get('chronometer') }).render().el);
this.$el.append(new BadgeListView({ collection: this.model.get('badges') }).render().el);
this.updateButton();
return this;
}
});
// The browser keeps the disabled state when refreshing
$('#enterbutton').prop('disabled', false);
if (window.localStorage) {
var savedname = window.localStorage.getItem('rumble-username');
if (savedname) {
$('#username').val(savedname);
}
}
var socket = io.connect();
$('#enterbutton').click(function() {
var name = $('#username').val();
socket.emit('chooseusername', { name: name });
if (window.localStorage) {
window.localStorage.setItem('rumble-username', name);
}
});
socket.on('wrongusername', function(data) {
alert(data.reason);
});
socket.on('entergame', function(data) {
window.rumbleApp = new App({
players: new PlayerList(data.players),
chronometer: new Chronometer({ duration: data.timeToChoose })
});
window.rumbleApp.setSize(data.gridsize);
window.rumbleApp.listen(socket);
new AppView({ model: window.rumbleApp, el: $('#game') }).render();
});
})