jdkjskdj
Some checks failed
Build and Deploy Nuxt / build (push) Successful in 1m10s
Test / test (push) Failing after 2s

This commit is contained in:
2026-10-19 19:42:10 +02:00
parent 1c98af69b2
commit b83f64ff9e
14 changed files with 299 additions and 9 deletions

View File

@@ -180,6 +180,8 @@ Hooks.once("init", () => {
onChange: handleAutoSyncChange,
});
registerPairingMenu();
const module = game.modules.get(MODULE_ID);
if (!module) {
console.warn(`${MODULE_ID} | Could not register the public API: module was not found.`);
@@ -427,6 +429,135 @@ async function refreshPairingStatus() {
await setPairingStatus(pairingStatusText());
}
// --- Connection actions ----------------------------------------------------
// The module settings open a small window for the two things a GM does about a
// pairing: hand its sign-up link to somebody, and let the campaign go again.
// The sign-up link lives in a client setting, so copying it is how it reaches
// the browser the user will open it in.
async function copyPairingLink() {
if (getSyncToken()) {
ui.notifications?.info("This world is already linked to a Dragonroll campaign; there is no sign-up link to copy.");
return;
}
if (!game.settings.get(MODULE_ID, SETTING_LINK_URL)?.trim()) await requestDragonrollLink();
const link = game.settings.get(MODULE_ID, SETTING_LINK_URL)?.trim();
if (!link) {
ui.notifications?.warn("Dragonroll does not have a sign-up link for this world right now. Check the Dragonroll Endpoint setting.");
return;
}
try {
await navigator.clipboard.writeText(link);
ui.notifications?.info("Dragonroll sign-up link copied to the clipboard.");
} catch (error) {
// A browser can refuse the clipboard; the link is still the GM's to copy.
console.warn(`${MODULE_ID} | Could not copy the Dragonroll sign-up link.`, error);
ui.notifications?.warn(`Copy the Dragonroll sign-up link yourself: ${link}`);
}
}
// Letting the campaign go from this side: Dragonroll forgets the world and drops
// the token, so this world can pair with a campaign again. An unreachable
// Dragonroll keeps the token — dropping it here would leave the campaign still
// listing a world that can no longer reach it.
async function unlinkDragonrollCampaign() {
if (!game.user?.isGM || !game.world) return;
const token = getSyncToken();
try {
if (token) await postDragonroll("foundry/sync/unlink", { worldId: game.world.id }, token);
} catch (error) {
if (error.message !== "unauthorized") {
console.error(`${MODULE_ID} | Could not unlink this world from Dragonroll.`, error);
ui.notifications?.warn(`Dragonroll could not unlink this world: ${error.message}`);
return;
}
}
await game.settings.set(MODULE_ID, SETTING_SYNC_TOKEN, "");
linkedCampaignName = "";
linkProblem = "";
await setLink("", "");
await setPairingStatus(pairingStatusText());
ui.notifications?.info("This Foundry world is no longer linked to its Dragonroll campaign.");
await requestDragonrollLink();
}
function registerPairingMenu() {
const ApplicationV2 = globalThis.foundry?.applications?.api?.ApplicationV2;
// The module is loaded without Foundry's settings UI in its tests, and no
// Foundry this module runs on lacks ApplicationV2.
if (typeof game.settings.registerMenu !== "function" || !ApplicationV2) return;
class DragonrollPairingMenu extends ApplicationV2 {
static DEFAULT_OPTIONS = {
id: "dragonroll-pairing-menu",
classes: [MODULE_ID],
window: { title: "Dragonroll Connection", icon: "fa-solid fa-link" },
position: { width: 560 },
actions: {
copyLink() {
return copyPairingLink().then(() => this.render());
},
unlink() {
return unlinkDragonrollCampaign().then(() => this.render());
},
},
};
async _renderHTML() {
const link = game.settings.get(MODULE_ID, SETTING_LINK_URL)?.trim();
const root = document.createElement("div");
root.className = "dragonroll-pairing-menu";
const status = document.createElement("p");
status.textContent = pairingStatusText();
root.append(status);
if (link) {
const field = document.createElement("input");
field.type = "text";
field.readOnly = true;
field.value = link;
field.addEventListener("focus", () => field.select());
root.append(field);
}
const actions = document.createElement("div");
actions.className = "dragonroll-pairing-actions";
const copy = document.createElement("button");
copy.type = "button";
copy.dataset.action = "copyLink";
copy.innerHTML = '<i class="fa-solid fa-copy"></i> Copy link';
copy.disabled = Boolean(getSyncToken());
const unlink = document.createElement("button");
unlink.type = "button";
unlink.dataset.action = "unlink";
unlink.innerHTML = '<i class="fa-solid fa-link-slash"></i> Unlink campaign';
unlink.disabled = !getSyncToken();
actions.append(copy, unlink);
root.append(actions);
return root;
}
_replaceHTML(result, content) {
content.replaceChildren(result);
}
}
game.settings.registerMenu(MODULE_ID, "pairingMenu", {
name: "Dragonroll Connection",
label: "Copy link / Unlink",
hint: "Copy this world's sign-up link, or unlink the Dragonroll campaign it was paired with.",
icon: "fa-solid fa-link",
type: DragonrollPairingMenu,
restricted: true,
});
}
function scheduleSnapshotSync(delay = 500) {
clearTimeout(syncTimeout);
syncTimeout = setTimeout(publishSnapshot, delay);