-
Notifications
You must be signed in to change notification settings - Fork 79
Титова Наталья #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Титова Наталья #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| * Сделано задание на звездочку | ||
| * Реализовано оба метода и tryLater | ||
| */ | ||
| exports.isStar = true; | ||
| exports.isStar = false; | ||
|
|
||
| /** | ||
| * @param {Object} schedule – Расписание Банды | ||
|
|
@@ -14,8 +14,24 @@ exports.isStar = true; | |
| * @param {String} workingHours.to – Время закрытия, например, "18:00+5" | ||
| * @returns {Object} | ||
| */ | ||
| var GTM_BANK = 0; | ||
| var WEEK_DAYS = ['ПН', 'ВТ', 'СР']; | ||
| var MINUTES_IN_HOUR = 60; | ||
| var HOUR_IN_DAY = 24; | ||
| var TOTAL_TIME_LINE = WEEK_DAYS.length * HOUR_IN_DAY * MINUTES_IN_HOUR; | ||
| // var TRY_LATER_MINUTES = 30; | ||
| var DURATION = 0; | ||
|
|
||
|
|
||
| exports.getAppropriateMoment = function (schedule, duration, workingHours) { | ||
| console.info(schedule, duration, workingHours); | ||
| GTM_BANK = workingHours.from.split('+')[1]; | ||
| DURATION = duration; | ||
|
|
||
| let gangPartyFreeTime = calcGangPartyFreeTime(schedule); | ||
| let bankFreeTime = getBankWorkingTime(workingHours); | ||
| gangPartyFreeTime.push(bankFreeTime); | ||
| let timeToGang = findTimeToGang(gangPartyFreeTime); | ||
| var currentGang = 0; | ||
|
|
||
| return { | ||
|
|
||
|
|
@@ -24,7 +40,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { | |
| * @returns {Boolean} | ||
| */ | ||
| exists: function () { | ||
| return false; | ||
| return Boolean(timeToGang[currentGang]); | ||
| }, | ||
|
|
||
| /** | ||
|
|
@@ -35,16 +51,126 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { | |
| * @returns {String} | ||
| */ | ||
| format: function (template) { | ||
| return template; | ||
| }, | ||
| if (!timeToGang[currentGang]) { | ||
| return ''; | ||
| } | ||
| let minutes = timeToGang[currentGang][0]; | ||
| let day = WEEK_DAYS[(minutes - minutes % (HOUR_IN_DAY * MINUTES_IN_HOUR)) / | ||
| (HOUR_IN_DAY * MINUTES_IN_HOUR)]; | ||
| minutes %= HOUR_IN_DAY * MINUTES_IN_HOUR; | ||
| let hours = ((minutes - minutes % MINUTES_IN_HOUR) / MINUTES_IN_HOUR).toString(); | ||
| minutes %= MINUTES_IN_HOUR; | ||
|
|
||
| /** | ||
| * Попробовать найти часы для ограбления позже [*] | ||
| * @star | ||
| * @returns {Boolean} | ||
| */ | ||
| tryLater: function () { | ||
| return false; | ||
| return template.replace('%DD', day) | ||
| .replace('%HH', (hours < 10 ? '0' : '') + hours) | ||
| .replace('%MM', (minutes < 10 ? '0' : '') + minutes); | ||
| } | ||
|
|
||
| // /** | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ??? |
||
| // * Попробовать найти часы для ограбления позже [*] | ||
| // * @star | ||
| // * @returns {Boolean} | ||
| // */ | ||
| // tryLater: function () { | ||
| // var newTimeStart = timeToGang[currentGang][0] + TRY_LATER_MINUTES; | ||
| // if ((timeToGang[currentGang][1] - newTimeStart) < duration) { | ||
| // if (!timeToGang[currentGang + 1]) { | ||
| // return false; | ||
| // } | ||
| // currentGang++; | ||
| // | ||
| // return true; | ||
| // } | ||
| // timeToGang[currentGang][0] += TRY_LATER_MINUTES; | ||
| // | ||
| // return true; | ||
| // } | ||
| }; | ||
| }; | ||
|
|
||
| function calcGangPartyFreeTime(scheduleArr) { | ||
| var result = []; | ||
| for (let personName in scheduleArr) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (!scheduleArr.hasOwnProperty(personName)) { | ||
| continue; | ||
| } | ||
| var personResult = []; | ||
| for (let i = 0; i < scheduleArr[personName].length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Используешь где-то |
||
| let start = stringToInt(scheduleArr[personName][i].from); | ||
| let end = stringToInt(scheduleArr[personName][i].to); | ||
| personResult.push([start, end]); | ||
| } | ||
| result.push(busyTimeToFreeTime(personResult)); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function stringToInt(time) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется, что это неподходящее название для метода, ибо |
||
| let format = time.match(/([А-Я]{2})\s(\d{2}):(\d{2})\+(\d+)/); | ||
| let hours = WEEK_DAYS.indexOf(format[1]) * HOUR_IN_DAY; | ||
| let delta = GTM_BANK - Number(format[4]); | ||
| hours += Number(format[2]) + delta; | ||
|
|
||
| return hours * MINUTES_IN_HOUR + Number(format[3]); | ||
| } | ||
|
|
||
| function busyTimeToFreeTime(timeArr) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Как-то много индексов и каких-то проверок в этом методе, постарайся хотя бы сделать, чтобы было обращение к свойствам, а не индексам, ибо вообще сложно разобраться что к чему здесь There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. За 2 минуты так и не смог понять, что делает этот метод |
||
| var result = []; | ||
| for (let i = 0; i <= timeArr.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Посмотри в сторону других перебирающих методов: |
||
| if (i === 0) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему такое исключение для первой итерации? |
||
| result.push([0, timeArr[i][0]]); | ||
| continue; | ||
| } | ||
| let end = timeArr[i] === undefined ? TOTAL_TIME_LINE : timeArr[i][0]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему оно может быть |
||
| result.push([timeArr[i - 1][1], end]); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function getBankWorkingTime(timeArr) { | ||
| var result = []; | ||
| for (let i = 0; i < WEEK_DAYS.length; i++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Только не |
||
| result.push([stringToInt(WEEK_DAYS[i] + ' ' + timeArr.from), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему бы здесь не использовать интерполяцию строк? |
||
| stringToInt(WEEK_DAYS[i] + ' ' + timeArr.to)]); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function findTimeToGang(arrays, compare) { | ||
| var newArr = []; | ||
| var arr1 = compare ? compare : arrays[0]; | ||
| var arr2 = compare ? arrays[0] : arrays[1]; | ||
| for (let i = 0; i < arr1.length; i++) { | ||
| for (let k = 0; k < arr2.length; k++) { | ||
| // if (arr1[i][0] >= arr2[k][1] || arr1[i][1] <= arr2[k][0]) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Удалить |
||
| // continue; | ||
| // } | ||
| // var el = [Math.max(arr1[i][0], arr2[k][0]), Math.min(arr1[i][1], arr2[k][1])]; | ||
| // if ((el[1] - el[0]) >= duration) { | ||
| // newArr.push(el); | ||
| // } | ||
| checkTimeInterval(arr1[i], arr2[k], newArr); | ||
| } | ||
| } | ||
| if (newArr.length && arrays[1]) { | ||
| arrays.shift(); | ||
| newArr = findTimeToGang(arrays, newArr); | ||
| } | ||
|
|
||
| return newArr; | ||
| } | ||
|
|
||
| function checkTimeInterval(arr1, arr2, newArr) { | ||
| if (arr1[0] >= arr2[1] || arr1[1] <= arr2[0]) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Индексы - сложно и непонятно |
||
| return; | ||
| } | ||
| var el = [Math.max(arr1[0], arr2[0]), Math.min(arr1[1], arr2[1])]; | ||
| if ((el[1] - el[0]) >= DURATION) { | ||
| newArr.push(el); | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Такого рода комментарии лучше удалять