From 99678f0b4e950f54956e6f7da35f9e06084977b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Wed, 26 Oct 2016 23:32:08 +0500 Subject: [PATCH 01/33] Update robbery.js --- robbery.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..124ce91 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,63 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = true; +exports.isStar = false; + +var DaysOfWeek = { + 'ПН': 1, + 'ВТ': 2, + 'СР': 3, + 'ЧТ': 4, + 'ПТ': 5, + 'СБ': 6, + 'ВС': 7 +} + +function toDate (date) { + var dayOfWeek = date.substring(0, 2); + var day = DaysOfWeek[dayOfWeek]; + var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)); + var minutes = parseInt (date.substring(6, 8)); + return new Date (2016, 09, day, hours, minutes); +} + +function newSchedule (schedule) { + var schedule1 = {}; + for (var key in schedule) { + var n = (schedule[key]).length; + schedule1[key] = []; + for (var i = 0; i < n; i++) { + schedule1[key][i] = { + from: toDate (schedule[key][i].from), + to: toDate (schedule[key][i].to) + } + schedule1[key].len = n; + } + } + return schedule1; +} + +function maxFrom (date1, date2, date3) { + if (date1.from > date2.from && date1.from > date3.from) return date1.from; + if (date2.from > date3.from) return date2.from; + return date3.from; +} + +function minTo (date1, date2, date3) { + if (date1.to < date2.to && date1.to < date3.to) return date1.to; + if (date2.to < date3.to) return date2.to; + return date3.to; +} + +function maxDate (date1, date2) { + if (date1 > date2) { return date1;} + return date2; +} + +function minDate (date1, date2) { + if (date1 < date2) { return date1;} + return date2; +} /** * @param {Object} schedule – Расписание Банды @@ -14,8 +70,13 @@ exports.isStar = true; * @param {String} workingHours.to – Время закрытия, например, "18:00+5" * @returns {Object} */ + exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + //console.info(schedule, duration, workingHours); + //переводим расписание в формат Date() + var schedule1 = {}; + schedule1 = newSchedule (schedule); + console.info(schedule1); return { @@ -36,15 +97,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { */ format: function (template) { return template; - }, - - /** - * Попробовать найти часы для ограбления позже [*] - * @star - * @returns {Boolean} - */ - tryLater: function () { - return false; } }; }; From e5d2f85bcd0e5ba9c786c698c4cb30e6a4b02d51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Thu, 27 Oct 2016 00:25:37 +0500 Subject: [PATCH 02/33] Update robbery.js --- robbery.js | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 124ce91..c7170c6 100644 --- a/robbery.js +++ b/robbery.js @@ -62,6 +62,109 @@ function minDate (date1, date2) { return date2; } +function toFreeSchedule (schedule) { + var freeSchedule = {}; + for (var key in schedule) { + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 09, 1, 0, 0), + to: schedule[key][0].from + } + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from + } + } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 09, 3, 23, 59) + } + freeSchedule[key].len = n + 1; + } + + return freeSchedule; +} + +function gangFreeTime (schedule) { + var freeTime = []; + var c = 0; + var D = schedule.Danny.len; + var R = schedule.Rusty.len; + var L = schedule.Linus.len; + //console.log (D, R, L); + for (var i = 0; i < D; i++) { + for (var j = 0; j < R; j++) { + for (var k = 0; k < L; k++) { + var date1 = schedule.Danny[i]; + var date2 = schedule.Rusty[j]; + var date3 = schedule.Linus[k]; + //console.log (date1); + var a = maxFrom (date1, date2, date3); + var b = minTo (date1, date2, date3); + if (b > a) { + freeTime[c] = { + from: a, + to: b + } + c++; + } + } + } + } + + return freeTime; +} + +function timeForRobbery (gangFreeTime, workingHours) { + var n = gangFreeTime.length; + var res = []; + var c = 0; + for (var i = 0; i < n; i++) { + //console.log ((gangFreeTime[i].from).getDate()); + //var day = 1; + var day = gangFreeTime[i].from.getDate(); + if (day > 3) { return res;} + var date1 = workingHours.from; + var date2 = workingHours.to; + var hour1 = parseInt (date1.substring (0, 2)) - parseInt (date1.substring (5)); + var hour2 = parseInt (date2.substring (0, 2)) - parseInt (date2.substring (5)); + var minutes1 = parseInt (date1.substring (3, 5)); + var minutes2 = parseInt (date2.substring (3, 5)); + var bankWorkFrom = new Date (2016, 09, day, hour1, minutes1); + var bankWorkTo = new Date (2016, 09, day, hour2, minutes2); + var a = maxDate (gangFreeTime[i].from, bankWorkFrom); + var b = minDate (gangFreeTime[i].to, bankWorkTo); + if (b > a) { + res[c] = { + from: a, + to: b + } + c++; + } + } + + return res; +} + +function mainFunction (time_to_robbery, duration) {//не знаю как назвать функцию=( + var msInDuration = duration*60*1000; + var res = []; + var n = time_to_robbery.length; + var c = 0; + for (var i = 0; i < n; i++) { + var a = (time_to_robbery[i].to - time_to_robbery[i].from); + if (a >= msInDuration) { + //console.log(a); + res[c] = time_to_robbery[i]; + c++; + } + } + //console.log (res); + return res; // время для ограбления +} + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -76,16 +179,26 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { //переводим расписание в формат Date() var schedule1 = {}; schedule1 = newSchedule (schedule); - console.info(schedule1); + //console.info(schedule1); + var freeSchedule = toFreeSchedule (schedule1); + //console.info(freeSchedule); + var freeTime = gangFreeTime (freeSchedule); + //console.info(freeTime); + var time_for_robbery = timeForRobbery (freeTime, workingHours); + //console.info(time_for_robbery); + var time_to_robbery = mainFunction (time_for_robbery, duration); + //console.info(time_to_robbery); return { + time: time_to_robbery, + /** * Найдено ли время * @returns {Boolean} */ exists: function () { - return false; + return (time_to_robbery !== []); }, /** From 2b51aa0b72b10782d937f90185ae448d0e01b40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Thu, 27 Oct 2016 00:48:48 +0500 Subject: [PATCH 03/33] Update robbery.js --- robbery.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/robbery.js b/robbery.js index c7170c6..e2a7d36 100644 --- a/robbery.js +++ b/robbery.js @@ -21,7 +21,7 @@ function toDate (date) { var day = DaysOfWeek[dayOfWeek]; var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)); var minutes = parseInt (date.substring(6, 8)); - return new Date (2016, 09, day, hours, minutes); + return new Date (2016, 9, day, hours, minutes); } function newSchedule (schedule) { @@ -68,7 +68,7 @@ function toFreeSchedule (schedule) { freeSchedule[key] = []; var n = (schedule[key]).length; freeSchedule[key][0] = { - from: new Date (2016, 09, 1, 0, 0), + from: new Date (2016, 9, 1, 0, 0), to: schedule[key][0].from } for (var i = 1; i < n; i++) { @@ -79,7 +79,7 @@ function toFreeSchedule (schedule) { } freeSchedule[key][n] = { from: schedule[key][n - 1].to, - to: new Date (2016, 09, 3, 23, 59) + to: new Date (2016, 9, 3, 23, 59) } freeSchedule[key].len = n + 1; } @@ -148,20 +148,22 @@ function timeForRobbery (gangFreeTime, workingHours) { return res; } -function mainFunction (time_to_robbery, duration) {//не знаю как назвать функцию=( +function mainFunction (time_to_robbery, duration, res) {//не знаю как назвать функцию=( var msInDuration = duration*60*1000; - var res = []; + //var res = []; var n = time_to_robbery.length; var c = 0; for (var i = 0; i < n; i++) { var a = (time_to_robbery[i].to - time_to_robbery[i].from); if (a >= msInDuration) { //console.log(a); + res[c] = {}; res[c] = time_to_robbery[i]; c++; } } //console.log (res); + if (c === 0) {return null;} return res; // время для ограбления } @@ -186,7 +188,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { //console.info(freeTime); var time_for_robbery = timeForRobbery (freeTime, workingHours); //console.info(time_for_robbery); - var time_to_robbery = mainFunction (time_for_robbery, duration); + var time_to_robbery = mainFunction (time_for_robbery, duration, []); //console.info(time_to_robbery); return { @@ -198,7 +200,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return (time_to_robbery !== []); + //console.log (this.time); + return (this.time !== null); }, /** From 3061156ca0a16d927c5b24d2bbb77d4747a2b96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Thu, 27 Oct 2016 00:52:37 +0500 Subject: [PATCH 04/33] Update robbery.js --- robbery.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index e2a7d36..3f9dbde 100644 --- a/robbery.js +++ b/robbery.js @@ -132,8 +132,8 @@ function timeForRobbery (gangFreeTime, workingHours) { var hour2 = parseInt (date2.substring (0, 2)) - parseInt (date2.substring (5)); var minutes1 = parseInt (date1.substring (3, 5)); var minutes2 = parseInt (date2.substring (3, 5)); - var bankWorkFrom = new Date (2016, 09, day, hour1, minutes1); - var bankWorkTo = new Date (2016, 09, day, hour2, minutes2); + var bankWorkFrom = new Date (2016, 9, day, hour1, minutes1); + var bankWorkTo = new Date (2016, 9, day, hour2, minutes2); var a = maxDate (gangFreeTime[i].from, bankWorkFrom); var b = minDate (gangFreeTime[i].to, bankWorkTo); if (b > a) { From b10d3696bf792675dbeb780373654a250f9bce80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 28 Oct 2016 00:38:36 +0500 Subject: [PATCH 05/33] Update robbery.js --- robbery.js | 127 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/robbery.js b/robbery.js index 3f9dbde..9a7b976 100644 --- a/robbery.js +++ b/robbery.js @@ -14,55 +14,73 @@ var DaysOfWeek = { 'ПТ': 5, 'СБ': 6, 'ВС': 7 -} +}; -function toDate (date) { +function toDate(date) { var dayOfWeek = date.substring(0, 2); var day = DaysOfWeek[dayOfWeek]; var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)); var minutes = parseInt (date.substring(6, 8)); + return new Date (2016, 9, day, hours, minutes); } -function newSchedule (schedule) { - var schedule1 = {}; +function toNewSchedule(schedule) { + var newSchedule = {}; for (var key in schedule) { var n = (schedule[key]).length; - schedule1[key] = []; + newSchedule[key] = []; for (var i = 0; i < n; i++) { - schedule1[key][i] = { + newSchedule[key][i] = { from: toDate (schedule[key][i].from), to: toDate (schedule[key][i].to) - } - schedule1[key].len = n; + }; + newSchedule[key].len = n; } } - return schedule1; + + return newSchedule; } -function maxFrom (date1, date2, date3) { - if (date1.from > date2.from && date1.from > date3.from) return date1.from; - if (date2.from > date3.from) return date2.from; +function maxFrom(date1, date2, date3) { + if (date1.from > date2.from && date1.from > date3.from) { + return date1.from; + } + if (date2.from > date3.from) { + return date2.from; + } + return date3.from; } -function minTo (date1, date2, date3) { - if (date1.to < date2.to && date1.to < date3.to) return date1.to; - if (date2.to < date3.to) return date2.to; +function minTo(date1, date2, date3) { + if (date1.to < date2.to && date1.to < date3.to) { + return date1.to; + } + if (date2.to < date3.to) { + return date2.to; + } + return date3.to; } -function maxDate (date1, date2) { - if (date1 > date2) { return date1;} +function maxDate(date1, date2) { + if (date1 > date2) { + return date1; + } + return date2; } -function minDate (date1, date2) { - if (date1 < date2) { return date1;} +function minDate(date1, date2) { + if (date1 < date2) { + return date1; + } + return date2; } -function toFreeSchedule (schedule) { +function toFreeSchedule(schedule) { var freeSchedule = {}; for (var key in schedule) { freeSchedule[key] = []; @@ -70,62 +88,62 @@ function toFreeSchedule (schedule) { freeSchedule[key][0] = { from: new Date (2016, 9, 1, 0, 0), to: schedule[key][0].from - } + }; for (var i = 1; i < n; i++) { freeSchedule[key][i] = { from: schedule[key][i - 1].to, to: schedule[key][i].from - } + }; } freeSchedule[key][n] = { from: schedule[key][n - 1].to, to: new Date (2016, 9, 3, 23, 59) - } + }; freeSchedule[key].len = n + 1; } - + return freeSchedule; } -function gangFreeTime (schedule) { +function gangFreeTime(schedule) { var freeTime = []; var c = 0; var D = schedule.Danny.len; var R = schedule.Rusty.len; var L = schedule.Linus.len; - //console.log (D, R, L); + // console.log (D, R, L); for (var i = 0; i < D; i++) { for (var j = 0; j < R; j++) { for (var k = 0; k < L; k++) { var date1 = schedule.Danny[i]; var date2 = schedule.Rusty[j]; var date3 = schedule.Linus[k]; - //console.log (date1); + // console.log (date1); var a = maxFrom (date1, date2, date3); var b = minTo (date1, date2, date3); if (b > a) { freeTime[c] = { from: a, to: b - } + }; c++; } } } } - + return freeTime; } -function timeForRobbery (gangFreeTime, workingHours) { +function timeForRobbery(gangFreeTime, workingHours) { var n = gangFreeTime.length; var res = []; var c = 0; for (var i = 0; i < n; i++) { - //console.log ((gangFreeTime[i].from).getDate()); - //var day = 1; + // console.log ((gangFreeTime[i].from).getDate()); + // var day = 1; var day = gangFreeTime[i].from.getDate(); - if (day > 3) { return res;} + if (day > 3) {return res; } var date1 = workingHours.from; var date2 = workingHours.to; var hour1 = parseInt (date1.substring (0, 2)) - parseInt (date1.substring (5)); @@ -140,17 +158,17 @@ function timeForRobbery (gangFreeTime, workingHours) { res[c] = { from: a, to: b - } + }; c++; } } - + return res; } -function mainFunction (time_to_robbery, duration, res) {//не знаю как назвать функцию=( - var msInDuration = duration*60*1000; - //var res = []; +function mainFunction(time_to_robbery, duration, res) {// не знаю как назвать функцию=( + var msInDuration = duration * 60 * 1000; + // var res = []; var n = time_to_robbery.length; var c = 0; for (var i = 0; i < n; i++) { @@ -162,8 +180,11 @@ function mainFunction (time_to_robbery, duration, res) {//не знаю как c++; } } - //console.log (res); - if (c === 0) {return null;} + // console.log (res); + if (c === 0) { + return null; + } + return res; // время для ограбления } @@ -177,31 +198,29 @@ function mainFunction (time_to_robbery, duration, res) {//не знаю как */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - //console.info(schedule, duration, workingHours); - //переводим расписание в формат Date() + // console.info(schedule, duration, workingHours); + // переводим расписание в формат Date() var schedule1 = {}; - schedule1 = newSchedule (schedule); - //console.info(schedule1); + schedule1 = toNewSchedule (schedule); + // console.info(schedule1); var freeSchedule = toFreeSchedule (schedule1); - //console.info(freeSchedule); + // console.info(freeSchedule); var freeTime = gangFreeTime (freeSchedule); - //console.info(freeTime); + // console.info(freeTime); var time_for_robbery = timeForRobbery (freeTime, workingHours); - //console.info(time_for_robbery); + // console.info(time_for_robbery); var time_to_robbery = mainFunction (time_for_robbery, duration, []); //console.info(time_to_robbery); return { - - time: time_to_robbery, - + /** * Найдено ли время * @returns {Boolean} */ exists: function () { - //console.log (this.time); - return (this.time !== null); + // console.log (this.time); + return (time_to_robbery !== null); }, /** @@ -212,6 +231,10 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { + if (!this.exists()) { + return ''; + } + return template; } }; From af12ee93196ddcd64814319e8ad1a29024c81c59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 28 Oct 2016 18:44:54 +0500 Subject: [PATCH 06/33] Update robbery.js --- robbery.js | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/robbery.js b/robbery.js index 9a7b976..7bb7fc1 100644 --- a/robbery.js +++ b/robbery.js @@ -107,26 +107,19 @@ function toFreeSchedule(schedule) { function gangFreeTime(schedule) { var freeTime = []; - var c = 0; var D = schedule.Danny.len; var R = schedule.Rusty.len; var L = schedule.Linus.len; - // console.log (D, R, L); for (var i = 0; i < D; i++) { for (var j = 0; j < R; j++) { for (var k = 0; k < L; k++) { - var date1 = schedule.Danny[i]; - var date2 = schedule.Rusty[j]; - var date3 = schedule.Linus[k]; - // console.log (date1); - var a = maxFrom (date1, date2, date3); - var b = minTo (date1, date2, date3); + var a = maxFrom (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); + var b = minTo (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); if (b > a) { - freeTime[c] = { + freeTime.push( { from: a, to: b - }; - c++; + }); } } } @@ -135,7 +128,7 @@ function gangFreeTime(schedule) { return freeTime; } -function timeForRobbery(gangFreeTime, workingHours) { +function FindtimeForRobbery(gangFreeTime, workingHours) { var n = gangFreeTime.length; var res = []; var c = 0; @@ -166,17 +159,17 @@ function timeForRobbery(gangFreeTime, workingHours) { return res; } -function mainFunction(time_to_robbery, duration, res) {// не знаю как назвать функцию=( +function mainFunction(timeToRobbery, duration) {// не знаю как назвать функцию=( var msInDuration = duration * 60 * 1000; - // var res = []; - var n = time_to_robbery.length; + var res = []; + var n = timeToRobbery.length; var c = 0; for (var i = 0; i < n; i++) { - var a = (time_to_robbery[i].to - time_to_robbery[i].from); + var a = (timeToRobbery[i].to - timeToRobbery[i].from); if (a >= msInDuration) { //console.log(a); res[c] = {}; - res[c] = time_to_robbery[i]; + res[c] = timeToRobbery[i]; c++; } } @@ -202,25 +195,20 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { // переводим расписание в формат Date() var schedule1 = {}; schedule1 = toNewSchedule (schedule); - // console.info(schedule1); var freeSchedule = toFreeSchedule (schedule1); - // console.info(freeSchedule); var freeTime = gangFreeTime (freeSchedule); - // console.info(freeTime); - var time_for_robbery = timeForRobbery (freeTime, workingHours); - // console.info(time_for_robbery); - var time_to_robbery = mainFunction (time_for_robbery, duration, []); - //console.info(time_to_robbery); + var timeForRobbery = FindtimeForRobbery (freeTime, workingHours); + var timeToRobbery = mainFunction (timeForRobbery, duration); return { - + /** * Найдено ли время * @returns {Boolean} */ exists: function () { // console.log (this.time); - return (time_to_robbery !== null); + return (timeToRobbery !== null); }, /** From 79354b5c23ca3832ce9df76e941b0b0d4487fff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 28 Oct 2016 21:22:36 +0500 Subject: [PATCH 07/33] Update robbery.js --- robbery.js | 134 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 54 deletions(-) diff --git a/robbery.js b/robbery.js index 7bb7fc1..d9187ff 100644 --- a/robbery.js +++ b/robbery.js @@ -28,14 +28,16 @@ function toDate(date) { function toNewSchedule(schedule) { var newSchedule = {}; for (var key in schedule) { - var n = (schedule[key]).length; - newSchedule[key] = []; - for (var i = 0; i < n; i++) { - newSchedule[key][i] = { - from: toDate (schedule[key][i].from), - to: toDate (schedule[key][i].to) - }; - newSchedule[key].len = n; + if ({}.hasOwnProperty.call(schedule, key)) { + var n = (schedule[key]).length; + newSchedule[key] = []; + for (var i = 0; i < n; i++) { + newSchedule[key][i] = { + from: toDate (schedule[key][i].from), + to: toDate (schedule[key][i].to) + }; + newSchedule[key].len = n; + } } } @@ -83,23 +85,25 @@ function minDate(date1, date2) { function toFreeSchedule(schedule) { var freeSchedule = {}; for (var key in schedule) { - freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from + if ({}.hasOwnProperty.call(schedule, key)) { + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: schedule[key][0].from + }; + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from + }; + } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 9, 3, 23, 59) }; + freeSchedule[key].len = n + 1; } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, - to: new Date (2016, 9, 3, 23, 59) - }; - freeSchedule[key].len = n + 1; } return freeSchedule; @@ -110,13 +114,14 @@ function gangFreeTime(schedule) { var D = schedule.Danny.len; var R = schedule.Rusty.len; var L = schedule.Linus.len; + function func() { for (var i = 0; i < D; i++) { for (var j = 0; j < R; j++) { for (var k = 0; k < L; k++) { var a = maxFrom (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); var b = minTo (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); if (b > a) { - freeTime.push( { + freeTime.push({ from: a, to: b }); @@ -124,42 +129,44 @@ function gangFreeTime(schedule) { } } } + } + func(); return freeTime; } -function FindtimeForRobbery(gangFreeTime, workingHours) { - var n = gangFreeTime.length; +function findTimeForRobbery(freeTime, workingHours) { + var n = freeTime.length; var res = []; - var c = 0; + var date = [workingHours.from, workingHours.to]; + var hours = []; + var minutes = []; + var bank = []; + hours[0] = parseInt (date[0].substring (0, 2)) - parseInt (date[0].substring (5)); + hours[1] = parseInt (date[1].substring (0, 2)) - parseInt (date[1].substring (5)); + minutes[0] = parseInt (date[0].substring (3, 5)); + minutes[1] = parseInt (date[1].substring (3, 5)); for (var i = 0; i < n; i++) { - // console.log ((gangFreeTime[i].from).getDate()); - // var day = 1; - var day = gangFreeTime[i].from.getDate(); - if (day > 3) {return res; } - var date1 = workingHours.from; - var date2 = workingHours.to; - var hour1 = parseInt (date1.substring (0, 2)) - parseInt (date1.substring (5)); - var hour2 = parseInt (date2.substring (0, 2)) - parseInt (date2.substring (5)); - var minutes1 = parseInt (date1.substring (3, 5)); - var minutes2 = parseInt (date2.substring (3, 5)); - var bankWorkFrom = new Date (2016, 9, day, hour1, minutes1); - var bankWorkTo = new Date (2016, 9, day, hour2, minutes2); - var a = maxDate (gangFreeTime[i].from, bankWorkFrom); - var b = minDate (gangFreeTime[i].to, bankWorkTo); + var day = freeTime[i].from.getDate(); + if (day > 3) { + return res; + } + bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); + bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); + var a = maxDate (freeTime[i].from, bank[0]); + var b = minDate (freeTime[i].to, bank[1]); if (b > a) { - res[c] = { + res.push({ from: a, to: b - }; - c++; + }) } } return res; } -function mainFunction(timeToRobbery, duration) {// не знаю как назвать функцию=( +function mainFunction(timeToRobbery, duration) { // не знаю как назвать функцию=( var msInDuration = duration * 60 * 1000; var res = []; var n = timeToRobbery.length; @@ -167,13 +174,11 @@ function mainFunction(timeToRobbery, duration) {// не знаю как назв for (var i = 0; i < n; i++) { var a = (timeToRobbery[i].to - timeToRobbery[i].from); if (a >= msInDuration) { - //console.log(a); res[c] = {}; res[c] = timeToRobbery[i]; c++; } } - // console.log (res); if (c === 0) { return null; } @@ -181,6 +186,23 @@ function mainFunction(timeToRobbery, duration) {// не знаю как назв return res; // время для ограбления } +function formateDate(date, gmt) { + var day = date.getDate(); + var hours = date.getHours() + gmt; + var minutes = date.getMinutes(); + var week = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; + day = week[day]; + if (hours < 10) { + hours = '0' + hours; + } + hours = '' + hours; + if (minutes < 10) { + minutes = '0' + minutes; + } + minutes = '' + minutes; + return [day, hours, minutes]; +} + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -191,14 +213,13 @@ function mainFunction(timeToRobbery, duration) {// не знаю как назв */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - // console.info(schedule, duration, workingHours); - // переводим расписание в формат Date() - var schedule1 = {}; - schedule1 = toNewSchedule (schedule); - var freeSchedule = toFreeSchedule (schedule1); + var newSchedule = {}; + newSchedule = toNewSchedule (schedule); + var freeSchedule = toFreeSchedule (newSchedule); var freeTime = gangFreeTime (freeSchedule); - var timeForRobbery = FindtimeForRobbery (freeTime, workingHours); + var timeForRobbery = findTimeForRobbery (freeTime, workingHours); var timeToRobbery = mainFunction (timeForRobbery, duration); + var gmt = parseInt (workingHours.from.substring(6)); return { @@ -222,8 +243,13 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (!this.exists()) { return ''; } + var format = formateDate(timeToRobbery[0].from, gmt); + var newTemplate = template + .replace('%DD', format[0]) + .replace('%HH', format[1]) + .replace('%MM', format[2]); - return template; + return newTemplate; } }; }; From d4f8f5459ffd32891bd62b11325d5459f622ea12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 28 Oct 2016 21:39:49 +0500 Subject: [PATCH 08/33] Update robbery.js --- robbery.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/robbery.js b/robbery.js index d9187ff..0c56ff8 100644 --- a/robbery.js +++ b/robbery.js @@ -114,23 +114,23 @@ function gangFreeTime(schedule) { var D = schedule.Danny.len; var R = schedule.Rusty.len; var L = schedule.Linus.len; - function func() { + function func(i, j, k) { + var a = maxFrom (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); + var b = minTo (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); + if (b > a) { + freeTime.push({ + from: a, + to: b + }); + } + } for (var i = 0; i < D; i++) { for (var j = 0; j < R; j++) { for (var k = 0; k < L; k++) { - var a = maxFrom (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); - var b = minTo (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); - if (b > a) { - freeTime.push({ - from: a, - to: b - }); - } + func(i, j, k); } } } - } - func(); return freeTime; } @@ -144,7 +144,7 @@ function findTimeForRobbery(freeTime, workingHours) { var bank = []; hours[0] = parseInt (date[0].substring (0, 2)) - parseInt (date[0].substring (5)); hours[1] = parseInt (date[1].substring (0, 2)) - parseInt (date[1].substring (5)); - minutes[0] = parseInt (date[0].substring (3, 5)); + minutes[0] = parseInt (date[0].substring (3, 5)); minutes[1] = parseInt (date[1].substring (3, 5)); for (var i = 0; i < n; i++) { var day = freeTime[i].from.getDate(); @@ -159,7 +159,7 @@ function findTimeForRobbery(freeTime, workingHours) { res.push({ from: a, to: b - }) + }); } } @@ -195,11 +195,12 @@ function formateDate(date, gmt) { if (hours < 10) { hours = '0' + hours; } - hours = '' + hours; + hours = String(hours); if (minutes < 10) { minutes = '0' + minutes; } - minutes = '' + minutes; + minutes = String(minutes); + return [day, hours, minutes]; } @@ -219,7 +220,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var freeTime = gangFreeTime (freeSchedule); var timeForRobbery = findTimeForRobbery (freeTime, workingHours); var timeToRobbery = mainFunction (timeForRobbery, duration); - var gmt = parseInt (workingHours.from.substring(6)); + var gmt = parseInt (workingHours.from.substring(5)); return { @@ -228,7 +229,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - // console.log (this.time); + return (timeToRobbery !== null); }, From fdea8b6d22055ef7650ce3feec485c283d5e16f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 28 Oct 2016 22:37:44 +0500 Subject: [PATCH 09/33] Update robbery.js --- robbery.js | 65 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/robbery.js b/robbery.js index 0c56ff8..d0b599a 100644 --- a/robbery.js +++ b/robbery.js @@ -27,17 +27,20 @@ function toDate(date) { function toNewSchedule(schedule) { var newSchedule = {}; + function func() { + var n = (schedule[key]).length; + newSchedule[key] = []; + for (var i = 0; i < n; i++) { + newSchedule[key].push({ + from: toDate (schedule[key][i].from), + to: toDate (schedule[key][i].to) + }); + newSchedule[key].len = n; + } + } for (var key in schedule) { if ({}.hasOwnProperty.call(schedule, key)) { - var n = (schedule[key]).length; - newSchedule[key] = []; - for (var i = 0; i < n; i++) { - newSchedule[key][i] = { - from: toDate (schedule[key][i].from), - to: toDate (schedule[key][i].to) - }; - newSchedule[key].len = n; - } + func(); } } @@ -85,25 +88,26 @@ function minDate(date1, date2) { function toFreeSchedule(schedule) { var freeSchedule = {}; for (var key in schedule) { - if ({}.hasOwnProperty.call(schedule, key)) { - freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from - }; - } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, - to: new Date (2016, 9, 3, 23, 59) + if (!({}.hasOwnProperty.call(schedule, key))) { + return []; + } + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: schedule[key][0].from + }; + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from }; - freeSchedule[key].len = n + 1; } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 9, 3, 23, 59) + }; + freeSchedule[key].len = n + 1; } return freeSchedule; @@ -142,19 +146,22 @@ function findTimeForRobbery(freeTime, workingHours) { var hours = []; var minutes = []; var bank = []; + var a; + var b; + var day; hours[0] = parseInt (date[0].substring (0, 2)) - parseInt (date[0].substring (5)); hours[1] = parseInt (date[1].substring (0, 2)) - parseInt (date[1].substring (5)); minutes[0] = parseInt (date[0].substring (3, 5)); minutes[1] = parseInt (date[1].substring (3, 5)); for (var i = 0; i < n; i++) { - var day = freeTime[i].from.getDate(); + day = freeTime[i].from.getDate(); if (day > 3) { return res; } bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); - var a = maxDate (freeTime[i].from, bank[0]); - var b = minDate (freeTime[i].to, bank[1]); + a = maxDate (freeTime[i].from, bank[0]); + b = minDate (freeTime[i].to, bank[1]); if (b > a) { res.push({ from: a, From 332cf192bfdcf8dc7ac640294e6fe721ecf33c65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Sat, 29 Oct 2016 00:28:49 +0500 Subject: [PATCH 10/33] Update robbery.js --- robbery.js | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/robbery.js b/robbery.js index d0b599a..975e99b 100644 --- a/robbery.js +++ b/robbery.js @@ -27,7 +27,7 @@ function toDate(date) { function toNewSchedule(schedule) { var newSchedule = {}; - function func() { + function func(key) { var n = (schedule[key]).length; newSchedule[key] = []; for (var i = 0; i < n; i++) { @@ -40,7 +40,7 @@ function toNewSchedule(schedule) { } for (var key in schedule) { if ({}.hasOwnProperty.call(schedule, key)) { - func(); + func(key); } } @@ -115,26 +115,22 @@ function toFreeSchedule(schedule) { function gangFreeTime(schedule) { var freeTime = []; - var D = schedule.Danny.len; - var R = schedule.Rusty.len; - var L = schedule.Linus.len; - function func(i, j, k) { - var a = maxFrom (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); - var b = minTo (schedule.Danny[i], schedule.Rusty[j], schedule.Linus[k]); - if (b > a) { - freeTime.push({ - from: a, - to: b - }); - } - } - for (var i = 0; i < D; i++) { - for (var j = 0; j < R; j++) { - for (var k = 0; k < L; k++) { - func(i, j, k); - } - } - } + var a, b; + schedule.Danny.forEach(function(i) { + schedule.Rusty.forEach(function(j) { + schedule.Linus.forEach(function(k) { + // console.log([i, j, k]); + a = maxFrom (i, j, k); + b = minTo (i, j, k); + if (b > a) { + freeTime.push({ + from: a, + to: b + }); + } + }) + }) + }); return freeTime; } From 146978c9e877d8c971bebcd1a97a8ea55dc29554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Sat, 29 Oct 2016 01:03:29 +0500 Subject: [PATCH 11/33] Update robbery.js --- robbery.js | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/robbery.js b/robbery.js index 975e99b..195db50 100644 --- a/robbery.js +++ b/robbery.js @@ -115,10 +115,11 @@ function toFreeSchedule(schedule) { function gangFreeTime(schedule) { var freeTime = []; - var a, b; - schedule.Danny.forEach(function(i) { - schedule.Rusty.forEach(function(j) { - schedule.Linus.forEach(function(k) { + var a; + var b; + schedule.Danny.forEach(function (i) { + schedule.Rusty.forEach(function (j) { + schedule.Linus.forEach(function (k) { // console.log([i, j, k]); a = maxFrom (i, j, k); b = minTo (i, j, k); @@ -128,43 +129,41 @@ function gangFreeTime(schedule) { to: b }); } - }) - }) + }); + }); }); return freeTime; } -function findTimeForRobbery(freeTime, workingHours) { - var n = freeTime.length; - var res = []; +function findTimeForRobbery(freeTime, workingHours, res) { + var a; + var b; var date = [workingHours.from, workingHours.to]; var hours = []; var minutes = []; var bank = []; - var a; - var b; var day; - hours[0] = parseInt (date[0].substring (0, 2)) - parseInt (date[0].substring (5)); - hours[1] = parseInt (date[1].substring (0, 2)) - parseInt (date[1].substring (5)); - minutes[0] = parseInt (date[0].substring (3, 5)); - minutes[1] = parseInt (date[1].substring (3, 5)); - for (var i = 0; i < n; i++) { - day = freeTime[i].from.getDate(); + hours[0] = date[0].substring (0, 2) - parseInt (date[0].substring (5)); + hours[1] = date[1].substring (0, 2) - parseInt (date[1].substring (5)); + minutes[0] = date[0].substring (3, 5); + minutes[1] = date[1].substring (3, 5); + freeTime.forEach(function (i) { + day = i.from.getDate(); if (day > 3) { return res; } bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); - a = maxDate (freeTime[i].from, bank[0]); - b = minDate (freeTime[i].to, bank[1]); + a = maxDate (i.from, bank[0]); + b = minDate (i.to, bank[1]); if (b > a) { res.push({ from: a, to: b }); } - } + }); return res; } @@ -221,7 +220,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { newSchedule = toNewSchedule (schedule); var freeSchedule = toFreeSchedule (newSchedule); var freeTime = gangFreeTime (freeSchedule); - var timeForRobbery = findTimeForRobbery (freeTime, workingHours); + var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); var gmt = parseInt (workingHours.from.substring(5)); From 51d9e5e7d675ce25beea5d3a8164734bbe91d121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 13:30:44 +0600 Subject: [PATCH 12/33] Update robbery.js --- robbery.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index 195db50..d400887 100644 --- a/robbery.js +++ b/robbery.js @@ -85,7 +85,7 @@ function minDate(date1, date2) { return date2; } -function toFreeSchedule(schedule) { +function toFreeSchedule(schedule, bankTimeZone) { var freeSchedule = {}; for (var key in schedule) { if (!({}.hasOwnProperty.call(schedule, key))) { @@ -94,7 +94,7 @@ function toFreeSchedule(schedule) { freeSchedule[key] = []; var n = (schedule[key]).length; freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), + from: new Date (2016, 9, 1, -bankTimeZone, 0), to: schedule[key][0].from }; for (var i = 1; i < n; i++) { @@ -217,12 +217,12 @@ function formateDate(date, gmt) { exports.getAppropriateMoment = function (schedule, duration, workingHours) { var newSchedule = {}; + var gmt = parseInt (workingHours.from.substring(5)); newSchedule = toNewSchedule (schedule); - var freeSchedule = toFreeSchedule (newSchedule); + var freeSchedule = toFreeSchedule (newSchedule, gmt); var freeTime = gangFreeTime (freeSchedule); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); - var gmt = parseInt (workingHours.from.substring(5)); return { From a753198dbe76d534c35677150945e321270b53f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 13:00:50 +0500 Subject: [PATCH 13/33] Update robbery.js 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 d400887..d402fc3 100644 --- a/robbery.js +++ b/robbery.js @@ -105,7 +105,7 @@ function toFreeSchedule(schedule, bankTimeZone) { } freeSchedule[key][n] = { from: schedule[key][n - 1].to, - to: new Date (2016, 9, 3, 23, 59) + to: new Date (2016, 9, 7, 23, 59) }; freeSchedule[key].len = n + 1; } From 22966d14ec6223ade90a6f9ea56de873efea9887 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 20:23:55 +0500 Subject: [PATCH 14/33] Update robbery.js --- robbery.js | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/robbery.js b/robbery.js index d402fc3..9d76c6d 100644 --- a/robbery.js +++ b/robbery.js @@ -16,24 +16,24 @@ var DaysOfWeek = { 'ВС': 7 }; -function toDate(date) { +function toDate(date, bankTime) { var dayOfWeek = date.substring(0, 2); var day = DaysOfWeek[dayOfWeek]; - var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)); + var hours = parseInt (date.substring(3, 5)) + parseInt (date.substring(8) - bankTime); var minutes = parseInt (date.substring(6, 8)); return new Date (2016, 9, day, hours, minutes); } -function toNewSchedule(schedule) { +function toNewSchedule(schedule, bankTime) { var newSchedule = {}; function func(key) { var n = (schedule[key]).length; newSchedule[key] = []; for (var i = 0; i < n; i++) { newSchedule[key].push({ - from: toDate (schedule[key][i].from), - to: toDate (schedule[key][i].to) + from: toDate (schedule[key][i].from, bankTime), + to: toDate (schedule[key][i].to, bankTime) }); newSchedule[key].len = n; } @@ -85,7 +85,7 @@ function minDate(date1, date2) { return date2; } -function toFreeSchedule(schedule, bankTimeZone) { +function toFreeSchedule(schedule) { var freeSchedule = {}; for (var key in schedule) { if (!({}.hasOwnProperty.call(schedule, key))) { @@ -94,7 +94,7 @@ function toFreeSchedule(schedule, bankTimeZone) { freeSchedule[key] = []; var n = (schedule[key]).length; freeSchedule[key][0] = { - from: new Date (2016, 9, 1, -bankTimeZone, 0), + from: new Date (2016, 9, 1, 0, 0), to: schedule[key][0].from }; for (var i = 1; i < n; i++) { @@ -143,20 +143,24 @@ function findTimeForRobbery(freeTime, workingHours, res) { var hours = []; var minutes = []; var bank = []; - var day; + var day = []; hours[0] = date[0].substring (0, 2) - parseInt (date[0].substring (5)); hours[1] = date[1].substring (0, 2) - parseInt (date[1].substring (5)); minutes[0] = date[0].substring (3, 5); minutes[1] = date[1].substring (3, 5); freeTime.forEach(function (i) { - day = i.from.getDate(); - if (day > 3) { + day[0] = i.from.getDate(); + day[1] = i.to.getDate(); + if (day > 3 && day < 8) { return res; } - bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); - bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); + bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); + bank[1] = new Date (2016, 9, day[1], hours[1], minutes[1]); a = maxDate (i.from, bank[0]); b = minDate (i.to, bank[1]); + console.log (a); + console.log (b); + console.log (''); if (b > a) { res.push({ from: a, @@ -188,9 +192,9 @@ function mainFunction(timeToRobbery, duration) { // не знаю как наз return res; // время для ограбления } -function formateDate(date, gmt) { +function formateDate(date) { var day = date.getDate(); - var hours = date.getHours() + gmt; + var hours = date.getHours(); var minutes = date.getMinutes(); var week = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; day = week[day]; @@ -218,10 +222,11 @@ function formateDate(date, gmt) { exports.getAppropriateMoment = function (schedule, duration, workingHours) { var newSchedule = {}; var gmt = parseInt (workingHours.from.substring(5)); - newSchedule = toNewSchedule (schedule); - var freeSchedule = toFreeSchedule (newSchedule, gmt); + newSchedule = toNewSchedule (schedule, gmt); + var freeSchedule = toFreeSchedule (newSchedule); var freeTime = gangFreeTime (freeSchedule); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); + // console.log (timeForRobbery); var timeToRobbery = mainFunction (timeForRobbery, duration); return { @@ -246,7 +251,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (!this.exists()) { return ''; } - var format = formateDate(timeToRobbery[0].from, gmt); + var format = formateDate(timeToRobbery[0].from); var newTemplate = template .replace('%DD', format[0]) .replace('%HH', format[1]) From 4ef3f09ae02b65db363d71e37c023454266dc3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 20:26:35 +0500 Subject: [PATCH 15/33] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B2=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=BA=20=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8=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 | 3 --- 1 file changed, 3 deletions(-) diff --git a/robbery.js b/robbery.js index 9d76c6d..579f913 100644 --- a/robbery.js +++ b/robbery.js @@ -158,9 +158,6 @@ function findTimeForRobbery(freeTime, workingHours, res) { bank[1] = new Date (2016, 9, day[1], hours[1], minutes[1]); a = maxDate (i.from, bank[0]); b = minDate (i.to, bank[1]); - console.log (a); - console.log (b); - console.log (''); if (b > a) { res.push({ from: a, From a6ffd79de6ae9e246aa808aca240d839e277ac42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 20:53:21 +0500 Subject: [PATCH 16/33] Update robbery.js --- robbery.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index 579f913..b768b0d 100644 --- a/robbery.js +++ b/robbery.js @@ -19,7 +19,7 @@ var DaysOfWeek = { function toDate(date, bankTime) { var dayOfWeek = date.substring(0, 2); var day = DaysOfWeek[dayOfWeek]; - var hours = parseInt (date.substring(3, 5)) + parseInt (date.substring(8) - bankTime); + var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)) + bankTime; var minutes = parseInt (date.substring(6, 8)); return new Date (2016, 9, day, hours, minutes); @@ -120,7 +120,6 @@ function gangFreeTime(schedule) { schedule.Danny.forEach(function (i) { schedule.Rusty.forEach(function (j) { schedule.Linus.forEach(function (k) { - // console.log([i, j, k]); a = maxFrom (i, j, k); b = minTo (i, j, k); if (b > a) { @@ -143,19 +142,18 @@ function findTimeForRobbery(freeTime, workingHours, res) { var hours = []; var minutes = []; var bank = []; - var day = []; - hours[0] = date[0].substring (0, 2) - parseInt (date[0].substring (5)); - hours[1] = date[1].substring (0, 2) - parseInt (date[1].substring (5)); + var day; + hours[0] = date[0].substring (0, 2); + hours[1] = date[1].substring (0, 2); minutes[0] = date[0].substring (3, 5); minutes[1] = date[1].substring (3, 5); freeTime.forEach(function (i) { - day[0] = i.from.getDate(); - day[1] = i.to.getDate(); - if (day > 3 && day < 8) { + day = i.from.getDate(); + if (day > 3) { return res; } - bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); - bank[1] = new Date (2016, 9, day[1], hours[1], minutes[1]); + bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); + bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); a = maxDate (i.from, bank[0]); b = minDate (i.to, bank[1]); if (b > a) { @@ -223,7 +221,6 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var freeSchedule = toFreeSchedule (newSchedule); var freeTime = gangFreeTime (freeSchedule); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); - // console.log (timeForRobbery); var timeToRobbery = mainFunction (timeForRobbery, duration); return { From 0784f5d1fc33be24da48ca2f0680960429991240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 22:56:18 +0500 Subject: [PATCH 17/33] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BD=D0=B0=20=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D1=8C=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20=D0=BF=D0=BE=D1=80?= =?UTF-8?q?=D1=8F=D0=B4=D0=BA=D0=B0=20=D0=B4=D0=B0=D1=82?= 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 b768b0d..44cbb8a 100644 --- a/robbery.js +++ b/robbery.js @@ -35,12 +35,15 @@ function toNewSchedule(schedule, bankTime) { from: toDate (schedule[key][i].from, bankTime), to: toDate (schedule[key][i].to, bankTime) }); - newSchedule[key].len = n; } } for (var key in schedule) { if ({}.hasOwnProperty.call(schedule, key)) { func(key); + newSchedule[key].sort(function(a, b) { + if (a.from < b.from) return -1; + return 1; + }); } } From fb0caa215642ddca8a7c0a35acaf64489f5fb5f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 22:58:55 +0500 Subject: [PATCH 18/33] Update robbery.js --- robbery.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/robbery.js b/robbery.js index 44cbb8a..7730f95 100644 --- a/robbery.js +++ b/robbery.js @@ -40,9 +40,10 @@ function toNewSchedule(schedule, bankTime) { for (var key in schedule) { if ({}.hasOwnProperty.call(schedule, key)) { func(key); - newSchedule[key].sort(function(a, b) { - if (a.from < b.from) return -1; - return 1; + newSchedule[key].sort(function (a, b) { + if (a.from < b.from) return -1; + + return 1; }); } } From db69cecaf643426328494a9e5a4fb515c3fdadb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 23:01:02 +0500 Subject: [PATCH 19/33] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D0=BB=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B8=D0=BD=D0=B3=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 7730f95..1ea2b94 100644 --- a/robbery.js +++ b/robbery.js @@ -41,7 +41,9 @@ function toNewSchedule(schedule, bankTime) { if ({}.hasOwnProperty.call(schedule, key)) { func(key); newSchedule[key].sort(function (a, b) { - if (a.from < b.from) return -1; + if (a.from < b.from) { + return -1; + } return 1; }); From d01906266c556b896c3143e3251b64fc4535ba9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Mon, 31 Oct 2016 23:35:54 +0500 Subject: [PATCH 20/33] Update robbery.js --- robbery.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/robbery.js b/robbery.js index 1ea2b94..c1dccca 100644 --- a/robbery.js +++ b/robbery.js @@ -97,23 +97,32 @@ function toFreeSchedule(schedule) { if (!({}.hasOwnProperty.call(schedule, key))) { return []; } - freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from + if (schedule[key] = []) { + freeSchedule[key] = []; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: new Date (2016, 9, 7, 23, 59) + }; + } + else { + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: schedule[key][0].from + }; + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from + }; + } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 9, 7, 23, 59) }; + freeSchedule[key].len = n + 1; } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, - to: new Date (2016, 9, 7, 23, 59) - }; - freeSchedule[key].len = n + 1; } return freeSchedule; @@ -228,6 +237,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var freeTime = gangFreeTime (freeSchedule); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); + console.log (freeTime); return { From a53fafdcdedbeab0a6a41fa6d6eb8fa76d7270cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:11:41 +0500 Subject: [PATCH 21/33] Update index.js --- index.js | 337 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 290 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 31b83d2..c9a3557 100644 --- a/index.js +++ b/index.js @@ -1,60 +1,303 @@ 'use strict'; -var robbery = require('./robbery'); - -var gangSchedule = { - Danny: [ - { from: 'ПН 12:00+5', to: 'ПН 17:00+5' }, - { from: 'ВТ 13:00+5', to: 'ВТ 16:00+5' } - ], - Rusty: [ - { from: 'ПН 11:30+5', to: 'ПН 16:30+5' }, - { from: 'ВТ 13:00+5', to: 'ВТ 16:00+5' } - ], - Linus: [ - { from: 'ПН 09:00+3', to: 'ПН 14:00+3' }, - { from: 'ПН 21:00+3', to: 'ВТ 09:30+3' }, - { from: 'СР 09:30+3', to: 'СР 15:00+3' } - ] -}; +/** + * Сделано задание на звездочку + * Реализовано оба метода и tryLater + */ +exports.isStar = false; -var bankWorkingHours = { - from: '10:00+5', - to: '18:00+5' +var DaysOfWeek = { + 'ПН': 1, + 'ВТ': 2, + 'СР': 3, + 'ЧТ': 4, + 'ПТ': 5, + 'СБ': 6, + 'ВС': 7 }; -// Время не существует -var longMoment = robbery.getAppropriateMoment(gangSchedule, 121, bankWorkingHours); +function toDate(date, bankTime) { + var dayOfWeek = date.substring(0, 2); + var day = DaysOfWeek[dayOfWeek]; + var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)) + bankTime; + var minutes = parseInt (date.substring(6, 8)); + + return new Date (2016, 9, day, hours, minutes); +} + +function toNewSchedule(schedule, bankTime) { + var newSchedule = {}; + function func(key) { + var n = (schedule[key]).length; + newSchedule[key] = []; + for (var i = 0; i < n; i++) { + newSchedule[key].push({ + from: toDate (schedule[key][i].from, bankTime), + to: toDate (schedule[key][i].to, bankTime) + }); + } + } + for (var key in schedule) { + if ({}.hasOwnProperty.call(schedule, key)) { + if ((schedule[key]).length === 0) { + newSchedule[key] = [{ + from: new Date (2016, 9, 1, 0, 0), + to: new Date (2016, 9, 1, 0, 0) + }]; + } else + { + func(key); + newSchedule[key].sort(function (a, b) { + if (a.from < b.from) { + return -1; + } + + return 1; + }); + } + } + } -// Выведется false и "" -console.info(longMoment.exists()); -console.info(longMoment.format('Метим на %DD, старт в %HH:%MM!')); + return newSchedule; +} -// Время существует -var moment = robbery.getAppropriateMoment(gangSchedule, 90, bankWorkingHours); +function maxFrom(date1, date2, date3) { + if (date1.from > date2.from && date1.from > date3.from) { + return date1.from; + } + if (date2.from > date3.from) { + return date2.from; + } -// Выведется true и "Метим на ВТ, старт в 11:30!" -console.info(moment.exists()); -console.info(moment.format('Метим на %DD, старт в %HH:%MM!')); + return date3.from; +} -if (robbery.isStar) { - // Вернет true - moment.tryLater(); - // "ВТ 16:00" - console.info(moment.format('%DD %HH:%MM')); +function minTo(date1, date2, date3) { + if (date1.to < date2.to && date1.to < date3.to) { + return date1.to; + } + if (date2.to < date3.to) { + return date2.to; + } - // Вернет true - moment.tryLater(); - // "ВТ 16:30" - console.info(moment.format('%DD %HH:%MM')); + return date3.to; +} - // Вернет true - moment.tryLater(); - // "СР 10:00" - console.info(moment.format('%DD %HH:%MM')); +function maxDate(date1, date2) { + if (date1 > date2) { + return date1; + } - // Вернет false - moment.tryLater(); - // "СР 10:00" - console.info(moment.format('%DD %HH:%MM')); + return date2; } + +function minDate(date1, date2) { + if (date1 < date2) { + return date1; + } + + return date2; +} + +function toFreeSchedule(schedule) { + var freeSchedule = {}; + for (var key in schedule) { + if (!({}.hasOwnProperty.call(schedule, key))) { + return []; + } + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: schedule[key][0].from + }; + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from + }; + } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 9, 7, 23, 59) + }; + freeSchedule[key].len = n + 1; + } + + return freeSchedule; +} + +function gangFreeTime(schedule) { + var freeTime = []; + var a; + var b; + schedule.Danny.forEach(function (i) { + schedule.Rusty.forEach(function (j) { + schedule.Linus.forEach(function (k) { + a = maxFrom (i, j, k); + b = minTo (i, j, k); + if (b > a) { + freeTime.push({ + from: a, + to: b + }); + } + }); + }); + }); + + return freeTime; +} + +function findTimeForRobbery(freeTime, workingHours, res) { + var a; + var b; + var date = [workingHours.from, workingHours.to]; + var hours = []; + var minutes = []; + var bank = []; + var day = []; + hours[0] = date[0].substring (0, 2); + hours[1] = date[1].substring (0, 2); + minutes[0] = date[0].substring (3, 5); + minutes[1] = date[1].substring (3, 5); + freeTime.forEach(function (interval) { + day[0] = interval.from.getDate(); + day[1] = interval.to.getDate(); + if (day > 3) { + return res; + } + bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); + bank[1] = new Date (2016, 9, day[0], hours[1], minutes[1]); + a = maxDate (interval.from, bank[0]); + b = minDate (interval.to, bank[1]); + if (b > a) { + res.push({ + from: a, + to: b + }); + } + }); + + return res; +} + +function mainFunction(timeToRobbery, duration) { // не знаю как назвать функцию=( + var msInDuration = duration * 60 * 1000; + var res = []; + var n = timeToRobbery.length; + var c = 0; + for (var i = 0; i < n; i++) { + var a = (timeToRobbery[i].to - timeToRobbery[i].from); + if (a >= msInDuration) { + res[c] = {}; + res[c] = timeToRobbery[i]; + c++; + } + } + if (c === 0) { + return null; + } + + return res; // время для ограбления +} + +function formateDate(date) { + var day = date.getDate(); + var hours = date.getHours(); + var minutes = date.getMinutes(); + var week = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; + day = week[day]; + if (hours < 10) { + hours = '0' + hours; + } + hours = String(hours); + if (minutes < 10) { + minutes = '0' + minutes; + } + minutes = String(minutes); + + return [day, hours, minutes]; +} + +function formateFreeTime (freeTime) { + var res = []; + freeTime.forEach(function (interval) { + var day1 = interval.from.getDate(); + var day2 = interval.to.getDate(); + if (day1 === day2) { + res.push({ + from: interval.from, + to: interval.to + }); + } + if (day2 === day1 + 1) { + res.push( + { + from: interval.from, + to: new Date (2016, 9, day1, 23, 59) + }, + { + from: new Date (2016, 9, day2, 0, 0), + to: interval.to + } + ); + } + }); + + return res; +} + +/** + * @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} + */ + +exports.getAppropriateMoment = function (schedule, duration, workingHours) { + var newSchedule = {}; + var gmt = parseInt (workingHours.from.substring(5)); + newSchedule = toNewSchedule (schedule, gmt); + var freeSchedule = toFreeSchedule (newSchedule); + // console.log (freeSchedule); + var freeTime = gangFreeTime (freeSchedule); + // console.log (freeTime); + var freeTime = formateFreeTime (freeTime); + var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); + var timeToRobbery = mainFunction (timeForRobbery, duration); + + return { + + /** + * Найдено ли время + * @returns {Boolean} + */ + exists: function () { + + return (timeToRobbery !== null); + }, + + /** + * Возвращает отформатированную строку с часами для ограбления + * Например, + * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" + * @param {String} template + * @returns {String} + */ + format: function (template) { + if (!this.exists()) { + return ''; + } + var format = formateDate(timeToRobbery[0].from); + var newTemplate = template + .replace('%DD', format[0]) + .replace('%HH', format[1]) + .replace('%MM', format[2]); + + return newTemplate; + } + }; +}; From a4a8844231b582b70208ff383357b4824441596c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:14:06 +0500 Subject: [PATCH 22/33] Update index.js --- index.js | 337 ++++++++----------------------------------------------- 1 file changed, 47 insertions(+), 290 deletions(-) diff --git a/index.js b/index.js index c9a3557..31b83d2 100644 --- a/index.js +++ b/index.js @@ -1,303 +1,60 @@ 'use strict'; -/** - * Сделано задание на звездочку - * Реализовано оба метода и tryLater - */ -exports.isStar = false; - -var DaysOfWeek = { - 'ПН': 1, - 'ВТ': 2, - 'СР': 3, - 'ЧТ': 4, - 'ПТ': 5, - 'СБ': 6, - 'ВС': 7 +var robbery = require('./robbery'); + +var gangSchedule = { + Danny: [ + { from: 'ПН 12:00+5', to: 'ПН 17:00+5' }, + { from: 'ВТ 13:00+5', to: 'ВТ 16:00+5' } + ], + Rusty: [ + { from: 'ПН 11:30+5', to: 'ПН 16:30+5' }, + { from: 'ВТ 13:00+5', to: 'ВТ 16:00+5' } + ], + Linus: [ + { from: 'ПН 09:00+3', to: 'ПН 14:00+3' }, + { from: 'ПН 21:00+3', to: 'ВТ 09:30+3' }, + { from: 'СР 09:30+3', to: 'СР 15:00+3' } + ] }; -function toDate(date, bankTime) { - var dayOfWeek = date.substring(0, 2); - var day = DaysOfWeek[dayOfWeek]; - var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)) + bankTime; - var minutes = parseInt (date.substring(6, 8)); - - return new Date (2016, 9, day, hours, minutes); -} - -function toNewSchedule(schedule, bankTime) { - var newSchedule = {}; - function func(key) { - var n = (schedule[key]).length; - newSchedule[key] = []; - for (var i = 0; i < n; i++) { - newSchedule[key].push({ - from: toDate (schedule[key][i].from, bankTime), - to: toDate (schedule[key][i].to, bankTime) - }); - } - } - for (var key in schedule) { - if ({}.hasOwnProperty.call(schedule, key)) { - if ((schedule[key]).length === 0) { - newSchedule[key] = [{ - from: new Date (2016, 9, 1, 0, 0), - to: new Date (2016, 9, 1, 0, 0) - }]; - } else - { - func(key); - newSchedule[key].sort(function (a, b) { - if (a.from < b.from) { - return -1; - } - - return 1; - }); - } - } - } - - return newSchedule; -} - -function maxFrom(date1, date2, date3) { - if (date1.from > date2.from && date1.from > date3.from) { - return date1.from; - } - if (date2.from > date3.from) { - return date2.from; - } - - return date3.from; -} - -function minTo(date1, date2, date3) { - if (date1.to < date2.to && date1.to < date3.to) { - return date1.to; - } - if (date2.to < date3.to) { - return date2.to; - } - - return date3.to; -} - -function maxDate(date1, date2) { - if (date1 > date2) { - return date1; - } - - return date2; -} - -function minDate(date1, date2) { - if (date1 < date2) { - return date1; - } - - return date2; -} - -function toFreeSchedule(schedule) { - var freeSchedule = {}; - for (var key in schedule) { - if (!({}.hasOwnProperty.call(schedule, key))) { - return []; - } - freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from - }; - } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, - to: new Date (2016, 9, 7, 23, 59) - }; - freeSchedule[key].len = n + 1; - } - - return freeSchedule; -} - -function gangFreeTime(schedule) { - var freeTime = []; - var a; - var b; - schedule.Danny.forEach(function (i) { - schedule.Rusty.forEach(function (j) { - schedule.Linus.forEach(function (k) { - a = maxFrom (i, j, k); - b = minTo (i, j, k); - if (b > a) { - freeTime.push({ - from: a, - to: b - }); - } - }); - }); - }); - - return freeTime; -} +var bankWorkingHours = { + from: '10:00+5', + to: '18:00+5' +}; -function findTimeForRobbery(freeTime, workingHours, res) { - var a; - var b; - var date = [workingHours.from, workingHours.to]; - var hours = []; - var minutes = []; - var bank = []; - var day = []; - hours[0] = date[0].substring (0, 2); - hours[1] = date[1].substring (0, 2); - minutes[0] = date[0].substring (3, 5); - minutes[1] = date[1].substring (3, 5); - freeTime.forEach(function (interval) { - day[0] = interval.from.getDate(); - day[1] = interval.to.getDate(); - if (day > 3) { - return res; - } - bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); - bank[1] = new Date (2016, 9, day[0], hours[1], minutes[1]); - a = maxDate (interval.from, bank[0]); - b = minDate (interval.to, bank[1]); - if (b > a) { - res.push({ - from: a, - to: b - }); - } - }); +// Время не существует +var longMoment = robbery.getAppropriateMoment(gangSchedule, 121, bankWorkingHours); - return res; -} +// Выведется false и "" +console.info(longMoment.exists()); +console.info(longMoment.format('Метим на %DD, старт в %HH:%MM!')); -function mainFunction(timeToRobbery, duration) { // не знаю как назвать функцию=( - var msInDuration = duration * 60 * 1000; - var res = []; - var n = timeToRobbery.length; - var c = 0; - for (var i = 0; i < n; i++) { - var a = (timeToRobbery[i].to - timeToRobbery[i].from); - if (a >= msInDuration) { - res[c] = {}; - res[c] = timeToRobbery[i]; - c++; - } - } - if (c === 0) { - return null; - } +// Время существует +var moment = robbery.getAppropriateMoment(gangSchedule, 90, bankWorkingHours); - return res; // время для ограбления -} +// Выведется true и "Метим на ВТ, старт в 11:30!" +console.info(moment.exists()); +console.info(moment.format('Метим на %DD, старт в %HH:%MM!')); -function formateDate(date) { - var day = date.getDate(); - var hours = date.getHours(); - var minutes = date.getMinutes(); - var week = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; - day = week[day]; - if (hours < 10) { - hours = '0' + hours; - } - hours = String(hours); - if (minutes < 10) { - minutes = '0' + minutes; - } - minutes = String(minutes); +if (robbery.isStar) { + // Вернет true + moment.tryLater(); + // "ВТ 16:00" + console.info(moment.format('%DD %HH:%MM')); - return [day, hours, minutes]; -} + // Вернет true + moment.tryLater(); + // "ВТ 16:30" + console.info(moment.format('%DD %HH:%MM')); -function formateFreeTime (freeTime) { - var res = []; - freeTime.forEach(function (interval) { - var day1 = interval.from.getDate(); - var day2 = interval.to.getDate(); - if (day1 === day2) { - res.push({ - from: interval.from, - to: interval.to - }); - } - if (day2 === day1 + 1) { - res.push( - { - from: interval.from, - to: new Date (2016, 9, day1, 23, 59) - }, - { - from: new Date (2016, 9, day2, 0, 0), - to: interval.to - } - ); - } - }); + // Вернет true + moment.tryLater(); + // "СР 10:00" + console.info(moment.format('%DD %HH:%MM')); - return res; + // Вернет false + moment.tryLater(); + // "СР 10:00" + console.info(moment.format('%DD %HH:%MM')); } - -/** - * @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} - */ - -exports.getAppropriateMoment = function (schedule, duration, workingHours) { - var newSchedule = {}; - var gmt = parseInt (workingHours.from.substring(5)); - newSchedule = toNewSchedule (schedule, gmt); - var freeSchedule = toFreeSchedule (newSchedule); - // console.log (freeSchedule); - var freeTime = gangFreeTime (freeSchedule); - // console.log (freeTime); - var freeTime = formateFreeTime (freeTime); - var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); - var timeToRobbery = mainFunction (timeForRobbery, duration); - - return { - - /** - * Найдено ли время - * @returns {Boolean} - */ - exists: function () { - - return (timeToRobbery !== null); - }, - - /** - * Возвращает отформатированную строку с часами для ограбления - * Например, - * "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)" - * @param {String} template - * @returns {String} - */ - format: function (template) { - if (!this.exists()) { - return ''; - } - var format = formateDate(timeToRobbery[0].from); - var newTemplate = template - .replace('%DD', format[0]) - .replace('%HH', format[1]) - .replace('%MM', format[2]); - - return newTemplate; - } - }; -}; From 4faba5e4b2a496f8d31c542f6d33291495385a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:14:38 +0500 Subject: [PATCH 23/33] Update robbery.js --- robbery.js | 110 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/robbery.js b/robbery.js index c1dccca..c9a3557 100644 --- a/robbery.js +++ b/robbery.js @@ -39,14 +39,22 @@ function toNewSchedule(schedule, bankTime) { } for (var key in schedule) { if ({}.hasOwnProperty.call(schedule, key)) { - func(key); - newSchedule[key].sort(function (a, b) { - if (a.from < b.from) { - return -1; - } - - return 1; - }); + if ((schedule[key]).length === 0) { + newSchedule[key] = [{ + from: new Date (2016, 9, 1, 0, 0), + to: new Date (2016, 9, 1, 0, 0) + }]; + } else + { + func(key); + newSchedule[key].sort(function (a, b) { + if (a.from < b.from) { + return -1; + } + + return 1; + }); + } } } @@ -97,32 +105,23 @@ function toFreeSchedule(schedule) { if (!({}.hasOwnProperty.call(schedule, key))) { return []; } - if (schedule[key] = []) { - freeSchedule[key] = []; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: new Date (2016, 9, 7, 23, 59) - }; - } - else { - freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from - }; - } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, - to: new Date (2016, 9, 7, 23, 59) + freeSchedule[key] = []; + var n = (schedule[key]).length; + freeSchedule[key][0] = { + from: new Date (2016, 9, 1, 0, 0), + to: schedule[key][0].from + }; + for (var i = 1; i < n; i++) { + freeSchedule[key][i] = { + from: schedule[key][i - 1].to, + to: schedule[key][i].from }; - freeSchedule[key].len = n + 1; } + freeSchedule[key][n] = { + from: schedule[key][n - 1].to, + to: new Date (2016, 9, 7, 23, 59) + }; + freeSchedule[key].len = n + 1; } return freeSchedule; @@ -157,20 +156,21 @@ function findTimeForRobbery(freeTime, workingHours, res) { var hours = []; var minutes = []; var bank = []; - var day; + var day = []; hours[0] = date[0].substring (0, 2); hours[1] = date[1].substring (0, 2); minutes[0] = date[0].substring (3, 5); minutes[1] = date[1].substring (3, 5); - freeTime.forEach(function (i) { - day = i.from.getDate(); + freeTime.forEach(function (interval) { + day[0] = interval.from.getDate(); + day[1] = interval.to.getDate(); if (day > 3) { return res; } - bank[0] = new Date (2016, 9, day, hours[0], minutes[0]); - bank[1] = new Date (2016, 9, day, hours[1], minutes[1]); - a = maxDate (i.from, bank[0]); - b = minDate (i.to, bank[1]); + bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); + bank[1] = new Date (2016, 9, day[0], hours[1], minutes[1]); + a = maxDate (interval.from, bank[0]); + b = minDate (interval.to, bank[1]); if (b > a) { res.push({ from: a, @@ -220,6 +220,34 @@ function formateDate(date) { return [day, hours, minutes]; } +function formateFreeTime (freeTime) { + var res = []; + freeTime.forEach(function (interval) { + var day1 = interval.from.getDate(); + var day2 = interval.to.getDate(); + if (day1 === day2) { + res.push({ + from: interval.from, + to: interval.to + }); + } + if (day2 === day1 + 1) { + res.push( + { + from: interval.from, + to: new Date (2016, 9, day1, 23, 59) + }, + { + from: new Date (2016, 9, day2, 0, 0), + to: interval.to + } + ); + } + }); + + return res; +} + /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -234,10 +262,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var gmt = parseInt (workingHours.from.substring(5)); newSchedule = toNewSchedule (schedule, gmt); var freeSchedule = toFreeSchedule (newSchedule); + // console.log (freeSchedule); var freeTime = gangFreeTime (freeSchedule); + // console.log (freeTime); + var freeTime = formateFreeTime (freeTime); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); - console.log (freeTime); return { From 123f6c2648f8933b00713bb85381ce40ac4edc69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:24:34 +0500 Subject: [PATCH 24/33] Update robbery.js --- robbery.js | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/robbery.js b/robbery.js index c9a3557..f633058 100644 --- a/robbery.js +++ b/robbery.js @@ -29,34 +29,33 @@ function toNewSchedule(schedule, bankTime) { var newSchedule = {}; function func(key) { var n = (schedule[key]).length; - newSchedule[key] = []; - for (var i = 0; i < n; i++) { - newSchedule[key].push({ - from: toDate (schedule[key][i].from, bankTime), - to: toDate (schedule[key][i].to, bankTime) - }); - } - } - for (var key in schedule) { - if ({}.hasOwnProperty.call(schedule, key)) { - if ((schedule[key]).length === 0) { + if (n === 0) { newSchedule[key] = [{ from: new Date (2016, 9, 1, 0, 0), to: new Date (2016, 9, 1, 0, 0) }]; - } else - { - func(key); - newSchedule[key].sort(function (a, b) { - if (a.from < b.from) { - return -1; - } - - return 1; + } else { + newSchedule[key] = []; + for (var i = 0; i < n; i++) { + newSchedule[key].push({ + from: toDate (schedule[key][i].from, bankTime), + to: toDate (schedule[key][i].to, bankTime) }); } } } + for (var key in schedule) { + if ({}.hasOwnProperty.call(schedule, key)) { + func(key); + newSchedule[key].sort(function (a, b) { + if (a.from < b.from) { + return -1; + } + + return 1; + }); + } + } return newSchedule; } @@ -220,7 +219,7 @@ function formateDate(date) { return [day, hours, minutes]; } -function formateFreeTime (freeTime) { +function formateFreeTime(freeTime) { var res = []; freeTime.forEach(function (interval) { var day1 = interval.from.getDate(); @@ -235,7 +234,7 @@ function formateFreeTime (freeTime) { res.push( { from: interval.from, - to: new Date (2016, 9, day1, 23, 59) + to: new Date (2016, 9, day1, 23, 59) }, { from: new Date (2016, 9, day2, 0, 0), @@ -265,7 +264,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { // console.log (freeSchedule); var freeTime = gangFreeTime (freeSchedule); // console.log (freeTime); - var freeTime = formateFreeTime (freeTime); + freeTime = formateFreeTime (freeTime); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); From 8d4a634726d3d52c589da638377111b418c35e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:27:14 +0500 Subject: [PATCH 25/33] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=20=D0=BB=D0=B8=D0=BD=D1=82=D0=B8=D0=BD=D0=B3?= =?UTF-8?q?,=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=BE=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D0=B4=D0=BD=D1=8F=D0=BC?= 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 f633058..9ecf3ec 100644 --- a/robbery.js +++ b/robbery.js @@ -30,10 +30,10 @@ function toNewSchedule(schedule, bankTime) { function func(key) { var n = (schedule[key]).length; if (n === 0) { - newSchedule[key] = [{ - from: new Date (2016, 9, 1, 0, 0), - to: new Date (2016, 9, 1, 0, 0) - }]; + newSchedule[key] = [{ + from: new Date (2016, 9, 1, 0, 0), + to: new Date (2016, 9, 1, 0, 0) + }]; } else { newSchedule[key] = []; for (var i = 0; i < n; i++) { From f06c1c85081718a9585e5fca380d00a5f8d89825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 22:36:38 +0500 Subject: [PATCH 26/33] Update robbery.js --- robbery.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/robbery.js b/robbery.js index 9ecf3ec..a45856f 100644 --- a/robbery.js +++ b/robbery.js @@ -242,6 +242,22 @@ function formateFreeTime(freeTime) { } ); } + if (day2 > day1 + 1) { + res.push( + { + from: interval.from, + to: new Date (2016, 9, day1, 23, 59) + }, + { + from: new Date (2016, 9, day1 + 1, 0, 0), + to: new Date (2016, 9, day1 + 1, 23, 59) + }, + { + from: new Date (2016, 9, day1 + 2, 0, 0), + to: interval.to + } + ); + } }); return res; @@ -265,6 +281,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var freeTime = gangFreeTime (freeSchedule); // console.log (freeTime); freeTime = formateFreeTime (freeTime); + // console.log (freeTime); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); var timeToRobbery = mainFunction (timeForRobbery, duration); From 18aaff82ede81476bafb1ca6eeca9b3596e34f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Tue, 1 Nov 2016 23:02:30 +0500 Subject: [PATCH 27/33] Update robbery.js --- robbery.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/robbery.js b/robbery.js index a45856f..3121e82 100644 --- a/robbery.js +++ b/robbery.js @@ -259,6 +259,11 @@ function formateFreeTime(freeTime) { ); } }); + var i = res.length - 1; + while (res[i].from.getDate() > 3) { + delete res[i]; + i--; + } return res; } From 42465df970451e5ac1b3e0847d51adf07bdd2c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 4 Nov 2016 20:24:39 +0500 Subject: [PATCH 28/33] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 240 +++++++++++++++++------------------------------------ 1 file changed, 78 insertions(+), 162 deletions(-) diff --git a/robbery.js b/robbery.js index 3121e82..823b67f 100644 --- a/robbery.js +++ b/robbery.js @@ -6,108 +6,64 @@ */ exports.isStar = false; -var DaysOfWeek = { - 'ПН': 1, - 'ВТ': 2, - 'СР': 3, - 'ЧТ': 4, - 'ПТ': 5, - 'СБ': 6, - 'ВС': 7 -}; - -function toDate(date, bankTime) { - var dayOfWeek = date.substring(0, 2); - var day = DaysOfWeek[dayOfWeek]; - var hours = parseInt (date.substring(3, 5)) - parseInt (date.substring(8)) + bankTime; - var minutes = parseInt (date.substring(6, 8)); - - return new Date (2016, 9, day, hours, minutes); +var WEEK = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; +var YEAR = 2016; +var MONTH = 9; +var BEGIN_OF_WEEK = new Date (YEAR, MONTH, 1, 0, 0); +var END_OF_WEEK = new Date (YEAR, MONTH, 7, 23, 59); + +function toTypeDate(date, bankTimeZone) { + var dayOfWeek = date.slice(0, 2); + var currentDay = WEEK.indexOf(dayOfWeek); + var hours = parseInt (date.slice(3, 5), 10) - parseInt (date.slice(8), 10) + bankTimeZone; + var minutes = parseInt (date.slice(6, 8), 10); + + return new Date (YEAR, MONTH, currentDay, hours, minutes); } -function toNewSchedule(schedule, bankTime) { +function toNewSchedule(schedule, bankTimeZone) { var newSchedule = {}; - function func(key) { + var keys = Object.keys(schedule); + keys.forEach(function (key) { var n = (schedule[key]).length; if (n === 0) { newSchedule[key] = [{ - from: new Date (2016, 9, 1, 0, 0), - to: new Date (2016, 9, 1, 0, 0) + from: BEGIN_OF_WEEK, + to: BEGIN_OF_WEEK }]; } else { newSchedule[key] = []; for (var i = 0; i < n; i++) { newSchedule[key].push({ - from: toDate (schedule[key][i].from, bankTime), - to: toDate (schedule[key][i].to, bankTime) + from: toTypeDate (schedule[key][i].from, bankTimeZone), + to: toTypeDate (schedule[key][i].to, bankTimeZone) }); } } - } - for (var key in schedule) { - if ({}.hasOwnProperty.call(schedule, key)) { - func(key); - newSchedule[key].sort(function (a, b) { - if (a.from < b.from) { - return -1; - } - - return 1; - }); - } - } + }); return newSchedule; } -function maxFrom(date1, date2, date3) { - if (date1.from > date2.from && date1.from > date3.from) { - return date1.from; - } - if (date2.from > date3.from) { - return date2.from; - } - - return date3.from; -} - -function minTo(date1, date2, date3) { - if (date1.to < date2.to && date1.to < date3.to) { - return date1.to; - } - if (date2.to < date3.to) { - return date2.to; - } - - return date3.to; -} - -function maxDate(date1, date2) { - if (date1 > date2) { - return date1; - } - - return date2; -} - -function minDate(date1, date2) { - if (date1 < date2) { - return date1; - } +// minOrMax = 1 если нужно найти максимум и +// -1 если нужно найти минимум +function compareDate(minOrMax) { + var dates = [].slice.call(arguments, 1); + dates.sort (function (date1, date2) { + return (date2 - date1) * minOrMax; + }); - return date2; + return dates[0]; } function toFreeSchedule(schedule) { var freeSchedule = {}; - for (var key in schedule) { - if (!({}.hasOwnProperty.call(schedule, key))) { - return []; - } + var keys = Object.keys(schedule); + keys.forEach(function (key) { freeSchedule[key] = []; var n = (schedule[key]).length; freeSchedule[key][0] = { - from: new Date (2016, 9, 1, 0, 0), + from: BEGIN_OF_WEEK, to: schedule[key][0].from }; for (var i = 1; i < n; i++) { @@ -118,27 +74,27 @@ function toFreeSchedule(schedule) { } freeSchedule[key][n] = { from: schedule[key][n - 1].to, - to: new Date (2016, 9, 7, 23, 59) + to: END_OF_WEEK }; freeSchedule[key].len = n + 1; - } + }); return freeSchedule; } function gangFreeTime(schedule) { var freeTime = []; - var a; - var b; + var begin; + var end; schedule.Danny.forEach(function (i) { schedule.Rusty.forEach(function (j) { schedule.Linus.forEach(function (k) { - a = maxFrom (i, j, k); - b = minTo (i, j, k); - if (b > a) { + begin = compareDate (1, i.from, j.from, k.from); + end = compareDate (-1, i.to, j.to, k.to); + if (end > begin) { freeTime.push({ - from: a, - to: b + from: begin, + to: end }); } }); @@ -148,32 +104,27 @@ function gangFreeTime(schedule) { return freeTime; } +function dateToObject(date) { + return { + hours: parseInt(date.slice(0, 2), 10), + minutes: parseInt(date. slice(3, 5), 10) + }; +} + function findTimeForRobbery(freeTime, workingHours, res) { - var a; - var b; - var date = [workingHours.from, workingHours.to]; - var hours = []; - var minutes = []; - var bank = []; - var day = []; - hours[0] = date[0].substring (0, 2); - hours[1] = date[1].substring (0, 2); - minutes[0] = date[0].substring (3, 5); - minutes[1] = date[1].substring (3, 5); + var startBankWork = dateToObject(workingHours.from); + var endBankWork = dateToObject(workingHours.to); + var bank = {}; freeTime.forEach(function (interval) { - day[0] = interval.from.getDate(); - day[1] = interval.to.getDate(); - if (day > 3) { - return res; - } - bank[0] = new Date (2016, 9, day[0], hours[0], minutes[0]); - bank[1] = new Date (2016, 9, day[0], hours[1], minutes[1]); - a = maxDate (interval.from, bank[0]); - b = minDate (interval.to, bank[1]); - if (b > a) { + var day = interval.from.getDate(); + bank.start = new Date (YEAR, MONTH, day, startBankWork.hours, startBankWork.minutes); + bank.end = new Date (YEAR, MONTH, day, endBankWork.hours, endBankWork.minutes); + var begin = compareDate (1, interval.from, bank.start); + var end = compareDate (-1, interval.to, bank.end); + if (end > begin) { res.push({ - from: a, - to: b + from: begin, + to: end }); } }); @@ -181,8 +132,7 @@ function findTimeForRobbery(freeTime, workingHours, res) { return res; } -function mainFunction(timeToRobbery, duration) { // не знаю как назвать функцию=( - var msInDuration = duration * 60 * 1000; +function mainFunction(timeToRobbery, msInDuration) { var res = []; var n = timeToRobbery.length; var c = 0; @@ -201,20 +151,22 @@ function mainFunction(timeToRobbery, duration) { // не знаю как наз return res; // время для ограбления } +function toString(number) { + if (number < 10) { + number = '0' + number; + } + number = String(number); + + return number; +} + function formateDate(date) { var day = date.getDate(); var hours = date.getHours(); var minutes = date.getMinutes(); - var week = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; - day = week[day]; - if (hours < 10) { - hours = '0' + hours; - } - hours = String(hours); - if (minutes < 10) { - minutes = '0' + minutes; - } - minutes = String(minutes); + day = WEEK[day]; + hours = toString (hours); + minutes = toString(minutes); return [day, hours, minutes]; } @@ -224,47 +176,13 @@ function formateFreeTime(freeTime) { freeTime.forEach(function (interval) { var day1 = interval.from.getDate(); var day2 = interval.to.getDate(); - if (day1 === day2) { + for (var i = day1; i <= Math.min(day2, 3); i++) { res.push({ - from: interval.from, - to: interval.to + from: compareDate (1, new Date (YEAR, MONTH, i, 0, 0), interval.from), + to: compareDate (-1, new Date (YEAR, MONTH, i, 23, 59), interval.to) }); } - if (day2 === day1 + 1) { - res.push( - { - from: interval.from, - to: new Date (2016, 9, day1, 23, 59) - }, - { - from: new Date (2016, 9, day2, 0, 0), - to: interval.to - } - ); - } - if (day2 > day1 + 1) { - res.push( - { - from: interval.from, - to: new Date (2016, 9, day1, 23, 59) - }, - { - from: new Date (2016, 9, day1 + 1, 0, 0), - to: new Date (2016, 9, day1 + 1, 23, 59) - }, - { - from: new Date (2016, 9, day1 + 2, 0, 0), - to: interval.to - } - ); - } }); - var i = res.length - 1; - while (res[i].from.getDate() > 3) { - delete res[i]; - i--; - } - return res; } @@ -279,16 +197,14 @@ function formateFreeTime(freeTime) { exports.getAppropriateMoment = function (schedule, duration, workingHours) { var newSchedule = {}; - var gmt = parseInt (workingHours.from.substring(5)); + var gmt = parseInt (workingHours.from.slice(5), 10); + var msInDuration = 60 * 1000 * duration; newSchedule = toNewSchedule (schedule, gmt); var freeSchedule = toFreeSchedule (newSchedule); - // console.log (freeSchedule); var freeTime = gangFreeTime (freeSchedule); - // console.log (freeTime); freeTime = formateFreeTime (freeTime); - // console.log (freeTime); var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); - var timeToRobbery = mainFunction (timeForRobbery, duration); + var timeToRobbery = mainFunction (timeForRobbery, msInDuration); return { From 2df9932ea1f86d3819b11f96d71a47a074edadb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 4 Nov 2016 20:29:22 +0500 Subject: [PATCH 29/33] =?UTF-8?q?=D0=BB=D0=B8=D0=BD=D1=82=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- robbery.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/robbery.js b/robbery.js index 823b67f..1e20bc1 100644 --- a/robbery.js +++ b/robbery.js @@ -45,7 +45,7 @@ function toNewSchedule(schedule, bankTimeZone) { return newSchedule; } -// minOrMax = 1 если нужно найти максимум и +// minOrMax = 1 если нужно найти максимум и // -1 если нужно найти минимум function compareDate(minOrMax) { var dates = [].slice.call(arguments, 1); @@ -107,7 +107,7 @@ function gangFreeTime(schedule) { function dateToObject(date) { return { hours: parseInt(date.slice(0, 2), 10), - minutes: parseInt(date. slice(3, 5), 10) + minutes: parseInt(date.slice(3, 5), 10) }; } @@ -183,6 +183,7 @@ function formateFreeTime(freeTime) { }); } }); + return res; } From 1712fe076828ff3f5c3604440f0b5d98f693cd50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Fri, 4 Nov 2016 20:44:16 +0500 Subject: [PATCH 30/33] Update robbery.js --- robbery.js | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/robbery.js b/robbery.js index 1e20bc1..8483fbe 100644 --- a/robbery.js +++ b/robbery.js @@ -82,7 +82,7 @@ function toFreeSchedule(schedule) { return freeSchedule; } -function gangFreeTime(schedule) { +function findGangFreeTime(schedule) { var freeTime = []; var begin; var end; @@ -100,10 +100,27 @@ function gangFreeTime(schedule) { }); }); }); + freeTime = formateFreeTime (freeTime); return freeTime; } +function formateFreeTime(freeTime) { + var res = []; + freeTime.forEach(function (interval) { + var day1 = interval.from.getDate(); + var day2 = interval.to.getDate(); + for (var i = day1; i <= Math.min(day2, 3); i++) { + res.push({ + from: compareDate (1, new Date (YEAR, MONTH, i, 0, 0), interval.from), + to: compareDate (-1, new Date (YEAR, MONTH, i, 23, 59), interval.to) + }); + } + }); + + return res; +} + function dateToObject(date) { return { hours: parseInt(date.slice(0, 2), 10), @@ -111,7 +128,7 @@ function dateToObject(date) { }; } -function findTimeForRobbery(freeTime, workingHours, res) { +function findFreeIntervals(freeTime, workingHours, res) { var startBankWork = dateToObject(workingHours.from); var endBankWork = dateToObject(workingHours.to); var bank = {}; @@ -171,22 +188,6 @@ function formateDate(date) { return [day, hours, minutes]; } -function formateFreeTime(freeTime) { - var res = []; - freeTime.forEach(function (interval) { - var day1 = interval.from.getDate(); - var day2 = interval.to.getDate(); - for (var i = day1; i <= Math.min(day2, 3); i++) { - res.push({ - from: compareDate (1, new Date (YEAR, MONTH, i, 0, 0), interval.from), - to: compareDate (-1, new Date (YEAR, MONTH, i, 23, 59), interval.to) - }); - } - }); - - return res; -} - /** * @param {Object} schedule – Расписание Банды * @param {Number} duration - Время на ограбление в минутах @@ -202,10 +203,9 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { var msInDuration = 60 * 1000 * duration; newSchedule = toNewSchedule (schedule, gmt); var freeSchedule = toFreeSchedule (newSchedule); - var freeTime = gangFreeTime (freeSchedule); - freeTime = formateFreeTime (freeTime); - var timeForRobbery = findTimeForRobbery (freeTime, workingHours, []); - var timeToRobbery = mainFunction (timeForRobbery, msInDuration); + var freeTime = findGangFreeTime (freeSchedule); + var timeForRobbery = findFreeIntervals (freeTime, workingHours, []); + timeForRobbery = mainFunction (timeForRobbery, msInDuration); return { @@ -215,7 +215,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { */ exists: function () { - return (timeToRobbery !== null); + return (timeForRobbery !== null); }, /** @@ -229,7 +229,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (!this.exists()) { return ''; } - var format = formateDate(timeToRobbery[0].from); + var format = formateDate(timeForRobbery[0].from); var newTemplate = template .replace('%DD', format[0]) .replace('%HH', format[1]) From 9a9bfed50093ef20fd82ce0330439836be1082bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Sun, 6 Nov 2016 03:00:34 +0500 Subject: [PATCH 31/33] Update robbery.js --- robbery.js | 132 ++++++++++++++++++++++------------------------------- 1 file changed, 55 insertions(+), 77 deletions(-) diff --git a/robbery.js b/robbery.js index 8483fbe..c9b01ea 100644 --- a/robbery.js +++ b/robbery.js @@ -7,90 +7,73 @@ exports.isStar = false; var WEEK = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; -var YEAR = 2016; -var MONTH = 9; -var BEGIN_OF_WEEK = new Date (YEAR, MONTH, 1, 0, 0); -var END_OF_WEEK = new Date (YEAR, MONTH, 7, 23, 59); +var BEGIN_OF_WEEK = new Date (0, 0, 1, 0, 0); +var END_OF_WEEK = new Date (0, 0, 7, 23, 59); +var msInMinute = 60000; -function toTypeDate(date, bankTimeZone) { +function leadToDataType(date, bankTimeZone) { var dayOfWeek = date.slice(0, 2); var currentDay = WEEK.indexOf(dayOfWeek); - var hours = parseInt (date.slice(3, 5), 10) - parseInt (date.slice(8), 10) + bankTimeZone; - var minutes = parseInt (date.slice(6, 8), 10); + var hours = parseInt(date.slice(3, 5), 10); + var timeZone = parseInt(date.slice(8), 10); + hours = hours - timeZone + bankTimeZone; + var minutes = parseInt(date.slice(6, 8), 10); - return new Date (YEAR, MONTH, currentDay, hours, minutes); + return new Date (0, 0, currentDay, hours, minutes); } -function toNewSchedule(schedule, bankTimeZone) { - var newSchedule = {}; - var keys = Object.keys(schedule); - keys.forEach(function (key) { - var n = (schedule[key]).length; - if (n === 0) { - newSchedule[key] = [{ - from: BEGIN_OF_WEEK, - to: BEGIN_OF_WEEK - }]; - } else { - newSchedule[key] = []; - for (var i = 0; i < n; i++) { - newSchedule[key].push({ - from: toTypeDate (schedule[key][i].from, bankTimeZone), - to: toTypeDate (schedule[key][i].to, bankTimeZone) - }); - } - } +function maxDate() { + var dates = [].slice.call(arguments); + dates.sort(function (date1, date2) { + + return (date2 - date1); }); - return newSchedule; + return dates[0]; } -// minOrMax = 1 если нужно найти максимум и -// -1 если нужно найти минимум -function compareDate(minOrMax) { - var dates = [].slice.call(arguments, 1); - dates.sort (function (date1, date2) { - return (date2 - date1) * minOrMax; +function minDate() { + var dates = [].slice.call(arguments); + dates.sort(function (date1, date2) { + + return (date1 - date2); }); return dates[0]; } -function toFreeSchedule(schedule) { +function toFreeSchedule(schedule, bankTimeZone) { var freeSchedule = {}; var keys = Object.keys(schedule); keys.forEach(function (key) { + var length = (schedule[key]).length; freeSchedule[key] = []; - var n = (schedule[key]).length; - freeSchedule[key][0] = { + freeSchedule[key].push({ from: BEGIN_OF_WEEK, - to: schedule[key][0].from - }; - for (var i = 1; i < n; i++) { - freeSchedule[key][i] = { - from: schedule[key][i - 1].to, - to: schedule[key][i].from - }; + to: leadToDataType (schedule[key][0].from, bankTimeZone) + }); + for (var i = 1; i < length; i++) { + freeSchedule[key].push({ + from: leadToDataType (schedule[key][i - 1].to, bankTimeZone), + to: leadToDataType (schedule[key][i].from, bankTimeZone) + }); } - freeSchedule[key][n] = { - from: schedule[key][n - 1].to, + freeSchedule[key].push({ + from: leadToDataType (schedule[key][length - 1].to, bankTimeZone), to: END_OF_WEEK - }; - freeSchedule[key].len = n + 1; + }); }); return freeSchedule; } -function findGangFreeTime(schedule) { +function getGangFreeTime(schedule) { var freeTime = []; - var begin; - var end; - schedule.Danny.forEach(function (i) { - schedule.Rusty.forEach(function (j) { - schedule.Linus.forEach(function (k) { - begin = compareDate (1, i.from, j.from, k.from); - end = compareDate (-1, i.to, j.to, k.to); + schedule.Danny.forEach(function (interval1) { + schedule.Rusty.forEach(function (interval2) { + schedule.Linus.forEach(function (interval3) { + var begin = maxDate (interval1.from, interval2.from, interval3.from); + var end = minDate (interval1.to, interval2.to, interval3.to); if (end > begin) { freeTime.push({ from: begin, @@ -112,8 +95,8 @@ function formateFreeTime(freeTime) { var day2 = interval.to.getDate(); for (var i = day1; i <= Math.min(day2, 3); i++) { res.push({ - from: compareDate (1, new Date (YEAR, MONTH, i, 0, 0), interval.from), - to: compareDate (-1, new Date (YEAR, MONTH, i, 23, 59), interval.to) + from: maxDate (new Date (0, 0, i, 0, 0), interval.from), + to: minDate (new Date (0, 0, i, 23, 59), interval.to) }); } }); @@ -128,16 +111,16 @@ function dateToObject(date) { }; } -function findFreeIntervals(freeTime, workingHours, res) { +function getFreeIntervals(freeTime, workingHours, res) { var startBankWork = dateToObject(workingHours.from); var endBankWork = dateToObject(workingHours.to); var bank = {}; freeTime.forEach(function (interval) { var day = interval.from.getDate(); - bank.start = new Date (YEAR, MONTH, day, startBankWork.hours, startBankWork.minutes); - bank.end = new Date (YEAR, MONTH, day, endBankWork.hours, endBankWork.minutes); - var begin = compareDate (1, interval.from, bank.start); - var end = compareDate (-1, interval.to, bank.end); + bank.start = new Date (0, 0, day, startBankWork.hours, startBankWork.minutes); + bank.end = new Date (0, 0, day, endBankWork.hours, endBankWork.minutes); + var begin = maxDate (interval.from, bank.start); + var end = minDate (interval.to, bank.end); if (end > begin) { res.push({ from: begin, @@ -151,17 +134,13 @@ function findFreeIntervals(freeTime, workingHours, res) { function mainFunction(timeToRobbery, msInDuration) { var res = []; - var n = timeToRobbery.length; - var c = 0; - for (var i = 0; i < n; i++) { + for (var i = 0; i < timeToRobbery.length; i++) { var a = (timeToRobbery[i].to - timeToRobbery[i].from); if (a >= msInDuration) { - res[c] = {}; - res[c] = timeToRobbery[i]; - c++; + res.push(timeToRobbery[i]); } } - if (c === 0) { + if (res.length === 0) { return null; } @@ -172,7 +151,7 @@ function toString(number) { if (number < 10) { number = '0' + number; } - number = String(number); + number.toString(); return number; } @@ -199,12 +178,11 @@ function formateDate(date) { exports.getAppropriateMoment = function (schedule, duration, workingHours) { var newSchedule = {}; - var gmt = parseInt (workingHours.from.slice(5), 10); - var msInDuration = 60 * 1000 * duration; - newSchedule = toNewSchedule (schedule, gmt); - var freeSchedule = toFreeSchedule (newSchedule); - var freeTime = findGangFreeTime (freeSchedule); - var timeForRobbery = findFreeIntervals (freeTime, workingHours, []); + var gmt = parseInt(workingHours.from.slice(5), 10); + var msInDuration = msInMinute * duration; + var freeSchedule = toFreeSchedule (schedule, gmt); + var freeTime = getGangFreeTime (freeSchedule); + var timeForRobbery = getFreeIntervals (freeTime, workingHours, []); timeForRobbery = mainFunction (timeForRobbery, msInDuration); return { From 5f8329640ada0eb55e6ad5074a137795b1e1f88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Sun, 6 Nov 2016 03:19:31 +0500 Subject: [PATCH 32/33] Update robbery.js --- robbery.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index c9b01ea..c0b0a5f 100644 --- a/robbery.js +++ b/robbery.js @@ -42,7 +42,7 @@ function minDate() { return dates[0]; } -function toFreeSchedule(schedule, bankTimeZone) { +function findFreeGangsterTime(schedule, bankTimeZone) { var freeSchedule = {}; var keys = Object.keys(schedule); keys.forEach(function (key) { @@ -104,7 +104,7 @@ function formateFreeTime(freeTime) { return res; } -function dateToObject(date) { +function leadDateToObject(date) { return { hours: parseInt(date.slice(0, 2), 10), minutes: parseInt(date.slice(3, 5), 10) @@ -112,8 +112,8 @@ function dateToObject(date) { } function getFreeIntervals(freeTime, workingHours, res) { - var startBankWork = dateToObject(workingHours.from); - var endBankWork = dateToObject(workingHours.to); + var startBankWork = leadDateToObject(workingHours.from); + var endBankWork = leadDateToObject(workingHours.to); var bank = {}; freeTime.forEach(function (interval) { var day = interval.from.getDate(); @@ -132,11 +132,10 @@ function getFreeIntervals(freeTime, workingHours, res) { return res; } -function mainFunction(timeToRobbery, msInDuration) { +function getTimeForRobbery(timeToRobbery, msInDuration) { var res = []; for (var i = 0; i < timeToRobbery.length; i++) { - var a = (timeToRobbery[i].to - timeToRobbery[i].from); - if (a >= msInDuration) { + if (timeToRobbery[i].to - timeToRobbery[i].from >= msInDuration) { res.push(timeToRobbery[i]); } } @@ -144,7 +143,7 @@ function mainFunction(timeToRobbery, msInDuration) { return null; } - return res; // время для ограбления + return res; } function toString(number) { @@ -177,13 +176,12 @@ function formateDate(date) { */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - var newSchedule = {}; var gmt = parseInt(workingHours.from.slice(5), 10); var msInDuration = msInMinute * duration; - var freeSchedule = toFreeSchedule (schedule, gmt); + var freeSchedule = findFreeGangsterTime (schedule, gmt); var freeTime = getGangFreeTime (freeSchedule); var timeForRobbery = getFreeIntervals (freeTime, workingHours, []); - timeForRobbery = mainFunction (timeForRobbery, msInDuration); + timeForRobbery = getTimeForRobbery (timeForRobbery, msInDuration); return { From 5d40043838d6759fef4740b84c5c0611198fe91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B5=D1=80=D0=B0?= Date: Sun, 6 Nov 2016 03:35:32 +0500 Subject: [PATCH 33/33] Update robbery.js --- robbery.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/robbery.js b/robbery.js index c0b0a5f..1b2fdb4 100644 --- a/robbery.js +++ b/robbery.js @@ -48,20 +48,27 @@ function findFreeGangsterTime(schedule, bankTimeZone) { keys.forEach(function (key) { var length = (schedule[key]).length; freeSchedule[key] = []; - freeSchedule[key].push({ - from: BEGIN_OF_WEEK, - to: leadToDataType (schedule[key][0].from, bankTimeZone) - }); - for (var i = 1; i < length; i++) { + if (length === 0) { + freeSchedule[key] = [{ + from: BEGIN_OF_WEEK, + to: END_OF_WEEK + }]; + } else { freeSchedule[key].push({ - from: leadToDataType (schedule[key][i - 1].to, bankTimeZone), - to: leadToDataType (schedule[key][i].from, bankTimeZone) + from: BEGIN_OF_WEEK, + to: leadToDataType (schedule[key][0].from, bankTimeZone) + }); + for (var i = 1; i < length; i++) { + freeSchedule[key].push({ + from: leadToDataType (schedule[key][i - 1].to, bankTimeZone), + to: leadToDataType (schedule[key][i].from, bankTimeZone) + }); + } + freeSchedule[key].push({ + from: leadToDataType (schedule[key][length - 1].to, bankTimeZone), + to: END_OF_WEEK }); } - freeSchedule[key].push({ - from: leadToDataType (schedule[key][length - 1].to, bankTimeZone), - to: END_OF_WEEK - }); }); return freeSchedule;