1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/server/Battle.js

173 lines
3.6 KiB
JavaScript
Raw Normal View History

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-14 18:50:53 +00:00
var Card = require("./Card");
2015-06-10 16:12:52 +00:00
2015-06-15 19:03:12 +00:00
/*var io = global.io;*/
2015-06-13 19:36:02 +00:00
2015-06-10 16:12:52 +00:00
var Battle = (function(){
2015-06-15 19:03:12 +00:00
var Battle = function(id, p1, p2, socket){
2015-06-10 16:12:52 +00:00
if(!(this instanceof Battle)){
2015-06-15 19:03:12 +00:00
return (new Battle(id, p1, p2, socket));
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-15 19:03:12 +00:00
this.socket = socket;
this.channel = {};
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-15 19:03:12 +00:00
r.socket = null;
r.channel = null;
2015-06-14 18:50:53 +00:00
2015-06-13 19:36:02 +00:00
r._id = null;
2015-06-10 16:12:52 +00:00
2015-06-17 14:53:44 +00:00
r.events = null;
2015-06-10 16:12:52 +00:00
2015-06-13 19:36:02 +00:00
r.init = function(){
2015-06-14 18:50:53 +00:00
PubSub.subscribe("update", this.update.bind(this));
2015-06-15 19:03:12 +00:00
this.channel = this.socket.subscribe(this._id);
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;
2015-06-14 18:50:53 +00:00
this.p1.setUpWeatherFieldWith(this.p2);
2015-06-13 19:36:02 +00:00
2015-06-15 19:03:12 +00:00
2015-06-13 19:36:02 +00:00
this.start();
2015-06-10 16:12:52 +00:00
}
2015-06-15 19:03:12 +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 18:50:53 +00:00
this.p1.hand.add(Card("decoy"));
this.p1.hand.add(Card("impenetrable_fog"));
this.p2.hand.add(Card("decoy"));
this.p2.hand.add(Card("impenetrable_fog"));
this.update();
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 18:50:53 +00:00
r.switchTurn = function(__flag){
2015-06-14 14:01:25 +00:00
/*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();*/
2015-06-14 18:50:53 +00:00
__flag = typeof __flag == "undefined" ? 0 : 1;
2015-06-14 14:01:25 +00:00
var side = this.turn++ % 2 ? this.p1 : this.p2;
2015-06-14 18:50:53 +00:00
if(side.isPassing()){
2015-06-15 19:03:12 +00:00
if(__flag){
2015-06-14 18:50:53 +00:00
return this.startNextRound();
}
return this.switchTurn(1);
}
2015-06-14 14:01:25 +00:00
PubSub.publish("onEachTurn");
PubSub.publish("turn/" + side.getID());
console.log("current Turn: ", side.getName());
}
2015-06-15 19:03:12 +00:00
r.startNextRound = function(){
2015-06-14 18:50:53 +00:00
}
r.update = function(){
this._update(this.p1);
this._update(this.p2);
}
r._update = function(p){
p.send("update:info", {
info: p.getInfo(),
leader: p.field[Card.TYPE.LEADER].get()[0]
})
p.send("update:hand", {
cards: JSON.stringify(p.hand.getCards())
});
p.send("update:fields", {
close: p.field[Card.TYPE.CLOSE_COMBAT],
ranged: p.field[Card.TYPE.RANGED],
siege: p.field[Card.TYPE.SIEGE],
weather: p.field[Card.TYPE.WEATHER]
})
}
2015-06-14 14:01:25 +00:00
r.send = function(event, data){
2015-06-15 19:03:12 +00:00
this.channel.publish({
event: event,
data: data
});
2015-06-13 19:36:02 +00:00
}
2015-06-10 16:12:52 +00:00
2015-06-17 14:53:44 +00:00
r.runEvent = function(eventid, target){
target = target || this;
this.events["on" + eventid].forEach(function(event) {
event.call(target);
});
}
r.on = function(eventid, cb){
if(!this.events["on" + eventid]) {
this.events["on" + eventid] = [];
}
this.events["on" + eventid].push(cb);
}
r.off = function(eventid) {
this.events["on" + eventid].forEach(function(event) {
event = null;
});
delete this.events["on" + eventid];
}
2015-06-15 19:03:12 +00:00
/*r._setUpChannel = function() {
var self = this;
this._abilityChannel.watch(function(d) {
var event = d.event, data = d.data;
if(event === "update") {
data();
}
})
}*/
2015-06-10 16:12:52 +00:00
return Battle;
})();
module.exports = Battle;