From c76fe9914847ed8bfc72634e92517723ce1678d7 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 26 Jul 2017 21:02:36 +0100 Subject: [PATCH] Can now move and copy spells between characters --- rpg-docs/Model/Character/Spells.js | 95 +++++++++++++++---- .../views/character/inventory/inventory.js | 32 ++++--- .../client/views/character/spells/spells.js | 31 ++++-- 3 files changed, 117 insertions(+), 41 deletions(-) diff --git a/rpg-docs/Model/Character/Spells.js b/rpg-docs/Model/Character/Spells.js index ae85cf5f..2b5876b2 100644 --- a/rpg-docs/Model/Character/Spells.js +++ b/rpg-docs/Model/Character/Spells.js @@ -130,53 +130,98 @@ var checkMovePermission = function(spellId, parent, destinationOnly) { } }; -var moveSpell = function(spellId, parentCollection, parentId) { //moving spells between characters is NOT YET SUPPORTED :O +var moveSpell = function(spellId, targetCollection, targetId) { var spell = Spells.findOne(spellId); if (!spell) return; - parentCollection = parentCollection || spell.parent.collection; - parentId = parentId || spell.parent.id; + targetCollection = targetCollection || spell.parent.collection; + targetId = targetId || spell.parent.id; if (Meteor.isServer) { - checkMovePermission(spellId, {collection: parentCollection, id: parentId}, false); + checkMovePermission(spellId, {collection: targetCollection, id: targetId}, false); } - //update the spell provided the update will actually change something - if ( - spell.parent.collection !== parentCollection || - spell.parent.id !== parentId - ){ + if (targetCollection == "Characters") { //then we are copying the spell to a different character. + targetList = SpellLists.findOne({"charId": targetId}); + targetListId = targetList && targetList._id; + if (!targetListId) { + targetListId = SpellLists.insert({ //create a spell list if we don't already have one + name: "New SpellList", + charId: targetId, + saveDC: "8 + intelligenceMod + proficiencyBonus", + attackBonus: "intelligenceMod + proficiencyBonus", + }); + } + Spells.update( spellId, {$set: { - "parent.collection": parentCollection, - "parent.id": parentId, + charId: targetId, + "parent.collection": "SpellLists", + "parent.id": targetListId, }} ); } + else { //we are moving the spell within the same character + //update the spell provided the update will actually change something + if ( + spell.parent.collection !== targetCollection || + spell.parent.id !== targetId + ){ + Spells.update( + spellId, + {$set: { + "parent.collection": targetCollection, + "parent.id": targetId, + }} + ); + } + } }; -var copySpell = function(spellId, parentCollection, parentId) { +var copySpell = function(spellId, targetCollection, targetId) { var spell = Spells.findOne(spellId); if (!spell) return; - parentCollection = parentCollection || spell.parent.collection; - parentId = parentId || spell.parent.id; + targetCollection = targetCollection || spell.parent.collection; + targetId = targetId || spell.parent.id; if (Meteor.isServer) { - checkMovePermission(spellId, {collection: parentCollection, id: parentId}, true); //we're only reading from the source character + checkMovePermission(spellId, {collection: targetCollection, id: targetId}, true); //we're only reading from the source character } - if (spell.parentCollection == "Characters") { //then we are copying the spell to a different character. - //TODO: handle this - } else { //else we are copying the spell within the same character + if (targetCollection == "Characters") { //then we are copying the spell to a different character. + targetList = SpellLists.findOne({"charId": targetId}); + targetListId = targetList && targetList._id; + if (!targetListId) { + targetListId = SpellLists.insert({ //create a spell list if we don't already have one + name: "New SpellList", + charId: targetId, + saveDC: "8 + intelligenceMod + proficiencyBonus", + attackBonus: "intelligenceMod + proficiencyBonus", + }); + } + newSpell = _.clone(spell); delete newSpell._id; newSpellId = Spells.insert(newSpell); //add a new copy of the spell Spells.update( newSpellId, {$set: { - "parent.collection": parentCollection, - "parent.id": parentId, + charId: targetId, + "parent.collection": "SpellLists", + "parent.id": targetListId, + }} + ); + } + else { //else we are copying the spell within the same character + newSpell = _.clone(spell); + delete newSpell._id; + newSpellId = Spells.insert(newSpell); //add a new copy of the spell + Spells.update( + newSpellId, + {$set: { + "parent.collection": targetCollection, + "parent.id": targetId, }} ); } @@ -194,4 +239,14 @@ Meteor.methods({ check(spellListId, String); copySpell(spellId, "SpellLists", spellListId); }, + moveSpellToCharacter: function(spellId, charId) { + check(spellId, String); + check(charId, String); + moveSpell(spellId, "Characters", charId); + }, + copySpellToCharacter: function(spellId, charId) { + check(spellId, String); + check(charId, String); + copySpell(spellId, "Characters", charId); + }, }); \ No newline at end of file diff --git a/rpg-docs/client/views/character/inventory/inventory.js b/rpg-docs/client/views/character/inventory/inventory.js index 5a06a21f..bddd2014 100644 --- a/rpg-docs/client/views/character/inventory/inventory.js +++ b/rpg-docs/client/views/character/inventory/inventory.js @@ -334,21 +334,23 @@ Template.layout.events({ Session.set("inventory.dragItemId", null); }, "drop .characterRepresentative": function(event, instance) { - var itemId = event.originalEvent.dataTransfer.getData("dicecloud-id/items"); - if (event.ctrlKey){ - //split the stack to the container - pushDialogStack({ - template: "splitStackDialog", - data: { - id: itemId, - parentCollection: "Characters", - parentId: this._id, - }, - }); - } else { - //move item to the character - Meteor.call("moveItemToCharacter", itemId, this._id); + if (_.contains(event.originalEvent.dataTransfer.types, "dicecloud-id/items")){ + var itemId = event.originalEvent.dataTransfer.getData("dicecloud-id/items"); + if (event.ctrlKey){ + //split the stack to the container + pushDialogStack({ + template: "splitStackDialog", + data: { + id: itemId, + parentCollection: "Characters", + parentId: this._id, + }, + }); + } else { + //move item to the character + Meteor.call("moveItemToCharacter", itemId, this._id); + } + Session.set("inventory.dragItemId", null); } - Session.set("inventory.dragItemId", null); }, }); diff --git a/rpg-docs/client/views/character/spells/spells.js b/rpg-docs/client/views/character/spells/spells.js index 7b5bf170..04a9ea63 100644 --- a/rpg-docs/client/views/character/spells/spells.js +++ b/rpg-docs/client/views/character/spells/spells.js @@ -337,26 +337,45 @@ Template.spells.events({ Template.layout.events({ "dragstart .spellItem": function(event, instance){ event.originalEvent.dataTransfer.setData("dicecloud-id/spells", this._id); - Session.set("inventory.dragSpellId", this._id); + Session.set("spellLists.dragSpellId", this._id); }, "dragend .spellItem": function(event, instance){ - Session.set("inventory.dragSpellId", null); + Session.set("spellLists.dragSpellId", null); }, - "dragover .spellList, dragenter .spellList": - function(event, instance){ + "dragover .spellList, dragenter .spellList": function(event, instance){ if (_.contains(event.originalEvent.dataTransfer.types, "dicecloud-id/spells")){ event.preventDefault(); } }, "drop .spellList": function(event, instance){ var spellId = event.originalEvent.dataTransfer.getData("dicecloud-id/spells"); - //move spell to new list if (event.ctrlKey){ + //copy spell to new list Meteor.call("copySpellToList", spellId, this._id); } else { + //move spell to new list Meteor.call("moveSpellToList", spellId, this._id); } - Session.set("inventory.dragSpellId", null); + Session.set("spellLists.dragSpellId", null); + }, + + "dragover .characterRepresentative, dragenter .characterRepresentative": function(event, instance){ + if (_.contains(event.originalEvent.dataTransfer.types, "dicecloud-id/spells")){ + event.preventDefault(); + } + }, + "drop .characterRepresentative": function(event, instance) { + if (_.contains(event.originalEvent.dataTransfer.types, "dicecloud-id/spells")){ //to prevent conflicts with item drag/drop + var spellId = event.originalEvent.dataTransfer.getData("dicecloud-id/spells"); + if (event.ctrlKey){ + //copy spell to character + Meteor.call("copySpellToCharacter", spellId, this._id); + } else { + //move spell to character + Meteor.call("moveSpellToCharacter", spellId, this._id); + } + Session.set("spellLists.dragSpellId", null); + } }, }); \ No newline at end of file