From f349ff1a0aec02ddbc707384ef2453ad4e883a4e Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 18:50:43 +0500 Subject: [PATCH 01/29] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B1=D1=83=D0=B5?= =?UTF-8?q?=D0=BC=20(=D0=BE=D1=87=D0=B5=D0=BD=D1=8C=20=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=81=D1=82=D1=80=D0=B0=D1=88=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=BA=D0=BE=D0=B4=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..c40d945 100644 --- a/robbery.js +++ b/robbery.js @@ -15,7 +15,41 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + //console.info(schedule, duration, workingHours); + + var freeTimesSchedule = getFreeTime(schedule, workingHours); + convertToTimeStamps(freeTimesSchedule, workingHours); + var attackTimes = getAttackTimes(freeTimesSchedule, workingHours); + //attackTimes.forEach(function (time) { + // console.log(timeToString(time.from) + ' - ' + timeToString(time.to)); + //}); + + var correctAttackTimes = filterAttackTimes(attackTimes, duration); + + var replacer = function (match, p) { + var format = { + '%HH': function(time) { + return new Date(time).getHours(); + }, + '%MM': function(time) { + var minutes = new Date(time).getMinutes(); + + return ('0'+-~(minutes - 1)).substr(-2,2); + }, + '%DD': function(time) { + var days = { + 1: 'ПН', + 2: 'ВТ', + 3: 'СР' + }; + + var day = new Date(time).getDay(); + return days[day]; + } + }; + return format[p](correctAttackTimes[0].from); + }; + return { @@ -24,7 +58,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + return correctAttackTimes.length > 0; }, /** @@ -35,7 +69,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (!this.exists()) { + + return ''; + } + + return template.replace(/(%\S\S)/g, replacer); }, /** @@ -44,7 +83,141 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { + correctAttackTimes[0].from += 30 * 60 * 1000; + var _correctAttackTimes = filterAttackTimes(correctAttackTimes, duration); + if (_correctAttackTimes.length > 0) { + correctAttackTimes = _correctAttackTimes; + + return true; + } + correctAttackTimes[0].from -= 30 * 60 * 1000; + return false; } }; }; + +function print(times) { + times.forEach(function (time) { + console.log(timeToString(time.from) + ' - ' + timeToString(time.to)); + }); +} + +function filterAttackTimes(attackTimes, duration) { + var durationInMilliseconds = duration * 60 * 1000; + + return attackTimes.filter(function(time) { + return time.from + durationInMilliseconds <= time.to; + }); +} + +function getAttackTimes(schedule, workingHours) { + var attackTimes = []; + + var names = Object.keys(schedule); + + var bankSchedule = []; + ['ПН ', 'ВТ ', 'СР '].forEach(function(day) { + bankSchedule.push({ + from: getGangstaTimeStamp(day + workingHours.from), + to: getGangstaTimeStamp(day + workingHours.to) + }); + }); + + attackTimes = getSchedulesIntersection(bankSchedule, schedule[names[0]]); + for (var i = 1; i < names.length; i++) { + attackTimes = getSchedulesIntersection(schedule[names[i]], attackTimes); + } + + return attackTimes; +} + +function timeToString(time) { + var days = { + 1: 'ПН', + 2: 'ВТ', + 3: 'СР' + }; + + var date = new Date(time); + return days[date.getDay()] + ' ' + date.getHours() + ':' + date.getMinutes(); +} + +function getSchedulesIntersection(schedule1, schedule2) { + var intersections = []; + for (var i = 0; i < schedule1.length; i++) { + for (var j = 0; j < schedule2.length; j++) { + var intersection = getTimeIntersection(schedule1[i], schedule2[j]); + if (intersection.exist) { + intersections.push(intersection); + } + } + } + + return intersections; +} + +function getFreeTime(schedule, workingHours) { + var freeSchedule = {}; + Object.keys(schedule).forEach(function(gang) { + var gangsta = schedule[gang]; + var freeTimes = []; + + freeTimes.push({ + from: 'ПН ' + workingHours.from, + to: gangsta[0].from + }); + + for (var i = 1; i < gangsta.length; i++) { + freeTimes.push({ + from: gangsta[i - 1].to, + to: gangsta[i].from + }); + } + + freeTimes.push({ + from: gangsta[gangsta.length - 1].to, + to: 'СР ' + workingHours.to + }); + + freeSchedule[gang] = freeTimes; + }); + + return freeSchedule; +} + +function convertToTimeStamps(schedule, workingHours) { + Object.keys(schedule).forEach(function(gangsta) { + schedule[gangsta].forEach(function(time, index) { + time.from = getGangstaTimeStamp(time.from); + time.to = getGangstaTimeStamp(time.to); + }); + }); +} + +function getTimeIntersection(firstRange, secondRange) { // ..Range : TimeStamp + var intersection = { + exist: false, + from: Math.max(firstRange.from, secondRange.from), + to: Math.min(firstRange.to, secondRange.to) + }; + + if (intersection.from < intersection.to) { + intersection.exist = true; + } + + return intersection; +} + +function getGangstaTimeStamp(date) { // 'ПН 09:00+3' + var convertWeekDay = { + ПН: '01-04-2016', + ВТ: '01-05-2016', + СР: '01-06-2016' + }; + var weekDay = date.split(' ')[0]; + var time = date.split(' ')[1]; + + return new Date(convertWeekDay[weekDay] + ' ' + time).getTime(); +} + From 83fa8e0e4ddf95dcbd94eb98b8793a54b5c86886 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 18:52:48 +0500 Subject: [PATCH 02/29] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=20=D1=81=20=D0=BE=D1=88?= =?UTF-8?q?=D0=B8=D0=B1=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index c40d945..862c9ac 100644 --- a/robbery.js +++ b/robbery.js @@ -15,14 +15,11 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); + console.info(schedule, duration, workingHours); var freeTimesSchedule = getFreeTime(schedule, workingHours); convertToTimeStamps(freeTimesSchedule, workingHours); var attackTimes = getAttackTimes(freeTimesSchedule, workingHours); - //attackTimes.forEach(function (time) { - // console.log(timeToString(time.from) + ' - ' + timeToString(time.to)); - //}); var correctAttackTimes = filterAttackTimes(attackTimes, duration); @@ -220,4 +217,3 @@ function getGangstaTimeStamp(date) { // 'ПН 09:00+3' return new Date(convertWeekDay[weekDay] + ' ' + time).getTime(); } - From 94758bbd2c8f1e6131db77323fdf959a5035e5ed Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 19:02:27 +0500 Subject: [PATCH 03/29] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20eslint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/robbery.js b/robbery.js index 862c9ac..5159ef7 100644 --- a/robbery.js +++ b/robbery.js @@ -25,15 +25,15 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var replacer = function (match, p) { var format = { - '%HH': function(time) { + '%HH': function (time) { return new Date(time).getHours(); }, - '%MM': function(time) { + '%MM': function (time) { var minutes = new Date(time).getMinutes(); - return ('0'+-~(minutes - 1)).substr(-2,2); + return ('0' +- ~(minutes - 1)).substr(-2, 2); }, - '%DD': function(time) { + '%DD': function (time) { var days = { 1: 'ПН', 2: 'ВТ', @@ -41,13 +41,14 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; var day = new Date(time).getDay(); + return days[day]; } }; + return format[p](correctAttackTimes[0].from); }; - return { /** @@ -94,16 +95,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; -function print(times) { - times.forEach(function (time) { - console.log(timeToString(time.from) + ' - ' + timeToString(time.to)); - }); -} function filterAttackTimes(attackTimes, duration) { var durationInMilliseconds = duration * 60 * 1000; - return attackTimes.filter(function(time) { + return attackTimes.filter(function (time) { + return time.from + durationInMilliseconds <= time.to; }); } @@ -114,7 +111,7 @@ function getAttackTimes(schedule, workingHours) { var names = Object.keys(schedule); var bankSchedule = []; - ['ПН ', 'ВТ ', 'СР '].forEach(function(day) { + ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { bankSchedule.push({ from: getGangstaTimeStamp(day + workingHours.from), to: getGangstaTimeStamp(day + workingHours.to) @@ -135,28 +132,28 @@ function timeToString(time) { 2: 'ВТ', 3: 'СР' }; - var date = new Date(time); + return days[date.getDay()] + ' ' + date.getHours() + ':' + date.getMinutes(); } function getSchedulesIntersection(schedule1, schedule2) { var intersections = []; - for (var i = 0; i < schedule1.length; i++) { - for (var j = 0; j < schedule2.length; j++) { - var intersection = getTimeIntersection(schedule1[i], schedule2[j]); + schedule1.forEach(function (sc1) { + schedule2.forEach(function (sc2) { + var intersection = getTimeIntersection(schedule1[sc1], schedule2[sc2]); if (intersection.exist) { intersections.push(intersection); } - } - } + }); + }); return intersections; } function getFreeTime(schedule, workingHours) { var freeSchedule = {}; - Object.keys(schedule).forEach(function(gang) { + Object.keys(schedule).forEach(function (gang) { var gangsta = schedule[gang]; var freeTimes = []; @@ -183,9 +180,9 @@ function getFreeTime(schedule, workingHours) { return freeSchedule; } -function convertToTimeStamps(schedule, workingHours) { - Object.keys(schedule).forEach(function(gangsta) { - schedule[gangsta].forEach(function(time, index) { +function convertToTimeStamps(schedule) { + Object.keys(schedule).forEach(function (gangsta) { + schedule[gangsta].forEach(function (time) { time.from = getGangstaTimeStamp(time.from); time.to = getGangstaTimeStamp(time.to); }); From 84c2c19ed4a6465231d60c32f5c5cca956158be4 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 19:07:58 +0500 Subject: [PATCH 04/29] =?UTF-8?q?=D0=95=D1=89=D0=B5=20=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/robbery.js b/robbery.js index 5159ef7..d109d40 100644 --- a/robbery.js +++ b/robbery.js @@ -31,7 +31,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { '%MM': function (time) { var minutes = new Date(time).getMinutes(); - return ('0' +- ~(minutes - 1)).substr(-2, 2); + return ('0' + - (minutes)).substr(-2, 2); }, '%DD': function (time) { var days = { @@ -126,22 +126,11 @@ function getAttackTimes(schedule, workingHours) { return attackTimes; } -function timeToString(time) { - var days = { - 1: 'ПН', - 2: 'ВТ', - 3: 'СР' - }; - var date = new Date(time); - - return days[date.getDay()] + ' ' + date.getHours() + ':' + date.getMinutes(); -} - function getSchedulesIntersection(schedule1, schedule2) { var intersections = []; schedule1.forEach(function (sc1) { schedule2.forEach(function (sc2) { - var intersection = getTimeIntersection(schedule1[sc1], schedule2[sc2]); + var intersection = getTimeIntersection(sc1, sc2); if (intersection.exist) { intersections.push(intersection); } From 06f718bc23ee32e913edaa2f187a9e886939c891 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 19:52:24 +0500 Subject: [PATCH 05/29] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D1=87=D0=B0=D1=81=D0=BE=D0=B2=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=8F=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index d109d40..ef5d80b 100644 --- a/robbery.js +++ b/robbery.js @@ -15,7 +15,8 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + //console.info(schedule, duration, workingHours); + var timeZone = parseInt(workingHours.from.split('+')[1]); var freeTimesSchedule = getFreeTime(schedule, workingHours); convertToTimeStamps(freeTimesSchedule, workingHours); @@ -26,10 +27,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var replacer = function (match, p) { var format = { '%HH': function (time) { - return new Date(time).getHours(); + return new Date(time).getUTCHours() + timeZone; }, '%MM': function (time) { - var minutes = new Date(time).getMinutes(); + var minutes = new Date(time).getUTCMinutes(); return ('0' + - (minutes)).substr(-2, 2); }, @@ -40,7 +41,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { 3: 'СР' }; - var day = new Date(time).getDay(); + var day = new Date(time).getUTCDay(); return days[day]; } From 8e7728e7baab5a7df17517b2e8aa09132c1a46b7 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sat, 22 Oct 2016 19:54:24 +0500 Subject: [PATCH 06/29] ... --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index ef5d80b..c8be1f1 100644 --- a/robbery.js +++ b/robbery.js @@ -15,7 +15,7 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); + // console.info(schedule, duration, workingHours); var timeZone = parseInt(workingHours.from.split('+')[1]); var freeTimesSchedule = getFreeTime(schedule, workingHours); From b927a3c3311985b60d9fa996fdf507761d873aa9 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 15:37:19 +0500 Subject: [PATCH 07/29] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 171 +++++++++++++++++++++++++++-------------------------- 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/robbery.js b/robbery.js index c8be1f1..7b28cb8 100644 --- a/robbery.js +++ b/robbery.js @@ -1,33 +1,23 @@ 'use strict'; -/** - * Сделано задание на звездочку - * Реализовано оба метода и tryLater - */ exports.isStar = true; -/** - * @param {Object} schedule – Расписание Банды - * @param {Number} duration - Время на ограбление в минутах - * @param {Object} workingHours – Время работы банка - * @param {String} workingHours.from – Время открытия, например, "10:00+5" - * @param {String} workingHours.to – Время закрытия, например, "18:00+5" - * @returns {Object} - */ +var TIME_ZONE; +var INDEX = 0; + exports.getAppropriateMoment = function (schedule, duration, workingHours) { // console.info(schedule, duration, workingHours); - var timeZone = parseInt(workingHours.from.split('+')[1]); - - var freeTimesSchedule = getFreeTime(schedule, workingHours); - convertToTimeStamps(freeTimesSchedule, workingHours); - var attackTimes = getAttackTimes(freeTimesSchedule, workingHours); + TIME_ZONE = parseInt(workingHours.from.split('+')[1]); - var correctAttackTimes = filterAttackTimes(attackTimes, duration); + schedule = invertSchedule(schedule); + setTimeStamps(schedule); + var bestAttackTime = filterSchedule(schedule, workingHours, duration); + var startTimesToAttack = getStartTimes(bestAttackTime, duration); var replacer = function (match, p) { var format = { '%HH': function (time) { - return new Date(time).getUTCHours() + timeZone; + return new Date(time).getUTCHours() + TIME_ZONE; }, '%MM': function (time) { var minutes = new Date(time).getUTCMinutes(); @@ -47,26 +37,14 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } }; - return format[p](correctAttackTimes[0].from); + return format[p](startTimesToAttack[INDEX]); }; return { - - /** - * Найдено ли время - * @returns {Boolean} - */ exists: function () { - return correctAttackTimes.length > 0; + return startTimesToAttack.length > 0; }, - /** - * Возвращает отформатированную строку с часами для ограбления - * Например, - * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" - * @param {String} template - * @returns {String} - */ format: function (template) { if (!this.exists()) { @@ -76,55 +54,79 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return template.replace(/(%\S\S)/g, replacer); }, - /** - * Попробовать найти часы для ограбления позже [*] - * @star - * @returns {Boolean} - */ tryLater: function () { - correctAttackTimes[0].from += 30 * 60 * 1000; - var _correctAttackTimes = filterAttackTimes(correctAttackTimes, duration); - if (_correctAttackTimes.length > 0) { - correctAttackTimes = _correctAttackTimes; + if (INDEX < startTimesToAttack.length - 1) { + INDEX++; return true; } - correctAttackTimes[0].from -= 30 * 60 * 1000; return false; } }; }; - -function filterAttackTimes(attackTimes, duration) { +function getStartTimes(bestAttackTime, duration) { + var times = []; + var halfHour = 30 * 60 * 1000; + var count = 0; var durationInMilliseconds = duration * 60 * 1000; + for (var i = 0; i < bestAttackTime.length; i++) { + if (bestAttackTime[i].from + count * halfHour + durationInMilliseconds <= bestAttackTime[i].to) { + times.push(bestAttackTime[i].from + count * halfHour); + i--; + count++; + } else { + count = 0; + } + } - return attackTimes.filter(function (time) { - - return time.from + durationInMilliseconds <= time.to; - }); + return times; } -function getAttackTimes(schedule, workingHours) { - var attackTimes = []; - +function filterSchedule(schedule, workingHours, duration) { var names = Object.keys(schedule); var bankSchedule = []; ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { bankSchedule.push({ - from: getGangstaTimeStamp(day + workingHours.from), - to: getGangstaTimeStamp(day + workingHours.to) + from: getTimeStamp(day + workingHours.from), + to: getTimeStamp(day + workingHours.to) }); }); - attackTimes = getSchedulesIntersection(bankSchedule, schedule[names[0]]); + var timeIntersection = getSchedulesIntersection(bankSchedule, schedule[names[0]]); for (var i = 1; i < names.length; i++) { - attackTimes = getSchedulesIntersection(schedule[names[i]], attackTimes); + timeIntersection = getSchedulesIntersection(schedule[names[i]], timeIntersection); + } + + var durationInMilliseconds = duration * 60 * 1000; + + return timeIntersection.filter(function (time) { + + return time.from + durationInMilliseconds <= time.to; + }); +} + +function print(schedule) { + for (var i = 0; i < schedule.length; i++) { + console.log(toStr(schedule[i].from) + ' - ' + toStr(schedule[i].to)); } +} - return attackTimes; +function toStr(time) { + var days = { + 0: 'ВС', + 1: 'ПН', + 2: 'ВТ', + 3: 'СР' + }; + var day = new Date(time).getUTCDay(); + day = days[day]; + var minutes = new Date(time).getUTCMinutes(); + minutes = ('0' + - (minutes)).substr(-2, 2); + var hours = new Date(time).getUTCHours() + TIME_ZONE; + return day + ' ' + hours + ':' + minutes; } function getSchedulesIntersection(schedule1, schedule2) { @@ -141,45 +143,44 @@ function getSchedulesIntersection(schedule1, schedule2) { return intersections; } -function getFreeTime(schedule, workingHours) { - var freeSchedule = {}; - Object.keys(schedule).forEach(function (gang) { - var gangsta = schedule[gang]; - var freeTimes = []; +function invertSchedule(schedule) { + var _new = {}; + Object.keys(schedule).forEach(function (name) { + var busyTime = schedule[name]; + var freeTime = []; - freeTimes.push({ - from: 'ПН ' + workingHours.from, - to: gangsta[0].from + freeTime.push({ + from: 'ПН 00:00+' + TIME_ZONE.toString(), + to: busyTime[0].from }); - for (var i = 1; i < gangsta.length; i++) { - freeTimes.push({ - from: gangsta[i - 1].to, - to: gangsta[i].from + for (var i = 1; i < busyTime.length; i++) { + freeTime.push({ + from: busyTime[i - 1].to, + to: busyTime[i].from }); } - freeTimes.push({ - from: gangsta[gangsta.length - 1].to, - to: 'СР ' + workingHours.to + freeTime.push({ + from: busyTime[busyTime.length - 1].to, + to: 'СР 23:59+' + TIME_ZONE.toString() }); - freeSchedule[gang] = freeTimes; + _new[name] = freeTime; }); - - return freeSchedule; + return _new; } -function convertToTimeStamps(schedule) { - Object.keys(schedule).forEach(function (gangsta) { - schedule[gangsta].forEach(function (time) { - time.from = getGangstaTimeStamp(time.from); - time.to = getGangstaTimeStamp(time.to); +function setTimeStamps(schedule) { + Object.keys(schedule).forEach(function (name) { + schedule[name].forEach(function (time) { + time.from = getTimeStamp(time.from); + time.to = getTimeStamp(time.to); }); }); } -function getTimeIntersection(firstRange, secondRange) { // ..Range : TimeStamp +function getTimeIntersection(firstRange, secondRange) { var intersection = { exist: false, from: Math.max(firstRange.from, secondRange.from), @@ -193,14 +194,14 @@ function getTimeIntersection(firstRange, secondRange) { // ..Range : TimeStamp return intersection; } -function getGangstaTimeStamp(date) { // 'ПН 09:00+3' - var convertWeekDay = { +function getTimeStamp(date) { + var toFullDate = { ПН: '01-04-2016', ВТ: '01-05-2016', СР: '01-06-2016' }; - var weekDay = date.split(' ')[0]; + var day = date.split(' ')[0]; var time = date.split(' ')[1]; - return new Date(convertWeekDay[weekDay] + ' ' + time).getTime(); + return new Date(toFullDate[day] + ' ' + time).getTime(); } From 40ac70de6b0744e17bc8ade1a4032111f5676a9c Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 15:45:40 +0500 Subject: [PATCH 08/29] Eslint --- robbery.js | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/robbery.js b/robbery.js index 7b28cb8..a836552 100644 --- a/robbery.js +++ b/robbery.js @@ -95,9 +95,9 @@ function filterSchedule(schedule, workingHours, duration) { }); }); - var timeIntersection = getSchedulesIntersection(bankSchedule, schedule[names[0]]); + var tIntrsc = getSchedulesIntersection(bankSchedule, schedule[names[0]]); for (var i = 1; i < names.length; i++) { - timeIntersection = getSchedulesIntersection(schedule[names[i]], timeIntersection); + tIntrsc = getSchedulesIntersection(schedule[names[i]], tIntrsc); } var durationInMilliseconds = duration * 60 * 1000; @@ -108,27 +108,6 @@ function filterSchedule(schedule, workingHours, duration) { }); } -function print(schedule) { - for (var i = 0; i < schedule.length; i++) { - console.log(toStr(schedule[i].from) + ' - ' + toStr(schedule[i].to)); - } -} - -function toStr(time) { - var days = { - 0: 'ВС', - 1: 'ПН', - 2: 'ВТ', - 3: 'СР' - }; - var day = new Date(time).getUTCDay(); - day = days[day]; - var minutes = new Date(time).getUTCMinutes(); - minutes = ('0' + - (minutes)).substr(-2, 2); - var hours = new Date(time).getUTCHours() + TIME_ZONE; - return day + ' ' + hours + ':' + minutes; -} - function getSchedulesIntersection(schedule1, schedule2) { var intersections = []; schedule1.forEach(function (sc1) { @@ -168,6 +147,7 @@ function invertSchedule(schedule) { _new[name] = freeTime; }); + return _new; } From 328a30ece6ce8da06356c57e9c4306ce1d3fc057 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 15:48:24 +0500 Subject: [PATCH 09/29] ... --- robbery.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index a836552..4e0887d 100644 --- a/robbery.js +++ b/robbery.js @@ -72,7 +72,9 @@ function getStartTimes(bestAttackTime, duration) { var count = 0; var durationInMilliseconds = duration * 60 * 1000; for (var i = 0; i < bestAttackTime.length; i++) { - if (bestAttackTime[i].from + count * halfHour + durationInMilliseconds <= bestAttackTime[i].to) { + if (bestAttackTime[i].from + + count * halfHour + + durationInMilliseconds <= bestAttackTime[i].to) { times.push(bestAttackTime[i].from + count * halfHour); i--; count++; @@ -102,7 +104,7 @@ function filterSchedule(schedule, workingHours, duration) { var durationInMilliseconds = duration * 60 * 1000; - return timeIntersection.filter(function (time) { + return tIntrsc.filter(function (time) { return time.from + durationInMilliseconds <= time.to; }); From 6464fc8b6445abe924d0384b0f24467f59dd2804 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 16:41:04 +0500 Subject: [PATCH 10/29] =?UTF-8?q?=D0=9D=D0=B5=D0=B1=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D1=88=D0=B8=D0=B5=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/robbery.js b/robbery.js index 4e0887d..e1b3671 100644 --- a/robbery.js +++ b/robbery.js @@ -15,23 +15,26 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var startTimesToAttack = getStartTimes(bestAttackTime, duration); var replacer = function (match, p) { + var timeDiff = TIME_ZONE * 60 * 60 * 1000; var format = { '%HH': function (time) { - return new Date(time).getUTCHours() + TIME_ZONE; + return new Date(time + timeDiff).getUTCHours(); }, '%MM': function (time) { - var minutes = new Date(time).getUTCMinutes(); + var minutes = new Date(time + timeDiff).getUTCMinutes(); return ('0' + - (minutes)).substr(-2, 2); }, '%DD': function (time) { var days = { + 0: 'ВС', 1: 'ПН', 2: 'ВТ', - 3: 'СР' + 3: 'СР', + 4: 'ЧТ' }; - var day = new Date(time).getUTCDay(); + var day = new Date(time + timeDiff).getUTCDay(); return days[day]; } @@ -185,5 +188,5 @@ function getTimeStamp(date) { var day = date.split(' ')[0]; var time = date.split(' ')[1]; - return new Date(toFullDate[day] + ' ' + time).getTime(); + return Date.parse(toFullDate[day] + ' ' + time); } From 933299c42bb88117509dc2b973e7757643fc9e66 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 21:17:33 +0500 Subject: [PATCH 11/29] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=B3?= =?UTF-8?q?=D0=BB=D0=BE=D0=B1=D0=B0=D0=BB=D1=8C=D0=BD=D1=83=D1=8E=20=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=83=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/robbery.js b/robbery.js index e1b3671..35b8724 100644 --- a/robbery.js +++ b/robbery.js @@ -3,9 +3,9 @@ exports.isStar = true; var TIME_ZONE; -var INDEX = 0; exports.getAppropriateMoment = function (schedule, duration, workingHours) { + var index = 0; // console.info(schedule, duration, workingHours); TIME_ZONE = parseInt(workingHours.from.split('+')[1]); @@ -22,7 +22,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }, '%MM': function (time) { var minutes = new Date(time + timeDiff).getUTCMinutes(); - return ('0' + - (minutes)).substr(-2, 2); }, '%DD': function (time) { @@ -33,14 +32,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { 3: 'СР', 4: 'ЧТ' }; - var day = new Date(time + timeDiff).getUTCDay(); - return days[day]; } }; - return format[p](startTimesToAttack[INDEX]); + return format[p](startTimesToAttack[index]); }; return { @@ -50,20 +47,16 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { format: function (template) { if (!this.exists()) { - return ''; } - return template.replace(/(%\S\S)/g, replacer); }, tryLater: function () { - if (INDEX < startTimesToAttack.length - 1) { - INDEX++; - + if (index < startTimesToAttack.length - 1) { + index++; return true; } - return false; } }; @@ -108,7 +101,6 @@ function filterSchedule(schedule, workingHours, duration) { var durationInMilliseconds = duration * 60 * 1000; return tIntrsc.filter(function (time) { - return time.from + durationInMilliseconds <= time.to; }); } From 03bda111556d1f6838d63b0deb7b6dc4e2388d13 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 21:20:38 +0500 Subject: [PATCH 12/29] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B1=D0=B5=D0=BB?= =?UTF-8?q?=D1=8B=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B4=20return?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/robbery.js b/robbery.js index 35b8724..a009e8a 100644 --- a/robbery.js +++ b/robbery.js @@ -22,6 +22,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }, '%MM': function (time) { var minutes = new Date(time + timeDiff).getUTCMinutes(); + return ('0' + - (minutes)).substr(-2, 2); }, '%DD': function (time) { @@ -33,6 +34,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { 4: 'ЧТ' }; var day = new Date(time + timeDiff).getUTCDay(); + return days[day]; } }; @@ -49,14 +51,17 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (!this.exists()) { return ''; } + return template.replace(/(%\S\S)/g, replacer); }, tryLater: function () { if (index < startTimesToAttack.length - 1) { index++; + return true; } + return false; } }; From 976cc954db6719fe6b3cd02612a0ba2e9be50cf4 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 21:22:42 +0500 Subject: [PATCH 13/29] probels --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index a009e8a..da06f41 100644 --- a/robbery.js +++ b/robbery.js @@ -61,7 +61,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return true; } - + return false; } }; From f4831e3a38a8eb0d8d9347ae6df5fa9f78342f18 Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 21:46:51 +0500 Subject: [PATCH 14/29] 0:0 -> 00:00 --- robbery.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index da06f41..3cc65fb 100644 --- a/robbery.js +++ b/robbery.js @@ -18,7 +18,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var timeDiff = TIME_ZONE * 60 * 60 * 1000; var format = { '%HH': function (time) { - return new Date(time + timeDiff).getUTCHours(); + var hours = new Date(time + timeDiff).getUTCHours(); + + return ('0' + - (hours)).substr(-2, 2); }, '%MM': function (time) { var minutes = new Date(time + timeDiff).getUTCMinutes(); @@ -146,13 +148,35 @@ function invertSchedule(schedule) { from: busyTime[busyTime.length - 1].to, to: 'СР 23:59+' + TIME_ZONE.toString() }); - _new[name] = freeTime; }); return _new; } +function print(schedule) { + for (var i = 0; i < schedule.length; i++) { + console.log(toStr(schedule[i].from) + ' - ' + toStr(schedule[i].to)); + } +} + +function toStr(time) { + var days = { + 0: 'ВС', + 1: 'ПН', + 2: 'ВТ', + 3: 'СР' + }; + var day = new Date(time).getUTCDay(); + day = days[day]; + var minutes = new Date(time).getUTCMinutes(); + minutes = ('0' + - (minutes)).substr(-2, 2); + var hours = new Date(time).getUTCHours() + TIME_ZONE; + return day + ' ' + hours + ':' + minutes; +} + + + function setTimeStamps(schedule) { Object.keys(schedule).forEach(function (name) { schedule[name].forEach(function (time) { From 031c182a219f9d508ae77c06fabe62ff6a6f1bcf Mon Sep 17 00:00:00 2001 From: FairTex Date: Sun, 23 Oct 2016 21:52:13 +0500 Subject: [PATCH 15/29] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) diff --git a/robbery.js b/robbery.js index 3cc65fb..a21916c 100644 --- a/robbery.js +++ b/robbery.js @@ -20,12 +20,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { '%HH': function (time) { var hours = new Date(time + timeDiff).getUTCHours(); - return ('0' + - (hours)).substr(-2, 2); + return addZero(hours); }, '%MM': function (time) { var minutes = new Date(time + timeDiff).getUTCMinutes(); - return ('0' + - (minutes)).substr(-2, 2); + return addZero(minutes); }, '%DD': function (time) { var days = { @@ -69,6 +69,15 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; +function addZero(digit) { + if (digit.toString().length === 1) { + + return '0' + digit.toString(); + } + + return digit; +} + function getStartTimes(bestAttackTime, duration) { var times = []; var halfHour = 30 * 60 * 1000; @@ -154,29 +163,6 @@ function invertSchedule(schedule) { return _new; } -function print(schedule) { - for (var i = 0; i < schedule.length; i++) { - console.log(toStr(schedule[i].from) + ' - ' + toStr(schedule[i].to)); - } -} - -function toStr(time) { - var days = { - 0: 'ВС', - 1: 'ПН', - 2: 'ВТ', - 3: 'СР' - }; - var day = new Date(time).getUTCDay(); - day = days[day]; - var minutes = new Date(time).getUTCMinutes(); - minutes = ('0' + - (minutes)).substr(-2, 2); - var hours = new Date(time).getUTCHours() + TIME_ZONE; - return day + ' ' + hours + ':' + minutes; -} - - - function setTimeStamps(schedule) { Object.keys(schedule).forEach(function (name) { schedule[name].forEach(function (time) { From 2ba63df8228717d6ad54a3a67fb91add11a8455d Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 02:11:08 +0500 Subject: [PATCH 16/29] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D1=88=D0=B8=D1=80?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=B4=D0=BE=20=D0=BD=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index a21916c..487a709 100644 --- a/robbery.js +++ b/robbery.js @@ -155,7 +155,7 @@ function invertSchedule(schedule) { freeTime.push({ from: busyTime[busyTime.length - 1].to, - to: 'СР 23:59+' + TIME_ZONE.toString() + to: 'ВС 23:59+' + TIME_ZONE.toString() }); _new[name] = freeTime; }); @@ -190,7 +190,11 @@ function getTimeStamp(date) { var toFullDate = { ПН: '01-04-2016', ВТ: '01-05-2016', - СР: '01-06-2016' + СР: '01-06-2016', + ЧТ: '01-07-2016', + ПТ: '01-08-2016', + СБ: '01-09-2016', + ВС: '01-10-2016' }; var day = date.split(' ')[0]; var time = date.split(' ')[1]; From c72779f78c116d2bf03745fbb060e7b58422b0b4 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 02:45:35 +0500 Subject: [PATCH 17/29] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B1=D1=83=D0=B5?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 487a709..80d3050 100644 --- a/robbery.js +++ b/robbery.js @@ -7,7 +7,10 @@ var TIME_ZONE; exports.getAppropriateMoment = function (schedule, duration, workingHours) { var index = 0; // console.info(schedule, duration, workingHours); - TIME_ZONE = parseInt(workingHours.from.split('+')[1]); + + TIME_ZONE = parseInt(workingHours.from.split('+')[1]) % 24; + workingHours.from = workingHours.from.slice(0, 8); + workingHours.to = workingHours.to.slice(0, 8); schedule = invertSchedule(schedule); setTimeStamps(schedule); @@ -104,8 +107,8 @@ function filterSchedule(schedule, workingHours, duration) { var bankSchedule = []; ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { bankSchedule.push({ - from: getTimeStamp(day + workingHours.from), - to: getTimeStamp(day + workingHours.to) + from: getTimeStamp(day + workingHours.from + TIME_ZONE.toString()), + to: getTimeStamp(day + workingHours.to + TIME_ZONE.toString()) }); }); From cf476ed6a6a70fd945edc6776e34368d728dfc6d Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 02:52:19 +0500 Subject: [PATCH 18/29] ... --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 80d3050..ffa2cf2 100644 --- a/robbery.js +++ b/robbery.js @@ -9,8 +9,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { // console.info(schedule, duration, workingHours); TIME_ZONE = parseInt(workingHours.from.split('+')[1]) % 24; - workingHours.from = workingHours.from.slice(0, 8); - workingHours.to = workingHours.to.slice(0, 8); + workingHours.from = workingHours.from.slice(0, 4); + workingHours.to = workingHours.to.slice(0, 4); schedule = invertSchedule(schedule); setTimeStamps(schedule); From 74119a0bee6c3e360deda1579640f7a574c6a4ee Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 03:02:47 +0500 Subject: [PATCH 19/29] ... --- robbery.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index ffa2cf2..f9403e1 100644 --- a/robbery.js +++ b/robbery.js @@ -9,8 +9,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { // console.info(schedule, duration, workingHours); TIME_ZONE = parseInt(workingHours.from.split('+')[1]) % 24; - workingHours.from = workingHours.from.slice(0, 4); - workingHours.to = workingHours.to.slice(0, 4); + workingHours.from = workingHours.from.replace(/\+\d+/, '+' + TIME_ZONE); + workingHours.to = workingHours.to.replace(/\+\d+/, '+' + TIME_ZONE); schedule = invertSchedule(schedule); setTimeStamps(schedule); @@ -107,8 +107,8 @@ function filterSchedule(schedule, workingHours, duration) { var bankSchedule = []; ['ПН ', 'ВТ ', 'СР '].forEach(function (day) { bankSchedule.push({ - from: getTimeStamp(day + workingHours.from + TIME_ZONE.toString()), - to: getTimeStamp(day + workingHours.to + TIME_ZONE.toString()) + from: getTimeStamp(day + workingHours.from), + to: getTimeStamp(day + workingHours.to) }); }); From 4334eb01ca90079c41eefcaf2338621c038d1e78 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 18:20:48 +0500 Subject: [PATCH 20/29] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20tryLater()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/robbery.js b/robbery.js index f9403e1..fa656dc 100644 --- a/robbery.js +++ b/robbery.js @@ -73,9 +73,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; function addZero(digit) { - if (digit.toString().length === 1) { + if (digit < 10) { - return '0' + digit.toString(); + return '0' + digit; } return digit; @@ -84,23 +84,46 @@ function addZero(digit) { function getStartTimes(bestAttackTime, duration) { var times = []; var halfHour = 30 * 60 * 1000; - var count = 0; var durationInMilliseconds = duration * 60 * 1000; - for (var i = 0; i < bestAttackTime.length; i++) { - if (bestAttackTime[i].from + - count * halfHour + - durationInMilliseconds <= bestAttackTime[i].to) { - times.push(bestAttackTime[i].from + count * halfHour); - i--; - count++; - } else { - count = 0; + + var currentTime = bestAttackTime[0].from; + times.push(currentTime); + var count = 0; + var isPossible = true; + + while (isPossible) { + isPossible = false; + var possibleTime = currentTime + halfHour; + if (checkPossibleTime(bestAttackTime, possibleTime, durationInMilliseconds)) { + times.push(possibleTime); + currentTime = possibleTime; + isPossible = true; } } return times; } +function checkPossibleTime(bestAttackTime, possibleTime, durationInMilliseconds) { + for (var i = 0; i < bestAttackTime.length; i ++) { + if (containsRange(bestAttackTime[i], { + from: possibleTime, + to: possibleTime + durationInMilliseconds + })) { + + return true; + } + } + + return false; +} + +function containsRange(interval, timeToCheck) { + + return interval.from <= timeToCheck.from && timeToCheck.from <= interval.to && + interval.from <= timeToCheck.to && timeToCheck.to <= interval.to; +} + function filterSchedule(schedule, workingHours, duration) { var names = Object.keys(schedule); From 5b205dfe4f8222097880df25d94a621208032edb Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 18:23:38 +0500 Subject: [PATCH 21/29] Eslint --- robbery.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index fa656dc..f44c880 100644 --- a/robbery.js +++ b/robbery.js @@ -88,7 +88,6 @@ function getStartTimes(bestAttackTime, duration) { var currentTime = bestAttackTime[0].from; times.push(currentTime); - var count = 0; var isPossible = true; while (isPossible) { @@ -107,9 +106,9 @@ function getStartTimes(bestAttackTime, duration) { function checkPossibleTime(bestAttackTime, possibleTime, durationInMilliseconds) { for (var i = 0; i < bestAttackTime.length; i ++) { if (containsRange(bestAttackTime[i], { - from: possibleTime, - to: possibleTime + durationInMilliseconds - })) { + from: possibleTime, + to: possibleTime + durationInMilliseconds + })) { return true; } From 55bdb24198d60504bcf176e62e60e7528d638244 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 19:50:11 +0500 Subject: [PATCH 22/29] tryLater() --- robbery.js | 85 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/robbery.js b/robbery.js index f44c880..5c179c7 100644 --- a/robbery.js +++ b/robbery.js @@ -1,6 +1,6 @@ 'use strict'; -exports.isStar = true; +exports.isStar = false; var TIME_ZONE; @@ -17,6 +17,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var bestAttackTime = filterSchedule(schedule, workingHours, duration); var startTimesToAttack = getStartTimes(bestAttackTime, duration); + // console.log(bestAttackTime); + + // console.log(startTimesToAttack.map(function (time) { + // return toStr(time); + // })); + var replacer = function (match, p) { var timeDiff = TIME_ZONE * 60 * 60 * 1000; var format = { @@ -82,45 +88,74 @@ function addZero(digit) { } function getStartTimes(bestAttackTime, duration) { + if (bestAttackTime.length === 0) { + + return []; + } + var times = []; - var halfHour = 30 * 60 * 1000; - var durationInMilliseconds = duration * 60 * 1000; + process(bestAttackTime, times, duration * 60 * 1000); - var currentTime = bestAttackTime[0].from; - times.push(currentTime); - var isPossible = true; + return times; +} + +function process(intervals, result, durationInMillisecond) { + var halfHour = 30 * 60 * 1000; + var index = 0; - while (isPossible) { - isPossible = false; + while (index < intervals.length) { + var currentTime = intervals[index].from; + result.push(currentTime); var possibleTime = currentTime + halfHour; - if (checkPossibleTime(bestAttackTime, possibleTime, durationInMilliseconds)) { - times.push(possibleTime); - currentTime = possibleTime; - isPossible = true; + while (check(possibleTime, durationInMillisecond, intervals)) { + result.push(possibleTime); + possibleTime += halfHour; + } + index = Math.ceil(getIndex(intervals, possibleTime)); + if ((index ^ 0) === index) { + index += 1; } } - - return times; } -function checkPossibleTime(bestAttackTime, possibleTime, durationInMilliseconds) { - for (var i = 0; i < bestAttackTime.length; i ++) { - if (containsRange(bestAttackTime[i], { - from: possibleTime, - to: possibleTime + durationInMilliseconds - })) { +function getIndex(intervals, possibleTime) { + for (var i = 0.0; i < intervals.length - 1; i ++) { + if (intervals[i].from <= possibleTime && + possibleTime <= intervals[i].to) { - return true; + return i; + } else if (intervals[i].to <= possibleTime && + possibleTime <= intervals[i + 1].from) { + + return i + 0.5; } } - return false; + if (intervals[intervals.length - 1].from <= possibleTime && + possibleTime <= intervals[intervals.length - 1].to) { + + return intervals.length - 1; + } + + if (intervals[intervals.length - 1].to <= possibleTime) { + + return intervals.length - 0.5; + } } -function containsRange(interval, timeToCheck) { +function check(possibleTime, durationInMillisecond, intervals) { + var index = getIndex(intervals, possibleTime); + try { + if (possibleTime + durationInMillisecond <= intervals[index].to) { + + return true; + } + } catch(e) { - return interval.from <= timeToCheck.from && timeToCheck.from <= interval.to && - interval.from <= timeToCheck.to && timeToCheck.to <= interval.to; + return false; + } + + return false; } function filterSchedule(schedule, workingHours, duration) { From f28c9e7278f70b113d83d4940fb31c7db2bdedd4 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 19:55:12 +0500 Subject: [PATCH 23/29] ... --- robbery.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 5c179c7..538e537 100644 --- a/robbery.js +++ b/robbery.js @@ -111,9 +111,11 @@ function process(intervals, result, durationInMillisecond) { result.push(possibleTime); possibleTime += halfHour; } - index = Math.ceil(getIndex(intervals, possibleTime)); - if ((index ^ 0) === index) { + index = getIndex(intervals, possibleTime); + if (Math.ceil(index) === index) { index += 1; + } else { + index = Math.ceil(index); } } } @@ -150,7 +152,7 @@ function check(possibleTime, durationInMillisecond, intervals) { return true; } - } catch(e) { + } catch (e) { return false; } From 7f26f097bc9be5a8beff7315f666f9e7de9d2863 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 19:56:45 +0500 Subject: [PATCH 24/29] isStart = true --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 538e537..cd3497c 100644 --- a/robbery.js +++ b/robbery.js @@ -1,6 +1,6 @@ 'use strict'; -exports.isStar = false; +exports.isStar = true; var TIME_ZONE; From 1874904c2b5cc703c4436654c95eb5d44adce40a Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 20:25:49 +0500 Subject: [PATCH 25/29] ... --- robbery.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/robbery.js b/robbery.js index cd3497c..e95afdf 100644 --- a/robbery.js +++ b/robbery.js @@ -14,6 +14,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { schedule = invertSchedule(schedule); setTimeStamps(schedule); + console.log(schedule); + fixErrors(schedule); + console.log(schedule); + var bestAttackTime = filterSchedule(schedule, workingHours, duration); var startTimesToAttack = getStartTimes(bestAttackTime, duration); @@ -78,6 +82,22 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; +function fixErrors(schedule) { + Object.keys(schedule).forEach(function (name) { + var busyTime = schedule[name]; + var delIndexes = []; + for (var i = 0; i < busyTime.length; i++) { + if (busyTime[i].from > busyTime[i].to) { + delIndexes.push(i); + } + } + + delIndexes.forEach(function (index) { + busyTime.splice(index, 1); + }); + }); +} + function addZero(digit) { if (digit < 10) { From a5f83c13faa2b8587ac5defe169d8d0870922f36 Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 20:28:11 +0500 Subject: [PATCH 26/29] ... --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index e95afdf..ff4975c 100644 --- a/robbery.js +++ b/robbery.js @@ -14,9 +14,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { schedule = invertSchedule(schedule); setTimeStamps(schedule); - console.log(schedule); + // console.log(schedule); fixErrors(schedule); - console.log(schedule); + // console.log(schedule); var bestAttackTime = filterSchedule(schedule, workingHours, duration); var startTimesToAttack = getStartTimes(bestAttackTime, duration); From e4ea90f6d8d1259cbde050736b83b9f76ca12c1c Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 19:51:03 +0100 Subject: [PATCH 27/29] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 54 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index ff4975c..521f91d 100644 --- a/robbery.js +++ b/robbery.js @@ -8,35 +8,38 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var index = 0; // console.info(schedule, duration, workingHours); - TIME_ZONE = parseInt(workingHours.from.split('+')[1]) % 24; - workingHours.from = workingHours.from.replace(/\+\d+/, '+' + TIME_ZONE); - workingHours.to = workingHours.to.replace(/\+\d+/, '+' + TIME_ZONE); + TIME_ZONE = parseInt(workingHours.from.split('+')[1]); + // workingHours.from = workingHours.from.replace(/\+\d+/, '+' + TIME_ZONE); + // workingHours.to = workingHours.to.replace(/\+\d+/, '+' + TIME_ZONE); + + schedule = invertSchedule(schedule); + setTimeStamps(schedule); // console.log(schedule); fixErrors(schedule); // console.log(schedule); - + print(schedule); var bestAttackTime = filterSchedule(schedule, workingHours, duration); var startTimesToAttack = getStartTimes(bestAttackTime, duration); // console.log(bestAttackTime); - // console.log(startTimesToAttack.map(function (time) { - // return toStr(time); - // })); + console.log(startTimesToAttack.map(function (time) { + return toStr(time); + })); var replacer = function (match, p) { var timeDiff = TIME_ZONE * 60 * 60 * 1000; var format = { '%HH': function (time) { - var hours = new Date(time + timeDiff).getUTCHours(); + var hours = new Date(time).getHours(); return addZero(hours); }, '%MM': function (time) { - var minutes = new Date(time + timeDiff).getUTCMinutes(); + var minutes = new Date(time).getMinutes(); return addZero(minutes); }, @@ -48,7 +51,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { 3: 'СР', 4: 'ЧТ' }; - var day = new Date(time + timeDiff).getUTCDay(); + var day = new Date(time).getDay(); return days[day]; } @@ -82,6 +85,32 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; +function print(schedule) { + var names = Object.keys(schedule); + names.forEach(function (name) { + console.log(name + ':'); + for (var i = 0; i < schedule[name].length; i++) { + console.log(toStr(schedule[name][i].from) + ' - ' + toStr(schedule[name][i].to)); + } + }); +} + +function toStr(time) { + var timeDiff = TIME_ZONE * 60 * 60 * 1000; + var days = { + 0: 'ВС', + 1: 'ПН', + 2: 'ВТ', + 3: 'СР', + 4: 'ЧТ' + }; + var day = new Date(time).getUTCDay(); + day = days[day]; + var minutes = new Date(time).getUTCMinutes(); + var hours = new Date(time ).getUTCHours(); + return day + ' ' + addZero(hours) + ':' + addZero(minutes); +} + function fixErrors(schedule) { Object.keys(schedule).forEach(function (name) { var busyTime = schedule[name]; @@ -281,5 +310,8 @@ function getTimeStamp(date) { var day = date.split(' ')[0]; var time = date.split(' ')[1]; - return Date.parse(toFullDate[day] + ' ' + time); + var _date = new Date(toFullDate[day] + ' ' + time); + _date.setHours(_date.getHours() + TIME_ZONE); + + return _date.getTime(); } From 6bc675448d5d1ce42a0a5c7f457c878ae08054ba Mon Sep 17 00:00:00 2001 From: FairTex Date: Wed, 26 Oct 2016 19:57:12 +0100 Subject: [PATCH 28/29] Eslint --- robbery.js | 40 +++++++--------------------------------- 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/robbery.js b/robbery.js index 521f91d..b7b8e13 100644 --- a/robbery.js +++ b/robbery.js @@ -12,26 +12,24 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { // workingHours.from = workingHours.from.replace(/\+\d+/, '+' + TIME_ZONE); // workingHours.to = workingHours.to.replace(/\+\d+/, '+' + TIME_ZONE); - - schedule = invertSchedule(schedule); setTimeStamps(schedule); // console.log(schedule); fixErrors(schedule); // console.log(schedule); - print(schedule); + // print(schedule); var bestAttackTime = filterSchedule(schedule, workingHours, duration); var startTimesToAttack = getStartTimes(bestAttackTime, duration); // console.log(bestAttackTime); - console.log(startTimesToAttack.map(function (time) { - return toStr(time); - })); + // console.log(startTimesToAttack.map(function (time) { + // return toStr(time); + // })); var replacer = function (match, p) { - var timeDiff = TIME_ZONE * 60 * 60 * 1000; + // var timeDiff = TIME_ZONE * 60 * 60 * 1000; var format = { '%HH': function (time) { var hours = new Date(time).getHours(); @@ -62,11 +60,13 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return { exists: function () { + return startTimesToAttack.length > 0; }, format: function (template) { if (!this.exists()) { + return ''; } @@ -85,32 +85,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { }; }; -function print(schedule) { - var names = Object.keys(schedule); - names.forEach(function (name) { - console.log(name + ':'); - for (var i = 0; i < schedule[name].length; i++) { - console.log(toStr(schedule[name][i].from) + ' - ' + toStr(schedule[name][i].to)); - } - }); -} - -function toStr(time) { - var timeDiff = TIME_ZONE * 60 * 60 * 1000; - var days = { - 0: 'ВС', - 1: 'ПН', - 2: 'ВТ', - 3: 'СР', - 4: 'ЧТ' - }; - var day = new Date(time).getUTCDay(); - day = days[day]; - var minutes = new Date(time).getUTCMinutes(); - var hours = new Date(time ).getUTCHours(); - return day + ' ' + addZero(hours) + ':' + addZero(minutes); -} - function fixErrors(schedule) { Object.keys(schedule).forEach(function (name) { var busyTime = schedule[name]; From f6f54f9f90521339004abefc233cb8a4ee56ab9a Mon Sep 17 00:00:00 2001 From: FairTex Date: Thu, 27 Oct 2016 00:14:38 +0500 Subject: [PATCH 29/29] ... --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index b7b8e13..9ebf6aa 100644 --- a/robbery.js +++ b/robbery.js @@ -227,7 +227,7 @@ function invertSchedule(schedule) { var freeTime = []; freeTime.push({ - from: 'ПН 00:00+' + TIME_ZONE.toString(), + from: 'ПН 00:00+23', // + TIME_ZONE.toString() to: busyTime[0].from }); @@ -240,7 +240,7 @@ function invertSchedule(schedule) { freeTime.push({ from: busyTime[busyTime.length - 1].to, - to: 'ВС 23:59+' + TIME_ZONE.toString() + to: 'ВС 23:59+23' // + TIME_ZONE.toString() }); _new[name] = freeTime; });