plugin-backend #1
@ -1,42 +1,73 @@
|
|||||||
async function refresh_overview(server_address) {
|
async function refresh_overview(server_address) {
|
||||||
try {
|
const response = await fetch_api(server_address, "overview");
|
||||||
const response = await fetch(`${server_address}overview`);
|
if (response.data) {
|
||||||
if (response.ok) {
|
return response.data;
|
||||||
const data = await response.json();
|
|
||||||
if (!("error" in data)) {
|
|
||||||
return { overview: data, error: null };
|
|
||||||
} else {
|
} else {
|
||||||
return { overview: null, error: data };
|
if (response.error) {
|
||||||
}
|
return response.error;
|
||||||
} else {
|
} else {
|
||||||
return { overview: null, error: null };
|
return null;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
return { overview: null, error: e };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refresh_hints(server_address) {
|
async function refresh_hints(server_address) {
|
||||||
const response = await fetch(`${server_address}hints`);
|
const response = await fetch_api(server_address, "hints");
|
||||||
const data = await response.json();
|
if (response.data) {
|
||||||
return data;
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refresh_checks(server_address) {
|
async function refresh_checks(server_address) {
|
||||||
const response = await fetch(`${server_address}items`);
|
const response = await fetch_api(server_address, "items");
|
||||||
const data = await response.json();
|
if (response.data) {
|
||||||
return data;
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refresh_entrances(server_address) {
|
async function refresh_entrances(server_address) {
|
||||||
const response = await fetch(`${server_address}doors`);
|
const response = await fetch_api(server_address, "doors");
|
||||||
const data = await response.json();
|
if (response.data) {
|
||||||
return data;
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetch_api(server_address, uri) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${server_address}${uri}`, {
|
||||||
|
signal: AbortSignal.timeout(1100),
|
||||||
|
});
|
||||||
|
if (response.ok) {
|
||||||
|
const data = await response.json();
|
||||||
|
if (!("error" in data)) {
|
||||||
|
return { data: data, error: null };
|
||||||
|
} else {
|
||||||
|
return { data: null, error: data };
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return { data: null, error: null };
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
return { data: null, error: e };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AbortSignal.timeout ??= function timeout(ms) {
|
||||||
|
const ctrl = new AbortController();
|
||||||
|
setTimeout(() => ctrl.abort(), ms);
|
||||||
|
return ctrl.signal;
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
refresh_overview,
|
refresh_overview,
|
||||||
refresh_checks,
|
refresh_checks,
|
||||||
refresh_entrances,
|
refresh_entrances,
|
||||||
refresh_hints,
|
refresh_hints,
|
||||||
|
fetch_api,
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,7 @@ var current_hints = 0;
|
|||||||
|
|
||||||
// Global state internal
|
// Global state internal
|
||||||
var server_address = "localhost:51111/";
|
var server_address = "localhost:51111/";
|
||||||
var can_access_api_server = true;
|
var can_access_api_server = false;
|
||||||
var is_timeout = false;
|
var is_timeout = false;
|
||||||
var hide_completed_areas = false;
|
var hide_completed_areas = false;
|
||||||
var cross_codes = {};
|
var cross_codes = {};
|
||||||
@ -22,7 +22,7 @@ var all_scenes = [];
|
|||||||
window.onload = async () => {
|
window.onload = async () => {
|
||||||
await get_updated_server_address();
|
await get_updated_server_address();
|
||||||
await parse_cross_codes();
|
await parse_cross_codes();
|
||||||
await initialize_elements();
|
// await initialize_elements();
|
||||||
await refresh_elements();
|
await refresh_elements();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -62,13 +62,23 @@ async function get_updated_server_address() {
|
|||||||
async function initialize_elements() {
|
async function initialize_elements() {
|
||||||
// Grab all updates from the backend.
|
// Grab all updates from the backend.
|
||||||
let overview = await update.refresh_overview(server_address);
|
let overview = await update.refresh_overview(server_address);
|
||||||
if (!overview.overview) {
|
if (!overview) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// console.log(overview)
|
||||||
const checks = await update.refresh_checks(server_address);
|
const checks = await update.refresh_checks(server_address);
|
||||||
|
if (!checks) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const entrances = await update.refresh_entrances(server_address);
|
const entrances = await update.refresh_entrances(server_address);
|
||||||
|
if (!entrances) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const hints = await update.refresh_hints(server_address);
|
const hints = await update.refresh_hints(server_address);
|
||||||
overview = overview.overview;
|
if (!hints) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Initialize global state
|
// Initialize global state
|
||||||
current_checks = checks.collected;
|
current_checks = checks.collected;
|
||||||
@ -111,12 +121,13 @@ async function initialize_elements() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function refresh_elements() {
|
async function refresh_elements() {
|
||||||
const response = await update.refresh_overview(server_address);
|
try {
|
||||||
|
const response = await update.fetch_api(server_address, "overview");
|
||||||
|
|
||||||
// Check if the "Hide completed areas" option is checked
|
// Check if the "Hide completed areas" option is checked
|
||||||
hide_completed_areas = document.getElementById("hideDone").checked;
|
hide_completed_areas = document.getElementById("hideDone").checked;
|
||||||
|
|
||||||
if (response.overview) {
|
if (response.data) {
|
||||||
if (!can_access_api_server) {
|
if (!can_access_api_server) {
|
||||||
console.info("I found the server!");
|
console.info("I found the server!");
|
||||||
document.getElementById("status-block").classList.add("hidden");
|
document.getElementById("status-block").classList.add("hidden");
|
||||||
@ -124,8 +135,11 @@ async function refresh_elements() {
|
|||||||
await initialize_elements();
|
await initialize_elements();
|
||||||
}
|
}
|
||||||
is_timeout = false;
|
is_timeout = false;
|
||||||
await update_if_changes(response.overview);
|
await update_if_changes(response.data);
|
||||||
} else if (response.error.error) {
|
setTimeout(refresh_elements, 500)
|
||||||
|
} else if (response.error) {
|
||||||
|
setTimeout(refresh_elements, 1100)
|
||||||
|
if (response.error.error) {
|
||||||
if (!is_timeout) {
|
if (!is_timeout) {
|
||||||
is_timeout = true;
|
is_timeout = true;
|
||||||
console.debug("Received timeout from API server.");
|
console.debug("Received timeout from API server.");
|
||||||
@ -142,7 +156,20 @@ async function refresh_elements() {
|
|||||||
}
|
}
|
||||||
can_access_api_server = false;
|
can_access_api_server = false;
|
||||||
}
|
}
|
||||||
setTimeout(refresh_elements, 500);
|
} else {
|
||||||
|
if (can_access_api_server) {
|
||||||
|
console.debug("Could not access the API server.");
|
||||||
|
document.getElementById("status-block").classList.remove("hidden");
|
||||||
|
}
|
||||||
|
can_access_api_server = false;
|
||||||
|
setTimeout(refresh_elements, 1100)
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
setTimeout(refresh_elements, 1100)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function update_if_changes(overview) {
|
async function update_if_changes(overview) {
|
||||||
|
Loading…
Reference in New Issue
Block a user