2015-06-13 19:36:02 +00:00
|
|
|
var Card = require("./Card");
|
|
|
|
/*var CardManager = require("./CardManager");*/
|
|
|
|
|
|
|
|
var Deck = (function(){
|
|
|
|
var Deck = function(deck){
|
|
|
|
if(!(this instanceof Deck)){
|
|
|
|
return (new Deck(deck));
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
this._deck = [];
|
|
|
|
|
2015-06-28 14:10:25 +00:00
|
|
|
if(typeof deck !== "object") throw new Error("Deck is not an object!");
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
this._originalDeck = [];
|
2015-06-28 14:10:25 +00:00
|
|
|
this._faction = deck.faction;
|
|
|
|
this.setDeck(deck.data);
|
2015-06-13 19:36:02 +00:00
|
|
|
};
|
|
|
|
var r = Deck.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
r._deck = null;
|
|
|
|
r._owner = null;
|
|
|
|
r._originalDeck = null;
|
2015-06-28 14:10:25 +00:00
|
|
|
r._faction = null;
|
|
|
|
|
|
|
|
Deck.FACTION = {
|
|
|
|
NORTHERN_REALM: "northern",
|
|
|
|
SCOIATAEL: "scoiatael",
|
|
|
|
NILFGAARDIAN_EMPIRE: "nilfgaardian",
|
|
|
|
MONSTERS: "monster"
|
|
|
|
}
|
2015-06-13 19:36:02 +00:00
|
|
|
|
|
|
|
r.setDeck = function(deckData){
|
|
|
|
this._originalDeck = deckData.slice();
|
|
|
|
this._deck = deckData.slice();
|
|
|
|
|
|
|
|
this._loadCards();
|
|
|
|
this.shuffle();
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:10:25 +00:00
|
|
|
r.getFaction = function() {
|
|
|
|
return this._faction;
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.getLength = function(){
|
|
|
|
return this._deck.length;
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r.length = function(){
|
2015-06-13 19:36:02 +00:00
|
|
|
return this.getLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
r.getDeck = function(){
|
|
|
|
return this._deck;
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:06:13 +00:00
|
|
|
r.draw = function(){
|
2015-06-13 19:36:02 +00:00
|
|
|
if(!this._deck.length) return 0;
|
|
|
|
var card = this.pop();
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r._loadCards = function(){
|
|
|
|
this._deck = this.getDeck().map(function(cardkey){
|
2015-06-13 19:36:02 +00:00
|
|
|
return Card(cardkey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
r.pop = function(){
|
2015-06-14 14:01:25 +00:00
|
|
|
var id = this._deck.pop();
|
|
|
|
/*
|
|
|
|
var card = CardManager().getCardById(id);*/
|
2015-06-13 19:36:02 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r.find = function(key, val){
|
2015-06-13 19:36:02 +00:00
|
|
|
var res = [];
|
2015-06-14 14:01:25 +00:00
|
|
|
this.getDeck().forEach(function(card){
|
2015-06-13 19:36:02 +00:00
|
|
|
if(card.getProperty(key) == val){
|
|
|
|
res.push(card);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return res;
|
2015-06-14 14:01:25 +00:00
|
|
|
}
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r.removeFromDeck = function(card){
|
2015-06-13 19:36:02 +00:00
|
|
|
var n = this.length();
|
|
|
|
|
|
|
|
for(var i = 0; i < n; i++) {
|
2015-06-14 14:01:25 +00:00
|
|
|
var c = this.getDeck()[i];
|
|
|
|
if(c.getID() === card.getID()){
|
2015-06-18 13:06:13 +00:00
|
|
|
return this.getDeck().splice(i, 1)[0];
|
2015-06-13 19:36:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.shuffle = function(){
|
|
|
|
var deck = this.getDeck();
|
|
|
|
|
|
|
|
var n = this.length();
|
|
|
|
for(var i = n - 1; i > 0; i--) {
|
|
|
|
var j = (Math.random() * i) | 0;
|
|
|
|
var tmp;
|
|
|
|
|
|
|
|
tmp = deck[j];
|
|
|
|
deck[j] = deck[i];
|
|
|
|
deck[i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-28 14:10:25 +00:00
|
|
|
r.add = function(card){
|
2015-06-23 16:12:11 +00:00
|
|
|
this._deck.push(card);
|
|
|
|
}
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
return Deck;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Deck;
|