From a2d7a61c696f9fb8ccdd414c21509519df37300f Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Thu, 3 Jun 2021 00:28:52 -0600 Subject: [PATCH 1/7] Add option to prevent access to options early --- common.js | 98 +++++++++++++++++++++++++++++++++++++++++----------- options.html | 4 +++ options.js | 16 +++++++++ 3 files changed, 97 insertions(+), 21 deletions(-) diff --git a/common.js b/common.js index 0a40d84..c80c004 100644 --- a/common.js +++ b/common.js @@ -42,6 +42,8 @@ const PER_SET_OPTIONS = { reloadSecs: { type: "string", def: "", id: "reloadSecs" }, allowOverride: { type: "boolean", def: false, id: "allowOverride" }, prevOpts: { type: "boolean", def: false, id: "prevOpts" }, + prevOffToggle: { type: "boolean", def: false, id: "prevOffToggle"}, + prevOffset: { type: "string", def: "", id: "prevOffset"}, prevGenOpts: { type: "boolean", def: false, id: "prevGenOpts" }, prevAddons: { type: "boolean", def: false, id: "prevAddons" }, prevSupport: { type: "boolean", def: false, id: "prevSupport" }, @@ -316,15 +318,30 @@ function getMinPeriods(times) { return minPeriods; } -// Clean time periods +// Convert minute periods to a string // -function cleanTimePeriods(times) { - // Convert to minute periods - let minPeriods = getMinPeriods(times); - if (minPeriods.length == 0) { - return ""; // nothing to do +function minPeriodsToString(minPeriods) { + // Convert back to string list of time periods + let cleanTimes = []; + for (let mp of minPeriods) { + let h1 = Math.floor(mp.start / 60); + let m1 = (mp.start % 60); + let h2 = Math.floor(mp.end / 60); + let m2 = (mp.end % 60); + let period = + ((h1 < 10) ? "0" : "") + h1 + + ((m1 < 10) ? "0" : "") + m1 + + "-" + + ((h2 < 10) ? "0" : "") + h2 + + ((m2 < 10) ? "0" : "") + m2; + cleanTimes.push(period); } + return cleanTimes.join(","); +} +// Trim, merge and sort minute periods +// +function processMinPeriods(minPeriods) { // Step 1: Fix any times > 2400 for (let mp of minPeriods) { mp.start = Math.min(mp.start, 1440); @@ -353,22 +370,21 @@ function cleanTimePeriods(times) { } } - // Convert back to string list of time periods - let cleanTimes = []; - for (let mp of minPeriods) { - let h1 = Math.floor(mp.start / 60); - let m1 = (mp.start % 60); - let h2 = Math.floor(mp.end / 60); - let m2 = (mp.end % 60); - let period = - ((h1 < 10) ? "0" : "") + h1 + - ((m1 < 10) ? "0" : "") + m1 + - "-" + - ((h2 < 10) ? "0" : "") + h2 + - ((m2 < 10) ? "0" : "") + m2; - cleanTimes.push(period); + return minPeriods; +} + +// Clean time periods +// +function cleanTimePeriods(times) { + // Convert to minute periods + let minPeriods = getMinPeriods(times); + if (minPeriods.length == 0) { + return ""; // nothing to do } - return cleanTimes.join(","); + + const cleanPeriods = processMinPeriods(minPeriods); + + return minPeriodsToString(cleanPeriods); } // Calculate start of time period from current time and time limit period @@ -404,6 +420,46 @@ function getTimePeriodStart(now, limitPeriod, limitOffset) { return 0; } +// Convert times to minute periods with adjusted start times +// +function getExtendedMinPeriods(times, startOffset) { + const fullDay = 24 * 60; + const offset = Math.max(0, Math.min(fullDay - 1, parseInt(startOffset))); + //(startOffset < 0 || startOffset >= fullDay) ? 0 : startOffset; + + // Convert input string to array + const minPeriods = getMinPeriods(times); + if (minPeriods.length == 0) { + return ""; // nothing to do + } + + // Merge and sort MPs + const mergedMPs = processMinPeriods(minPeriods); + + // Skip adjustment if offset is 0/NaN/null/etc + if (!startOffset || !offset) { + return mergedMPs; + } + + // Offset start of MPs + const offsetMPs = mergedMPs.reduce((newMPs, mp) => { + mp.start -= offset; + // Wrap MPs. This will act strangely on days preceding/following an unblocked day + if (mp.start < 0) { + newMPs.push({ + start: mp.start + fullDay, + end: fullDay + }); + mp.start = 0; + } + newMPs.push(mp); + return newMPs; + }, []); + + // Return merged and sorted MPs + return processMinPeriods(offsetMPs); +} + // Format a time in seconds to HH:MM:SS format // function formatTime(secs) { diff --git a/options.html b/options.html index f210259..fcb6390 100644 --- a/options.html +++ b/options.html @@ -209,6 +209,10 @@

+

+ + +

diff --git a/options.js b/options.js index 6da60e0..8c2c98c 100644 --- a/options.js +++ b/options.js @@ -127,6 +127,7 @@ function saveOptions(event) { let delaySecs = $(`#delaySecs${set}`).val(); let reloadSecs = $(`#reloadSecs${set}`).val(); let blockURL = $(`#blockURL${set}`).val(); + let optBlockOffset = $(`#prevOffset${set}`).val(); // Check field values if (!checkTimePeriodsFormat(times)) { @@ -166,6 +167,12 @@ function saveOptions(event) { $("#alertBadBlockURL").dialog("open"); return false; } + if (!checkPosIntFormat(optBlockOffset)) { + $("#tabs").tabs("option", "active", (set - 1)); + $(`#prevOffset${set}`).focus(); + $("#alertBadSeconds").dialog("open"); + return false; + } } // Check format for text fields in general options @@ -393,10 +400,19 @@ function retrieveOptions() { let periodStart = getTimePeriodStart(now, limitPeriod, limitOffset); let conjMode = options[`conjMode${set}`]; let days = options[`days${set}`]; + let blockOffToggle = options[`prevOffToggle${set}`]; + let blockOffset = options[`prevOffset${set}`]; // Check day let onSelectedDay = days[timedate.getDay()]; + if(blockOffToggle && blockOffset) { + minPeriods = getExtendedMinPeriods(times, blockOffset); + times = minPeriodsToString(minPeriods); + + limitMins = limitMins ? limitMins - blockOffset : limitMins; + } + // Check time periods let withinTimePeriods = false; if (onSelectedDay && times) { From 95bf4f1ca10550fc66e706dcac72e638db00b207 Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Fri, 11 Jun 2021 01:20:24 -0600 Subject: [PATCH 2/7] Update/fix early options blocking --- common.js | 40 ---------------------------- options.html | 2 +- options.js | 73 +++++++++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 68 deletions(-) diff --git a/common.js b/common.js index c80c004..966c9ee 100644 --- a/common.js +++ b/common.js @@ -420,46 +420,6 @@ function getTimePeriodStart(now, limitPeriod, limitOffset) { return 0; } -// Convert times to minute periods with adjusted start times -// -function getExtendedMinPeriods(times, startOffset) { - const fullDay = 24 * 60; - const offset = Math.max(0, Math.min(fullDay - 1, parseInt(startOffset))); - //(startOffset < 0 || startOffset >= fullDay) ? 0 : startOffset; - - // Convert input string to array - const minPeriods = getMinPeriods(times); - if (minPeriods.length == 0) { - return ""; // nothing to do - } - - // Merge and sort MPs - const mergedMPs = processMinPeriods(minPeriods); - - // Skip adjustment if offset is 0/NaN/null/etc - if (!startOffset || !offset) { - return mergedMPs; - } - - // Offset start of MPs - const offsetMPs = mergedMPs.reduce((newMPs, mp) => { - mp.start -= offset; - // Wrap MPs. This will act strangely on days preceding/following an unblocked day - if (mp.start < 0) { - newMPs.push({ - start: mp.start + fullDay, - end: fullDay - }); - mp.start = 0; - } - newMPs.push(mp); - return newMPs; - }, []); - - // Return merged and sorted MPs - return processMinPeriods(offsetMPs); -} - // Format a time in seconds to HH:MM:SS format // function formatTime(secs) { diff --git a/options.html b/options.html index fcb6390..e296560 100644 --- a/options.html +++ b/options.html @@ -211,7 +211,7 @@

- +

diff --git a/options.js b/options.js index 8c2c98c..12babe8 100644 --- a/options.js +++ b/options.js @@ -393,7 +393,7 @@ function retrieveOptions() { // Get options let timedata = options[`timedata${set}`]; let times = options[`times${set}`]; - let minPeriods = getMinPeriods(times); + let minPeriods = processMinPeriods(getMinPeriods(times)); let limitMins = options[`limitMins${set}`]; let limitPeriod = options[`limitPeriod${set}`]; let limitOffset = options[`limitOffset${set}`]; @@ -404,42 +404,61 @@ function retrieveOptions() { let blockOffset = options[`prevOffset${set}`]; // Check day - let onSelectedDay = days[timedate.getDay()]; - - if(blockOffToggle && blockOffset) { - minPeriods = getExtendedMinPeriods(times, blockOffset); - times = minPeriodsToString(minPeriods); - - limitMins = limitMins ? limitMins - blockOffset : limitMins; - } - - // Check time periods - let withinTimePeriods = false; - if (onSelectedDay && times) { - // Check each time period in turn - for (let mp of minPeriods) { - if (mins >= mp.start && mins < mp.end) { - withinTimePeriods = true; + let currentDay = timedate.getDay(); + let onSelectedDay = days[currentDay]; + + const useBlockTimes = Boolean(times); + const useTimeLimit = Boolean(limitMins && limitPeriod); + + // Check if any special lock scheme is used + const lockScheme = Boolean(blockOffToggle && blockOffset); + let locked = false; + if (lockScheme) { + const fullDay = 24 * 60; // 24 hours in minutes + const startOffset = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; + const lockedMPs = minPeriods.map(mp => { return { start: mp.start - startOffset, end: mp.end } }); + const unlockedMins = limitMins - blockOffset; + + const tomorrowSelected = days[(currentDay + 1) % 7]; + // Negative start times should affect the previous day. + const timePeriodLock = (onSelectedDay && lockedMPs.some(mp => mins >= mp.start && mins < mp.end)) || + (tomorrowSelected && lockedMPs.some(mp => (mins - fullDay) >= mp.start && (mins - fullDay) < mp.end)); + + const timeLimitLock = useTimeLimit && onSelectedDay && unlockedMins && + (unlockedMins < 0 || (timedata[2] == periodStart && timedata[3] >= (unlockedMins * 60))); + + locked = (!conjMode && (timePeriodLock || timeLimitLock)) || + (conjMode && (timePeriodLock && timeLimitLock)); + } else { + // Check time periods + let withinTimePeriods = false; + if (useBlockTimes && onSelectedDay) { + // Check each time period in turn + for (let mp of minPeriods) { + if (mins >= mp.start && mins < mp.end) { + withinTimePeriods = true; + } } } - } - // Check time limit - let afterTimeLimit = false; - if (onSelectedDay && limitMins && limitPeriod) { - // Check time period and time limit - if (timedata[2] == periodStart && timedata[3] >= (limitMins * 60)) { - afterTimeLimit = true; + // Check time limit + let afterTimeLimit = false; + if (useTimeLimit && onSelectedDay) { + // Check time period and time limit + if (timedata[2] == periodStart && timedata[3] >= (limitMins * 60)) { + afterTimeLimit = true; + } } + + locked = (!conjMode && (withinTimePeriods || afterTimeLimit)) + || (conjMode && (withinTimePeriods && afterTimeLimit)); } // Check lockdown condition let lockdown = (timedata[4] > now); // Disable options if specified block conditions are fulfilled - if (lockdown - || (!conjMode && (withinTimePeriods || afterTimeLimit)) - || (conjMode && (withinTimePeriods && afterTimeLimit))) { + if (lockdown || locked) { if (options[`prevOpts${set}`]) { gNumSetsMin = set; // Disable options for this set From eb043b2a5951145b4d59dafffdfe071eb39f0d37 Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Wed, 23 Jun 2021 21:15:33 -0600 Subject: [PATCH 3/7] Updated option blocking and code style --- common.js | 2 +- options.js | 71 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/common.js b/common.js index 966c9ee..6ab05be 100644 --- a/common.js +++ b/common.js @@ -346,7 +346,7 @@ function processMinPeriods(minPeriods) { for (let mp of minPeriods) { mp.start = Math.min(mp.start, 1440); mp.end = Math.min(mp.end, 1440); - } + } // Step 2: Remove any periods without +ve duration for (let i = 0; i < minPeriods.length; i++) { diff --git a/options.js b/options.js index 12babe8..ec354ca 100644 --- a/options.js +++ b/options.js @@ -407,28 +407,63 @@ function retrieveOptions() { let currentDay = timedate.getDay(); let onSelectedDay = days[currentDay]; - const useBlockTimes = Boolean(times); - const useTimeLimit = Boolean(limitMins && limitPeriod); + // Check inputs + let useBlockTimes = times; + let useTimeLimit = limitMins && limitPeriod && ((limitMins * 60) < limitPeriod); - // Check if any special lock scheme is used - const lockScheme = Boolean(blockOffToggle && blockOffset); - let locked = false; + // Check option lock scheme + let lockScheme = blockOffToggle && blockOffset; + + // Change 'if(lockScheme)' to 'switch(lockScheme)' if more schemes are added + let optionsLocked = false; if (lockScheme) { const fullDay = 24 * 60; // 24 hours in minutes - const startOffset = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; - const lockedMPs = minPeriods.map(mp => { return { start: mp.start - startOffset, end: mp.end } }); - const unlockedMins = limitMins - blockOffset; - const tomorrowSelected = days[(currentDay + 1) % 7]; - // Negative start times should affect the previous day. - const timePeriodLock = (onSelectedDay && lockedMPs.some(mp => mins >= mp.start && mins < mp.end)) || - (tomorrowSelected && lockedMPs.some(mp => (mins - fullDay) >= mp.start && (mins - fullDay) < mp.end)); + // Get required info + let startOffset = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; + let tomorrowSelected = days[(currentDay + 1) % 7]; + + // Check time period proximity + let nearTimePeriods = false; + for (let mp of minPeriods) { + if (onSelectedDay) { + if (mins >= (mp.start - startOffset) && mins <= mp.end) { + nearTimePeriods = true; + } + } - const timeLimitLock = useTimeLimit && onSelectedDay && unlockedMins && - (unlockedMins < 0 || (timedata[2] == periodStart && timedata[3] >= (unlockedMins * 60))); + if (tomorrowSelected) { + // Check across day boundary + let shiftedMins = mins - fullDay; + if (shiftedMins >= (mp.start - startOffset) && shiftedMins <= mp.end) { + nearTimePeriods = true; + } + } + } + + // Check 'selected day' proximity + let checkTomorrow = ((now / 60) - fullDay + startOffset) > 0; + let checkTimeLimit = onSelectedDay || (checkTomorrow && tomorrowSelected); + + // Check time limit proximity + let nearTimeLimit = false; + if (useTimeLimit && checkTimeLimit) { + // Check option-specific time limit + let maxUnlockTime = limitMins - blockOffset; + if (maxUnlockTime > 0) { + let secondsLeft = maxUnlockTime * 60; + if (timedata[2] == periodStart) { + secondsLeft = Math.max(0, secondsLeft - timedata[3]); + } + nearTimeLimit = secondsLeft <= 0; + } else { + // Done for simplicity, technically incorrect in certain situations + nearTimeLimit = true; + } + } - locked = (!conjMode && (timePeriodLock || timeLimitLock)) || - (conjMode && (timePeriodLock && timeLimitLock)); + optionsLocked = (!conjMode && (nearTimePeriods || nearTimeLimit)) || + (conjMode && (nearTimePeriods && nearTimeLimit)); } else { // Check time periods let withinTimePeriods = false; @@ -450,7 +485,7 @@ function retrieveOptions() { } } - locked = (!conjMode && (withinTimePeriods || afterTimeLimit)) + optionsLocked = (!conjMode && (withinTimePeriods || afterTimeLimit)) || (conjMode && (withinTimePeriods && afterTimeLimit)); } @@ -458,7 +493,7 @@ function retrieveOptions() { let lockdown = (timedata[4] > now); // Disable options if specified block conditions are fulfilled - if (lockdown || locked) { + if (lockdown || optionsLocked) { if (options[`prevOpts${set}`]) { gNumSetsMin = set; // Disable options for this set From 70ac0cc44b2f265b96ebd8d62fb50b4814dec48c Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Thu, 24 Jun 2021 01:32:59 -0600 Subject: [PATCH 4/7] Timelimit fix and small comment updates --- options.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/options.js b/options.js index ec354ca..8038b27 100644 --- a/options.js +++ b/options.js @@ -417,7 +417,8 @@ function retrieveOptions() { // Change 'if(lockScheme)' to 'switch(lockScheme)' if more schemes are added let optionsLocked = false; if (lockScheme) { - const fullDay = 24 * 60; // 24 hours in minutes + /** 24 hours in minutes */ + const fullDay = 24 * 60; // Get required info let startOffset = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; @@ -442,14 +443,15 @@ function retrieveOptions() { } // Check 'selected day' proximity - let checkTomorrow = ((now / 60) - fullDay + startOffset) > 0; + let checkTomorrow = (mins - fullDay + startOffset) > 0; let checkTimeLimit = onSelectedDay || (checkTomorrow && tomorrowSelected); // Check time limit proximity let nearTimeLimit = false; if (useTimeLimit && checkTimeLimit) { - // Check option-specific time limit + // Lock 'X' mins early ==> Reduce time limit by 'X' mins let maxUnlockTime = limitMins - blockOffset; + // Positive lock limit: normal behaviour if (maxUnlockTime > 0) { let secondsLeft = maxUnlockTime * 60; if (timedata[2] == periodStart) { @@ -457,7 +459,8 @@ function retrieveOptions() { } nearTimeLimit = secondsLeft <= 0; } else { - // Done for simplicity, technically incorrect in certain situations + // Negative lock limit: Complex* interactions with time periods/timedata/day borders + // *skipped for simplicity nearTimeLimit = true; } } From 80442e6d01f6a65b1588054a4d9701eaa73c736a Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Thu, 1 Jul 2021 03:51:33 -0600 Subject: [PATCH 5/7] Block prediction updated --- options.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/options.js b/options.js index 8038b27..3a24060 100644 --- a/options.js +++ b/options.js @@ -450,8 +450,9 @@ function retrieveOptions() { let nearTimeLimit = false; if (useTimeLimit && checkTimeLimit) { // Lock 'X' mins early ==> Reduce time limit by 'X' mins + let maxUnlockTime = limitMins - blockOffset; - // Positive lock limit: normal behaviour + // Time limit > lock offset: normal behaviour if (maxUnlockTime > 0) { let secondsLeft = maxUnlockTime * 60; if (timedata[2] == periodStart) { @@ -459,9 +460,62 @@ function retrieveOptions() { } nearTimeLimit = secondsLeft <= 0; } else { - // Negative lock limit: Complex* interactions with time periods/timedata/day borders + // Negative lock limit: Complex* interactions between time periods/timedata/day borders // *skipped for simplicity - nearTimeLimit = true; + nearTimeLimit = false; + // Possible: Check if future time limit contains time period start + //const midnight = new Date(timedate.getTime()); + //const midStamp = midnight.setHours(0, 0, 0, 0); + const intersect = (a, b) => Math.max(0, Math.min(a[1], b[1]) - Math.max(a[0], b[0])); + const limitSecs = limitMins * 60; + const predictionCutoff = now + (blockOffset * 60); + const mps = conjMode ? minPeriods : cleanTimePeriods(ALL_DAY_TIMES); + + for(let startTP = periodStart; !nearTimeLimit && (startTP <= predictionCutoff); startTP = getTimePeriodStart(+startTP + +limitPeriod, limitPeriod, limitOffset)) { + const nextTP = getTimePeriodStart(+startTP + +limitPeriod, limitPeriod, limitOffset); + const startTime = Math.max(startTP, now); // Don't check the past + const endTime = nextTP - 1; + + let thisDay = new Date(startTime * 1000); + let dayStart = thisDay.setHours(0, 0, 0, 0) / 1000; + + let usedSeconds = (timedata[2] == periodStart) ? timedata[3] : 0; + for(;!nearTimeLimit && (dayStart <= endTime); thisDay.setDate(thisDay.getDate() + 1)) { + dayStart = thisDay.setHours(0, 0, 0, 0) / 1000; + + for (const mp of mps) { + const adjusted = { + start: mp.start * 60 + dayStart, + end: mp.end * 60 + dayStart + } + + + const activeDay = days[thisDay.getDay()]; + if (!activeDay || adjusted.start >= endTime) + break; + + const overlap = intersect([adjusted.start, adjusted.end], [startTime, endTime]); + // Time periods MUST overlamp with block periods + if (overlap > 0) { + const totalUsed = usedSeconds + overlap; + + // This MP is where locking could occur + if (totalUsed >= limitSecs) { + // Calc __WHEN__ lock could activate + const engageTime = adjusted.start + Math.max(0, limitSecs - usedSeconds); + const lockTime = engageTime - blockOffset * 60; + if (now >= lockTime) { + nearTimeLimit = true; + break; + } + + } else { + usedSeconds = totalUsed; + } + } + } + } + } } } From 30716a842bd9921ec5ea3c6319f27a1f4435f2bf Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Wed, 14 Jul 2021 01:12:06 -0600 Subject: [PATCH 6/7] Revert "Block prediction updated" This reverts commit 80442e6d01f6a65b1588054a4d9701eaa73c736a. --- options.js | 60 +++--------------------------------------------------- 1 file changed, 3 insertions(+), 57 deletions(-) diff --git a/options.js b/options.js index 3a24060..8038b27 100644 --- a/options.js +++ b/options.js @@ -450,9 +450,8 @@ function retrieveOptions() { let nearTimeLimit = false; if (useTimeLimit && checkTimeLimit) { // Lock 'X' mins early ==> Reduce time limit by 'X' mins - let maxUnlockTime = limitMins - blockOffset; - // Time limit > lock offset: normal behaviour + // Positive lock limit: normal behaviour if (maxUnlockTime > 0) { let secondsLeft = maxUnlockTime * 60; if (timedata[2] == periodStart) { @@ -460,62 +459,9 @@ function retrieveOptions() { } nearTimeLimit = secondsLeft <= 0; } else { - // Negative lock limit: Complex* interactions between time periods/timedata/day borders + // Negative lock limit: Complex* interactions with time periods/timedata/day borders // *skipped for simplicity - nearTimeLimit = false; - // Possible: Check if future time limit contains time period start - //const midnight = new Date(timedate.getTime()); - //const midStamp = midnight.setHours(0, 0, 0, 0); - const intersect = (a, b) => Math.max(0, Math.min(a[1], b[1]) - Math.max(a[0], b[0])); - const limitSecs = limitMins * 60; - const predictionCutoff = now + (blockOffset * 60); - const mps = conjMode ? minPeriods : cleanTimePeriods(ALL_DAY_TIMES); - - for(let startTP = periodStart; !nearTimeLimit && (startTP <= predictionCutoff); startTP = getTimePeriodStart(+startTP + +limitPeriod, limitPeriod, limitOffset)) { - const nextTP = getTimePeriodStart(+startTP + +limitPeriod, limitPeriod, limitOffset); - const startTime = Math.max(startTP, now); // Don't check the past - const endTime = nextTP - 1; - - let thisDay = new Date(startTime * 1000); - let dayStart = thisDay.setHours(0, 0, 0, 0) / 1000; - - let usedSeconds = (timedata[2] == periodStart) ? timedata[3] : 0; - for(;!nearTimeLimit && (dayStart <= endTime); thisDay.setDate(thisDay.getDate() + 1)) { - dayStart = thisDay.setHours(0, 0, 0, 0) / 1000; - - for (const mp of mps) { - const adjusted = { - start: mp.start * 60 + dayStart, - end: mp.end * 60 + dayStart - } - - - const activeDay = days[thisDay.getDay()]; - if (!activeDay || adjusted.start >= endTime) - break; - - const overlap = intersect([adjusted.start, adjusted.end], [startTime, endTime]); - // Time periods MUST overlamp with block periods - if (overlap > 0) { - const totalUsed = usedSeconds + overlap; - - // This MP is where locking could occur - if (totalUsed >= limitSecs) { - // Calc __WHEN__ lock could activate - const engageTime = adjusted.start + Math.max(0, limitSecs - usedSeconds); - const lockTime = engageTime - blockOffset * 60; - if (now >= lockTime) { - nearTimeLimit = true; - break; - } - - } else { - usedSeconds = totalUsed; - } - } - } - } - } + nearTimeLimit = true; } } From 79e1d2952293dac13351029c3f18c09507369fe6 Mon Sep 17 00:00:00 2001 From: None <54128056+slickscreen@users.noreply.github.com> Date: Wed, 14 Jul 2021 01:13:03 -0600 Subject: [PATCH 7/7] Updated comments and variable names --- options.js | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/options.js b/options.js index 8038b27..eb89ead 100644 --- a/options.js +++ b/options.js @@ -420,47 +420,53 @@ function retrieveOptions() { /** 24 hours in minutes */ const fullDay = 24 * 60; - // Get required info - let startOffset = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; + // Setup + let earlyStart = Math.max(0, Math.min(fullDay, parseInt(blockOffset))) || 0; let tomorrowSelected = days[(currentDay + 1) % 7]; + let checkTomorrow = tomorrowSelected && ((mins - fullDay + earlyStart) >= 0); - // Check time period proximity + /** If any minPeriod is active or within range */ let nearTimePeriods = false; + // Check time period proximity for (let mp of minPeriods) { + // Check time periods as normal if today is selected if (onSelectedDay) { - if (mins >= (mp.start - startOffset) && mins <= mp.end) { + if (mins >= (mp.start - earlyStart) && mins <= mp.end) { nearTimePeriods = true; } } + // Check across day boundary if tomorrow is selected if (tomorrowSelected) { - // Check across day boundary + /** 'mins' relative to midnight tomorrow */ let shiftedMins = mins - fullDay; - if (shiftedMins >= (mp.start - startOffset) && shiftedMins <= mp.end) { + // Check tomorrow's time periods + if (shiftedMins >= (mp.start - earlyStart) && shiftedMins <= mp.end) { nearTimePeriods = true; } } } - // Check 'selected day' proximity - let checkTomorrow = (mins - fullDay + startOffset) > 0; - let checkTimeLimit = onSelectedDay || (checkTomorrow && tomorrowSelected); + /** If timelimits need to be checked at all */ + let checkTimeLimit = onSelectedDay || checkTomorrow; - // Check time limit proximity + /** If a current/upcoming timelimit does not have enough time left */ let nearTimeLimit = false; + // Check time limit proximity if (useTimeLimit && checkTimeLimit) { - // Lock 'X' mins early ==> Reduce time limit by 'X' mins - let maxUnlockTime = limitMins - blockOffset; - // Positive lock limit: normal behaviour - if (maxUnlockTime > 0) { - let secondsLeft = maxUnlockTime * 60; + /** The amount of the timelimit that can be used before options lock */ + let lockTimeLimit = limitMins - earlyStart; + + // Positive lock TL: Check only the active time limit period + if (lockTimeLimit > 0) { + let secondsLeft = lockTimeLimit * 60; if (timedata[2] == periodStart) { secondsLeft = Math.max(0, secondsLeft - timedata[3]); } nearTimeLimit = secondsLeft <= 0; } else { - // Negative lock limit: Complex* interactions with time periods/timedata/day borders - // *skipped for simplicity + // Negative lock TL: Complex interactions with timedata/time periods/day borders + // skipped for simplicity nearTimeLimit = true; } }