Skip to content
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

Рыльских Алексей #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 129 additions & 4 deletions robbery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* Сделано задание на звездочку
* Реализовано оба метода и tryLater
*/
const isStar = true;
const isStar = false;
let bankTimeZone = 0;
const days = { 'ПН': 0, 'ВТ': 1, 'СР': 2 };

/**
* @param {Object} schedule – Расписание Банды
Expand All @@ -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 {

Expand All @@ -24,7 +41,8 @@ function getAppropriateMoment(schedule, duration, workingHours) {
* @returns {Boolean}
*/
exists: function () {
return false;

return heh.length !== 0;
},

/**
Expand All @@ -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 '';
},

/**
Expand All @@ -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,

Expand Down