-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathcomms.js
164 lines (139 loc) · 5.13 KB
/
comms.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
var Comms = (function() {
var self = {};
var sock;
var fmt = d3.format("0,000");
var clickTimes = new Array(61);
var timeoutId;
$('#loading-indicator').show();
connectToWebsocket();
function connectToWebsocket() {
var redditRequester = new XMLHttpRequest();
redditRequester.onreadystatechange = function () {
if (redditRequester.readyState !== 4) {
return;
}
var websocketURL;
if (redditRequester.status !== 200) {
console.error("Something went wrong, reconnecting in 5 seconds!");
setTimeout(connectToWebsocket, 5000);
return;
}
var regex = /"(wss:\/\/wss\.redditmedia\.com\/thebutton\?h=[^"]*)"/g;
websocketURL = regex.exec(redditRequester.responseText)[1];
console.log("Connecting to: " + websocketURL);
sock = new WebSocket(websocketURL);
sock.onmessage = tick;
};
// Use CORS proxy by lezed1 to get the Reddit homepage!
redditRequester.open("get", "//cors-unblocker.herokuapp.com/get?url=https%3A%2F%2Fwww.reddit.com%2Fr%2Fthebutton", true);
redditRequester.send();
}
function reconnectToWebsocket() {
sock.close();
data.push({seconds_left: -.01});
Chart.render(data);
Timer.sync("0");
$('#loading-indicator').show();
connectToWebsocket();
}
// Set initial values of clickTimes
clickTimes.total = 0;
for (var i = 0; i < clickTimes.length; i++) {
clickTimes[i] = 0;
};
function tick(evt) {
$('#loading-indicator').hide();
$('#stats').show();
Stats.start = Stats.start || moment();
// {"type": "ticking", "payload": {"participants_text": "585,177", "tick_mac": "362a88a8ae0a89c909395f587e329992c656b4d8", "seconds_left": 59.0, "now_str": "2015-04-04-23-44-42"}}
var packet = JSON.parse(evt.data);
if (packet.type != "ticking") {
return;
}
// Reconnect if no information is recieved for 10 seconds.
clearTimeout(timeoutId);
timeoutId = setTimeout(reconnectToWebsocket, 10000);
packet.payload.now = moment(packet.payload.now_str + " 0000", "YYYY-MM-DD-HH-mm-ss Z");
Stats.lag = d3.format("0,000")(packet.payload.now - moment());
packet.payload.participants = parseInt(packet.payload.participants_text.replace(/[^0-9]/g, ''))
var last = _.last(data);
if (data.length > 0 && packet.payload.seconds_left >= last.seconds_left && last.seconds_left >= 0 && packet.payload.participants !== last.participants) {
last.is_click = true;
last.clicks = packet.payload.participants - last.participants;
Stats.clicks += last.clicks;
var total_resets = _.filter(data, 'is_click').length;
var last_time = last.seconds_left;
// Update color stats
if (last_time <= 11) {
Stats.total_reds += last.clicks;
}
else if (last_time <= 21) {
Stats.total_oranges += last.clicks;
}
else if (last_time <= 31) {
Stats.total_yellows += last.clicks;
}
else if (last_time <= 41) {
Stats.total_greens += last.clicks;
}
else if (last_time <= 51) {
Stats.total_blues += last.clicks;
}
else if (last_time <= 60) {
Stats.total_purples += last.clicks;
}
clickTimes[last_time] += 1;
clickTimes.total += 1;
// Find the median value, or the lower of the two needed to be averaged
var current_index = 0;
var values_left = Math.floor((clickTimes.total + 1) / 2);
while (clickTimes[current_index] < values_left) {
values_left -= clickTimes[current_index];
current_index += 1;
}
if (clickTimes[current_index] === values_left && (clickTimes.total % 2) === 0) {
var next_index = current_index + 1;
while (clickTimes[next_index] === 0) {
next_index += 1;
}
Stats.median_click_time = (current_index + next_index) / 2;
}
else {
Stats.median_click_time = current_index;
}
// Update percentages
Stats.purple_percentage = "(" + (100.0 * Stats.total_purples / Stats.clicks).toFixed(3) + "%)";
Stats.blue_percentage = "(" + (100.0 * Stats.total_blues / Stats.clicks).toFixed(3) + "%)";
Stats.green_percentage = "(" + (100.0 * Stats.total_greens / Stats.clicks).toFixed(3) + "%)";
Stats.yellow_percentage = "(" + (100.0 * Stats.total_yellows / Stats.clicks).toFixed(3) + "%)";
Stats.orange_percentage = "(" + (100.0 * Stats.total_oranges / Stats.clicks).toFixed(3) + "%)";
Stats.red_percentage = "(" + (100.0 * Stats.total_reds / Stats.clicks).toFixed(3) + "%)";
// Update lowest time if needed
if (Stats.lowest_time > last_time) {
Stats.lowest_time = last_time;
}
// Update average time
Stats.sum_of_times += last_time;
Stats.average_time = (1.0 * Stats.sum_of_times / total_resets).toFixed(3);
// Resets per minute
Stats.resets_per_minute = (60.0 * total_resets / data.length).toFixed(3);
}
else if (data.length > 0 && last.seconds_left < 0) {
last.is_click = true;
}
data.push(packet.payload);
Stats.ticks = fmt(data.length);
Stats.participants = packet.payload.participants_text;
Chart.render(data);
Timer.sync(packet.payload.seconds_left);
Stats.render();
$('#settings').show();
Settings.render();
}
self.resize = function() {
$('#loading-indicator')
.css('left', ($(window).width() - $('#loading-indicator').width()) / 2)
.css('top', ($(window).height() - $('#loading-indicator').height()) * (1/3));
}
return self;
}())