From 0d941bff4787eda0b4e74152efc10eb337140cb1 Mon Sep 17 00:00:00 2001 From: Thaum Date: Mon, 3 Nov 2014 14:07:55 +0000 Subject: [PATCH] Began implementing features that change stats --- rpg-docs/Model/Character/Characters.js | 61 ++++++++++++++----- .../Model/Character/Constructors/Effect.js | 42 ------------- .../Model/Character/Constructors/Feature.js | 4 -- rpg-docs/Model/Character/Features.js | 9 +++ rpg-docs/Routes/Routes.js | 33 +++++----- .../client/views/character/character.html | 3 + rpg-docs/client/views/character/features.html | 8 +++ rpg-docs/client/views/character/features.js | 20 ++++++ 8 files changed, 103 insertions(+), 77 deletions(-) delete mode 100644 rpg-docs/Model/Character/Constructors/Feature.js create mode 100644 rpg-docs/Model/Character/Features.js create mode 100644 rpg-docs/client/views/character/features.html create mode 100644 rpg-docs/client/views/character/features.js diff --git a/rpg-docs/Model/Character/Characters.js b/rpg-docs/Model/Character/Characters.js index 2bd69fa2..108d9a67 100644 --- a/rpg-docs/Model/Character/Characters.js +++ b/rpg-docs/Model/Character/Characters.js @@ -1,14 +1,5 @@ //set up the collection for characters -Characters = new Meteor.Collection("characters", { - //transform function alters the object returned by the database - transform: function (doc) { - //extend character with its protoypal functions - var newInstance = Object.create(protoCharacter); - doc = _.extend(newInstance, doc); - - return doc; - } -}); +Characters = new Meteor.Collection("characters"); var attributes = [ {name: "strength"}, @@ -100,6 +91,7 @@ var strings = [ "alignment", "gender", "race", + "class", "description", "personality", "ideals", @@ -132,18 +124,20 @@ Character = function(owner){ success : 0, fail: 0 }; - - this.hitDice = []; - + this.weaponProficiencies = []; this.toolProficiencies = []; this.languages = []; + /* + //anything that needs to be edited rather than added or removed + //needs its own collection. + this.hitDice = []; + this.features = []; this.spells = []; - - this.classes = []; + */ this.vulnerability = {}; for(var i = 0, l = DamageTypes.length; i < l; i++){ @@ -159,7 +153,7 @@ Character = function(owner){ } //functions and calculated values go here -var protoCharacter = { +protoCharacter = { attributeValue: function(attribute){ if (attribute === undefined) return; //base value @@ -252,6 +246,41 @@ var protoCharacter = { passiveAbility: function(attribute){ var mod = +getMod(this.attributeValue(attribute)); return 10 + mod; + }, + + pushEffects : function(effectName, effectsArray){ + //check that the arguments are of the right for + check(effectName, String); + check(effectsArray, [{ _id: String, stat: String, value: Number}]); + + for(var i = 0; i < effectsArray.length; i++){ + var effect = effectsArray[i]; + + //check if the character exists with the field we are changing + if(pop(effect.stat, this) !== undefined){ + var newEffect = {}; + newEffect[effect.stat] = {_id: effect.id, name: effectName, value: effect.value}; + //update the field + Characters.update(this._id, {$push: newEffect}); + } + } + }, + + pullEffects : function(effectsArray){ + //check that the arguments are of the right form + check(effectsArray, [{ _id: String, stat: String, value: Number}]); + + for(var i = 0; i < effectsArray.length; i++){ + var effect = effectsArray[i]; + + //check if the character exists with the field we are changing + if(pop(effect.stat, this) !== undefined){ + var effectToPull = {}; + effectToPull[effect.stat] = {_id: effect.id}; + //update the field + Characters.update(this._id, {$pull: effectToPull}); + } + } } } diff --git a/rpg-docs/Model/Character/Constructors/Effect.js b/rpg-docs/Model/Character/Constructors/Effect.js index 071f3698..2ca880ee 100644 --- a/rpg-docs/Model/Character/Constructors/Effect.js +++ b/rpg-docs/Model/Character/Constructors/Effect.js @@ -2,46 +2,4 @@ Effect = function(stat, value){ this.stat = stat; this.value = value; this._id = new Mongo.ObjectID()._str; -} - - -pushEffects = function(characterId, effectName, effectsArray){ - //check that the arguments are of the right form - check(characterId, String); - check(effectName, String); - check(effectsArray, [{ _id: String, stat: String, value: Number}]); - - for(var i = 0; i < effectsArray.length; i++){ - var effect = effectsArray[i]; - - //check if the character exists with the field we are changing - var chk = {_id: characterId}; //right id - chk[effect.stat] = {$exists: true}; //has a field for the stat already - if(Characters.findOne(chk)){ - var newEffect = {}; - newEffect[effect.stat] = {_id: effect.id, name: effectName, value: effect.value}; - //update the field - Characters.update(characterId, {$push: newEffect}); - } - } -} - -pullEffects = function(characterId, effectsArray){ - //check that the arguments are of the right form - check(characterId, String); - check(effectsArray, [{ _id: String, stat: String, value: Number}]); - - for(var i = 0; i < effectsArray.length; i++){ - var effect = effectsArray[i]; - - //check if the character exists with the field we are changing - var chk = {_id: characterId}; //right id - chk[effect.stat] = {$exists: true}; //has a field for the stat already - if(Characters.findOne(chk)){ - var effectToPull = {}; - effectToPull[effect.stat] = {_id: effect.id}; - //update the field - Characters.update(characterId, {$pull: effectToPull}); - } - } } \ No newline at end of file diff --git a/rpg-docs/Model/Character/Constructors/Feature.js b/rpg-docs/Model/Character/Constructors/Feature.js deleted file mode 100644 index 942473ab..00000000 --- a/rpg-docs/Model/Character/Constructors/Feature.js +++ /dev/null @@ -1,4 +0,0 @@ -Feature = function(name){ - this.name = name; - this.description = description; -} \ No newline at end of file diff --git a/rpg-docs/Model/Character/Features.js b/rpg-docs/Model/Character/Features.js new file mode 100644 index 00000000..52303808 --- /dev/null +++ b/rpg-docs/Model/Character/Features.js @@ -0,0 +1,9 @@ +Features = new Meteor.Collection("features"); + +Feature = function(characterId){ + this.character = characterId; + this.name = "New Feature"; + this.description = ""; + this.effects = []; + this.enabled = false; +} \ No newline at end of file diff --git a/rpg-docs/Routes/Routes.js b/rpg-docs/Routes/Routes.js index 3ee65e61..52fd9867 100644 --- a/rpg-docs/Routes/Routes.js +++ b/rpg-docs/Routes/Routes.js @@ -1,20 +1,23 @@ Router.map( function () { - this.route('home', { - path: '/', - data: { characters: function(){ - console.log('id ' + Meteor.userId()); - return Characters.find({owner: Meteor.userId()}) - } - } + this.route('home', + { + path: '/', + data: { + characters: function(){ + return Characters.find({owner: Meteor.userId()}) + } + } }); - + this.route('character', { - path: '/character/:_id', - notFoundTemplate: 'characterNotFound', - data: function() { - character = Characters.findOne({_id: this.params._id}); - if (character) character.items = Items.find({owner: this.params._id}); - return character; - } + path: '/character/:_id', + notFoundTemplate: 'characterNotFound', + data: function() { + var data = Characters.findOne({_id: this.params._id}); + data.features = Features.find({character: data._id}); + var newInstance = Object.create(protoCharacter); + data = _.extend(newInstance, data); + return data; + } }); }); \ No newline at end of file diff --git a/rpg-docs/client/views/character/character.html b/rpg-docs/client/views/character/character.html index 11dea4ed..ca32f394 100644 --- a/rpg-docs/client/views/character/character.html +++ b/rpg-docs/client/views/character/character.html @@ -17,5 +17,8 @@
{{> characterStats}}
+
+ {{> features}} +
\ No newline at end of file diff --git a/rpg-docs/client/views/character/features.html b/rpg-docs/client/views/character/features.html new file mode 100644 index 00000000..853f5fbd --- /dev/null +++ b/rpg-docs/client/views/character/features.html @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/rpg-docs/client/views/character/features.js b/rpg-docs/client/views/character/features.js new file mode 100644 index 00000000..99651b64 --- /dev/null +++ b/rpg-docs/client/views/character/features.js @@ -0,0 +1,20 @@ +Template.features.helpers = { + features: function(){ + var features = Features.find({character: this._id}); + console.log('found: ', features); + return features; + } +} + +Template.features.events = { + // Fires when any element is clicked + 'change .enabled': function (event) { + var enable = event.target.checked + Features.update(this._id, {$set: {enabled: enable}}); + if(enable){ + Template.parentData(1).pushEffects(this.name, this.effects); + } else { + Template.parentData(1).pullEffects(this.effects); + } + } +} \ No newline at end of file