- Service: accounts with Argon2 password hashes and session tokens; the first login creates the admin (shared savegames of before move to the admin); the admin creates and deletes users; everybody can change the password; savegames are stored and listed per user - Game: login page before the title (only when the service is reachable), with licence and repository link; account page (password, logout; users for the admin); text fields with Tab/Enter, masked passwords, browser dialog on touch screens; long status messages wrap - Download Standalone: DungeonCraft.html with the game embedded (built in the Docker image, served to logged-in users via nginx auth_request); works from file:// without server and opens downloaded savegames - Without the service (standalone, local files) there is no login, savegame files as before Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
// DungeonCraft service worker: makes the game installable and playable offline.
|
|
// Network first (updates arrive at once), the cache is the fallback when offline.
|
|
// The savegame service (/api/) is never cached.
|
|
const CACHE = "dungeoncraft-v2";
|
|
const CORE = [
|
|
"./",
|
|
"index.html",
|
|
"mq_js_bundle.js",
|
|
"dc_plugin.js",
|
|
"dungeoncraft.wasm",
|
|
"manifest.webmanifest",
|
|
"icons/icon-192.png",
|
|
"icons/icon-512.png",
|
|
];
|
|
|
|
self.addEventListener("install", (event) => {
|
|
event.waitUntil(caches.open(CACHE).then((c) => c.addAll(CORE)).then(() => self.skipWaiting()));
|
|
});
|
|
|
|
self.addEventListener("activate", (event) => {
|
|
event.waitUntil(
|
|
caches.keys()
|
|
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
|
.then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener("fetch", (event) => {
|
|
const req = event.request;
|
|
const url = new URL(req.url);
|
|
if (req.method !== "GET" || url.origin !== self.location.origin || url.pathname.includes("/api/") || url.pathname.includes("/standalone/")) {
|
|
return;
|
|
}
|
|
event.respondWith(
|
|
fetch(req)
|
|
.then((res) => {
|
|
if (res.ok) {
|
|
const copy = res.clone();
|
|
caches.open(CACHE).then((c) => c.put(req, copy));
|
|
}
|
|
return res;
|
|
})
|
|
.catch(() => caches.match(req).then((hit) => hit || caches.match("index.html")))
|
|
);
|
|
});
|