Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for clock with quarantine #145

Merged
merged 3 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions public/js/clock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
let current_schedule = "regular";
let schedules, logo;
let period_names = {black:[], silver:[]};

Expand All @@ -16,6 +15,15 @@ large_ctx.translate(large_radius, large_radius);

logo = document.getElementById("logo");

// Controls whether to use the covid-19 schedule or the regular schedule
const covid_schedule = true;
let current_schedule = covid_schedule ? "covid" : "regular";

// For testing.
// If this is set to a valid date/time string, that will be used instead of the
// current date and time.
let date_override = undefined;

let schedulesCallback = function(response) {
schedules = response;
redraw_clock();
Expand Down Expand Up @@ -104,15 +112,20 @@ function fitText(ctx, text, fontface, width) {
}

function update_lunch() {
switch(Number(document.getElementById("lunch_range").value)) {
case 0:
current_schedule = "regular-a";
break;
case 1:
current_schedule = "regular-b";
break;
case 2:
current_schedule = "regular-c";
if (covid_schedule) {
current_schedule = "covid";
}
else {
switch(Number(document.getElementById("lunch_range").value)) {
case 0:
current_schedule = "regular-a";
break;
case 1:
current_schedule = "regular-b";
break;
case 2:
current_schedule = "regular-c";
}
}
redraw_clock();
}
Expand Down Expand Up @@ -158,54 +171,50 @@ function get_period_name(default_name) {
period_names.silver.push(currentTableData.schedule.silver[i]);
}
}
// Guess lunch
current_schedule = get_schedule(period_names.black[2].room, period_names.black[2].id);
// Guess lunch if there is a period 3 and we are not following the
// covid schedule
if (!covid_schedule && period_names.black[2]) {
current_schedule = get_schedule(period_names.black[2].room, period_names.black[2].id);
}
}
let bs_day = document.getElementById("schedule_title").innerHTML.toLowerCase();
// period_names has class names now
let index = Number(default_name.charAt(default_name.length - 1)) - 1;
if(isNaN(index)) {
if(isNaN(index) || !period_names[bs_day][index]) {
return default_name;
}
return period_names[bs_day][index].name;
}

function school_day() {
let now = new Date();
if (now.getDay() % 6 === 0) { // If it's a weekend
return false;
}
return true;
}

function redraw_clock() {
// Fake call to get_period_name to set current_schedule
get_period_name("Period 1");
// UTC to EST
let number = 0;
let period_name = "";
// let now = Date.now() - 5 * 60 * 60 * 1000;
let now = Date.now() - 4 * 60 * 60 * 1000;
let tod = now % (24 * 60 * 60 * 1000);
const now = date_override ? new Date(date_override) : new Date();
// Time of day
const tod = now.getHours() * 60 * 60 * 1000
+ now.getMinutes() * 60 * 1000
+ now.getSeconds() * 1000
+ now.getMilliseconds();
let pos;
if (school_day()) {
// let tod = 41399000; // Simulate time

if (![0, 6].includes(now.getDay())) {
// School day
let current_period_i = 0;// Get current period from array
while (current_period_i < schedules[current_schedule].length - 1 &&
tod > schedules[current_schedule][current_period_i + 1].start) {
current_period_i++;
}

let current_period = schedules[current_schedule][current_period_i];
let next_period = schedules[current_schedule][current_period_i + 1];
const current_period = schedules[current_schedule][current_period_i];
const next_period = schedules[current_schedule][current_period_i + 1];

if(tod < current_period.start) { // Before school
if (tod < current_period.start) { // Before school
period_name = "Before School";
pos = tod / current_period.start;
number = current_period.start - tod;
}
else if(!next_period && tod > current_period.end) { // After school
else if (!next_period && tod > current_period.end) { // After school
// Realtime
period_name = "";
pos = tod % (12 * 60 * 60 * 1000) / (12 * 60 * 60 * 1000);
Expand All @@ -214,7 +223,7 @@ function redraw_clock() {
number += 12 * 60 * 60 * 1000;
}
}
else if(tod > current_period.end) { // Between classes
else if (tod > current_period.end) { // Between classes
period_name = get_period_name(current_period.name) +
" ➡ " + get_period_name(next_period.name);
pos = (tod - current_period.end) / (next_period.start - current_period.end);
Expand Down Expand Up @@ -248,4 +257,3 @@ function redraw_clock() {
drawHand(large_ctx, large_radius, pos, large_radius * .94, large_radius * .095);
drawNumber(large_ctx, large_radius, pos, number);
}

27 changes: 27 additions & 0 deletions public/schedule.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,32 @@
"start": 47400000,
"end": 52200000
}
],
"covid":[
{
"name":"Period 1",
"start": 33000000,
"end": 36000000
},
{
"name":"Period 2",
"start": 36600000,
"end": 39600000
},
{
"name":"Period 3",
"start": 40200000,
"end": 43200000
},
{
"name":"Lunch",
"start": 43200000,
"end": 46800000
},
{
"name":"Period 4",
"start": 46800000,
"end": 49800000
}
]
}