When adding a new door to a synchronization group bring it in sync with the other doors

This commit is contained in:
2020-12-05 18:17:24 +01:00
parent 983bb9764f
commit 948a7eb06d
3 changed files with 31 additions and 14 deletions

2
CHANGELOG.md Normal file
View File

@@ -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

42
main.js
View File

@@ -73,22 +73,25 @@ function hookWallConfigUpdate() {
// Store our custom data from the WallConfig dialog // Store our custom data from the WallConfig dialog
async function onWallConfigUpdate(event, formData) { async function onWallConfigUpdate(event, formData) {
// TODO Bring newly merged doors in sync
const updateData = {flags: {smartdoors: {synchronizationGroup: formData.synchronizationGroup}}} 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 a synchronization group is set, get the state of existing doors and assume their state
if (ids.length > 0) { if (formData.synchronizationGroup) {
// Multiple walls are edited at once. Update all of them const doorInGroup = findInAllWalls(wall => wall.door && wall.flags.smartdoors?.synchronizationGroup == formData.synchronizationGroup && !ids.includes(wall._id));
const updateDataset = ids.reduce((dataset, id) => { if (doorInGroup)
dataset.push({_id: id, ...updateData}) updateData.ds = doorInGroup.ds;
return dataset
}, [])
return canvas.scene.updateEmbeddedEntity("Wall", updateDataset)
}
else {
// Only one wall is being edited
return this.object.update(updateData);
} }
// 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. // 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. // Searches through all scenes for walls and returns those that match the given filter criteria.
function filterAllWalls(filterFn) { 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 // Our custom handler for mousedown events on doors

View File

@@ -20,5 +20,6 @@
"manifest": "https://raw.githubusercontent.com/manuelVo/foundryvtt-smart-doors/master/module.json", "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", "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", "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" "bugs": "https://github.com/manuelVo/foundryvtt-smart-doors/issues"
} }