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

Грехов Кирилл #56

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Линтинг
  • Loading branch information
Grehhov authored Oct 26, 2016
commit f9a93b5badb56df6da08ead00bc4f53ad4b0a948
92 changes: 54 additions & 38 deletions robbery.js
Original file line number Diff line number Diff line change
@@ -4,24 +4,27 @@ exports.isStar = false;

function convertSchedule(schedule) {
var res = {};
for (var i in schedule) {
res[i] = [];
for (var j = 0; j < schedule[i].length; j++) {
var from = convertToDate(schedule[i][j].from);
var to = convertToDate(schedule[i][j].to);
res[i].push({from: from, to: to});
var names = Object.keys(schedule);
for (var i = 0; i < names.length; i++) {
var name = names[i];
res[name] = [];
for (var j = 0; j < schedule[name].length; j++) {
var from = convertToDate(schedule[name][j].from);
var to = convertToDate(schedule[name][j].to);
res[name].push({ from: from, to: to });
}
}

return res;
}

function convertToDate(stringDate) {
var days = {'ПН': 1, 'ВТ': 2, 'СР': 3, 'ЧТ': 4, 'ПТ': 5, 'СБ': 6, 'ВС': 7};
var days = { 'ПН': 1, 'ВТ': 2, 'СР': 3, 'ЧТ': 4, 'ПТ': 5, 'СБ': 6, 'ВС': 7 };
var re = /^([А-Я]{2}) (.*)(\+\d+)$/;
var parseDate = stringDate.match(re);

return new Date(Date.parse(days[parseDate[1]] + ' Jan 1900 ' + parseDate[2] + ' GMT' + parseDate[3] + '00'));
return new Date(Date.parse(days[parseDate[1]] + ' Jan 1900 '
+ parseDate[2] + ' GMT' + parseDate[3] + '00'));
}

function getSchedBank(workingHours) {
@@ -30,73 +33,86 @@ function getSchedBank(workingHours) {
for (var i = 0; i < daysOfWorkBank.length; i++) {
var from = convertToDate(daysOfWorkBank[i] + ' ' + workingHours.from);
var to = convertToDate(daysOfWorkBank[i] + ' ' + workingHours.to);
sched.push({from: from, to: to});
sched.push({ from: from, to: to });
}

return sched;
}

function getNeperesekItem(svTime, zTime) {
var neperesekItem = [];
var neperes = true;
for (var j = 0; j < zTime.length; j++) {
if (zTime[j].from.getTime() <= svTime.to.getTime() &&
zTime[j].from.getTime() >= svTime.from.getTime()) {
neperesekItem.push({from: svTime.from, to: zTime[j].from});
neperes = false;
}
if (zTime[j].to.getTime() <= svTime.to.getTime() &&
zTime[j].to.getTime() >= svTime.from.getTime()) {
neperesekItem.push({from: zTime[j].to, to: svTime.to});
neperes = false;
}
if (zTime[j].to.getTime() >= svTime.to.getTime() &&
zTime[j].from.getTime() <= svTime.from.getTime()) {
neperes = false;
}
}
if (neperes) {
neperesekItem.push(svTime);
}
return neperesekItem;
}

exports.getAppropriateMoment = function (schedule, duration, workingHours) {
var sched = convertSchedule(schedule);
//console.log(sched);
var schedBank = getSchedBank(workingHours);
//console.log(schedBank);
for (var name in sched) {
var names = Object.keys(sched);
for (var j = 0; j < names.length; j++) {
var name = names[j];
var neperesek = [];
for (var i = 0; i < schedBank.length; i++) {
var neperes = true;
for (var j = 0; j < sched[name].length; j++) {
if (sched[name][j].from.getTime() <= schedBank[i].to.getTime() &&
sched[name][j].from.getTime() >= schedBank[i].from.getTime()) {
neperesek.push({from: schedBank[i].from, to: sched[name][j].from});
neperes = false;
}
if (sched[name][j].to.getTime() <= schedBank[i].to.getTime() &&
sched[name][j].to.getTime() >= schedBank[i].from.getTime()) {
neperesek.push({from: sched[name][j].to, to: schedBank[i].to});
neperes = false;
}
}
if (neperes) {
neperesek.push(schedBank[i]);
}
var item = getNeperesekItem(schedBank[i], sched[name]);
neperesek = neperesek.concat(item);
}
schedBank = neperesek;
}
var time = null;
for (var i = 0; i < schedBank.length; i++) {
var min = (schedBank[i].to.getTime() - schedBank[i].from.getTime()) / 60000;
for (var n = 0; n < schedBank.length; n++) {
var min = (schedBank[n].to.getTime() - schedBank[n].from.getTime()) / 60000;
if (min >= duration) {
time = schedBank[i];
time = schedBank[n];
break;
}
}

return {

exists: function () {
if (time) {
return true;
}

return false;
},

format: function (template) {
if (time){
if (time) {
var days = ['ПН', 'ВТ', 'СР'];
template = template.replace('%DD', days[time.from.getDay() - 1]);
var bankTimeZone = parseInt(workingHours.from.split('+')[1]);
var currentTimeZone = -time.from.getTimezoneOffset()/60;
var currentTimeZone = - time.from.getTimezoneOffset() / 60;
var h = (time.from.getHours() + (bankTimeZone - currentTimeZone)).toString();
if (h.length == 1) h = '0' + h;
if (h.length === 1) {
h = '0' + h;
}
template = template.replace('%HH', h);
var m = time.from.getMinutes().toString();
if (m.length == 1) m = '0' + m;
if (m.length === 1) {
m = '0' + m;
}
template = template.replace('%MM', m);

return template;
}

return '';
},