2015-06-10 16:12:52 +00:00
|
|
|
var Battleside = require("./Battleside");
|
2015-06-14 14:01:25 +00:00
|
|
|
var PubSub = require("pubsub-js");
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
var io = global.io;
|
|
|
|
|
2015-06-10 16:12:52 +00:00
|
|
|
var Battle = (function(){
|
2015-06-13 22:33:20 +00:00
|
|
|
var Battle = function(id, p1, p2){
|
2015-06-10 16:12:52 +00:00
|
|
|
if(!(this instanceof Battle)){
|
2015-06-13 22:33:20 +00:00
|
|
|
return (new Battle(id, p1, p2));
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* constructor here
|
|
|
|
*/
|
2015-06-13 19:36:02 +00:00
|
|
|
this._id = id;
|
2015-06-13 22:33:20 +00:00
|
|
|
this._user1 = p1;
|
|
|
|
this._user2 = p2;
|
2015-06-10 16:12:52 +00:00
|
|
|
};
|
|
|
|
var r = Battle.prototype;
|
|
|
|
/**
|
|
|
|
* methods && properties here
|
|
|
|
* r.property = null;
|
|
|
|
* r.getProperty = function() {...}
|
|
|
|
*/
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.p1 = null;
|
|
|
|
r.p2 = null;
|
2015-06-13 22:33:20 +00:00
|
|
|
r._user1 = null;
|
|
|
|
r._user2 = null;
|
2015-06-13 19:36:02 +00:00
|
|
|
r.turn = 0;
|
2015-06-10 16:12:52 +00:00
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r._id = null;
|
2015-06-10 16:12:52 +00:00
|
|
|
|
|
|
|
|
2015-06-13 19:36:02 +00:00
|
|
|
r.init = function(){
|
2015-06-14 14:01:25 +00:00
|
|
|
this.p1 = Battleside(this._user1.getName(), 0, this, this._user1);
|
|
|
|
this.p2 = Battleside(this._user2.getName(), 1, this, this._user2);
|
2015-06-13 19:36:02 +00:00
|
|
|
this.p1.foe = this.p2;
|
|
|
|
this.p2.foe = this.p1;
|
|
|
|
|
|
|
|
this.start();
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r.start = function(){
|
|
|
|
this.p1.setLeadercard();
|
|
|
|
this.p2.setLeadercard();
|
2015-06-13 19:36:02 +00:00
|
|
|
this.p1.draw(10);
|
|
|
|
this.p2.draw(10);
|
2015-06-13 22:33:20 +00:00
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
PubSub.subscribe("nextTurn", this.switchTurn.bind(this));
|
|
|
|
|
|
|
|
this.switchTurn();
|
2015-06-10 16:12:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 14:01:25 +00:00
|
|
|
r.switchTurn = function(){
|
|
|
|
/*this.playerManager.renderInfos();
|
|
|
|
if(this.playerManager.bothPassed() && !this._roundCheck) {
|
|
|
|
//start new round
|
|
|
|
this._roundCheck = true;
|
|
|
|
this.checkRound();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(this.playerManager.bothPassed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var entity = this.playerManager.getNextPlayer();
|
|
|
|
|
|
|
|
this.playerManager.renderInfos();*/
|
|
|
|
var side = this.turn++ % 2 ? this.p1 : this.p2;
|
|
|
|
|
|
|
|
|
|
|
|
PubSub.publish("onEachTurn");
|
|
|
|
PubSub.publish("turn/" + side.getID());
|
|
|
|
console.log("current Turn: ", side.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
r.send = function(event, data){
|
2015-06-13 19:36:02 +00:00
|
|
|
io.to(this._id).emit(event, data);
|
|
|
|
}
|
2015-06-10 16:12:52 +00:00
|
|
|
|
|
|
|
return Battle;
|
|
|
|
})();
|
|
|
|
|
|
|
|
module.exports = Battle;
|