Add usimp.chooseDomainServer
This commit is contained in:
@ -37,26 +37,34 @@ function createLoginWindow() {
|
|||||||
form.removeChild(d);
|
form.removeChild(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formError(msg) {
|
||||||
|
let div = document.createElement("div");
|
||||||
|
div.classList.add("error");
|
||||||
|
div.innerText = msg;
|
||||||
|
form.appendChild(div);
|
||||||
|
}
|
||||||
|
|
||||||
usimp.lookup(form.domain.value)
|
usimp.lookup(form.domain.value)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
res.json()
|
res.json()
|
||||||
.then(data => console.log(data))
|
.then(data => {
|
||||||
.catch(reason => console.error(reason))
|
console.log(data["domain"]);
|
||||||
|
let domainServer = usimp.chooseDomainServer(data["domain_servers"]);
|
||||||
|
console.log(domainServer);
|
||||||
|
})
|
||||||
|
.catch(reason => {
|
||||||
|
console.error(reason);
|
||||||
|
formError("Could not communicate with USIMP domain");
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
|
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
|
||||||
let div = document.createElement("div");
|
formError("Invalid USIMP domain");
|
||||||
div.classList.add("error");
|
|
||||||
div.innerText = "Invalid USIMP domain";
|
|
||||||
form.appendChild(div);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(reason => {
|
.catch(reason => {
|
||||||
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
|
document.getElementsByName("domain")[0].setAttribute("invalid", "invalid");
|
||||||
let div = document.createElement("div");
|
formError("Invalid USIMP domain");
|
||||||
div.classList.add("error");
|
|
||||||
div.innerText = "Invalid USIMP domain";
|
|
||||||
form.appendChild(div);
|
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
for (let e of form) e.disabled = false;
|
for (let e of form) e.disabled = false;
|
||||||
|
@ -7,3 +7,31 @@ usimp.lookup = function (domain_name) {
|
|||||||
redirect: "manual",
|
redirect: "manual",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usimp.chooseDomainServer = function (domainServers, invalidDomainServers = []) {
|
||||||
|
if (domainServers.length === 0) throw Error("No domain servers specified");
|
||||||
|
|
||||||
|
domainServers.filter(srv => invalidDomainServers.map(srv => srv.host).includes(srv));
|
||||||
|
if (domainServers.length === 0) throw Error("No domain servers reachable");
|
||||||
|
if (domainServers.length === 1) return domainServers[0];
|
||||||
|
|
||||||
|
let priority = domainServers.reduce((min, srv) => Math.min(min, srv.priority), Infinity);
|
||||||
|
domainServers = domainServers.filter(srv => srv.priority === priority);
|
||||||
|
if (domainServers.length === 1) return domainServers[0];
|
||||||
|
|
||||||
|
let totalWeight = domainServers.reduce((total, srv) => total + srv.weight, 0);
|
||||||
|
let w = Math.floor(Math.random() * totalWeight);
|
||||||
|
|
||||||
|
let accumulator = 0;
|
||||||
|
for (let srv of domainServers) {
|
||||||
|
accumulator += srv.weight;
|
||||||
|
if (w < accumulator) return srv;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn("Domain server selection not worked correctly");
|
||||||
|
return domainServers[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
usimp.login = function (domainServer, accountName, password) {
|
||||||
|
return fetch(``)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user