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

602 lines
15 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-13 19:36:02 +00:00
2015-06-14 18:50:53 +00:00
var Battleside;
Battleside = (function(){
2015-06-14 14:01:25 +00:00
var Battleside = function(name, n, battle, user){
2015-06-10 16:12:52 +00:00
if(!(this instanceof Battleside)){
2015-06-14 14:01:25 +00:00
return (new Battleside(name, n, battle, user));
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;
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);
/*this.field[Card.TYPE.HORN] = {
close: Field(this),
range: Field(this),
siege: Field(this)
};*/
2015-06-13 19:36:02 +00:00
this.n = n ? "p2" : "p1";
this._name = name;
this.battle = battle;
this.hand = Hand();
2015-06-19 12:14:37 +00:00
this.deck = Deck(DeckData[deck]);
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-06-22 16:48:08 +00:00
console.log("leader activated");
2015-06-18 13:06:13 +00:00
var ability = leaderCard.getAbility();
ability.onActivate.apply(self);
leaderCard.setDisabled(true);
self.update();
})
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-18 13:06:13 +00:00
self.update();
2015-06-17 19:18:14 +00:00
self.runEvent("NextTurn", null, [self.foe]);
2015-06-14 18:50:53 +00:00
})
2015-06-19 12:14:37 +00:00
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);
})
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]);
self.runEvent("NextTurn", null, [self.foe]);
})
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]);
self.runEvent("NextTurn", null, [self.foe]);
})
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-13 19:36:02 +00:00
r.foe = null;
r.hand = null;
r.battle = null;
r.deck = null;
2015-06-18 13:06:13 +00:00
r.isPassing = function(){
2015-06-14 18:50:53 +00:00
return this._passing;
}
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;
}
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-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);
}
console.log("update:hand fired");
2015-06-14 14:01:25 +00:00
this.update();
}
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-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);
}
r.update = function(){
2015-06-17 16:10:23 +00:00
//PubSub.publish("update");
this.runEvent("Update");
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-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-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-22 16:48:08 +00:00
if(typeof card === "string" ) {
card = Card(card);
}
2015-06-14 18:50:53 +00:00
this.checkAbilities(card, obj);
2015-06-22 16:48:08 +00:00
if(obj._cancelPlacement && !obj.forceField) 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-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
this.update();
2015-06-19 12:14:37 +00:00
if(obj._waitResponse){
this.hand.remove(card);
this.update();
return 0;
}
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-22 16:48:08 +00:00
field = typeof field === "undefined" ? null : field;
if(typeof card === "string"){
card = Card(card);
}
if(typeof field === "number") {
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-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){
if(_card.getID() == id) return;
if(_card.getType() != card.getType()) return;
if(_card.hasAbility("hero")) return;
_card.setBoost(id, 0);
})
this.off("EachCardPlace", card.getUidEvents("EachCardPlace"));
return;
}
field.get().forEach(function(_card){
if(_card.getID() == id) return;
if(_card.getType() != card.getType()) return;
if(_card.hasAbility("hero")) return;
_card.setBoost(id, 0);
_card.setBoost(id, _card.getPower());
})
}
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-18 18:47:17 +00:00
var ability = Array.isArray(__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();
ret = ret.splice(0, 1);
this.checkAbilities(card, obj, ret);
ability = ability[0];
}
2015-06-14 18:50:53 +00:00
2015-06-19 12:14:37 +00:00
if(ability && ability.name === obj.suppress){
this.update();
}
if(ability && !Array.isArray(ability)){
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-22 16:48:08 +00:00
if(ability.isCommandersHornCard && !obj.isHorn){
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-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-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);
self.update();
2015-06-14 18:50:53 +00:00
2015-06-17 19:18:14 +00:00
self.runEvent("NextTurn", null, [self.foe]);
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-18 13:06:13 +00:00
this.update();
2015-06-14 18:50:53 +00:00
}
}
2015-06-19 12:14:37 +00:00
r.checkAbilityOnAfterPlace = function(card, obj){
2015-06-14 18:50:53 +00:00
var ability = card.getAbility();
if(ability){
2015-06-19 12:14:37 +00:00
if(ability.name && ability.name === obj.suppress){
this.update();
return;
}
2015-06-14 18:50:53 +00:00
if(ability.onAfterPlace){
ability.onAfterPlace.call(this, card)
}
}
}
2015-06-21 14:50:50 +00:00
r.setWeather = function(weather){
var targetRow = weather;
var field;
if(typeof targetRow === "undefined") return;
//console.log(this.field[Card.TYPE.WEATHER]);
if(targetRow === Card.TYPE.WEATHER){
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");
this.update();
}
2015-06-18 13:06:13 +00:00
r.clearMainFields = function(){
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();
var cards = cards1.concat(cards2.concat(cards3));
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-18 13:06:13 +00:00
cards.forEach(function(card){
2015-06-17 19:18:14 +00:00
self._discard.push(card);
});
}
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();
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);
}
}
else if(card.getProperty(prop) !== val){
res.push(card);
}
})
arr = _.intersection(arr, res);
}
return arr;
}
2015-06-10 16:12:52 +00:00
return Battleside;
})();
module.exports = Battleside;