plugin-backend #1

Merged
ada merged 34 commits from plugin-backend into main 2024-05-01 00:21:12 -05:00
2 changed files with 96 additions and 38 deletions
Showing only changes of commit 5e3115fb23 - Show all commits

View File

@ -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(); } else {
if (!("error" in data)) { if (response.error) {
return { overview: data, error: null }; return response.error;
} else {
return { overview: null, error: data };
}
} 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,
}; };

View File

@ -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,16 +135,26 @@ 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)
if (!is_timeout) { } else if (response.error) {
is_timeout = true; setTimeout(refresh_elements, 1100)
console.debug("Received timeout from API server."); if (response.error.error) {
} if (!is_timeout) {
if (!can_access_api_server) { is_timeout = true;
console.info("I found the server!"); console.debug("Received timeout from API server.");
document.getElementById("status-block").classList.add("hidden"); }
can_access_api_server = true; if (!can_access_api_server) {
console.info("I found the server!");
document.getElementById("status-block").classList.add("hidden");
can_access_api_server = true;
}
} 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;
} }
} else { } else {
if (can_access_api_server) { if (can_access_api_server) {
@ -141,8 +162,14 @@ async function refresh_elements() {
document.getElementById("status-block").classList.remove("hidden"); document.getElementById("status-block").classList.remove("hidden");
} }
can_access_api_server = false; can_access_api_server = false;
setTimeout(refresh_elements, 1100)
} }
setTimeout(refresh_elements, 500);
} catch (error) {
setTimeout(refresh_elements, 1100)
}
} }
async function update_if_changes(overview) { async function update_if_changes(overview) {