2015-07-01 07:17:16 +00:00
|
|
|
var _ = require("underscore");
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
var Field = (function() {
|
|
|
|
var Field = function(side, hasHornField) {
|
|
|
|
if(!(this instanceof Field)) {
|
2015-06-21 14:50:50 +00:00
|
|
|
return (new Field(side, hasHornField));
|
2015-06-14 14:01:25 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
|
|
|
|
2015-06-21 14:50:50 +00:00
|
|
|
this._hasHornField = hasHornField || false;
|
2015-06-14 14:01:25 +00:00
|
|
|
this._cards = [];
|
2015-06-21 14:50:50 +00:00
|
|
|
this.side = side;
|
2015-06-14 14:01:25 +00:00
|
|
|
};
|
|
|
|
var r = Field.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
|
|
|
|
r._cards = null;
|
|
|
|
r._score = 0;
|
2015-06-21 14:50:50 +00:00
|
|
|
r._hasHornField = null;
|
|
|
|
r._hornCard = null;
|
|
|
|
r.side = null;
|
2015-06-14 14:01:25 +00:00
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.add = function(card, isHorn) {
|
2015-06-21 14:50:50 +00:00
|
|
|
/*if(card.hasAbility("commanders_horn")) {
|
|
|
|
this.setHorn(card);
|
|
|
|
return;
|
|
|
|
}*/
|
2015-12-23 12:09:07 +00:00
|
|
|
if(isHorn && this._hasHornField) {
|
2015-06-21 14:50:50 +00:00
|
|
|
this.setHorn(card);
|
|
|
|
return;
|
|
|
|
}
|
2015-06-14 14:01:25 +00:00
|
|
|
this._cards.push(card);
|
|
|
|
this.updateScore();
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.get = function() {
|
2015-06-14 14:01:25 +00:00
|
|
|
return this._cards;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getScore = function() {
|
2015-06-14 18:50:53 +00:00
|
|
|
this.updateScore();
|
2015-06-14 14:01:25 +00:00
|
|
|
return this._score;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.updateScore = function() {
|
2015-06-14 14:01:25 +00:00
|
|
|
this._score = 0;
|
2015-06-14 18:50:53 +00:00
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
2015-06-14 14:01:25 +00:00
|
|
|
var card = this._cards[i];
|
|
|
|
this._score += card.getPower();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getPosition = function(card) {
|
2015-06-14 18:50:53 +00:00
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
|
|
|
if(this._cards[i].getID() === card.getID()) return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.isOnField = function(card) {
|
|
|
|
if(this._hasHornField && this.getHorn() && card.getID() === this.getHorn().getID()) {
|
2015-06-21 14:50:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-06-19 19:53:48 +00:00
|
|
|
return this.getPosition(card) >= 0;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.replaceWith = function(oldCard, newCard) {
|
2015-06-14 18:50:53 +00:00
|
|
|
var index = this.getPosition(oldCard);
|
|
|
|
this._cards[index] = newCard;
|
2015-06-19 19:53:48 +00:00
|
|
|
oldCard.reset();
|
2015-07-05 08:55:27 +00:00
|
|
|
for(var event in oldCard._uidEvents) {
|
2015-12-23 12:09:07 +00:00
|
|
|
if(this.side && this.side.off) {
|
2015-07-05 11:21:56 +00:00
|
|
|
this.side.off(event, oldCard.getUidEvents(event));
|
|
|
|
}
|
2015-07-05 08:55:27 +00:00
|
|
|
}
|
2015-06-14 18:50:53 +00:00
|
|
|
return oldCard;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getCard = function(id) {
|
2015-06-14 18:50:53 +00:00
|
|
|
for(var i = 0; i < this._cards.length; i++) {
|
|
|
|
var card = this._cards[i];
|
|
|
|
if(card.getID() == id) return card;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.removeAll = function() {
|
2015-06-17 19:18:14 +00:00
|
|
|
var tmp = this._cards.slice();
|
2015-06-21 14:50:50 +00:00
|
|
|
var self = this;
|
2015-07-05 08:55:27 +00:00
|
|
|
/*for(var i = 0; i < tmp.length; i++) {
|
2015-07-01 07:17:16 +00:00
|
|
|
var card = tmp[i];
|
2015-07-03 07:47:28 +00:00
|
|
|
card.reset();
|
2015-07-01 07:17:16 +00:00
|
|
|
if(card.__lock){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for(var event in card._uidEvents) {
|
|
|
|
self.side.off(event, card.getUidEvents(event));
|
|
|
|
}
|
|
|
|
this._cards[i] = null;
|
2015-07-05 08:55:27 +00:00
|
|
|
}*/
|
2015-12-23 12:09:07 +00:00
|
|
|
tmp.forEach(function(card, i) {
|
2015-07-05 08:55:27 +00:00
|
|
|
card.reset();
|
2015-12-23 12:09:07 +00:00
|
|
|
if(card.__lock) {
|
2015-07-05 08:55:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(var event in card._uidEvents) {
|
2015-12-23 12:09:07 +00:00
|
|
|
if(this.side && this.side.off) {
|
2015-07-05 11:21:56 +00:00
|
|
|
this.side.off(event, card.getUidEvents(event));
|
|
|
|
}
|
2015-07-05 08:55:27 +00:00
|
|
|
}
|
|
|
|
this._cards[i] = null;
|
|
|
|
}, this)
|
2015-07-01 07:17:16 +00:00
|
|
|
|
|
|
|
this._cards = _.without(this._cards, null);
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
if(this.getHorn()) {
|
2015-06-21 14:50:50 +00:00
|
|
|
var card = this.getHorn();
|
|
|
|
card.reset();
|
|
|
|
this.setHorn(null);
|
|
|
|
for(var event in card._uidEvents) {
|
|
|
|
self.side.off(event, card.getUidEvents(event));
|
|
|
|
}
|
|
|
|
tmp.push(card);
|
|
|
|
}
|
2015-06-17 19:18:14 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
2015-06-14 14:01:25 +00:00
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.removeCard = function(cards) {
|
2015-06-26 11:17:28 +00:00
|
|
|
var res = [];
|
|
|
|
var _cards = this.get();
|
2015-12-23 12:09:07 +00:00
|
|
|
if(!Array.isArray(cards)) {
|
2015-07-01 07:17:16 +00:00
|
|
|
cards = [cards];
|
2015-06-26 11:17:28 +00:00
|
|
|
}
|
|
|
|
var self = this;
|
2015-12-23 12:09:07 +00:00
|
|
|
cards.forEach(function(card) {
|
2015-07-05 08:55:27 +00:00
|
|
|
card.reset();
|
|
|
|
for(var event in card._uidEvents) {
|
2015-12-23 12:09:07 +00:00
|
|
|
if(this.side && this.side.off) {
|
2015-07-05 11:21:56 +00:00
|
|
|
this.side.off(event, card.getUidEvents(event));
|
|
|
|
}
|
2015-07-05 08:55:27 +00:00
|
|
|
}
|
2015-06-26 11:17:28 +00:00
|
|
|
res.push(_cards.splice(self.getPosition(card), 1)[0]);
|
|
|
|
})
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getInfo = function() {
|
2015-06-21 14:50:50 +00:00
|
|
|
var self = this;
|
|
|
|
return {
|
|
|
|
cards: self._cards,
|
|
|
|
horn: self.getHorn(),
|
|
|
|
score: self._score
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getHorn = function() {
|
2015-06-21 14:50:50 +00:00
|
|
|
if(!this._hasHornField) return null;
|
|
|
|
return this._hornCard;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.setHorn = function(card) {
|
2015-06-21 14:50:50 +00:00
|
|
|
if(!this._hasHornField) return null;
|
|
|
|
this._hornCard = card;
|
|
|
|
}
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
r.getHighestCards = function(noHeroes) {
|
2015-06-26 12:40:19 +00:00
|
|
|
noHeroes = noHeroes || false;
|
2015-06-26 11:17:28 +00:00
|
|
|
var res = [];
|
|
|
|
var highest = 0;
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
this.get().forEach(function(card) {
|
2015-06-30 16:26:24 +00:00
|
|
|
if(noHeroes && card.hasAbility("hero")) return;
|
2015-06-26 11:17:28 +00:00
|
|
|
highest = card.getPower() > highest ? card.getPower() : highest;
|
|
|
|
})
|
|
|
|
|
2015-12-23 12:09:07 +00:00
|
|
|
this.get().forEach(function(card) {
|
2015-06-30 16:26:24 +00:00
|
|
|
if(noHeroes && card.hasAbility("hero")) return;
|
2015-06-26 11:17:28 +00:00
|
|
|
if(card.getPower() === highest) res.push(card);
|
|
|
|
});
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
return Field;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Field;
|