From 2f605767ecb81c09e9e684fa385e2081a9bd079c Mon Sep 17 00:00:00 2001 From: exane Date: Tue, 30 Jun 2015 12:11:50 +0200 Subject: [PATCH] add cardmanager (handling card ids) --- server/Battle.js | 26 ++++---------------------- server/Battleside.js | 17 ++++++++++++++--- server/Card.js | 12 +++++++----- server/CardManager.js | 31 +++++++++++++++++++++++++++++++ server/Deck.js | 12 +++++++++--- 5 files changed, 65 insertions(+), 33 deletions(-) create mode 100644 server/CardManager.js diff --git a/server/Battle.js b/server/Battle.js index 41900b9..aa93f0e 100644 --- a/server/Battle.js +++ b/server/Battle.js @@ -3,6 +3,7 @@ var Card = require("./Card"); var Deck = require("./Deck"); var shortid = require("shortid"); var Promise = require("jquery-deferred"); +var CardManager = require("./CardManager") var Battle = (function(){ @@ -13,12 +14,12 @@ var Battle = (function(){ /** * constructor here */ + this.cm = CardManager(); this.events = {}; this._id = id; this._user1 = p1; this._user2 = p2; this.socket = socket; - this.channel = {}; }; var r = Battle.prototype; /** @@ -33,6 +34,8 @@ var Battle = (function(){ r._user2 = null; r.turn = 0; + r.cm = null; + r.socket = null; r._id = null; @@ -57,27 +60,6 @@ var Battle = (function(){ this.p2.setLeadercard(); this.p1.draw(10); this.p2.draw(10); - /* - - this.p1.placeCard("ves"); - this.p2.placeCard("ves"); - this.p1.placeCard("yarpen_zigrin"); - this.p2.placeCard("yarpen_zigrin"); - - this.p1.hand.add(Card("scorch")); - this.p2.hand.add(Card("scorch")); - this.p1.hand.add(Card("villentretenmerth")); - this.p2.hand.add(Card("villentretenmerth")); - - this.p1.hand.add(Card("impenetrable_fog")); - this.p2.hand.add(Card("impenetrable_fog")); - this.p1.hand.add(Card("biting_frost")); - this.p2.hand.add(Card("biting_frost")); - this.p1.hand.add(Card("torrential_rain")); - this.p2.hand.add(Card("torrential_rain")); - this.p1.hand.add(Card("clear_weather")); - this.p2.hand.add(Card("clear_weather")); - */ this.update(); diff --git a/server/Battleside.js b/server/Battleside.js index 3c8a69d..6ccac0b 100644 --- a/server/Battleside.js +++ b/server/Battleside.js @@ -22,6 +22,8 @@ Battleside = (function(){ this._isWaiting = true; this.socket = user.socket; + this.cm = battle.cm; + this.field = {}; this.field[Card.TYPE.LEADER] = Field(this); this.field[Card.TYPE.CLOSE_COMBAT] = Field(this, true); @@ -32,7 +34,7 @@ Battleside = (function(){ this._name = user.getName(); this.battle = battle; this.hand = Hand(); - this.deck = Deck(DeckData[deck]); + this.deck = Deck(DeckData[deck], this); this._discard = []; this.runEvent = this.battle.runEvent.bind(this.battle); @@ -132,11 +134,17 @@ Battleside = (function(){ r.socket = null; r.n = null; + r.cm = null; + r.foe = null; r.hand = null; r.battle = null; r.deck = null; + r.createCard = function(key) { + return this.cm.create(key, this.n); + } + r.isPassing = function(){ return this._passing; } @@ -308,7 +316,8 @@ Battleside = (function(){ obj = _.extend({}, obj); if(typeof card === "string"){ - card = Card(card); + //card = Card(card); + card = this.createCard(card); } this.checkAbilities(card, obj); @@ -357,7 +366,9 @@ Battleside = (function(){ field = typeof field === "undefined" ? null : field; if(typeof card === "string"){ - card = Card(card); + //card = Card(card); + //card = this.cm.create(card); + card = this.createCard(card); } if(typeof field === "number"){ diff --git a/server/Card.js b/server/Card.js index 5b33492..f54fa57 100644 --- a/server/Card.js +++ b/server/Card.js @@ -2,13 +2,15 @@ var CardData = require("../assets/data/cards"); var AbilityData = require("../assets/data/abilities"); var Card = (function(){ - var Card = function(key){ + var Card = function(key, owner, id){ if(!(this instanceof Card)){ - return (new Card(key)); + return (new Card(key, owner, id)); } /** * constructor here */ + this._owner = owner; + this._id = id; this.boost = 0; this._uidEvents = {}; this.setDisabled(false); @@ -17,7 +19,7 @@ var Card = (function(){ this._data.key = key; this._boost = {}; this._forcedPower = -1; - this._init(); + //this._init(); }; var r = Card.prototype; /** @@ -33,7 +35,7 @@ var Card = (function(){ r._forcedPower = null; r._disabled = null; r._changedType = null; - Card.__id = 0; + //Card.__id = 0; Card.TYPE = { CLOSE_COMBAT: 0, RANGED: 1, @@ -54,7 +56,7 @@ var Card = (function(){ } r._init = function(){ - this._id = ++Card.__id; + //this._id = ++Card.__id; } r.getName = function(){ diff --git a/server/CardManager.js b/server/CardManager.js new file mode 100644 index 0000000..f87a936 --- /dev/null +++ b/server/CardManager.js @@ -0,0 +1,31 @@ +var Card = require("./Card"); + +var CardManager = (function(){ + var CardManager = function(){ + if(!(this instanceof CardManager)){ + return (new CardManager()); + } + /** + * constructor here + */ + + this._id = 0; + }; + var r = CardManager.prototype; + /** + * methods && properties here + * r.property = null; + * r.getProperty = function() {...} + */ + + r._id = null; + + r.create = function(key, owner) { + return this._cards[this._id] = Card(key, owner, this._id++); + } + + + return CardManager; +})(); + +module.exports = CardManager; \ No newline at end of file diff --git a/server/Deck.js b/server/Deck.js index 7484b7a..c16798c 100644 --- a/server/Deck.js +++ b/server/Deck.js @@ -2,13 +2,15 @@ var Card = require("./Card"); /*var CardManager = require("./CardManager");*/ var Deck = (function(){ - var Deck = function(deck){ + var Deck = function(deck, side){ if(!(this instanceof Deck)){ - return (new Deck(deck)); + return (new Deck(deck, side)); } /** * constructor here */ + + this.side = side; this._deck = []; if(typeof deck !== "object") throw new Error("Deck is not an object!"); @@ -28,6 +30,8 @@ var Deck = (function(){ r._originalDeck = null; r._faction = null; + r.side = null; + Deck.FACTION = { NORTHERN_REALM: "northern", SCOIATAEL: "scoiatael", @@ -66,8 +70,10 @@ var Deck = (function(){ } r._loadCards = function(){ + var self = this; this._deck = this.getDeck().map(function(cardkey){ - return Card(cardkey); + //return Card(cardkey); + return self.side.createCard(cardkey); }); }