From 6bc7bb0b51ed993c720578af8835ad62ca61a341 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Tue, 25 Oct 2016 02:27:31 +0500 Subject: [PATCH 01/22] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?,=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BF=D1=80=D0=BE=D0=B9=D0=B4?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 195 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..6e1cab1 100644 --- a/robbery.js +++ b/robbery.js @@ -15,15 +15,55 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + var timeZone = parseInt(workingHours.from.split('+')[1]); + var convertedSchedule = convertIntervals( + getCommonSchedule(schedule), timeZone); + var convertedWorkingHours = convertIntervals( + getIntervals(workingHours), timeZone); return { + possibleIntervals: getPossibleIntervals( + convertedWorkingHours, convertedSchedule), + duration: duration, + robberyTime: undefined, + + /** + * Находит время, не раньше переданного + * @param {Date} date - Граница времени + * @returns {Date} Подходящее время + */ + findNotEarlier: function (date) { + var appropriateTimes = []; + var robberyDuration = this.duration; + this.possibleIntervals.forEach(function (interval) { + var startTime = interval.from; + if (date > startTime) { + startTime = date; + } + if (interval.to > startTime && + new Date(interval.to - startTime) >= + new Date(1000 * 60 * robberyDuration)) { + appropriateTimes.push(startTime); + } + }); + appropriateTimes.sort(); + + return appropriateTimes[0]; + }, /** * Найдено ли время * @returns {Boolean} */ exists: function () { + var appropriateTime = this.findNotEarlier( + new Date(2016, 9, 24, 5)); + if (typeof appropriateTime !== 'undefined') { + this.robberyTime = appropriateTime; + + return true; + } + return false; }, @@ -35,7 +75,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + return typeof this.robberyTime === 'undefined' ? '' + : formatTime(this.robberyTime, template); }, /** @@ -44,7 +85,159 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { + if (typeof this.robberyTime === 'undefined') { + throw new TypeError(); + } + var border = new Date(Number(this.robberyTime) + + Number(new Date(1000 * 60 * 30))); + var newTime = this.findNotEarlier(border); + if (typeof newTime !== 'undefined') { + this.robberyTime = newTime; + + return true; + } + return false; } }; }; + +/** + * Удаляет пересечение доступных интервалов с недоступными + * @param {Object} available - Доступные временные интервалы + * @param {Object} unavalable - Недоступные временные интервалы + * @returns {Object} Подходящие временные интервалы + */ +function getPossibleIntervals(available, unavalable) { + available.forEach(function (possible) { + unavalable.forEach(function (impossible) { + if (possible.from <= impossible.from && + impossible.from <= possible.to && + possible.to <= impossible.to) { + possible.to = impossible.from; + } + if (impossible.from <= possible.from && + possible.from <= impossible.to && + impossible.to <= possible.to) { + possible.from = impossible.to; + } + if (impossible.from <= possible.from && + possible.to <= impossible.to) { + possible.from = possible.to; + } + if (possible.from <= impossible.from && + impossible.to <= possible.to) { + var tempPossibleTo = possible.to; + possible.to = impossible.from; + available.push({ + from: impossible.to, + to: tempPossibleTo + }); + } + }); + }); + var intervals = []; + available.forEach(function (interval) { + if (interval.from < interval.to) { + intervals.push(interval); + } + }); + + return intervals; +} + +/** + * @param {Object} workHours - Часы работы + * @returns {Object} Временные интервалы в пределах задачи (ПН 00:00 - СР 23:59) + */ +function getIntervals(workHours) { + var intervals = []; + ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { + intervals.push({ + from: day + workHours.from, + to: day + workHours.to + }); + }); + + return intervals; +} + +/** + * @param {Object} gangSchedule - Расписание Банды + * @returns {Object} Общее расписание (список из интервалов) + */ +function getCommonSchedule(gangSchedule) { + var intervals = []; + gangSchedule.Danny.forEach(function (interval) { + intervals.push({ + from: interval.from, + to: interval.to + }); + }); + gangSchedule.Rusty.forEach(function (interval) { + intervals.push({ + from: interval.from, + to: interval.to + }); + }); + gangSchedule.Linus.forEach(function (interval) { + intervals.push({ + from: interval.from, + to: interval.to + }); + }); + + return intervals; +} + +var days = ['ВС', 'ПН', 'ВТ', 'СР']; + +/** + * @param {Date} time - Время для форматирования + * @param {String} template - Шаблон + * @returns {String} + */ +function formatTime(time, template) { + var components = [ + time.getHours().toString(), + time.getMinutes().toString() + ]; + components.forEach(function (component, idx, arr) { + if (component.length === 1) { + arr[idx] = '0' + component; + } + }); + + return template.replace('%HH', components[0]) + .replace('%MM', components[1]) + .replace('%DD', days[time.getDay()]); +} + +/** + * @param {Object} intervals - Временные интервалы в строках + * @param {Number} timeZone - Часовой пояс банка + * @returns {Object} Временные интервалы в Date + */ +function convertIntervals(intervals, timeZone) { + intervals.forEach(function (interval) { + interval.from = convertTime(interval.from, timeZone); + interval.to = convertTime(interval.to, timeZone); + }); + + return intervals; +} + +var dayToDate = { 'ПН': 24, 'ВТ': 25, 'СР': 26 }; + +/** + * @param {String} time - Строковое время + * @param {Number} timeZone - Часовой пояс банка + * @returns {Date} Время в формате Date + */ +function convertTime(time, timeZone) { + var dateParts = time.split(/[ :+]/); + var hour = parseInt(dateParts[1]) + timeZone - parseInt(dateParts[3]); + var minutes = parseInt(dateParts[2]); + + return new Date(2016, 9, dayToDate[dateParts[0]], hour, minutes); +} From 7db0021eeba242969fdd14fd92f71a7f82e97636 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Tue, 25 Oct 2016 02:36:34 +0500 Subject: [PATCH 02/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20tryLater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 6e1cab1..b7e55ba 100644 --- a/robbery.js +++ b/robbery.js @@ -86,7 +86,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { */ tryLater: function () { if (typeof this.robberyTime === 'undefined') { - throw new TypeError(); + return false; } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); From cc5a2e63e97a11ab2785fafdf700f83136674437 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Tue, 25 Oct 2016 02:46:06 +0500 Subject: [PATCH 03/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20tryLater=20=D1=81=D0=BD=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index b7e55ba..0f5d9fb 100644 --- a/robbery.js +++ b/robbery.js @@ -86,7 +86,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { */ tryLater: function () { if (typeof this.robberyTime === 'undefined') { - return false; + this.exists(); } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); From 0584d0cab5d4a251811f1c47dc8968698bcbc945 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Tue, 25 Oct 2016 13:00:13 +0500 Subject: [PATCH 04/22] =?UTF-8?q?=D0=9F=D1=80=D0=B8=20=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D0=B0=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=BD=D0=B0=D1=85=D0=BE=D0=B6=D1=83=20=D0=B2=D1=80?= =?UTF-8?q?=D0=B5=D0=BC=D1=8F,=20=D0=B5=D1=81=D0=BB=D0=B8=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B5=D0=B6=D0=B4=D0=B5=20=D0=BE=D0=BD=D0=BE=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=B1=D1=8B=D0=BB=D0=BE=20=D0=BD=D0=B0=D0=B9=D0=B4=D0=B5=D0=BD?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/robbery.js b/robbery.js index 0f5d9fb..937349d 100644 --- a/robbery.js +++ b/robbery.js @@ -75,6 +75,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { + if (typeof this.robberyTime === 'undefined') { + this.exists(); + } + return typeof this.robberyTime === 'undefined' ? '' : formatTime(this.robberyTime, template); }, From 788af543a762bade52cfca5c72d4d9c3f9bac77f Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 01:01:28 +0500 Subject: [PATCH 05/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2=20?= =?UTF-8?q?=D0=B2=20=D0=BA=D0=BE=D0=BB=D0=BB=D0=B5=D0=BA=D1=86=D0=B8=D1=8E?= =?UTF-8?q?=20=D0=B2=D0=BE=20=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D1=8B=20forEach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 90 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 30 deletions(-) diff --git a/robbery.js b/robbery.js index 937349d..f623810 100644 --- a/robbery.js +++ b/robbery.js @@ -109,39 +109,13 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { /** * Удаляет пересечение доступных интервалов с недоступными * @param {Object} available - Доступные временные интервалы - * @param {Object} unavalable - Недоступные временные интервалы + * @param {Object} unavailable - Недоступные временные интервалы * @returns {Object} Подходящие временные интервалы */ -function getPossibleIntervals(available, unavalable) { - available.forEach(function (possible) { - unavalable.forEach(function (impossible) { - if (possible.from <= impossible.from && - impossible.from <= possible.to && - possible.to <= impossible.to) { - possible.to = impossible.from; - } - if (impossible.from <= possible.from && - possible.from <= impossible.to && - impossible.to <= possible.to) { - possible.from = impossible.to; - } - if (impossible.from <= possible.from && - possible.to <= impossible.to) { - possible.from = possible.to; - } - if (possible.from <= impossible.from && - impossible.to <= possible.to) { - var tempPossibleTo = possible.to; - possible.to = impossible.from; - available.push({ - from: impossible.to, - to: tempPossibleTo - }); - } - }); - }); +function getPossibleIntervals(available, unavailable) { + var intervalsToCheck = removeIntersection(available, unavailable); var intervals = []; - available.forEach(function (interval) { + intervalsToCheck.forEach(function (interval) { if (interval.from < interval.to) { intervals.push(interval); } @@ -150,6 +124,62 @@ function getPossibleIntervals(available, unavalable) { return intervals; } +/** + * @param {Object} available - Доступные временные интервалы + * @param {Object} unavailable - Недоступные временные интервалы + * @returns {Object} Набор подходящих интервалов + */ +function removeIntersection(available, unavailable) { + var intervalsToCheck = []; + while (available.length > 0) { + var possible = available.pop(); + var newInterval = checkIntersection(possible, unavailable); + if (typeof newInterval !== 'undefined') { + available.push(newInterval); + } + intervalsToCheck.push(possible); + } + + return intervalsToCheck; +} + +/** + * @param {Object} possible - Доступный временной интервал + * @param {Object} unavailable - Набор недоступных интервалов + * @returns {Object} - Новый интервал, образованный при разрыве предыдущего + */ +function checkIntersection(possible, unavailable) { + var newInterval; + unavailable.forEach(function (impossible) { + if (possible.from <= impossible.from && + impossible.from <= possible.to && + possible.to <= impossible.to) { + possible.to = impossible.from; + } + if (impossible.from <= possible.from && + possible.from <= impossible.to && + impossible.to <= possible.to) { + possible.from = impossible.to; + } + if (impossible.from <= possible.from && + possible.from <= possible.to && + possible.to <= impossible.to) { + possible.from = possible.to; + } + if (possible.from <= impossible.from && + impossible.to <= possible.to) { + var tempPossibleTo = possible.to; + possible.to = impossible.from; + newInterval = { + from: impossible.to, + to: tempPossibleTo + }; + } + }); + + return newInterval; +} + /** * @param {Object} workHours - Часы работы * @returns {Object} Временные интервалы в пределах задачи (ПН 00:00 - СР 23:59) From 2ef1e2c0330e1cd9f0869d71f6e36feda23a7a3f Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 01:21:26 +0500 Subject: [PATCH 06/22] =?UTF-8?q?=D0=A1=D0=B4=D0=B2=D0=B8=D0=B3=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=B2=D1=81=D0=B5=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D1=8B=20=D0=BF=D1=80=D0=B8=20=D0=B2=D1=8B?= =?UTF-8?q?=D0=B7=D0=BE=D0=B2=D0=B5=20tryLater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/robbery.js b/robbery.js index f623810..a7875ea 100644 --- a/robbery.js +++ b/robbery.js @@ -36,14 +36,13 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var appropriateTimes = []; var robberyDuration = this.duration; this.possibleIntervals.forEach(function (interval) { - var startTime = interval.from; - if (date > startTime) { - startTime = date; + if (date > interval.from) { + interval.from = date; } - if (interval.to > startTime && - new Date(interval.to - startTime) >= + if (interval.to > interval.from && + new Date(interval.to - interval.from) >= new Date(1000 * 60 * robberyDuration)) { - appropriateTimes.push(startTime); + appropriateTimes.push(interval.from); } }); appropriateTimes.sort(); From f1d58f05a760e918586bd0ae8ec90a125915612e Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 01:50:28 +0500 Subject: [PATCH 07/22] =?UTF-8?q?=D0=9C=D0=BE=D0=B3=D1=83=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B2=D0=B5=D1=80=D1=88=D0=B8=D1=82=D1=8C=20=D0=BE=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D0=B1=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=200?= =?UTF-8?q?=20=D0=BC=D0=B8=D0=BD=D1=83=D1=82=20=D0=B2=20=D0=BD=D1=83=D0=BB?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B9=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index a7875ea..b3d9617 100644 --- a/robbery.js +++ b/robbery.js @@ -39,7 +39,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (date > interval.from) { interval.from = date; } - if (interval.to > interval.from && + if (interval.to >= interval.from && new Date(interval.to - interval.from) >= new Date(1000 * 60 * robberyDuration)) { appropriateTimes.push(interval.from); @@ -115,7 +115,7 @@ function getPossibleIntervals(available, unavailable) { var intervalsToCheck = removeIntersection(available, unavailable); var intervals = []; intervalsToCheck.forEach(function (interval) { - if (interval.from < interval.to) { + if (interval.from <= interval.to) { intervals.push(interval); } }); From e417fa273b88ea9ebaa17ae5be1d94809bccac1c Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 02:34:48 +0500 Subject: [PATCH 08/22] =?UTF-8?q?=D0=92=D1=80=D0=B5=D0=BC=D1=8F=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D1=87=D0=B8=20=D0=BE=D0=B3=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=87=D0=B5=D0=BD=D0=BE=20=D0=9F=D0=9D=2000:00=20=D0=B8?= =?UTF-8?q?=20=D0=A1=D0=A0=2023:59?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/robbery.js b/robbery.js index b3d9617..5c398ba 100644 --- a/robbery.js +++ b/robbery.js @@ -29,15 +29,19 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { /** * Находит время, не раньше переданного - * @param {Date} date - Граница времени + * @param {Date} lower - Нижняя граница времени + * @param {Date} upper - Верхняя граница времени * @returns {Date} Подходящее время */ - findNotEarlier: function (date) { + findNotEarlier: function (lower, upper) { var appropriateTimes = []; var robberyDuration = this.duration; this.possibleIntervals.forEach(function (interval) { - if (date > interval.from) { - interval.from = date; + if (lower > interval.from) { + interval.from = lower; + } + if (upper < interval.to) { + interval.to = upper; } if (interval.to >= interval.from && new Date(interval.to - interval.from) >= @@ -56,7 +60,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { */ exists: function () { var appropriateTime = this.findNotEarlier( - new Date(2016, 9, 24, 5)); + new Date(2016, 9, 24, 5), + new Date(2016, 9, 26, 28, 59)); if (typeof appropriateTime !== 'undefined') { this.robberyTime = appropriateTime; @@ -93,7 +98,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); - var newTime = this.findNotEarlier(border); + var newTime = this.findNotEarlier(border, + new Date(2016, 9, 26, 28, 59)); if (typeof newTime !== 'undefined') { this.robberyTime = newTime; @@ -185,6 +191,7 @@ function checkIntersection(possible, unavailable) { */ function getIntervals(workHours) { var intervals = []; + ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { intervals.push({ from: day + workHours.from, From 7b59a585b30e982e15c5ee1b07bb69993ae0d645 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 02:46:53 +0500 Subject: [PATCH 09/22] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B2=D1=81=D0=B5=20=D0=B4=D0=BD=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 5c398ba..7910ba1 100644 --- a/robbery.js +++ b/robbery.js @@ -267,7 +267,8 @@ function convertIntervals(intervals, timeZone) { return intervals; } -var dayToDate = { 'ПН': 24, 'ВТ': 25, 'СР': 26 }; +var dayToDate = { 'ВС': 23, 'ПН': 24, 'ВТ': 25, 'СР': 26, + 'ЧТ': 27, 'ПТ': 28, 'СББ': 29 }; /** * @param {String} time - Строковое время From 309025af249bd836adcc490b67940c607536ecf9 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 03:24:54 +0500 Subject: [PATCH 10/22] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=82=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=20=D0=B7=D0=B0=D0=BA=D1=80=D1=8B=D1=82=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B1=D0=B0=D0=BD=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/robbery.js b/robbery.js index 7910ba1..d6ef39c 100644 --- a/robbery.js +++ b/robbery.js @@ -18,8 +18,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var timeZone = parseInt(workingHours.from.split('+')[1]); var convertedSchedule = convertIntervals( getCommonSchedule(schedule), timeZone); - var convertedWorkingHours = convertIntervals( - getIntervals(workingHours), timeZone); + var convertedWorkingHours = checkWorkingHours(convertIntervals( + getIntervals(workingHours), timeZone)); return { possibleIntervals: getPossibleIntervals( @@ -185,6 +185,26 @@ function checkIntersection(possible, unavailable) { return newInterval; } +/** + * @param {Object} workHours - Часы работы банка + * @returns {Object} Список валидных интервалов + */ +function checkWorkingHours(workHours) { + var intervals = []; + workHours.forEach(function (interval) { + if (new Date(2016, 9, 24, 5) <= interval.from && + interval.to <= new Date(2016, 9, 24, 28, 59) || + new Date(2016, 9, 25, 5) <= interval.from && + interval.to <= new Date(2016, 9, 25, 28, 59) || + new Date(2016, 9, 26, 5) <= interval.from && + interval.to <= new Date(2016, 9, 26, 28, 59)) { + intervals.push(interval); + } + }); + + return intervals; +} + /** * @param {Object} workHours - Часы работы * @returns {Object} Временные интервалы в пределах задачи (ПН 00:00 - СР 23:59) @@ -230,7 +250,7 @@ function getCommonSchedule(gangSchedule) { return intervals; } -var days = ['ВС', 'ПН', 'ВТ', 'СР']; +var DAYS = ['ВС', 'ПН', 'ВТ', 'СР']; /** * @param {Date} time - Время для форматирования @@ -250,7 +270,7 @@ function formatTime(time, template) { return template.replace('%HH', components[0]) .replace('%MM', components[1]) - .replace('%DD', days[time.getDay()]); + .replace('%DD', DAYS[time.getDay()]); } /** @@ -267,7 +287,7 @@ function convertIntervals(intervals, timeZone) { return intervals; } -var dayToDate = { 'ВС': 23, 'ПН': 24, 'ВТ': 25, 'СР': 26, +var DAY_TO_DATE = { 'ВС': 23, 'ПН': 24, 'ВТ': 25, 'СР': 26, 'ЧТ': 27, 'ПТ': 28, 'СББ': 29 }; /** @@ -280,5 +300,5 @@ function convertTime(time, timeZone) { var hour = parseInt(dateParts[1]) + timeZone - parseInt(dateParts[3]); var minutes = parseInt(dateParts[2]); - return new Date(2016, 9, dayToDate[dateParts[0]], hour, minutes); + return new Date(2016, 9, DAY_TO_DATE[dateParts[0]], hour, minutes); } From bda48422c5c037f9f7e9fcdffd9d1457a56604d0 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 03:29:42 +0500 Subject: [PATCH 11/22] =?UTF-8?q?=D0=9E=D1=82=D0=BA=D0=B0=D1=82=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=D1=81=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/robbery.js b/robbery.js index d6ef39c..8693b36 100644 --- a/robbery.js +++ b/robbery.js @@ -18,8 +18,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var timeZone = parseInt(workingHours.from.split('+')[1]); var convertedSchedule = convertIntervals( getCommonSchedule(schedule), timeZone); - var convertedWorkingHours = checkWorkingHours(convertIntervals( - getIntervals(workingHours), timeZone)); + var convertedWorkingHours = convertIntervals( + getIntervals(workingHours), timeZone); return { possibleIntervals: getPossibleIntervals( @@ -185,26 +185,6 @@ function checkIntersection(possible, unavailable) { return newInterval; } -/** - * @param {Object} workHours - Часы работы банка - * @returns {Object} Список валидных интервалов - */ -function checkWorkingHours(workHours) { - var intervals = []; - workHours.forEach(function (interval) { - if (new Date(2016, 9, 24, 5) <= interval.from && - interval.to <= new Date(2016, 9, 24, 28, 59) || - new Date(2016, 9, 25, 5) <= interval.from && - interval.to <= new Date(2016, 9, 25, 28, 59) || - new Date(2016, 9, 26, 5) <= interval.from && - interval.to <= new Date(2016, 9, 26, 28, 59)) { - intervals.push(interval); - } - }); - - return intervals; -} - /** * @param {Object} workHours - Часы работы * @returns {Object} Временные интервалы в пределах задачи (ПН 00:00 - СР 23:59) From 69ce06d4f1bc6dd3f3a5d56a2e5925244ab8b0d2 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 16:34:30 +0500 Subject: [PATCH 12/22] =?UTF-8?q?=D0=9F=D1=80=D0=B8=D0=BD=D0=B8=D0=BC?= =?UTF-8?q?=D0=B0=D1=8E=20=D1=80=D0=B0=D1=81=D0=BF=D0=B8=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BB=D1=8E=D0=B1=D0=BE=D0=B3=D0=BE=20=D0=BA?= =?UTF-8?q?=D0=BE=D0=BB=D0=B8=D1=87=D0=B5=D1=81=D1=82=D0=B2=D0=B0=20=D0=B1?= =?UTF-8?q?=D0=B0=D0=BD=D0=B4=D0=B8=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/robbery.js b/robbery.js index 8693b36..17be738 100644 --- a/robbery.js +++ b/robbery.js @@ -208,24 +208,16 @@ function getIntervals(workHours) { */ function getCommonSchedule(gangSchedule) { var intervals = []; - gangSchedule.Danny.forEach(function (interval) { - intervals.push({ - from: interval.from, - to: interval.to - }); - }); - gangSchedule.Rusty.forEach(function (interval) { - intervals.push({ - from: interval.from, - to: interval.to - }); - }); - gangSchedule.Linus.forEach(function (interval) { - intervals.push({ - from: interval.from, - to: interval.to - }); - }); + for (var gangster in gangSchedule) { + if (typeof gangster !== 'undefined') { + gangSchedule[gangster].forEach(function (interval) { + intervals.push({ + from: interval.from, + to: interval.to + }); + }); + } + } return intervals; } From 4501f4606e433da303dca597c7503e8477ff01cc Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 17:16:25 +0500 Subject: [PATCH 13/22] =?UTF-8?q?=D0=A1=D0=B4=D0=B2=D0=B8=D0=BD=D1=83?= =?UTF-8?q?=D1=82=D0=B0=20=D0=BD=D0=B8=D0=B6=D0=BD=D1=8F=D1=8F=20=D0=B3?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D0=B0=20=D0=BD=D0=B0=201=20?= =?UTF-8?q?=D0=BC=D0=B8=D0=BD=D1=83=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 17be738..b0aa22b 100644 --- a/robbery.js +++ b/robbery.js @@ -61,7 +61,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { exists: function () { var appropriateTime = this.findNotEarlier( new Date(2016, 9, 24, 5), - new Date(2016, 9, 26, 28, 59)); + new Date(2016, 9, 27, 5)); if (typeof appropriateTime !== 'undefined') { this.robberyTime = appropriateTime; @@ -99,7 +99,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); var newTime = this.findNotEarlier(border, - new Date(2016, 9, 26, 28, 59)); + new Date(2016, 9, 27, 5)); if (typeof newTime !== 'undefined') { this.robberyTime = newTime; From 5470d03619594eb8c47deec78dd2aab8cd63a9c8 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 22:15:37 +0500 Subject: [PATCH 14/22] no star --- robbery.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index b0aa22b..df4bc45 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = true; +exports.isStar = false; /** * @param {Object} schedule – Расписание Банды @@ -33,7 +33,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @param {Date} upper - Верхняя граница времени * @returns {Date} Подходящее время */ - findNotEarlier: function (lower, upper) { + findWithin: function (lower, upper) { var appropriateTimes = []; var robberyDuration = this.duration; this.possibleIntervals.forEach(function (interval) { @@ -59,7 +59,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - var appropriateTime = this.findNotEarlier( + var appropriateTime = this.findWithin( new Date(2016, 9, 24, 5), new Date(2016, 9, 27, 5)); if (typeof appropriateTime !== 'undefined') { @@ -98,7 +98,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); - var newTime = this.findNotEarlier(border, + var newTime = this.findWithin(border, new Date(2016, 9, 27, 5)); if (typeof newTime !== 'undefined') { this.robberyTime = newTime; From 3bfabcb599ed8908b9785d5912691c03d0dccd6a Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 22:26:30 +0500 Subject: [PATCH 15/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20format?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index df4bc45..2573052 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = false; +exports.isStar = true; /** * @param {Object} schedule – Расписание Банды @@ -239,6 +239,9 @@ function formatTime(time, template) { arr[idx] = '0' + component; } }); + if (components[0] === '24') { + components[0] = '00'; + } return template.replace('%HH', components[0]) .replace('%MM', components[1]) From 320ead4fefb68721594148219188aa7b456219ee Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 22:46:48 +0500 Subject: [PATCH 16/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20tryLater?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 2573052..4f3dcc8 100644 --- a/robbery.js +++ b/robbery.js @@ -96,6 +96,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (typeof this.robberyTime === 'undefined') { this.exists(); } + if (this.robberyTime < new Date(2016, 9, 24, 5) || + this.robberyTime > new Date(2016, 9, 26, 28, 59)) { + return false; + } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); var newTime = this.findWithin(border, @@ -191,7 +195,6 @@ function checkIntersection(possible, unavailable) { */ function getIntervals(workHours) { var intervals = []; - ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { intervals.push({ from: day + workHours.from, From 89dba6b5a200c669148b47536561f195f7522cb3 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 22:49:50 +0500 Subject: [PATCH 17/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D0=BA=D0=BE=D0=B2=D0=B0=D1=80=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5=20=D1=82=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 4f3dcc8..9016025 100644 --- a/robbery.js +++ b/robbery.js @@ -97,8 +97,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { this.exists(); } if (this.robberyTime < new Date(2016, 9, 24, 5) || - this.robberyTime > new Date(2016, 9, 26, 28, 59)) { - return false; + this.robberyTime > new Date(2016, 9, 26, 28, 59)) { + return false; } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); From b89c5c5f9e72b042abf48168509c83f25e82ff75 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 23:11:53 +0500 Subject: [PATCH 18/22] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20tryLater=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B0=D1=8F,=20=D0=B5=D1=81=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D1=8F=20=D0=BD=D0=B5=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=B9=D0=B4=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/robbery.js b/robbery.js index 9016025..07b0dae 100644 --- a/robbery.js +++ b/robbery.js @@ -79,12 +79,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (typeof this.robberyTime === 'undefined') { - this.exists(); - } - - return typeof this.robberyTime === 'undefined' ? '' - : formatTime(this.robberyTime, template); + return typeof this.robberyTime === 'undefined' && + !this.exists() ? '' : formatTime(this.robberyTime, template); }, /** @@ -96,7 +92,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (typeof this.robberyTime === 'undefined') { this.exists(); } - if (this.robberyTime < new Date(2016, 9, 24, 5) || + if (typeof this.robberyTime === 'undefined' || + this.robberyTime < new Date(2016, 9, 24, 5) || this.robberyTime > new Date(2016, 9, 26, 28, 59)) { return false; } From 2ae96cc7b9eae5da5cd622a7edbb1e619a8878ee Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 23:15:43 +0500 Subject: [PATCH 19/22] =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D1=80=D1=80=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B0=D1=8F=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D1=86=D0=B0=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index 07b0dae..4c67b98 100644 --- a/robbery.js +++ b/robbery.js @@ -61,7 +61,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { exists: function () { var appropriateTime = this.findWithin( new Date(2016, 9, 24, 5), - new Date(2016, 9, 27, 5)); + new Date(2016, 9, 26, 28, 59, 59)); if (typeof appropriateTime !== 'undefined') { this.robberyTime = appropriateTime; @@ -93,14 +93,14 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { this.exists(); } if (typeof this.robberyTime === 'undefined' || - this.robberyTime < new Date(2016, 9, 24, 5) || - this.robberyTime > new Date(2016, 9, 26, 28, 59)) { + this.robberyTime < new Date(2016, 9, 24, 5) || + this.robberyTime > new Date(2016, 9, 26, 28, 59, 59)) { return false; } var border = new Date(Number(this.robberyTime) + Number(new Date(1000 * 60 * 30))); var newTime = this.findWithin(border, - new Date(2016, 9, 27, 5)); + new Date(2016, 9, 26, 28, 59, 59)); if (typeof newTime !== 'undefined') { this.robberyTime = newTime; From 4efad33b032110b99d92f6caf6af3a4e87432811 Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 23:46:32 +0500 Subject: [PATCH 20/22] no star --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 4c67b98..60949cd 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = true; +exports.isStar = false; /** * @param {Object} schedule – Расписание Банды From b5f57a5f49e2e9a8c5df3f06f1e02c37a503396f Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Wed, 26 Oct 2016 23:59:54 +0500 Subject: [PATCH 21/22] star --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 60949cd..4c67b98 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = false; +exports.isStar = true; /** * @param {Object} schedule – Расписание Банды From 4415ace063401d98901546143ebd4155ba4a7ddd Mon Sep 17 00:00:00 2001 From: Semen Makhaev Date: Thu, 27 Oct 2016 00:56:24 +0500 Subject: [PATCH 22/22] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA=D1=83=20=D0=BD=D1=83?= =?UTF-8?q?=D0=BB=D0=B5=D0=B2=D1=8B=D1=85=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=B2=D0=B0=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 4c67b98..8876acb 100644 --- a/robbery.js +++ b/robbery.js @@ -43,7 +43,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (upper < interval.to) { interval.to = upper; } - if (interval.to >= interval.from && + if (interval.to > interval.from && new Date(interval.to - interval.from) >= new Date(1000 * 60 * robberyDuration)) { appropriateTimes.push(interval.from);