jdkjskdj
This commit is contained in:
@@ -54,6 +54,13 @@ gives the reason when a pairing code can never be redeemed, and says the world i
|
||||
once the campaign unlinks it. Because both the pairing code and the sync token are client settings,
|
||||
another browser of the same world reads as not paired.
|
||||
|
||||
**Dragonroll Connection** in the module settings opens the same state with the two actions a GM needs:
|
||||
**Copy link** puts the sign-up link on the clipboard to hand to whoever will pick the campaign, and
|
||||
**Unlink campaign** lets the world go. Unlinking drops the token on this client and on the campaign
|
||||
(`POST /api/foundry/sync/unlink`), so the campaign's settings stop listing the world and it can be
|
||||
paired again. If Dragonroll cannot be reached the token is kept and the failure is reported instead —
|
||||
dropping it alone would leave the campaign still believing the world is connected.
|
||||
|
||||
The campaign settings show the linked world's title and system. A Foundry world can be linked to
|
||||
only one campaign at a time. The pairing endpoint accepts cross-origin requests from the Foundry
|
||||
client and uses the one-time code instead of a Dragonroll login token.
|
||||
@@ -182,6 +189,7 @@ The module calls these Dragonroll routes with the campaign sync token (no Dragon
|
||||
| `GET /api/foundry/sync/notes/revision` | A hash of the campaign's notes and folder paths, for the module's change check |
|
||||
| `POST /api/foundry/sync/map-note` | Store a note's journal page id after creating that page |
|
||||
| `GET /api/foundry/sync/status` | The campaign this world's token belongs to, for the module's pairing status |
|
||||
| `POST /api/foundry/sync/unlink` | The module unlinks the world; the campaign forgets it and the token is revoked |
|
||||
| `POST /api/foundry/sync/map` / `map-item` | Store an id mapping after creating a document |
|
||||
| `POST /api/foundry/sync/delete` | An actor was deleted |
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -25,3 +25,19 @@
|
||||
text-decoration: underline dashed;
|
||||
cursor: help;
|
||||
}
|
||||
|
||||
/* The connection window the module settings open: the sign-up link and its actions. */
|
||||
.dragonroll-pairing-menu {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.dragonroll-pairing-menu input[readonly] {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.dragonroll-pairing-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user