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

343 lines
7.8 KiB
JavaScript
Raw Normal View History

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");
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-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;
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();
})
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-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){
this.field[Card.TYPE.WEATHER] = p2.field[Card.TYPE.WEATHER] = Field(Card.TYPE.WEATHER);
}
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;
}
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-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);
2015-06-17 16:10:23 +00:00
//PubSub.publish("onEachCardPlace");
this.runEvent("OnEachCardPlace");
2015-06-14 18:50:53 +00:00
this.checkAbilityOnAfterPlace(card);
2015-06-17 19:18:14 +00:00
this.update();
2015-06-14 18:50:53 +00:00
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;
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);
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-17 16:10:23 +00:00
this.on("EachTurn", ability.onEachTurn, this, [card])
2015-06-14 18:50:53 +00:00
}
if(ability.onEachCardPlace){
2015-06-17 16:10:23 +00:00
//PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
this.on("EachCardPlace", ability.onEachCardPlace, this, [card]);
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
}
}
r.checkAbilityOnAfterPlace = function(card){
var ability = card.getAbility();
if(ability){
if(ability.onAfterPlace){
ability.onAfterPlace.call(this, card)
}
}
}
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-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
return Battleside;
})();
module.exports = Battleside;