From 6ee4ef9aa031ed06faa0305ea73d1dd427c729a2 Mon Sep 17 00:00:00 2001 From: cfgi Date: Mon, 29 Oct 2018 18:58:56 +0500 Subject: [PATCH] I know there is many mistakes but I have to test it. I will refactor it later --- robbery.js | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index ed84b5d..bd377e2 100755 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,9 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -const isStar = true; +const isStar = false; +let bankTimeZone = 0; +const days = { 'ПН': 0, 'ВТ': 1, 'СР': 2 }; /** * @param {Object} schedule – Расписание Банды @@ -15,7 +17,22 @@ const isStar = true; * @returns {Object} */ function getAppropriateMoment(schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + + bankTimeZone = parseInt(workingHours.from.substr(5, 10)); + schedule.bank = [workingHours]; + + Object.keys(schedule) + .forEach((time) => { + if (time === 'bank') { + convertTimeToMinutesBank(schedule[time]); + } else { + convertTimeToMinutes(schedule[time], bankTimeZone); + } + }); + schedule = normalizingScheduleAndSort(schedule); + let unionTime = findEveryFreeInterval(schedule); + + let heh = existArray(unionTime, duration); return { @@ -24,7 +41,8 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + + return heh.length !== 0; }, /** @@ -34,7 +52,17 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + let date = makeDateValid(heh); + if (heh.length !== 0) { + + template = template.replace('%DD', date[0]); + template = template.replace('%HH', date[1]); + template = template.replace('%MM', date[2]); + + return template; + } + + return ''; }, /** @@ -43,11 +71,108 @@ function getAppropriateMoment(schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { + return false; } }; } +function normalizingScheduleAndSort(schedule) { + let arr = []; + Object.keys(schedule) + .forEach((time) => { + schedule[time].forEach((lul) => { + arr.push(lul); + }); + }); + arr.sort((a, b) => { + let genreA = a.from; + let genreB = b.from; + let comparison = 0; + if (genreA > genreB) { + comparison = 1; + } else if (genreA < genreB) { + comparison = -1; + } + + return comparison; + }); + + return arr; +} + +// convert time when bank doesn't work to minutes (start at 00:00 by its timezone) + +function convertTimeToMinutesBank(workingHours) { + let to = 0; + let from = 0; + workingHours.forEach((time) => { + from = (parseInt(time.from.substr(0, 2)) * 60 + parseInt(time.from.substr(3, 2))); + to = (parseInt(time.to.substr(0, 2)) * 60 + parseInt(time.to.substr(3, 2))); + time.from = 0; + time.to = from; + }); + workingHours.push({ from: 0, to: from }); + workingHours.push({ from: to, to: from + 1440 }); + workingHours.push({ from: to + 1440, to: from + 2880 }); + workingHours.push({ from: to + 2880, to: 4320 }); + +} + +function findEveryFreeInterval(arr) { + let unionTime = []; + let a = arr[0].from; + let b = arr[0].to; + arr.forEach((time, index) => { + if (a <= time.from && time.from <= b) { + if (b < time.to) { + b = time.to; + } + } else if (index < arr.length - 1) { + unionTime.push({ from: b, to: arr[index].from }); + a = arr[index].from; + b = arr[index].to; + } + }); + + return unionTime; +} + +function existArray(unionTime, duration) { + for (let time in unionTime) { + if (unionTime[time].to - unionTime[time].from >= duration) { + + return unionTime[time]; + } + } + + return ''; +} + +function makeDateValid(time) { + let day = Object.keys(days)[Math.floor(time.from / 1440)]; + let hours = Math.floor((time.from % 1440) / 60) < 10 ? '0' + Math.floor((time.from % 1440) / + 60) : Math.floor((time.from % 1440) / 60); + let minutes = (time.from % 1440) % 60 < 10 ? '0' + (time.from % 1440) % 60 + : (time.from % 1440) % 60; + + return [day, hours, minutes]; +} + +// convert schedule ofk every rubber to minutes (start at 00:00 by bank timezone) + +function convertTimeToMinutes(schedule, bankTimeZone1) { + schedule.forEach((time) => { + time.from = (parseInt(time.from.substr(3, 2)) * 60 + parseInt(time.from.substr(6, 2)) + + (bankTimeZone1 - parseInt(time.from.substr(8, 10))) * 60 + + days[time.from.substr(0, 2)] * 24 * 60); + time.to = (parseInt(time.to.substr(3, 2)) * 60 + parseInt(time.to.substr(6, 2)) + + (bankTimeZone1 - parseInt(time.to.substr(8, 10))) * 60 + days[time.to.substr(0, 2)] * + 24 * 60); + + }); +} + module.exports = { getAppropriateMoment,