1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-27 03:46:54 +00:00
not-gwent-online/server/Deck.js

139 lines
2.7 KiB
JavaScript
Raw Normal View History

2015-06-13 19:36:02 +00:00
var Card = require("./Card");
/*var CardManager = require("./CardManager");*/
2015-06-30 16:26:24 +00:00
var DeckData = require("../assets/data/deck");
2015-07-01 16:55:50 +00:00
var _ = require("underscore");
2015-06-13 19:36:02 +00:00
var Deck = (function(){
2015-06-30 10:11:50 +00:00
var Deck = function(deck, side){
2015-06-13 19:36:02 +00:00
if(!(this instanceof Deck)){
2015-06-30 10:11:50 +00:00
return (new Deck(deck, side));
2015-06-13 19:36:02 +00:00
}
/**
* constructor here
*/
2015-06-30 10:11:50 +00:00
this.side = side;
2015-06-13 19:36:02 +00:00
this._deck = [];
2015-07-01 16:55:50 +00:00
//if(typeof deck !== "object") throw new Error("Deck is not an object!");
2015-06-28 14:10:25 +00:00
2015-06-13 19:36:02 +00:00
this._originalDeck = [];
2015-07-01 16:55:50 +00:00
this.setDeck(deck);
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;
2015-06-30 10:11:50 +00:00
r.side = null;
2015-06-28 14:10:25 +00:00
Deck.FACTION = {
NORTHERN_REALM: "northern",
SCOIATAEL: "scoiatael",
NILFGAARDIAN_EMPIRE: "nilfgaardian",
MONSTERS: "monster"
}
2015-06-13 19:36:02 +00:00
2015-07-01 16:55:50 +00:00
r.setDeck = function(deckKey){
var deck = DeckData[deckKey] ? DeckData[deckKey] : DeckData["northern"];
if(deckKey === "random"){
var decks = _.allKeys(DeckData);
deck = DeckData[decks[(Math.random() * decks.length) | 0]];
2015-06-30 16:26:24 +00:00
}
2015-07-01 16:55:50 +00:00
this._originalDeck = deck.data.slice();
this._deck = deck.data.slice();
this._faction = deck.faction;
2015-06-13 19:36:02 +00:00
this._loadCards();
this.shuffle();
}
2015-07-01 16:55:50 +00:00
r.getFaction = function(){
2015-06-28 14:10:25 +00:00
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(){
2015-06-30 10:11:50 +00:00
var self = this;
2015-06-14 14:01:25 +00:00
this._deck = this.getDeck().map(function(cardkey){
2015-06-30 10:11:50 +00:00
//return Card(cardkey);
return self.side.createCard(cardkey);
2015-06-13 19:36:02 +00:00
});
}
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;