-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathrobbery.js
More file actions
146 lines (117 loc) · 3.29 KB
/
robbery.js
File metadata and controls
146 lines (117 loc) · 3.29 KB
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
'use strict';
exports.isStar = true;
const DAYS_WEEK = {
'ПН': 0,
'ВТ': 1440,
'СР': 2880
};
exports.getAppropriateMoment = function (schedule, duration, workingHours) {
let timeLine = returnInterval([], [{
from: 0,
to: 4320
}], 0);
let friends = getFriendsIntervals();
let bankIntervals = mathIntervalsBank(workingHours);
timeLine = returnInterval(timeLine, bankIntervals, 1);
timeLine = returnInterval(timeLine, friends, 0);
let currentTime = getProfitInterval(0);
return {
exists() {
return currentTime;
},
format(template) {
if (!currentTime) {
return '';
}
let tmp = intervalToString(getProfitInterval(currentTime));
return template
.replace('%DD', tmp.DD)
.replace('%HH', tmp.HH)
.replace('%MM', tmp.MM);
},
tryLater() {
let nextInterval = getProfitInterval(currentTime + 30);
if (nextInterval > 0) {
currentTime = nextInterval;
return true;
}
return false;
}
};
function getFriendsIntervals() {
let arr = [];
Object.keys(schedule).forEach(key => {
schedule[key].forEach(el => {
arr.push({
from: timeConverter(el.from),
to: timeConverter(el.to)
});
});
});
return arr;
}
function getProfitInterval(from) {
let starts = [];
let start = timeLine.indexOf(1, from);
let stop = timeLine.indexOf(0, start);
while ((start !== -1)) {
if (stop - start >= duration) {
starts.push(start);
}
start = timeLine.indexOf(1, stop);
stop = timeLine.indexOf(0, start);
}
return starts.length === 0 ? false : starts[0];
}
};
function timeConverter(T) {
let timeArr = T.replace(/[+:]/ig, ' ').split(' ');
return Number(DAYS_WEEK[timeArr[0]]) + Number(timeArr[1]) * 60 -
Number(timeArr[3]) * 60 + Number(timeArr[2]);
}
function mathIntervalsBank(obj) {
return [{
from: timeConverter('ПН ' + obj.from),
to: timeConverter('ПН ' + obj.to)
}, {
from: timeConverter('ВТ ' + obj.from),
to: timeConverter('ВТ ' + obj.to)
}, {
from: timeConverter('СР ' + obj.from),
to: timeConverter('СР ' + obj.to)
}];
}
function intervalToString(go) {
go = go + 300;
let objTime = {};
let mins;
let time = String(go / 1440).split('.')[0];
if (time === '0') {
objTime.DD = 'ПН';
mins = go;
} else if (time === '1') {
objTime.DD = 'ВТ';
mins = go - 1440;
} else if (time === '2') {
objTime.DD = 'СР';
mins = go - 2880;
}
let [hour, min] = (mins / 60).toFixed(1).split('.');
objTime.HH = twoSign(hour);
objTime.MM = twoSign(min * 6);
return objTime;
}
function twoSign(num) {
if (num > 9) {
return num;
}
return '0' + num;
}
function returnInterval(arr, obj, val) {
obj.forEach(el => {
for (var i = el.from; i < el.to; i++) {
arr[i] = val;
}
});
return arr;
}