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

893 lines
23 KiB
JavaScript
Raw Normal View History

2015-06-13 19:36:02 +00:00
var DeckData = require("../assets/data/deck");
var Deck = require("./Deck");
var Hand = require("./Hand");
2015-06-14 14:01:25 +00:00
var Card = require("./Card");
var Field = require("./Field");
2015-06-19 12:14:37 +00:00
var _ = require("underscore");
2015-06-23 16:12:11 +00:00
var Promise = require("jquery-deferred");
2015-06-13 19:36:02 +00:00
2015-06-14 18:50:53 +00:00
var Battleside;
Battleside = (function(){
2015-06-30 08:46:31 +00:00
var Battleside = function(user, n, battle){
2015-06-10 16:12:52 +00:00
if(!(this instanceof Battleside)){
2015-06-30 08:46:31 +00:00
return (new Battleside(user, n, battle));
2015-06-10 16:12:52 +00:00
}
/**
* constructor here
*/
2015-06-19 12:14:37 +00:00
var deck = user.getDeck();
2015-06-14 18:50:53 +00:00
var self = this;
2015-06-14 14:01:25 +00:00
this._isWaiting = true;
this.socket = user.socket;
2015-06-30 08:46:31 +00:00
2015-06-30 10:11:50 +00:00
this.cm = battle.cm;
2015-06-14 14:01:25 +00:00
this.field = {};
2015-06-21 14:50:50 +00:00
this.field[Card.TYPE.LEADER] = Field(this);
this.field[Card.TYPE.CLOSE_COMBAT] = Field(this, true);
this.field[Card.TYPE.RANGED] = Field(this, true);
this.field[Card.TYPE.SIEGE] = Field(this, true);
2015-06-30 08:46:31 +00:00
2015-06-13 19:36:02 +00:00
this.n = n ? "p2" : "p1";
2015-06-30 08:46:31 +00:00
this._name = user.getName();
2015-06-13 19:36:02 +00:00
this.battle = battle;
this.hand = Hand();
2015-07-01 16:55:50 +00:00
this.deck = Deck(deck, this);
2015-06-17 19:18:14 +00:00
this._discard = [];
2015-06-14 14:01:25 +00:00
2015-06-17 16:10:23 +00:00
this.runEvent = this.battle.runEvent.bind(this.battle);
this.on = this.battle.on.bind(this.battle);
this.off = this.battle.off.bind(this.battle);
2015-06-14 14:01:25 +00:00
2015-06-18 13:06:13 +00:00
this.receive("activate:leader", function(){
if(self._isWaiting) return;
if(self.isPassing()) return;
var leaderCard = self.getLeader();
if(leaderCard.isDisabled()) return;
2015-07-01 18:18:09 +00:00
//console.log("leader activated");
2015-06-18 13:06:13 +00:00
var ability = leaderCard.getAbility();
2015-06-30 16:26:07 +00:00
ability.onActivate.apply(self, [leaderCard]);
2015-06-18 13:06:13 +00:00
leaderCard.setDisabled(true);
2015-06-30 11:43:03 +00:00
self.battle.sendNotification(self.getName() + " activated " + leaderCard.getName() + "! (leadercard)");
2015-06-18 13:06:13 +00:00
self.update();
2015-07-02 17:21:24 +00:00
if(ability.waitResponse){
2015-07-01 09:38:31 +00:00
return;
}
//self.runEvent("NextTurn", null, [self.foe]);
self.endTurn();
2015-06-18 13:06:13 +00:00
})
2015-06-14 18:50:53 +00:00
this.receive("play:cardFromHand", function(data){
if(self._isWaiting) return;
if(self.isPassing()) return;
var cardID = data.id;
var card = self.hand.getCard(cardID);
self.playCard(card);
})
this.receive("decoy:replaceWith", function(data){
if(self._isWaiting) return;
var card = self.findCardOnFieldByID(data.cardID);
2015-06-17 16:10:23 +00:00
if(card === -1) throw new Error("decoy:replace | unknown card");
self.runEvent("Decoy:replaceWith", self, [card]);
2015-06-14 18:50:53 +00:00
})
2015-06-18 13:06:13 +00:00
this.receive("cancel:decoy", function(){
self.off("Decoy:replaceWith");
})
this.receive("set:passing", function(){
2015-06-14 18:50:53 +00:00
self.setPassing(true);
2015-06-25 15:51:21 +00:00
self.update();
2015-06-30 11:43:03 +00:00
self.battle.sendNotification(self.getName() + " passed!");
2015-07-01 09:38:31 +00:00
//self.runEvent("NextTurn", null, [self.foe]);
self.endTurn();
2015-06-14 18:50:53 +00:00
})
2015-06-19 12:14:37 +00:00
this.receive("medic:chooseCardFromDiscard", function(data){
if(!data){
2015-07-01 09:38:31 +00:00
//self.runEvent("NextTurn", null, [self.foe]);
self.endTurn();
2015-06-19 12:14:37 +00:00
return;
}
var cardID = data.cardID;
var card = self.getCardFromDiscard(cardID);
if(card === -1) throw new Error("medic:chooseCardFromDiscard | unknown card: ", card);
self.removeFromDiscard(card);
self.playCard(card);
})
2015-06-30 16:26:07 +00:00
this.receive("emreis_leader4:chooseCardFromDiscard", function(data){
if(!data){
2015-07-01 09:38:31 +00:00
self.endTurn();
2015-06-30 16:26:07 +00:00
//self.runEvent("NextTurn", null, [self.foe]);
return;
}
var cardID = data.cardID;
var card = self.foe.getCardFromDiscard(cardID);
if(card === -1) throw new Error("emreis_leader4:chooseCardFromDiscard | unknown card: ", card);
self.foe.removeFromDiscard(card);
self.placeCard(card);
2015-07-01 09:38:31 +00:00
self.endTurn();
// self.runEvent("NextTurn", null, [self.foe]);
2015-06-30 16:26:07 +00:00
})
2015-06-21 14:50:50 +00:00
this.receive("agile:field", function(data){
2015-06-19 15:15:26 +00:00
var fieldType = data.field;
2015-06-21 14:50:50 +00:00
if(!(fieldType in [0, 1])) throw new Error("set field agile: false fieldtype " + fieldType);
2015-06-19 15:15:26 +00:00
self.runEvent("agile:setField", null, [fieldType]);
2015-07-01 09:38:31 +00:00
self.endTurn();
//self.runEvent("NextTurn", null, [self.foe]);
2015-06-19 15:15:26 +00:00
})
this.receive("cancel:agile", function(){
self.off("agile:setField");
})
2015-06-21 14:50:50 +00:00
this.receive("horn:field", function(data){
var fieldType = data.field;
if(!(fieldType in [0, 1, 2])) throw new Error("set field horn: false fieldtype " + fieldType);
self.runEvent("horn:setField", null, [fieldType]);
2015-07-01 09:38:31 +00:00
self.endTurn();
//self.runEvent("NextTurn", null, [self.foe]);
2015-06-21 14:50:50 +00:00
})
this.receive("cancel:horn", function(){
self.off("horn:setField");
})
2015-06-19 12:14:37 +00:00
2015-06-14 18:50:53 +00:00
2015-06-17 16:10:23 +00:00
this.on("Turn" + this.getID(), this.onTurnStart, this);
2015-06-10 16:12:52 +00:00
};
var r = Battleside.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
2015-06-13 19:36:02 +00:00
r._name = null;
2015-06-10 16:12:52 +00:00
r._discard = null;
2015-06-18 13:06:13 +00:00
2015-06-17 19:18:14 +00:00
r._rubies = 2;
2015-06-13 22:33:20 +00:00
r._score = 0;
2015-06-14 14:01:25 +00:00
r._isWaiting = null;
2015-06-14 18:50:53 +00:00
r._passing = null;
2015-06-14 14:01:25 +00:00
r.field = null;
r.socket = null;
r.n = null;
2015-06-10 16:12:52 +00:00
2015-06-30 10:11:50 +00:00
r.cm = null;
2015-06-13 19:36:02 +00:00
r.foe = null;
r.hand = null;
r.battle = null;
r.deck = null;
2015-06-30 11:43:03 +00:00
r.createCard = function(key){
2015-06-30 10:11:50 +00:00
return this.cm.create(key, this.n);
}
2015-06-18 13:06:13 +00:00
r.isPassing = function(){
2015-06-14 18:50:53 +00:00
return this._passing;
}
2015-06-23 16:12:11 +00:00
r.isWaiting = function(){
2015-06-23 13:01:39 +00:00
return this._isWaiting;
}
2015-06-14 18:50:53 +00:00
r.setUpWeatherFieldWith = function(p2){
2015-06-21 14:50:50 +00:00
this.field[Card.TYPE.WEATHER] = p2.field[Card.TYPE.WEATHER] = Field(this);
2015-06-14 18:50:53 +00:00
}
2015-06-18 13:06:13 +00:00
r.findCardOnFieldByID = function(id){
2015-06-14 18:50:53 +00:00
for(var key in this.field) {
var field = this.field[key];
var card = field.getCard(id);
if(card !== -1) return card;
}
2015-06-19 12:14:37 +00:00
/*
for(var i = 0; i < this._discard.length; i++) {
var c = this._discard[i];
if(c.getID() === id) return c;
}*/
return -1;
}
2015-06-30 11:43:03 +00:00
r.getRandomCardOnField = function(){
2015-07-01 09:38:31 +00:00
var allCards = this.getFieldCards();
2015-06-30 11:43:03 +00:00
var rnd = (Math.random() * allCards.length) | 0;
2015-06-28 14:10:25 +00:00
2015-06-29 17:57:51 +00:00
2015-06-28 14:10:25 +00:00
return allCards[rnd];
}
2015-06-19 12:14:37 +00:00
r.getCardFromDiscard = function(id){
for(var i = 0; i < this._discard.length; i++) {
var c = this._discard[i];
if(c.getID() === id) return c;
}
2015-06-14 18:50:53 +00:00
return -1;
}
2015-07-02 17:21:24 +00:00
r.getFieldCards = function(){
2015-07-01 09:38:31 +00:00
var close, range, siege;
close = this.field[Card.TYPE.CLOSE_COMBAT].get();
range = this.field[Card.TYPE.RANGED].get();
siege = this.field[Card.TYPE.SIEGE].get();
return close.concat(range.concat(siege));
}
2015-06-18 13:06:13 +00:00
r.setPassing = function(b){
2015-06-14 18:50:53 +00:00
this._passing = b;
2015-06-17 19:18:14 +00:00
this.send("set:passing", {passing: this._passing}, true);
2015-06-14 18:50:53 +00:00
}
2015-06-14 14:01:25 +00:00
r.wait = function(){
this._isWaiting = true;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
2015-06-14 18:50:53 +00:00
r.turn = function(){
2015-06-14 14:01:25 +00:00
this._isWaiting = false;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
2015-06-13 19:36:02 +00:00
2015-06-14 14:01:25 +00:00
r.setLeadercard = function(){
var leaderCard = this.deck.find("type", Card.TYPE.LEADER);
this.deck.removeFromDeck(leaderCard[0]);
/*
this.getYourside().setField("leader", leaderCard[0]);*/
this.field[Card.TYPE.LEADER].add(leaderCard[0]);
}
2015-06-18 13:06:13 +00:00
r.getLeader = function(){
return this.field[Card.TYPE.LEADER].get()[0];
}
2015-06-14 18:50:53 +00:00
r.getID = function(){
2015-06-14 14:01:25 +00:00
return this.n;
}
r.draw = function(times){
2015-06-13 19:36:02 +00:00
while(times--) {
var card = this.deck.draw();
this.hand.add(card);
}
2015-06-14 14:01:25 +00:00
}
2015-06-14 18:50:53 +00:00
r.calcScore = function(){
2015-06-14 14:01:25 +00:00
var score = 0;
for(var key in this.field) {
score += +this.field[key].getScore();
}
return this._score = score;
2015-06-13 19:36:02 +00:00
}
2015-06-14 14:01:25 +00:00
r.getInfo = function(){
2015-06-13 22:33:20 +00:00
return {
name: this.getName(),
2015-06-17 19:18:14 +00:00
lives: this._rubies,
2015-06-14 14:01:25 +00:00
score: this.calcScore(),
2015-06-14 18:50:53 +00:00
hand: this.hand.length(),
2015-06-25 15:51:21 +00:00
deck: this.deck.length(),
2015-06-18 13:06:13 +00:00
discard: this.getDiscard(true),
2015-06-14 18:50:53 +00:00
passing: this._passing
2015-06-13 22:33:20 +00:00
}
}
2015-06-18 13:06:13 +00:00
r.getRubies = function(){
2015-06-17 19:18:14 +00:00
return this._rubies;
}
2015-06-18 13:06:13 +00:00
r.getScore = function(){
2015-06-17 19:18:14 +00:00
return +this.calcScore();
}
2015-06-18 13:06:13 +00:00
r.removeRuby = function(){
2015-06-17 19:18:14 +00:00
this._rubies--;
}
2015-06-14 14:01:25 +00:00
r.getName = function(){
2015-06-13 22:33:20 +00:00
return this._name;
}
2015-06-14 14:01:25 +00:00
r.send = function(event, msg, isPrivate){
2015-06-13 19:36:02 +00:00
msg = msg || {};
2015-06-14 14:01:25 +00:00
isPrivate = typeof isPrivate === "undefined" ? false : isPrivate;
2015-06-13 19:36:02 +00:00
msg._roomSide = this.n;
2015-06-14 14:01:25 +00:00
2015-06-14 18:50:53 +00:00
if(isPrivate){
2015-06-14 14:01:25 +00:00
return this.socket.emit(event, msg);
}
2015-06-13 19:36:02 +00:00
this.battle.send(event, msg);
}
2015-06-14 18:50:53 +00:00
r.receive = function(event, cb){
2015-06-14 14:01:25 +00:00
this.socket.on(event, cb);
}
2015-06-24 13:10:54 +00:00
r.update = function(self){
self = self || false;
this.runEvent("Update", null, [self]);
2015-06-14 14:01:25 +00:00
}
2015-06-14 18:50:53 +00:00
r.onTurnStart = function(){
2015-06-14 14:01:25 +00:00
this.foe.wait();
this.turn();
2015-06-14 18:50:53 +00:00
//wait for cardplay event
2015-06-14 14:01:25 +00:00
};
2015-06-14 18:50:53 +00:00
r.playCard = function(card){
if(card === null || card === -1) return;
2015-06-23 13:01:39 +00:00
if(this.isWaiting()) return;
if(this.isPassing()) return;
2015-06-14 14:01:25 +00:00
2015-06-14 18:50:53 +00:00
if(!this.placeCard(card)) return;
2015-06-14 14:01:25 +00:00
2015-06-18 13:06:13 +00:00
this.hand.remove(card);
2015-06-14 14:01:25 +00:00
this.update();
2015-06-17 19:18:14 +00:00
2015-07-01 09:38:31 +00:00
//this.runEvent("NextTurn", null, [this.foe]);
this.endTurn();
}
2015-07-02 17:21:24 +00:00
r.endTurn = function(){
2015-07-01 09:38:31 +00:00
this.update();
2015-06-18 13:06:13 +00:00
this.runEvent("NextTurn", null, [this.foe]);
2015-06-14 14:01:25 +00:00
}
2015-06-13 19:36:02 +00:00
2015-06-19 12:14:37 +00:00
r.placeCard = function(card, obj){
obj = _.extend({}, obj);
2015-06-14 18:50:53 +00:00
2015-06-23 13:01:39 +00:00
if(typeof card === "string"){
2015-06-30 10:11:50 +00:00
//card = Card(card);
card = this.createCard(card);
2015-06-22 16:48:08 +00:00
}
2015-06-14 18:50:53 +00:00
this.checkAbilities(card, obj);
2015-06-30 11:43:03 +00:00
if(obj._cancelPlacement && !obj.forceField){
//this.battle.sendNotification(this.getName() + " played " + card.getName() + "!");
return 0;
}
if(obj._nextTurn && !obj.forceField){
2015-06-26 11:17:28 +00:00
this.update();
2015-07-01 09:38:31 +00:00
//this.runEvent("NextTurn", null, [this.foe]);
this.endTurn();
2015-06-26 11:17:28 +00:00
return 0;
}
2015-06-14 18:50:53 +00:00
2015-06-21 14:50:50 +00:00
2015-06-22 16:48:08 +00:00
var field = obj.forceField || null;
2015-06-21 14:50:50 +00:00
if(typeof obj.isHorn !== "undefined"){
2015-06-22 16:48:08 +00:00
if(!field){
field = obj.targetSide.field[obj.isHorn];
}
2015-06-21 14:50:50 +00:00
field.add(card, true);
}
else {
2015-06-22 16:48:08 +00:00
if(!field){
field = obj.targetSide.field[card.getType()];
}
2015-06-29 17:57:51 +00:00
2015-06-21 14:50:50 +00:00
field.add(card);
}
2015-06-14 18:50:53 +00:00
2015-06-19 12:14:37 +00:00
this.runEvent("EachCardPlace");
this.checkAbilityOnAfterPlace(card, obj);
2015-06-14 18:50:53 +00:00
2015-06-17 19:18:14 +00:00
2015-06-19 12:14:37 +00:00
if(obj._waitResponse){
this.hand.remove(card);
this.update();
return 0;
}
2015-06-24 13:10:54 +00:00
this.update();
2015-06-14 18:50:53 +00:00
return 1;
}
2015-06-22 16:48:08 +00:00
r.setHorn = function(card, field){
2015-06-21 14:50:50 +00:00
var self = this;
2015-06-23 13:01:39 +00:00
field = typeof field === "undefined" ? null : field;
2015-06-22 16:48:08 +00:00
if(typeof card === "string"){
2015-06-30 10:11:50 +00:00
//card = Card(card);
//card = this.cm.create(card);
card = this.createCard(card);
2015-06-22 16:48:08 +00:00
}
2015-06-23 13:01:39 +00:00
if(typeof field === "number"){
2015-06-22 16:48:08 +00:00
card.changeType(field);
this.placeCard(card, {
isHorn: field,
forcePlace: true
});
self.hand.remove(card);
return;
}
2015-06-21 14:50:50 +00:00
this.send("played:horn", {cardID: card.getID()}, true)
this.on("horn:setField", function(type){
self.off("horn:setField");
card.changeType(type);
self.placeCard(card, {
isHorn: type,
disabled: true
});
self.hand.remove(card);
2015-06-30 11:43:03 +00:00
self.battle.sendNotification(self.getName() + " played " + card.getName());
2015-06-21 14:50:50 +00:00
})
}
2015-06-22 16:48:08 +00:00
r.commanderHornAbility = function(card){
2015-06-21 14:50:50 +00:00
var field = this.field[card.getType()];
var id = "commanders_horn";
2015-06-22 16:48:08 +00:00
if(typeof field === "undefined"){
2015-06-21 14:50:50 +00:00
//console.log("field unknown | %s", card.getName());
return;
}
if(!field.isOnField(card)){
field.get().forEach(function(_card){
2015-06-30 16:26:07 +00:00
if(_card.getID() === id) return;
if(_card.getID() === card.getID()) return;
if(_card.getType() !== card.getType()) return;
2015-06-21 14:50:50 +00:00
if(_card.hasAbility("hero")) return;
_card.setBoost(id, 0);
})
this.off("EachCardPlace", card.getUidEvents("EachCardPlace"));
return;
}
field.get().forEach(function(_card){
2015-06-30 16:26:07 +00:00
if(_card.getID() === id) return;
if(_card.getID() === card.getID()) return;
2015-06-21 14:50:50 +00:00
if(_card.getType() != card.getType()) return;
if(_card.hasAbility("hero")) return;
_card.setBoost(id, 0);
_card.setBoost(id, _card.getPower());
})
}
2015-07-02 17:21:24 +00:00
r.setTightBond = function(card){
2015-07-01 08:40:57 +00:00
var field = this.field[card.getType()];
var pos = field.getPosition(card);
var cards = field.get();
if(pos < 0) return;
2015-07-02 17:21:24 +00:00
if(pos >= 1 && cards[pos - 1].getName() === cards[pos].getName()){
cards[pos].setBoost(cards[pos].getID() + "|left", "tight_bond");
}
else {
cards[pos].setBoost(cards[pos].getID() + "|left", 0);
2015-07-01 08:40:57 +00:00
}
2015-07-02 17:21:24 +00:00
if(pos < cards.length - 1 && cards[pos + 1].getName() === cards[pos].getName()){
cards[pos].setBoost(cards[pos].getID() + "|right", "tight_bond");
}
else {
cards[pos].setBoost(cards[pos].getID() + "|right", 0);
2015-07-01 08:40:57 +00:00
}
}
2015-06-18 18:47:17 +00:00
r.checkAbilities = function(card, obj, __flag){
2015-06-14 18:50:53 +00:00
var self = this;
obj.targetSide = this;
2015-06-19 15:15:26 +00:00
if(obj.disabled) return;
2015-06-26 06:51:30 +00:00
var ability = Array.isArray(__flag) ? __flag : card.getAbility();
2015-06-19 12:14:37 +00:00
2015-06-18 18:47:17 +00:00
if(Array.isArray(ability) && ability.length){
var ret = ability.slice();
2015-06-26 06:51:30 +00:00
ret.splice(0, 1);
2015-06-18 18:47:17 +00:00
this.checkAbilities(card, obj, ret);
ability = ability[0];
}
2015-06-14 18:50:53 +00:00
2015-07-01 08:40:57 +00:00
/*if(ability && ability.name === obj.suppress){
2015-06-24 13:10:54 +00:00
//this.update();
2015-07-01 08:40:57 +00:00
}*/
2015-06-19 12:14:37 +00:00
if(ability && !Array.isArray(ability)){
2015-07-01 08:40:57 +00:00
2015-06-21 14:50:50 +00:00
if(ability.onBeforePlace){
2015-06-19 15:15:26 +00:00
ability.onBeforePlace.apply(this, [card]);
}
2015-06-23 13:01:39 +00:00
if(ability.isCommandersHornCard && typeof obj.isHorn === "undefined"){
2015-06-21 14:50:50 +00:00
this.setHorn(card);
}
2015-06-22 16:48:08 +00:00
if(ability.commandersHorn){
2015-06-21 14:50:50 +00:00
ability.onEachCardPlace = this.commanderHornAbility;
ability.onWeatherChange = this.commanderHornAbility;
}
2015-06-22 16:48:08 +00:00
if(ability.cancelPlacement && !obj.forcePlace){
2015-06-19 15:15:26 +00:00
obj._cancelPlacement = true;
}
2015-06-30 11:43:03 +00:00
if(ability.nextTurn){
2015-06-26 11:17:28 +00:00
obj._nextTurn = ability.nextTurn;
}
2015-07-01 08:40:57 +00:00
if(ability.tightBond){
//this.setTightBond(card);
ability.onAfterPlace = this.setTightBond;
ability.onEachCardPlace = this.setTightBond;
//ability.onWeatherChange = this.setTightBond;
}
2015-06-30 11:43:03 +00:00
if(ability.scorch){
2015-06-26 11:17:28 +00:00
this.scorch(card);
}
2015-07-01 09:38:31 +00:00
if(ability.scorchMelee){
this.scorchMelee(card);
}
2015-06-30 11:43:03 +00:00
if(ability.removeImmediately){
2015-06-26 11:17:28 +00:00
this.hand.remove(card);
this.addToDiscard(card);
}
2015-06-22 16:48:08 +00:00
if(ability.waitResponse && !obj.forcePlace){
2015-06-19 12:14:37 +00:00
obj._waitResponse = true;
}
2015-06-14 18:50:53 +00:00
if(ability.changeSide){
obj.targetSide = this.foe;
2015-06-21 14:50:50 +00:00
}
if(typeof ability.weather !== "undefined"){
ability.onEachTurn = this.setWeather.bind(this, ability.weather);
ability.onEachCardPlace = this.setWeather.bind(this, ability.weather);
}
2015-06-22 16:48:08 +00:00
if(ability.replaceWith && !obj.forcePlace){
2015-06-19 15:15:26 +00:00
obj._cancelPlacement = true;
2015-06-18 13:06:13 +00:00
this.on("Decoy:replaceWith", function(replaceCard){
2015-06-14 18:50:53 +00:00
if(replaceCard.getType() == Card.TYPE.LEADER ||
replaceCard.getType() == Card.TYPE.WEATHER ||
replaceCard.getType() == Card.TYPE.SPECIAL){
return;
}
2015-06-17 19:18:14 +00:00
if(replaceCard.getName() === card.getName()) return;
2015-06-26 06:51:30 +00:00
if(replaceCard.hasAbility("hero")) return;
2015-06-17 16:10:23 +00:00
self.off("Decoy:replaceWith");
2015-06-14 18:50:53 +00:00
var field = self.field[replaceCard.getType()];
2015-06-18 13:06:13 +00:00
2015-06-14 18:50:53 +00:00
field.replaceWith(replaceCard, card);
2015-06-19 19:53:48 +00:00
self.runEvent("EachCardPlace");
2015-06-14 18:50:53 +00:00
self.hand.add(replaceCard);
2015-06-18 13:06:13 +00:00
self.hand.remove(card);
2015-06-14 18:50:53 +00:00
2015-06-25 15:51:21 +00:00
self.update();
2015-07-01 09:38:31 +00:00
//self.runEvent("NextTurn", null, [self.foe]);
self.endTurn();
2015-06-30 11:43:03 +00:00
self.battle.sendNotification(self.getName() + " played Decoy!");
2015-06-14 18:50:53 +00:00
})
}
if(ability.onEachTurn){
2015-06-19 19:53:48 +00:00
var uid = this.on("EachTurn", ability.onEachTurn, this, [card])
card._uidEvents["EachTurn"] = uid;
2015-06-14 18:50:53 +00:00
}
if(ability.onEachCardPlace){
2015-06-19 19:53:48 +00:00
var uid = this.on("EachCardPlace", ability.onEachCardPlace, this, [card]);
card._uidEvents["EachCardPlace"] = uid;
2015-06-14 18:50:53 +00:00
}
2015-06-21 14:50:50 +00:00
if(ability.onWeatherChange){
var uid = this.on("WeatherChange", ability.onWeatherChange, this, [card]);
card._uidEvents["WeatherChange"] = uid;
}
2015-06-14 18:50:53 +00:00
2015-06-24 13:10:54 +00:00
//this.update();
2015-07-01 08:40:57 +00:00
2015-06-14 18:50:53 +00:00
}
}
2015-06-30 16:26:07 +00:00
r.checkAbilityOnAfterPlace = function(card, obj, __flag){
//var ability = card.getAbility();
var ability = Array.isArray(__flag) ? __flag : card.getAbility();
if(Array.isArray(ability) && ability.length){
var ret = ability.slice();
ret.splice(0, 1);
this.checkAbilityOnAfterPlace(card, obj, ret);
ability = ability[0];
}
if(ability && !Array.isArray(ability)){
2015-06-19 12:14:37 +00:00
if(ability.name && ability.name === obj.suppress){
2015-06-24 13:10:54 +00:00
//this.update();
2015-06-19 12:14:37 +00:00
return;
}
2015-06-14 18:50:53 +00:00
if(ability.onAfterPlace){
ability.onAfterPlace.call(this, card)
}
}
}
2015-06-30 11:43:03 +00:00
r.setWeather = function(weather, opt){
2015-06-21 14:50:50 +00:00
var targetRow = weather;
var field;
2015-07-02 17:34:30 +00:00
if(typeof targetRow === "undefined") {
console.log("setWeather: targetRow undefined", targetRow);
console.trace(this);
return;
}
2015-06-30 11:43:03 +00:00
opt = opt || {};
var onRoundEnd = opt.onTurnEnd || false;
2015-06-21 14:50:50 +00:00
if(targetRow === Card.TYPE.WEATHER){
2015-06-30 11:43:03 +00:00
if(!onRoundEnd){
this.battle.sendNotification(this.getName() + " played Clear Weather!");
}
2015-06-21 14:50:50 +00:00
field = this.field[targetRow];
field.removeAll();
for(var i = Card.TYPE.CLOSE_COMBAT; i <= Card.TYPE.SIEGE; i++) {
var _field1, _field2, _field;
_field1 = this.field[i].get();
_field2 = this.foe.field[i].get();
_field = _field1.concat(_field2);
_field.forEach(function(_card){
if(_card.hasAbility("hero")) return;
_card.setForcedPower(-1);
});
}
this.runEvent("WeatherChange");
return;
}
var forcedPower = 1;
if(typeof targetRow === "undefined"){
console.trace(this);
}
var field1 = this.field[targetRow].get();
var field2 = this.foe.field[targetRow].get();
field = field1.concat(field2);
field.forEach(function(_card){
if(_card.hasAbility("hero")) return;
_card.setForcedPower(forcedPower);
});
this.runEvent("WeatherChange");
}
2015-07-02 17:21:24 +00:00
r.scorchMelee = function(card){
2015-06-26 11:17:28 +00:00
var side = this.foe;
var field = side.field[Card.TYPE.CLOSE_COMBAT];
2015-07-01 09:38:31 +00:00
this.battle.sendNotification(this.getName() + " played " + card.getName());
2015-07-02 17:21:24 +00:00
if(field.getScore() < 10){
2015-07-01 09:38:31 +00:00
this.battle.sendNotification("Scorch: Score is under 10! Nothing happens.");
return;
}
2015-06-30 16:26:07 +00:00
var cards = field.getHighestCards(true);
2015-06-26 11:17:28 +00:00
var removeCards = field.removeCard(cards);
2015-06-30 11:43:03 +00:00
2015-06-30 12:47:09 +00:00
var txt = "Scorch destroyed:";
2015-07-02 17:21:24 +00:00
for(var i = 0; i < removeCards.length; i++) {
2015-06-30 11:43:03 +00:00
var c = removeCards[i];
2015-06-30 12:47:09 +00:00
txt += "\n" + c.getName();
2015-06-30 11:43:03 +00:00
}
this.battle.sendNotification(txt);
side.addToDiscard(removeCards);
2015-06-26 11:17:28 +00:00
}
2015-07-01 09:38:31 +00:00
r.scorch = function(card){/*
var side = this.foe;
var field = side.field[Card.TYPE.CLOSE_COMBAT];
var cards = field.getHighestCards(true);
var removeCards = field.removeCard(cards);*/
var cards = this.getFieldCards();
cards = cards.concat(this.foe.getFieldCards());
var noHeroes = true;
var res = [];
var highest = 0;
var self = this;
this.battle.sendNotification(this.getName() + " played " + card.getName());
cards.forEach(function(card){
if(noHeroes && card.hasAbility("hero")) return;
highest = card.getPower() > highest ? card.getPower() : highest;
})
cards.forEach(function(card){
if(noHeroes && card.hasAbility("hero")) return;
if(card.getPower() === highest) res.push(card);
});
2015-07-02 17:21:24 +00:00
res.forEach(function(card){
2015-07-01 09:38:31 +00:00
var side = self;
2015-07-02 17:21:24 +00:00
if(self.foe.field[card.getType()].isOnField(card)){
2015-07-01 09:38:31 +00:00
side = self.foe;
}
var removed = side.field[card.getType()].removeCard(card);
side.addToDiscard(removed);
})
var txt = "Scorch destroyed:";
2015-07-02 17:21:24 +00:00
for(var i = 0; i < res.length; i++) {
2015-07-01 09:38:31 +00:00
var c = res[i];
txt += "\n" + c.getName();
}
this.battle.sendNotification(txt);
}
2015-06-18 13:06:13 +00:00
r.clearMainFields = function(){
2015-06-28 14:10:25 +00:00
var rndCard = null;
2015-06-30 11:43:03 +00:00
if(this.deck.getFaction() === Deck.FACTION.MONSTERS){
2015-06-28 14:10:25 +00:00
rndCard = this.getRandomCardOnField();
2015-07-02 17:21:24 +00:00
if(rndCard){
2015-07-01 07:17:16 +00:00
rndCard.__lock = true;
this.sendNotification(this.getName() + ": Monsters faction ability triggered! " + rndCard.getName());
2015-07-02 17:21:24 +00:00
}
else {
2015-07-01 07:17:16 +00:00
this.sendNotification(this.getName() + ": Monsters faction ability triggered! But no card found.");
}
2015-06-28 14:10:25 +00:00
}
2015-06-17 19:18:14 +00:00
var cards1 = this.field[Card.TYPE.CLOSE_COMBAT].removeAll();
var cards2 = this.field[Card.TYPE.RANGED].removeAll();
var cards3 = this.field[Card.TYPE.SIEGE].removeAll();
2015-06-23 13:01:39 +00:00
var cards4 = this.field[Card.TYPE.WEATHER].removeAll();
2015-06-17 19:18:14 +00:00
2015-06-23 13:01:39 +00:00
var cards = cards1.concat(cards2.concat(cards3.concat(cards4)));
2015-06-17 19:18:14 +00:00
this.addToDiscard(cards);
}
2015-06-18 13:06:13 +00:00
r.addToDiscard = function(cards){
2015-06-17 19:18:14 +00:00
var self = this;
2015-06-30 11:43:03 +00:00
if(!Array.isArray(cards)){
cards = [cards];
2015-06-26 11:17:28 +00:00
}
cards.forEach(function(_card){
2015-07-02 17:21:24 +00:00
if(_card.__lock){
delete _card.__lock;
return;
}
2015-06-26 11:17:28 +00:00
self._discard.push(_card);
2015-06-17 19:18:14 +00:00
});
}
2015-06-19 12:14:37 +00:00
r.removeFromDiscard = function(card){
for(var i = 0; i < this._discard.length; i++) {
var c = this._discard[i];
if(c.getID() === card.getID()){
this._discard.splice(i, 1);
return
}
}
}
2015-06-18 13:06:13 +00:00
r.getDiscard = function(json){
if(json){
return JSON.stringify(this._discard);
}
return this._discard;
}
r.resetNewRound = function(){
2015-06-17 19:18:14 +00:00
this.clearMainFields();
2015-06-30 11:43:03 +00:00
this.setWeather(5, {
onTurnEnd: true
}); //clear weather
2015-06-17 19:18:14 +00:00
this.setPassing(false);
}
2015-06-10 16:12:52 +00:00
2015-06-19 12:14:37 +00:00
r.filter = function(arrCards, opt){
var arr = arrCards.slice();
for(var key in opt) {
var res = [];
var prop = key, val = opt[key];
arrCards.forEach(function(card){
var property = card.getProperty(prop);
if(_.isArray(property)){
var _f = false;
for(var i = 0; i < property.length; i++) {
2015-06-21 14:50:50 +00:00
if(property[i] === val){
2015-06-19 12:14:37 +00:00
_f = true;
break;
}
}
if(!_f){
res.push(card);
}
}
2015-06-23 16:12:11 +00:00
else if(_.isArray(val)){
2015-06-23 13:00:49 +00:00
var _f = false;
for(var i = 0; i < val.length; i++) {
if(property === val[i]){
_f = true;
break;
}
}
if(!_f){
res.push(card);
}
}
2015-06-19 12:14:37 +00:00
else if(card.getProperty(prop) !== val){
res.push(card);
}
})
arr = _.intersection(arr, res);
}
return arr;
}
2015-06-23 16:12:11 +00:00
r.reDraw = function(n){
2015-07-02 17:21:24 +00:00
//var hand = this.hand.getCards();
2015-06-23 16:12:11 +00:00
var self = this;
var left = n;
var deferred = Promise.Deferred();
this.send("redraw:cards", null, true);
2015-06-24 16:22:56 +00:00
this.receive("redraw:reDrawCard", function(data){
2015-06-23 16:12:11 +00:00
var id = data.cardID;
if(!left) return;
left--;
var card = self.hand.remove(id)[0];
2015-07-01 18:18:09 +00:00
//console.log("hand -> deck: ", card.getName());
2015-06-23 16:12:11 +00:00
self.deck.add(card);
self.deck.shuffle();
self.draw(1);
2015-06-30 11:43:03 +00:00
if(!left){
2015-06-23 16:12:11 +00:00
self.send("redraw:close", null, true);
2015-07-01 18:18:09 +00:00
//console.log("redraw finished");
2015-07-02 16:17:33 +00:00
self.wait();
2015-06-23 16:12:11 +00:00
deferred.resolve("done");
2015-06-24 16:22:56 +00:00
//self.socket.off("redraw:reDrawCard", h1);
2015-06-23 16:12:11 +00:00
}
2015-06-24 16:22:56 +00:00
/*self.update(self);*/
self.battle.updateSelf(self);
2015-06-23 16:12:11 +00:00
})
2015-06-30 11:43:03 +00:00
this.receive("redraw:close_client", function(){
2015-07-01 18:18:09 +00:00
//console.log("redraw finished!");
self.wait();
2015-06-23 16:12:11 +00:00
deferred.resolve("done");
2015-06-24 16:22:56 +00:00
//self.socket.off("redraw:close_client", h2);
2015-06-23 16:12:11 +00:00
})
return deferred;
}
2015-07-02 17:21:24 +00:00
r.sendNotification = function(msg){
2015-06-30 16:26:07 +00:00
this.battle.sendNotification(msg);
}
2015-06-10 16:12:52 +00:00
return Battleside;
})();
module.exports = Battleside;