From 948a7eb06d2aee0d9b244f413a6392a2041a4870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Sat, 5 Dec 2020 18:17:24 +0100 Subject: [PATCH] When adding a new door to a synchronization group bring it in sync with the other doors --- CHANGELOG.md | 2 ++ main.js | 42 ++++++++++++++++++++++++++++-------------- module.json | 1 + 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..36ca5c5 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,2 @@ +## v1.0.1 (in development) +- When adding a door to a synchronization group adjust it's state to bring it in sync with the other doors diff --git a/main.js b/main.js index 49f83b6..5888332 100644 --- a/main.js +++ b/main.js @@ -73,22 +73,25 @@ function hookWallConfigUpdate() { // Store our custom data from the WallConfig dialog async function onWallConfigUpdate(event, formData) { - // TODO Bring newly merged doors in sync const updateData = {flags: {smartdoors: {synchronizationGroup: formData.synchronizationGroup}}} + let ids = this.options.editTargets; + if (ids.length == 0) { + ids = [this.object.data._id]; + } - const ids = this.options.editTargets; - if (ids.length > 0) { - // Multiple walls are edited at once. Update all of them - const updateDataset = ids.reduce((dataset, id) => { - dataset.push({_id: id, ...updateData}) - return dataset - }, []) - return canvas.scene.updateEmbeddedEntity("Wall", updateDataset) - } - else { - // Only one wall is being edited - return this.object.update(updateData); + // If a synchronization group is set, get the state of existing doors and assume their state + if (formData.synchronizationGroup) { + const doorInGroup = findInAllWalls(wall => wall.door && wall.flags.smartdoors?.synchronizationGroup == formData.synchronizationGroup && !ids.includes(wall._id)); + if (doorInGroup) + updateData.ds = doorInGroup.ds; } + + // Update all the edited walls + const updateDataset = ids.reduce((dataset, id) => { + dataset.push({_id: id, ...updateData}) + return dataset + }, []) + return canvas.scene.updateEmbeddedEntity("Wall", updateDataset) } // Hook mouse events on DoorControls to perform our logic. @@ -117,7 +120,18 @@ function hookDoorEvents() { // Searches through all scenes for walls and returns those that match the given filter criteria. function filterAllWalls(filterFn) { - return game.scenes.map((scene) => {return {scene: scene, walls: scene.data.walls.filter(filterFn)}}); + // Find all walls that match the filter criteria + const scenes = game.scenes.map((scene) => {return {scene: scene, walls: scene.data.walls.filter(filterFn)}}) + // Drop all scenes that don't contain any results + return scenes.filter(scene => scene.walls.length > 0) +} + +// Searches through all scenes for a wall that matches the given filter criteria +function findInAllWalls(filterFn) { + // TODO The performance of this could be increased by stopping the search on the first hit + const scenes = filterAllWalls(filterFn) + // If results were found take the first wall from the first scene. + return scenes[0]?.walls[0] } // Our custom handler for mousedown events on doors diff --git a/module.json b/module.json index f7fd6eb..d70de6b 100644 --- a/module.json +++ b/module.json @@ -20,5 +20,6 @@ "manifest": "https://raw.githubusercontent.com/manuelVo/foundryvtt-smart-doors/master/module.json", "download": "https://github.com/manuelVo/foundryvtt-smart-doors/archive/v1.0.0.zip", "readme": "https://github.com/manuelVo/foundryvtt-smart-doors/blob/master/README.md", + "changelog": "https://github.com/manuelVo/foundryvtt-smart-doors/blob/master/CHANGELOG.md", "bugs": "https://github.com/manuelVo/foundryvtt-smart-doors/issues" }