1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/server/Battleside.js
2015-06-19 17:15:26 +02:00

455 lines
11 KiB
JavaScript

var DeckData = require("../assets/data/deck");
var Deck = require("./Deck");
var Hand = require("./Hand");
var Card = require("./Card");
var Field = require("./Field");
var _ = require("underscore");
var Battleside;
Battleside = (function(){
var Battleside = function(name, n, battle, user){
if(!(this instanceof Battleside)){
return (new Battleside(name, n, battle, user));
}
/**
* constructor here
*/
var deck = user.getDeck();
var self = this;
this._isWaiting = true;
this.socket = user.socket;
this.field = {};
this.field[Card.TYPE.LEADER] = Field(Card.TYPE.LEADER);
this.field[Card.TYPE.CLOSE_COMBAT] = Field(Card.TYPE.CLOSE_COMBAT);
this.field[Card.TYPE.RANGED] = Field(Card.TYPE.RANGED);
this.field[Card.TYPE.SIEGE] = Field(Card.TYPE.SIEGE);
this.n = n ? "p2" : "p1";
this._name = name;
this.battle = battle;
this.hand = Hand();
this.deck = Deck(DeckData[deck]);
this._discard = [];
this.runEvent = this.battle.runEvent.bind(this.battle);
this.on = this.battle.on.bind(this.battle);
this.off = this.battle.off.bind(this.battle);
this.receive("activate:leader", function(){
if(self._isWaiting) return;
if(self.isPassing()) return;
console.log("leader activated");
var leaderCard = self.getLeader();
if(leaderCard.isDisabled()) return;
var ability = leaderCard.getAbility();
ability.onActivate.apply(self);
leaderCard.setDisabled(true);
self.update();
})
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);
if(card === -1) throw new Error("decoy:replace | unknown card");
self.runEvent("Decoy:replaceWith", self, [card]);
})
this.receive("cancel:decoy", function(){
self.off("Decoy:replaceWith");
})
this.receive("set:passing", function(){
self.setPassing(true);
self.update();
self.runEvent("NextTurn", null, [self.foe]);
})
this.receive("medic:chooseCardFromDiscard", function(data){
if(!data){
self.runEvent("NextTurn", null, [self.foe]);
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);
})
this.receive("agile:field", function(data) {
var fieldType = data.field;
self.runEvent("agile:setField", null, [fieldType]);
self.runEvent("NextTurn", null, [self.foe]);
})
this.receive("cancel:agile", function(){
self.off("agile:setField");
})
this.on("Turn" + this.getID(), this.onTurnStart, this);
};
var r = Battleside.prototype;
/**
* methods && properties here
* r.property = null;
* r.getProperty = function() {...}
*/
r._name = null;
r._discard = null;
r._rubies = 2;
r._score = 0;
r._isWaiting = null;
r._passing = null;
r.field = null;
r.socket = null;
r.n = null;
r.foe = null;
r.hand = null;
r.battle = null;
r.deck = null;
r.isPassing = function(){
return this._passing;
}
r.setUpWeatherFieldWith = function(p2){
this.field[Card.TYPE.WEATHER] = p2.field[Card.TYPE.WEATHER] = Field(Card.TYPE.WEATHER);
}
r.findCardOnFieldByID = function(id){
for(var key in this.field) {
var field = this.field[key];
var card = field.getCard(id);
if(card !== -1) return card;
}
/*
for(var i = 0; i < this._discard.length; i++) {
var c = this._discard[i];
if(c.getID() === id) return c;
}*/
return -1;
}
r.getCardFromDiscard = function(id){
for(var i = 0; i < this._discard.length; i++) {
var c = this._discard[i];
if(c.getID() === id) return c;
}
return -1;
}
r.setPassing = function(b){
this._passing = b;
this.send("set:passing", {passing: this._passing}, true);
}
r.wait = function(){
this._isWaiting = true;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
r.turn = function(){
this._isWaiting = false;
this.send("set:waiting", {waiting: this._isWaiting}, true);
}
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]);
}
r.getLeader = function(){
return this.field[Card.TYPE.LEADER].get()[0];
}
r.getID = function(){
return this.n;
}
r.draw = function(times){
while(times--) {
var card = this.deck.draw();
this.hand.add(card);
}
console.log("update:hand fired");
this.update();
}
r.calcScore = function(){
var score = 0;
for(var key in this.field) {
score += +this.field[key].getScore();
}
return this._score = score;
}
r.getInfo = function(){
return {
name: this.getName(),
lives: this._rubies,
score: this.calcScore(),
hand: this.hand.length(),
discard: this.getDiscard(true),
passing: this._passing
}
}
r.getRubies = function(){
return this._rubies;
}
r.getScore = function(){
return +this.calcScore();
}
r.removeRuby = function(){
this._rubies--;
}
r.getName = function(){
return this._name;
}
r.send = function(event, msg, isPrivate){
msg = msg || {};
isPrivate = typeof isPrivate === "undefined" ? false : isPrivate;
msg._roomSide = this.n;
if(isPrivate){
return this.socket.emit(event, msg);
}
this.battle.send(event, msg);
}
r.receive = function(event, cb){
this.socket.on(event, cb);
}
r.update = function(){
//PubSub.publish("update");
this.runEvent("Update");
}
r.onTurnStart = function(){
this.foe.wait();
this.turn();
//wait for cardplay event
};
r.playCard = function(card){
if(card === null || card === -1) return;
if(!this.placeCard(card)) return;
this.hand.remove(card);
this.update();
this.runEvent("NextTurn", null, [this.foe]);
}
r.placeCard = function(card, obj){
obj = _.extend({}, obj);
this.checkAbilities(card, obj);
if(obj._cancelPlacement) return 0;
var field = obj.targetSide.field[card.getType()];
field.add(card);
this.runEvent("EachCardPlace");
this.checkAbilityOnAfterPlace(card, obj);
/*
this.runEvent("AfterPlace", this, [card, obj]);*/
this.update();
if(obj._waitResponse){
this.hand.remove(card);
this.update();
return 0;
}
return 1;
}
r.checkAbilities = function(card, obj, __flag){
var self = this;
obj.targetSide = this;
if(obj.disabled) return;
var ability = Array.isArray(__flag) || card.getAbility();
if(Array.isArray(ability) && ability.length){
var ret = ability.slice();
ret = ret.splice(0, 1);
this.checkAbilities(card, obj, ret);
ability = ability[0];
}
if(ability && ability.name === obj.suppress){
this.update();
}
if(ability && !Array.isArray(ability)){
if(ability.onBeforePlace) {
ability.onBeforePlace.apply(this, [card]);
}
if(ability.cancelPlacement) {
obj._cancelPlacement = true;
}
if(ability.waitResponse){
obj._waitResponse = true;
}
if(ability.changeSide){
obj.targetSide = this.foe;
}
if(ability.replaceWith){
obj._cancelPlacement = true;
this.on("Decoy:replaceWith", function(replaceCard){
if(replaceCard.getType() == Card.TYPE.LEADER ||
replaceCard.getType() == Card.TYPE.WEATHER ||
replaceCard.getType() == Card.TYPE.SPECIAL){
return;
}
if(replaceCard.getName() === card.getName()) return;
self.off("Decoy:replaceWith");
var field = self.field[replaceCard.getType()];
field.replaceWith(replaceCard, card);
self.hand.add(replaceCard);
self.hand.remove(card);
self.update();
self.runEvent("NextTurn", null, [self.foe]);
})
}
if(ability.onEachTurn){
this.on("EachTurn", ability.onEachTurn, this, [card])
}
if(ability.onEachCardPlace){
//PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
this.on("EachCardPlace", ability.onEachCardPlace, this, [card]);
}
this.update();
}
}
r.checkAbilityOnAfterPlace = function(card, obj){
var ability = card.getAbility();
if(ability){
if(ability.name && ability.name === obj.suppress){
this.update();
return;
}
if(ability.onAfterPlace){
ability.onAfterPlace.call(this, card)
}
}
}
r.clearMainFields = function(){
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();
var cards = cards1.concat(cards2.concat(cards3));
this.addToDiscard(cards);
}
r.addToDiscard = function(cards){
var self = this;
cards.forEach(function(card){
self._discard.push(card);
});
}
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
}
}
}
r.getDiscard = function(json){
if(json){
return JSON.stringify(this._discard);
}
return this._discard;
}
r.resetNewRound = function(){
this.clearMainFields();
this.setPassing(false);
}
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++) {
if(property[i] === val) {
_f = true;
break;
}
}
if(!_f){
res.push(card);
}
}
else if(card.getProperty(prop) !== val){
res.push(card);
}
})
arr = _.intersection(arr, res);
}
return arr;
}
return Battleside;
})();
module.exports = Battleside;