forked from urfu-2016/javascript-task-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
robbery.js
176 lines (147 loc) · 5.61 KB
/
robbery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
/**
* Сделано задание на звездочку
* Реализовано оба метода и tryLater
*/
exports.isStar = false;
/**
* @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}
*/
var WEEK = ['ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС'];
var DAYS_ROBBERY = ['ПН ', 'ВТ ', 'СР '];
var MS_IN_MINUTE = 60000;
var MS_IN_HOUR = MS_IN_MINUTE * 60;
function parseDate(strDate) {
var regEx = /(\S+) (\d+):(\d+)\+(\d+)/g;
var regDate = regEx.exec(strDate);
return new Date(1970, 0, (WEEK.indexOf(regDate[1]) + 1),
(Number(regDate[2]) - Number(regDate[4])), Number(regDate[3]));
}
function initAppropriateIntervals(bankHours) {
var returnableInterval = [];
for (var i = 0; i < DAYS_ROBBERY.length; i ++) {
var from = parseDate(DAYS_ROBBERY[i] + bankHours.from);
var to = parseDate(DAYS_ROBBERY[i] + bankHours.to);
returnableInterval.push({ from: from, to: to });
}
return returnableInterval;
}
function getFormattedSchedule(gangSchedule) {
var formatGangSchedule = [];
var namesRobbers = Object.keys(gangSchedule);
namesRobbers.forEach(function (name) {
gangSchedule[name].forEach(function (busyTime) {
var from = parseDate(busyTime.from);
var to = parseDate(busyTime.to);
formatGangSchedule.push({ from: from, to: to });
});
});
return formatGangSchedule;
}
function isInInterval(sheduleValue, goodIntrvVal) {
return (sheduleValue.from <= goodIntrvVal.to && sheduleValue.to >= goodIntrvVal.from);
}
function isFullInInterval(sheduleValue, goodIntrvVal) {
return (sheduleValue.to < goodIntrvVal.to && sheduleValue.from > goodIntrvVal.from);
}
function changeInterval(sheduleValue, intervals, i) {
if (!isInInterval(sheduleValue, intervals[i])) {
return intervals;
}
if (isFullInInterval(sheduleValue, intervals[i])) {
intervals.push({ from: sheduleValue.to, to: intervals[i].to });
intervals.splice(i, 1, { from: intervals[i].from, to: sheduleValue.from });
return intervals;
}
return continueChangeInterval(sheduleValue, intervals, i);
}
function continueChangeInterval(sheduleValue, intervals, i) {
if (sheduleValue.from <= intervals[i].from && sheduleValue.to >= intervals[i].to) {
intervals.splice(i, 1, { from: intervals[i].from, to: intervals[i].to });
return intervals;
}
var startTime = intervals[i].from;
var endTime = sheduleValue.from;
if (intervals[i].from >= sheduleValue.from) {
startTime = sheduleValue.to;
endTime = intervals[i].to;
}
intervals.splice(i, 1, { from: startTime, to: endTime });
return intervals;
}
function getGoodTimeIndex(appropriateIntervals, duration) {
for (var i = 0; i < appropriateIntervals.length; i++) {
var durIntrv = (appropriateIntervals[i].to - appropriateIntervals[i].from) / MS_IN_MINUTE;
if (durIntrv >= duration) {
return i;
}
}
return -1;
}
function addZeros(num) {
return num < 10 ? String('0' + num) : String(num);
}
function makeInterval(appropriateIntervals, formatSchedule) {
for (var i = 0; i < formatSchedule.length; i++) {
for (var j = 0; j < appropriateIntervals.length; j++) {
appropriateIntervals = changeInterval(formatSchedule[i],
appropriateIntervals, j);
}
}
return appropriateIntervals;
}
exports.getAppropriateMoment = function (schedule, duration, workingHours) {
var timeZoneBank = Number(workingHours.from.substr(6));
var formatSchedule = getFormattedSchedule(schedule);
var appropriateIntervals = initAppropriateIntervals(workingHours);
appropriateIntervals = makeInterval(appropriateIntervals, formatSchedule);
appropriateIntervals.sort(function (a, b) {
return a.from - b.from;
});
var indGoodTime = getGoodTimeIndex(appropriateIntervals, duration);
var isExists = false;
var goodTime;
if (indGoodTime !== -1) {
isExists = true;
goodTime = new Date(Number(appropriateIntervals[indGoodTime].from) +
MS_IN_HOUR * timeZoneBank);
}
return {
/**
* Найдено ли время
* @returns {Boolean}
*/
exists: function () {
return isExists;
},
/**
* Возвращает отформатированную строку с часами для ограбления
* Например,
* "Начинаем в %HH:%MM (%DD)" -> "Начинаем в 14:59 (СР)"
* @param {String} template
* @returns {String}
*/
format: function (template) {
if (!isExists) {
return '';
}
return template
.replace('%HH', addZeros(goodTime.getHours()))
.replace('%MM', addZeros(goodTime.getMinutes()))
.replace('%DD', WEEK[goodTime.getDate() - 1]);
},
/**
* Попробовать найти часы для ограбления позже [*]
* @star
* @returns {Boolean}
*/
tryLater: function () {
return isExists;
}
};
};