2015-06-13 19:36:02 +00:00
|
|
|
var io = global.io;
|
|
|
|
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");
|
|
|
|
var PubSub = require("pubsub-js");
|
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-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 = {};
|
|
|
|
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);
|
2015-06-13 19:36:02 +00:00
|
|
|
this.n = n ? "p2" : "p1";
|
|
|
|
this._name = name;
|
|
|
|
this.battle = battle;
|
|
|
|
this.hand = Hand();
|
|
|
|
this.deck = Deck(DeckData["test"]);
|
2015-06-14 14:01:25 +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);
|
|
|
|
if(card === -1) throw "decoy:replace | unknown card";
|
|
|
|
PubSub.publish("decoy:replaceWith", card);
|
|
|
|
})
|
|
|
|
this.receive("set:passing", function() {
|
|
|
|
self.setPassing(true);
|
|
|
|
self.update();
|
|
|
|
PubSub.publish("nextTurn");
|
|
|
|
})
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(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-14 14:01:25 +00:00
|
|
|
/*r.leaderField = null;
|
|
|
|
r.closeField = null;
|
2015-06-10 16:12:52 +00:00
|
|
|
r._range = null;
|
|
|
|
r._siege = null;
|
2015-06-14 14:01:25 +00:00
|
|
|
r._field = null;*/
|
2015-06-13 22:33:20 +00:00
|
|
|
r._lives = 2;
|
|
|
|
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-14 18:50:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.setPassing = function(b) {
|
|
|
|
this._passing = b;
|
|
|
|
}
|
|
|
|
|
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-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(),
|
|
|
|
lives: this._lives,
|
2015-06-14 14:01:25 +00:00
|
|
|
score: this.calcScore(),
|
2015-06-14 18:50:53 +00:00
|
|
|
hand: this.hand.length(),
|
|
|
|
passing: this._passing
|
2015-06-13 22:33:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-14 18:50:53 +00:00
|
|
|
/*
|
|
|
|
this.send("update:info", {
|
|
|
|
info: this.getInfo(),
|
|
|
|
leader: this.field[Card.TYPE.LEADER].get()[0]
|
|
|
|
})
|
|
|
|
this.send("update:hand", {
|
|
|
|
cards: JSON.stringify(this.hand.getCards())
|
|
|
|
});
|
|
|
|
this.send("update:fields", {
|
|
|
|
close: this.field[Card.TYPE.CLOSE_COMBAT],
|
|
|
|
ranged: this.field[Card.TYPE.RANGED],
|
|
|
|
siege: this.field[Card.TYPE.SIEGE]
|
|
|
|
})*/
|
|
|
|
PubSub.publish("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
|
|
|
this.hand.remove(card);
|
|
|
|
|
|
|
|
if(!this.placeCard(card)) return;
|
2015-06-14 14:01:25 +00:00
|
|
|
|
|
|
|
this.update();
|
|
|
|
|
|
|
|
PubSub.publish("nextTurn");
|
|
|
|
}
|
2015-06-13 19:36:02 +00:00
|
|
|
|
2015-06-14 18:50:53 +00:00
|
|
|
r.placeCard = function(card){
|
|
|
|
var obj = {};
|
|
|
|
|
|
|
|
this.checkAbilities(card, obj);
|
|
|
|
if(obj._canclePlacement) return 0;
|
|
|
|
|
|
|
|
var field = obj.targetSide.field[card.getType()];
|
|
|
|
field.add(card);
|
|
|
|
|
|
|
|
PubSub.publish("onEachCardPlace");
|
|
|
|
|
|
|
|
this.checkAbilityOnAfterPlace(card);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
r.checkAbilities = function(card, obj){
|
|
|
|
var self = this;
|
|
|
|
obj.targetSide = this;
|
|
|
|
|
|
|
|
if(card.getAbility()){
|
|
|
|
var ability = card.getAbility();
|
|
|
|
if(ability.changeSide){
|
|
|
|
obj.targetSide = this.foe;
|
|
|
|
}
|
|
|
|
if(ability.replaceWith){
|
|
|
|
obj._canclePlacement = true;
|
|
|
|
|
|
|
|
var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){
|
|
|
|
if(replaceCard.getType() == Card.TYPE.LEADER ||
|
|
|
|
replaceCard.getType() == Card.TYPE.WEATHER ||
|
|
|
|
replaceCard.getType() == Card.TYPE.SPECIAL){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PubSub.unsubscribe(decoy);
|
|
|
|
var field = self.field[replaceCard.getType()];
|
|
|
|
|
|
|
|
field.replaceWith(replaceCard, card);
|
|
|
|
|
|
|
|
self.hand.add(replaceCard);
|
|
|
|
|
|
|
|
self.update();
|
|
|
|
|
|
|
|
PubSub.publish("nextTurn");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if(ability.onEachTurn){
|
|
|
|
PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card));
|
|
|
|
}
|
|
|
|
if(ability.onEachCardPlace){
|
|
|
|
PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.checkAbilityOnAfterPlace = function(card){
|
|
|
|
var ability = card.getAbility();
|
|
|
|
if(ability){
|
|
|
|
if(ability.onAfterPlace){
|
|
|
|
ability.onAfterPlace.call(this, card)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-10 16:12:52 +00:00
|
|
|
|
|
|
|
return Battleside;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Battleside;
|