1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/server/Deck.js

106 lines
1.9 KiB
JavaScript
Raw Normal View History

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 = [];
this._originalDeck = [];
this.setDeck(deck);
};
var r = Deck.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._deck = null;
r._owner = null;
r._originalDeck = null;
r.setDeck = function(deckData){
this._originalDeck = deckData.slice();
this._deck = deckData.slice();
this._loadCards();
this.shuffle();
}
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;
}
}
return Deck;
})();
module.exports = Deck;