-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.js
187 lines (154 loc) · 4.41 KB
/
route.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
177
178
179
180
181
182
183
184
185
186
187
'use strict';
let mantelas;
async function
generateMantelas(firstMantela, output = undefined)
{
function
updateStatus(msg)
{
if (output)
output.textContent = msg;
}
const start = performance.now();
const mantelas = new Map();
const queue = [ firstMantela ];
const visited = new Set();
while (queue.length > 0) {
const url = queue.shift();
if (visited.has(url))
continue;
updateStatus(url);
const mantela = await fetch(url, { mode: 'cors' })
.then(res => res.json())
.catch(err => new Error(err));
if (mantela instanceof Error)
continue;
visited.add(url);
if (!('aboutMe' in mantela))
continue;
mantelas.set(mantela.aboutMe.identifier, mantela);
if (visited.has(mantela.aboutMe.identifier))
continue;
visited.add(mantela.aboutMe.identifier);
mantela.providers.forEach(e => {
if (e.mantela)
queue.push(e.mantela);
});
}
const finish = performance.now()
updateStatus(`Done. (${finish-start} ms)`);
return mantelas;
}
async function
loadMantela(e)
{
e.preventDefault();
btnLoad.disabled = true;
mantelas = await generateMantelas(urlMantela.value, outputMantela);
btnLoad.disabled = false;
mantelas.forEach(e => {
const option = document.createElement('option');
option.value = e.aboutMe.identifier;
option.textContent = `${e.aboutMe.name} (${e.aboutMe.identifier})`;
pbxFrom.append(option);
});
mantelas.forEach(e => {
const option = document.createElement('option');
option.value = e.aboutMe.identifier;
option.textContent = `${e.aboutMe.name} (${e.aboutMe.identifier})`;
pbxTo.append(option);
});
}
formMantela.addEventListener('submit', loadMantela);
async function
updateTerminals(e)
{
const clone = terminalTo.cloneNode(false);
terminalTo.parentNode.replaceChild(clone, terminalTo);
const option = document.createElement('option');
option.textContent = '---';
clone.append(option);
const mantela = mantelas.get(pbxTo.value);
if (!mantela)
return;
mantela.extensions.forEach(e => {
const option = document.createElement('option');
option.value = e.extension;
option.textContent = `${e.name} (${e.extension})`;
clone.append(option);
});
}
pbxTo.addEventListener('change', updateTerminals);
const costs = {
pb: n => 120 * n.length,
dp10: n => 600 * n.length + [...n].reduce((c, e) => c + 100 * ((+e+9) % 10 + 1), 0),
dp20: n => 450 * n.length + [...n].reduce((c, e) => c + 50 * ((+e+9) % 10 + 1), 0),
};
async function
searchRoute(mantelas, from, to, cost, init = '', output = undefined)
{
function
updateStatus(msg)
{
if (output)
output.textContent = msg;
}
const minimal = new Map();
minimal.set(from, cost(init));
const queue = new Map();
queue.set(cost(init), [ { number: init, identifier: from, first: true } ]);
while (queue.size > 0) {
const min = [...queue.keys()].sort((a, b) => a - b)[0];
const current = queue.get(min).shift();
if (queue.get(min).length === 0)
queue.delete(min);
const mantela = mantelas.get(current.identifier);
if (!mantela || mantela.aboutMe.unavailable)
continue;
if (current.identifier === to)
return current.number;
updateStatus(current.number);
await new Promise(r => setTimeout(r));
const hoppable = mantela.providers.find(e => {
return e.identifier === current.identifier;
});
if (!hoppable && !current.first)
continue;
mantela.providers.forEach(e => {
const hopPrefix = current.first ? '' : hoppable.prefix;
const elem = {
number: current.number + hopPrefix + e.prefix,
identifier: e.identifier,
};
const minimalCost = minimal.get(elem.identifier) || Infinity;
if (cost(elem.number) >= minimalCost || !e.prefix)
return;
minimal.set(elem.identifier, cost(elem.number));
const array = queue.get(cost(elem.number)) || [];
array.push(elem);
queue.set(cost(elem.number), array);
});
}
return null;
}
async function
calculateRoute(e)
{
e.preventDefault();
const from = pbxFrom.value;
const cost = costs[callBy.value];
const to = pbxTo.value;
btnSearch.disabled = true;
const route = await searchRoute(mantelas, from, to, cost, '', outputSearch);
btnSearch.disabled = false;
if (route === null)
outputSearch.textContent = 'No route found.';
else
outputSearch.textContent = 'Found: ' + route + terminalTo.value;
}
formRouter.addEventListener('submit', calculateRoute);
const urlSearch = new URLSearchParams(document.location.search);
if (urlSearch.get('first')) {
urlMantela.value = urlSearch.get('first');
btnLoad.click();
}