-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown.js
318 lines (265 loc) · 7.4 KB
/
countdown.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
if (typeof String.prototype.contains !== "function") {
String.prototype.contains = function contains() {
return String.prototype.indexOf.apply(this, arguments) !== -1;
}
}
if (typeof Date.now !== "function") {
Date.now = function now() {
return +new Date();
}
}
// constants and stuff
var NEW_YEAR = 0,
TERM_1 = 1,
HOLIDAYS_AUTUMN = 2,
TERM_2 = 3,
HOLIDAYS_WINTER = 4,
TERM_3 = 5,
HOLIDAYS_SPRING = 6,
TERM_4 = 7,
CHRISTMAS = 8;
var SOME_TERM = -1; // not sure about term dates
// gotta initialise those bells
var bells = [
// Monday
{
"hours": [ 8, 8, 9, 10, 10, 11, 12, 13, 14, 14],
"minutes": [40, 50, 41, 32, 51, 42, 33, 13, 04, 55],
"desc": ["Rollcall", 1, 2, "Recess", 3, 4, "Lunch", 5,6, "School Ends"]
}];
// Tuesday
bells.push({
"hours": bells[0].hours.slice(),
"minutes": bells[0].minutes.slice(),
"desc": bells[0].desc.slice()
});
// Wednesday
bells.push({
"hours": [ 8, 8, 9, 10, 10, 11, 12, 12, 14],
"minutes": [40, 50, 32, 14, 36, 18, 00, 40, 30],
"desc": ["Rollcall", 1, 2, "Recess", 3,4, "Lunch", "Sport", "School ends"]
});
// Thursday
bells.push({
"hours": [ 8, 8, 9, 10, 10, 11, 12, 13, 14],
"minutes": [40, 50, 41, 32, 51, 42, 33, 13, 04],
"desc": ["Rollcall", 1, 2, "Recess", 3, 4, "Lunch", 5, "School Ends"]
});
// Friday
bells.push({
"hours": bells[0].hours.slice(),
"minutes": bells[0].minutes.slice(),
"desc": bells[0].desc.slice()
});
// extension classes, yay...
function addExtClasses() {
if (window.localStorage && localStorage.useTimetable) {
var days = JSON.parse(localStorage.days);
for (var day = 0; day < 5; day++) {
var dayEvents = bells[day];
var classes = days[day];
if (classes && classes[0] && classes[0].classId != -2) {
// P0 aka morning class
dayEvents.hours.unshift(7);
dayEvents.minutes.unshift(30);
dayEvents.desc.unshift(0);
}
if (classes && classes[5] && classes[5].classId != -2) {
// P5 aka afternoon class
dayEvents.hours.push(4);
dayEvents.minutes.push(0);
dayEvents.desc.splice(dayBells.desc.length - 1, 0, 5);
}
}
}
}
addExtClasses();
// halp what am I even doing
$.ajaxSetup({ timeout: 5000 }); // make it usable
function BellEvent(day, eventNo) {
var dayEvents;
if (day.constructor === Date) {
dayEvents = bells[day.getDay() - 1];
this.holidays = true;
} else {
dayEvents = bells[day];
}
this.day = day;
this.eventNo = eventNo;
this.hour = dayEvents.hours[eventNo];
this.minute = dayEvents.minutes[eventNo];
this._desc = dayEvents.desc[eventNo];
}
BellEvent.prototype.getDesc = function getDesc() {
if (typeof this._desc === "number") {
var pNum = this._desc;
var desc = "Period " + pNum;
if (window.localStorage && localStorage.useTimetable) {
var day = this.holidays ? this.day.getDay() - 1 : this.day;
var todayClasses = JSON.parse(localStorage.days)[day];
var classes = JSON.parse(localStorage.classes);
if (classes && classes.length && todayClasses && todayClasses.length >= pNum) {
var period = todayClasses[pNum];
if (period && typeof period.classId === "number") {
if (period.classId === -1) {
// study period
return "Period " + pNum + " (study)";
}
if (period.classId === -2) {
// extension period, but we don't have class
// this should never happen.
return "Period " + pNum + "!?";
}
var subject = classes[period.classId];
var room = period.room;
if (subject) {
room = room || subject.room;
if (subject.subjectName) {
desc = "Period " + pNum + " - " + subject.subjectName;
}
}
if (room) {
desc += " (" + room + ")";
}
}
}
}
return desc;
}
if (this.holidays) {
return "School Starts";
}
return this._desc;
}
BellEvent.prototype.getDate = function getDate() {
var date;
if (this.day.constructor === Date) {
date = new Date(this.day);
} else {
date = new Date();
var weekday = date.getDay();
if (weekday === 6) {
// Saturday today, wrap to Monday
date.setDate(date.getDate() + 2);
} else if (weekday === this.day) {
// event is tomorrow
date.setDate(date.getDate() + 1);
} else if (weekday === 5 && !this.day) {
// Friday after school, event is Monday
date.setDate(date.getDate() + 3);
}
}
date.setHours(this.hour);
date.setMinutes(this.minute);
date.setSeconds(secsOffset);
return date;
};
BellEvent.getNext = function getNext() {
var now = new Date();
now.setSeconds(now.getSeconds() + secsOffset - 10);
var term = getTerm(now);
if (!(term & 1)) {
// holidays, yay
return new this(terms[term], 0);
}
var day = now.getDay() - 1;
var nowH = now.getHours(), nowM = now.getMinutes();
var dayEvents = bells[day], eventNo = 0;
var lastEvNo = dayEvents && dayEvents.hours.length - 1;
if (day === -1 || day === 5) {
// weekend, wrap around to Monday
day = 0;
} else if (nowH > dayEvents.hours[lastEvNo] || (nowH === dayEvents.hours[lastEvNo] && nowM > dayEvents.minutes[lastEvNo])) {
// past the school day, wrap to next morning
day++;
if (day === 5) {
// Friday after school, wrap to Monday
day = 0;
}
} else {
// calculate next event
while (eventNo < lastEvNo && (nowH > dayEvents.hours[eventNo] || (nowH == dayEvents.hours[eventNo] && nowM > dayEvents.minutes[eventNo]))) {
eventNo++;
}
}
return new this(day, eventNo);
}
function updateCountdown(event) {
var format = "%-Mm %Ss";
if (event.offset.totalDays) {
// some days left, show days *and* hours
format = "%-Dd %-Hh " + format;
} else if (event.offset.hours) {
// some hours left, show hours
format = "%-Hh " + format;
}
$(this).text(event.strftime(format));
}
function finishCountdown() {
$(this).text("... about now.");
// set the new countdown after half a minute
setTimeout(theFinalCountdown, 30000);
}
// https://youtu.be/9jK-NcRmVcw
function theFinalCountdown() {
setCountdown(BellEvent.getNext());
}
function setCountdown(ev) {
$("#bell-countdown").countdown(ev.getDate())
.on("update.countdown", updateCountdown)
.on("finish.countdown", finishCountdown);
$("#bell-descript").text(ev.getDesc());
if (ev.eventNo > 0 && typeof ev.day === "number") {
$("#bell-current").text("Now: " + new BellEvent(ev.day, ev.eventNo - 1).getDesc()).show();
} else {
$("#bell-current").hide();
}
}
// school computers don't even know what time it is
// amount of time system clock is ahead by
var secsOffset = 39;
function getRealTime() {
$.ajax({
url: "http://vovo.id.au/scripts/time.php",
async: false,
dataType: "text",
success: function (data) {
secsOffset = (Date.now()/1000>>>0) - data;
}
});
}
// oh god holidays
function parseTerms(data) {
var termDates = data.results.termDates;
var terms = [];
for (var i = 1, term; term = termDates[i]; i++) {
// we only care for students in the eastern division
if (term.title.contains(" for students (Eastern ")) {
terms.push(new Date(term.start));
var end = new Date(term.end);
var endEvents = bells[end.getDay() - 1];
end.setHours(endEvents.hours[9]);
end.setMinutes(endEvents.minutes[9]);
terms.push(end);
}
}
window.terms = terms;
}
function getTerm(date) {
if (!window.terms) {
// we have no idea when the terms are, bail
return SOME_TERM;
}
var time = +date;
for (var i = 0; i < 8; i++) {
if (time < terms[i]) {
return i;
}
}
return CHRISTMAS;
}
$(function () {
getRealTime();
$.getJSON("https://www.kimonolabs.com/api/8puk29vu?apikey=CynVJv6skGTKh5o5Q2CDEmWo1ix62b75", parseTerms)
.always(theFinalCountdown);
});