Fix issue #99: Make maps sync
All checks were successful
Test / test (push) Successful in 2s
All checks were successful
Test / test (push) Successful in 2s
This commit is contained in:
@@ -33,9 +33,13 @@ let itemSyncRunning = false;
|
||||
let itemSyncErrorLogged = false;
|
||||
let folderSyncTimeout;
|
||||
let folderSyncRunning = false;
|
||||
let sceneSyncTimeout;
|
||||
let sceneSyncRetryTimeout;
|
||||
let sceneSyncErrorLogged = false;
|
||||
const syncingItemIds = new Set();
|
||||
const syncingFolderIds = new Set();
|
||||
const queuedItemChanges = new Map();
|
||||
const queuedSceneChanges = new Map();
|
||||
|
||||
function getCollectionContents(collection, collectionName) {
|
||||
if (!collection) {
|
||||
@@ -300,6 +304,7 @@ async function applyDragonrollLink(result) {
|
||||
scheduleSnapshotSync(0);
|
||||
setTimeout(initializeCharacterSync, 0);
|
||||
setTimeout(reconcileDragonrollItems, 2000);
|
||||
setTimeout(initializeSceneSync, 1000);
|
||||
} else {
|
||||
// Without a token this client cannot sync, and only a fresh link gives one.
|
||||
linkProblem = "this client could not save the token it was given";
|
||||
@@ -441,6 +446,8 @@ function handleAutoSyncChange(value) {
|
||||
clearTimeout(actorSyncTimeout);
|
||||
clearTimeout(itemSyncTimeout);
|
||||
clearTimeout(folderSyncTimeout);
|
||||
clearTimeout(sceneSyncTimeout);
|
||||
clearTimeout(sceneSyncRetryTimeout);
|
||||
return;
|
||||
}
|
||||
if (!game.user?.isGM || !getSyncToken()) return;
|
||||
@@ -450,6 +457,7 @@ function handleAutoSyncChange(value) {
|
||||
initializeCharacterSync();
|
||||
reconcileDragonrollItems();
|
||||
queueFoundryItemFolders(400);
|
||||
initializeSceneSync();
|
||||
if (queuedActorChanges.size) {
|
||||
clearTimeout(actorSyncTimeout);
|
||||
actorSyncTimeout = setTimeout(flushActorChanges, 400);
|
||||
@@ -458,6 +466,10 @@ function handleAutoSyncChange(value) {
|
||||
clearTimeout(itemSyncTimeout);
|
||||
itemSyncTimeout = setTimeout(flushItemChanges, 400);
|
||||
}
|
||||
if (queuedSceneChanges.size) {
|
||||
clearTimeout(sceneSyncTimeout);
|
||||
sceneSyncTimeout = setTimeout(flushSceneChanges, 400);
|
||||
}
|
||||
}
|
||||
|
||||
function dragonrollActorId(actor) {
|
||||
@@ -1356,6 +1368,102 @@ function queueFoundryActorChange(kind, actor) {
|
||||
queueActorChange(kind, actor);
|
||||
}
|
||||
|
||||
// --- Scene sync ---------------------------------------------------------------
|
||||
// A scene of this world is one of the campaign's maps (Syndria98/dragonroll#99): its
|
||||
// background picture and the grid laid over it travel to Dragonroll, which keeps a map per
|
||||
// scene. That is all of a scene that crosses over — pinning notes on a map, standing tokens
|
||||
// on it and saying how far one of its cells goes are the campaign's own business, so none
|
||||
// of it comes back to the world.
|
||||
|
||||
// A scene travels as plain document data with its picture read from this world: Dragonroll
|
||||
// draws it outside Foundry, so the path becomes an absolute URL the way a portrait does.
|
||||
// Since V13 the picture sits under `background.src`, and under plain `img` before that.
|
||||
function serializeFoundryScene(scene) {
|
||||
const data = toPlainData(scene);
|
||||
if (!data) return null;
|
||||
|
||||
const absolute = (path) => new URL(path, window.location.origin).toString();
|
||||
if (data.background?.src && !/^(https?:|data:|\/\/)/i.test(data.background.src)) {
|
||||
data.background = { ...data.background, src: absolute(data.background.src) };
|
||||
} else if (data.img && !/^(https?:|data:|\/\/)/i.test(data.img)) {
|
||||
data.img = absolute(data.img);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// The scenes the campaign mirrors as its maps. Every scene goes, including one with no
|
||||
// picture: Dragonroll decides what of a scene is a map and, since such a scene's id is
|
||||
// still listed, no map of the campaign is pruned because of it.
|
||||
async function pushFoundryScenes(scenes, fullSync = false) {
|
||||
const token = getSyncToken();
|
||||
// A full push travels even with no scenes left: it is the one that tells the campaign
|
||||
// which of the maps it mirrors from this world the world has let go of.
|
||||
if (!token || !game.user?.isGM || !game.world || (!scenes.length && !fullSync)) return;
|
||||
|
||||
await postDragonroll("foundry/sync/scenes", {
|
||||
worldId: game.world.id,
|
||||
scenes: scenes.map(serializeFoundryScene).filter(Boolean),
|
||||
fullSync,
|
||||
}, token);
|
||||
sceneSyncErrorLogged = false;
|
||||
}
|
||||
|
||||
async function deleteDragonrollScene(foundrySceneId) {
|
||||
const token = getSyncToken();
|
||||
if (!token || !game.world || !foundrySceneId) return;
|
||||
await postDragonroll("foundry/sync/scene-delete", {
|
||||
worldId: game.world.id,
|
||||
foundrySceneId,
|
||||
}, token);
|
||||
}
|
||||
|
||||
function reportSceneSyncError(error) {
|
||||
if (sceneSyncErrorLogged) return;
|
||||
sceneSyncErrorLogged = true;
|
||||
console.warn(`${MODULE_ID} | Map sync failed and will retry.`, error);
|
||||
}
|
||||
|
||||
function queueFoundrySceneChange(kind, scene) {
|
||||
if (!scene || !game.user?.isGM || !getSyncToken()) return;
|
||||
|
||||
queuedSceneChanges.set(scene.id, { kind, scene });
|
||||
clearTimeout(sceneSyncTimeout);
|
||||
sceneSyncTimeout = setTimeout(flushSceneChanges, 400);
|
||||
}
|
||||
|
||||
async function flushSceneChanges() {
|
||||
// Autosync off: the scene edits wait here, like the actor and item changes do.
|
||||
if (!isAutoSyncEnabled()) return;
|
||||
const changes = [...queuedSceneChanges.values()];
|
||||
queuedSceneChanges.clear();
|
||||
const deleted = changes.filter((change) => change.kind === "delete");
|
||||
const changed = changes.filter((change) => change.kind !== "delete");
|
||||
|
||||
try {
|
||||
for (const change of deleted) await deleteDragonrollScene(change.scene.id);
|
||||
if (changed.length) await pushFoundryScenes(changed.map((change) => change.scene));
|
||||
} catch (error) {
|
||||
reportSceneSyncError(error);
|
||||
for (const change of changes) queuedSceneChanges.set(change.scene.id, change);
|
||||
clearTimeout(sceneSyncTimeout);
|
||||
sceneSyncTimeout = setTimeout(flushSceneChanges, 5000);
|
||||
}
|
||||
}
|
||||
|
||||
// The whole scene list travels when the world links and when it loads: the campaign's map
|
||||
// list mirrors this world's scenes, and only a push holding every scene can say which of
|
||||
// those maps the world has let go of.
|
||||
async function initializeSceneSync() {
|
||||
if (!getSyncToken() || !game.user?.isGM || !game.world || !isAutoSyncEnabled()) return;
|
||||
try {
|
||||
await pushFoundryScenes(game.scenes?.contents || [], true);
|
||||
} catch (error) {
|
||||
reportSceneSyncError(error);
|
||||
clearTimeout(sceneSyncRetryTimeout);
|
||||
sceneSyncRetryTimeout = setTimeout(initializeSceneSync, 15000);
|
||||
}
|
||||
}
|
||||
|
||||
async function publishSnapshot() {
|
||||
if (!game.user?.isGM || !game.world) return;
|
||||
if (syncInProgress) {
|
||||
@@ -1448,6 +1556,10 @@ Hooks.once("ready", () => {
|
||||
// already seen the world directory when it starts sending its own items back.
|
||||
setTimeout(reconcileDragonrollItems, 2000);
|
||||
setInterval(reconcileDragonrollItems, 5000);
|
||||
// The campaign's maps mirror this world's scenes: the whole list goes once the world is
|
||||
// loaded, and that push alone tells the campaign which of its synced maps this world has
|
||||
// let go of.
|
||||
setTimeout(initializeSceneSync, 3000);
|
||||
|
||||
for (const event of [
|
||||
"createActor", "updateActor", "deleteActor",
|
||||
@@ -1481,6 +1593,13 @@ Hooks.once("ready", () => {
|
||||
queueFoundryItemChange(isDelete ? "delete" : "upsert", document);
|
||||
});
|
||||
}
|
||||
|
||||
// A scene the GM makes, renames or throws away is one of the campaign's maps being drawn
|
||||
// or let go of (Syndria98/dragonroll#99). Only its picture and grid cross over, so this
|
||||
// goes one way and needs no snapshot of the world behind it.
|
||||
for (const event of ["createScene", "updateScene", "deleteScene"]) {
|
||||
Hooks.on(event, (document) => queueFoundrySceneChange(event.startsWith("delete") ? "delete" : "upsert", document));
|
||||
}
|
||||
});
|
||||
|
||||
// A GM sends selected actors to Dragonroll from the actor directory's context
|
||||
|
||||
Reference in New Issue
Block a user