-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorsync-client.js
308 lines (289 loc) · 9.23 KB
/
colorsync-client.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
(function () {
"use strict";
function rgb2hsb(rgb) {
var hsb = { h: 0, s: 0, b: 0 };
var min = Math.min(rgb.r, rgb.g, rgb.b);
var max = Math.max(rgb.r, rgb.g, rgb.b);
var delta = max - min;
hsb.b = max;
hsb.s = max != 0 ? 255 * delta / max : 0;
if (hsb.s != 0) {
if (rgb.r == max) {
hsb.h = (rgb.g - rgb.b) / delta;
} else if (rgb.g == max) {
hsb.h = 2 + (rgb.b - rgb.r) / delta;
} else {
hsb.h = 4 + (rgb.r - rgb.g) / delta;
}
} else {
hsb.h = -1;
}
hsb.h *= 60;
if (hsb.h < 0) {
hsb.h += 360;
}
hsb.s *= 100 / 255;
hsb.b *= 100 / 255;
hsb.h = hsb.h | 0;
hsb.s = hsb.s | 0;
hsb.b = hsb.b | 0;
return hsb;
}
var numberToColor = function (n) {
/* We want to get random color but to avoid too strong colors, so...
color will be choosen from inside of ball just fit in the RGB cube,
and will weighten to close to the sphere of that ball to keep it colorful.
step1) get 3 factors (a,b,c) for radius, theta and phi from given random number
step2) make randomized radius, theta and phi
step3) calculate x,y,z of the point specified by radius, theta and phi
then it's RGB ;)
*/
var a = (n & 0x000000FF)
, b = ((n & 0x0000FF00) >> 8)
, c = ((n & 0x00FF0000) >> 16)
, r = ( 1 - Math.pow(a / 256, 2)) * 128 // a bit trick to gain intensity
, theta = Math.acos( Math.sqrt( b/255 ) ) * 2
, phi = (c / 256) * Math.PI * 2
, red = Math.floor(128 + r * Math.sin(theta) * Math.cos(phi))
, green = Math.floor(128 + r * Math.sin(theta) * Math.sin(phi))
, blue = Math.floor(128 + r * Math.cos(theta))
;
var hsb = rgb2hsb({r:red,g:green,b:blue});
return {
r: red, g: green, b: blue, hsb: hsb
}
};
var storage = ( 'undefined' !== typeof ColorSyncClientAndroid )
? ColorSyncClientAndroid
: Peatix.Pref
;
var locationManager;
var ColorSyncClient = function (opts) {
this._retryTimeout = 1000;
return this.init(opts);
};
ColorSyncClient.prototype = {
_set_options: function (opts) {
var defaultCallbacks = {
onwelcome: function () {}
, oninitstep: function(step) {}
, onjoin: function () {}
, onleave: function () {}
, onclose: function () {}
, onsynchronize: function () {}
};
for ( var ev in defaultCallbacks ) {
this[ev] = opts[ev] || defaultCallbacks[ev];
}
this.server = opts.server;
this.token = opts.token;
this.didSync = false;
this.isInProgress = false;
}
, init: function (opts) {
if ( opts ) this._set_options(opts);
var that = this;
var lastSessionJSON = storage.getValue( 'lastSession:' + this.server );
var lastSession = JSON.parse(lastSessionJSON || '{}');
var url = this.server;
if ( lastSession.resume ) {
url += '&resume=' + encodeURIComponent( lastSession.resume ) + '&end=' + encodeURIComponent( lastSession.end );
}
else {
url += '&token=' + encodeURIComponent(this.token);
}
if ( typeof ColorSyncClientAndroid !== 'undefined' ) {
ColorSyncClientAndroid.connect(url);
this.ws = ColorSyncClientAndroid;
}
else if ( typeof WebSocket !== 'undefined' ) {
this.ws = new WebSocket(url);
this.ws.onopen = function () { that.__onOpen.apply(that, arguments); };
this.ws.onclose = function () { that.__onClose.apply(that, arguments); };
this.ws.onmessage = function () { that.__onMessage.apply(that, arguments); };
this.ws.onerror = function () { that.__onError.apply(that, arguments); };
}
else {
throw "No WebSocket module";
}
// default values for clock sync times
this.__clockSyncPings = 8;
}
, __onOpen: function () {
//this.startClockSync();
}
, __onError: function (event) {
// console.log('error', event);
}
, __onClose: function (event) {
nervousTimer.clearAll();
if ( this.syncTimer ) {
clearTimeout(this.syncTimer);
this.syncTimer = undefined;
}
this.onclose();
if ( event.code ) {
if ( event.code === 4001 ) {
// ColorSync session is timed out expectedly.
alert('ColorSync is finished');
storage.deleteKey( 'lastSession:' + this.server );
}
else if ( event.code === 4401 ) {
// Authentication failed
alert('Token was wrong. please refresh ths page and try again');
}
else if ( event.code === 4410 ) {
// Ticket is already used.
alert('This ticket is already checked in.');
}
else if ( event.code === 4411 ) {
// Ticket is already used.
alert('This ticket is already checked in.');
storage.deleteKey( 'lastSession:' + this.server );
}
else if ( event.code === 4402 ) {
// Ticket is already used.
alert('This ticket is already used by other client.');
}
else if ( event.code === 4503 ) {
alert('Reception is closed now.');
}
else {
// Unexpected close. Retry.
// console.log( 'failed to start colorsync: ', event.code, event.reason );
var that = this;
setTimeout( function () {
// console.log('retrying');
that.init();
}, this._retryTimeout );
this._retryTimeout *= 2;
}
}
}
, __onMessage: function (msg) {
var data = JSON.parse(msg.data);
var meth = 'on_msg_' + data.type;
if ( 'undefined' !== typeof this[meth] ) {
this[meth].call( this, data );
}
else {
throw 'unknown message "' + data.type + '"';
}
}
, checkIn: function (aid) {
this.ws.send(JSON.stringify({ type: 'checkin', aid: aid }));
}
, startClockSync: function(cb) {
this._clockSyncResults = [];
var that = this;
for ( var i = 0; i < this.__clockSyncPings; i++ ) {
setTimeout( function () {
that.clockSync();
}, 100 * i );
}
}
, clockSync: function () {
var origin = (new Date()).getTime();
var msg = JSON.stringify({ type: 'clock', origin: origin });
this.ws.send(msg);
}
, on_msg_clock: function (res) {
var now = (new Date()).getTime();
var latency = ((now - res.origin ) / 2 );
var delay = res.time + latency - now;
this._clockSyncResults.push([latency, delay]);
this.oninitstep(this._clockSyncResults.length);
if ( this._clockSyncResults.length >= this.__clockSyncPings ) {
this._finalizeClockSync();
}
}
, _finalizeClockSync: function () {
this._clockSyncResults.sort( function (a,b) {
return a[0] - b[0];
});
this.delay = this._clockSyncResults[0][1];
this.onClockFixed();
// Re-sync timer for each 3 mins
var that = this;
setTimeout( function () {
that.startClockSync();
}, 1000 * 60 * 3);
}
, onClockFixed: function () {
var msg = JSON.stringify({ type: 'sync'});
this.ws.send(msg);
}
, on_msg_welcome: function (res) {
var lastSession = {
resume: res.resume,
end: res.end
};
storage.setValue( 'lastSession:' + this.server, JSON.stringify(lastSession) );
this.onwelcome(res);
this.startClockSync();
}
, on_msg_join: function (res) {
this._retryTimeout = 1000;
if ( res.checkin_token ) {
storage.setValue( 'checkInToken:' + this.server, res.checkin_token );
}
this.onjoin(res);
}
, on_msg_leave: function (res) {
this.onleave(res);
}
, on_msg_sync: function (res) {
nervousTimer.clearAll();
var nextSync = res.seeds[1].interval * res.seeds[1].limit * 0.75;
var that = this;
this.syncTimer = setTimeout( function () {
var msg = JSON.stringify({ type: 'sync'});
that.ws.send(msg);
}, nextSync);
var now = ( new Date() ).getTime();
for ( var s = 0; s < res.seeds.length; s++) {
var seed = res.seeds[s];
var xors = new XorShift( seed.seed );
for ( var i = 0; i < seed.limit; i++ ) {
(function () {
var v = xors.next();
var bg = numberToColor(v);
var origin = seed.origin;
var idx = i;
var interval = seed.interval;
nervousTimer.setTimeout( function () {
colorsync(bg);
},
function (a,c) {
//console.log(' oops skipped ', a, c );
},
idx * interval, origin - that.delay);
})();
}
}
if ( res.fadeto ) {
setTimeout( function () {
$('.colorsync').stop().animate({ backgroundColor: res.fadeto }, 1000);
}, 10);
}
this.onsynchronize(res);
}
};
ColorSyncClient.hasSessionFor = function ( uri ) {
return storage.getValue('lastSession:' + uri) ? true : false;
};
ColorSyncClient.clearSessionFor = function ( uri ) {
storage.deleteKey( 'lastSession:' + uri );
};
ColorSyncClient.storage = storage;
if( 'undefined' !== typeof ColorSyncClientAndroid &&
'undefined' !== typeof ColorSyncClientAndroid.getKnownLocation ) {
ColorSyncClient.locationManager = ColorSyncClientAndroid;
}
if ( typeof module !== 'undefined') {
module.exports = ColorSyncClient;
}
if ( typeof window !== 'undefined' ) {
window.ColorSyncClient = ColorSyncClient;
}
})();