1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-11-23 19:36:53 +00:00
not-gwent-online/server/Hand.js

91 lines
1.7 KiB
JavaScript
Raw Normal View History

2015-06-13 19:36:02 +00:00
/*var $ = require("jquery");*//*
var CardManager = require("./CardManager");*//*
var PubSub = require("./pubsub");*/
2015-06-14 18:50:53 +00:00
var Card = require("./Card");
2015-06-13 19:36:02 +00:00
var Hand = (function(){
var Hand = function(){
if(!(this instanceof Hand)){
return (new Hand());
}
/**
* constructor here
*/
this._hand = [];
};
var r = Hand.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._hand = null;
2015-06-21 14:50:50 +00:00
r.add = function(card){/*
console.log(card.getID(), card.getName());*/
2015-06-13 19:36:02 +00:00
this._hand.push(card);
}
r.getCards = function(){
return this._hand;
}
2015-06-14 14:01:25 +00:00
r.getCard = function(id) {
for(var i=0; i< this.length(); i++) {
var card = this.getCards()[i];
if(card.getID() === id) return card;
}
return -1;
}
2015-06-13 19:36:02 +00:00
r.remove = function(id){
var n = this.length();
2015-06-18 13:06:13 +00:00
//console.trace(id);
2015-06-14 18:50:53 +00:00
id = id instanceof Card ? id.getID() : id;
2015-06-21 14:50:50 +00:00
if(!n) return -1;
2015-06-13 19:36:02 +00:00
for(var i = 0; i < n; i++) {
2015-06-20 11:08:28 +00:00
if(!this._hand[i]) {
console.trace(this._hand[i]);
continue;
}
2015-06-14 14:01:25 +00:00
if(this._hand[i].getID() != id) continue;
2015-06-13 19:36:02 +00:00
return this._hand.splice(i, 1);
}
2015-06-19 12:14:37 +00:00
2015-06-13 19:36:02 +00:00
return -1;
}
r.getRandomCard = function(){
var rnd = (Math.random() * this._hand.length) | 0;
2015-06-21 14:50:50 +00:00
if(!this._hand.length) return -1;
2015-06-13 19:36:02 +00:00
return this._hand[rnd];
}
r.getLength = function(){
return this._hand.length;
}
r.length = function(){
return this._hand.length;
}
2015-06-19 12:14:37 +00:00
r.find = function(key, val) {
var res = [];
this._hand.forEach(function(card){
if(card.getProperty(key) == val){
res.push(card);
}
});
return res;
}
2015-06-13 19:36:02 +00:00
return Hand;
})();
module.exports = Hand;