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

247 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-06-10 16:12:52 +00:00
var Battleside = require("./Battleside");
2015-06-14 18:50:53 +00:00
var Card = require("./Card");
2015-06-10 16:12:52 +00:00
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-17 16:10:23 +00:00
this.events = {};
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-17 16:10:23 +00:00
/*PubSub.subscribe("update", this.update.bind(this));*/
2015-06-19 12:14:37 +00:00
this.on("Update", this.update);/*
this.on("AfterPlace", this.checkAbilityOnAfterPlace)*/
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-14 14:01:25 +00:00
r.start = function(){
this.p1.setLeadercard();
this.p2.setLeadercard();
2015-06-19 15:15:26 +00:00
this.p1.draw(10);
this.p2.draw(10);
this.p1.hand.add(Card("harpy"));
this.p2.hand.add(Card("harpy"));
/*
2015-06-19 12:14:37 +00:00
this.p1.hand.add(Card("dun_banner_medic"));
this.p2.hand.add(Card("dun_banner_medic"));
this.p1.hand.add(Card("isengrim_faoiltiarnah"));
2015-06-19 15:15:26 +00:00
this.p2.hand.add(Card("isengrim_faoiltiarnah"));*/
2015-06-19 12:14:37 +00:00
2015-06-19 15:15:26 +00:00
/*this.p1.addToDiscard([Card("kaedweni_siege_expert")]);
this.p2.addToDiscard([Card("kaedweni_siege_expert")]);*/
2015-06-19 12:14:37 +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"));
2015-06-19 12:14:37 +00:00
this.p2.hand.add(Card("impenetrable_fog"));*/
2015-06-14 18:50:53 +00:00
this.update();
2015-06-17 16:10:23 +00:00
/*PubSub.subscribe("nextTurn", this.switchTurn.bind(this));*/
this.on("NextTurn", this.switchTurn);
2015-06-14 14:01:25 +00:00
2015-06-17 19:18:14 +00:00
this.switchTurn(Math.random() > .5 ? this.p1 : this.p2);
2015-06-10 16:12:52 +00:00
}
2015-06-17 19:18:14 +00:00
r.switchTurn = function(side, __flag){
2015-06-14 18:50:53 +00:00
__flag = typeof __flag == "undefined" ? 0 : 1;
2015-06-14 14:01:25 +00:00
2015-06-18 13:31:36 +00:00
2015-06-18 13:06:13 +00:00
if(!(side instanceof Battleside)){
console.trace("side is not a battleside!");
return
}
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();
}
2015-06-17 19:18:14 +00:00
return this.switchTurn(side.foe, 1);
2015-06-14 18:50:53 +00:00
}
2015-06-14 14:01:25 +00:00
2015-06-17 16:10:23 +00:00
this.runEvent("EachTurn");
this.runEvent("Turn" + side.getID());
2015-06-14 14:01:25 +00:00
console.log("current Turn: ", side.getName());
}
2015-06-15 19:03:12 +00:00
r.startNextRound = function(){
2015-06-17 19:18:14 +00:00
var loser = this.checkRubies();
if(this.checkIfIsOver()){
console.log("its over!");
2015-06-18 13:06:13 +00:00
this.update();
2015-06-17 19:18:14 +00:00
return;
}
this.p1.resetNewRound();
this.p2.resetNewRound();
console.log("start new round!");
2015-06-14 18:50:53 +00:00
2015-06-17 19:18:14 +00:00
this.update();
this.switchTurn(loser);
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 19:18:14 +00:00
r.runEvent = function(eventid, ctx, args){
ctx = ctx || this;
2015-06-17 16:10:23 +00:00
args = args || [];
var event = "on" + eventid;
2015-06-17 19:18:14 +00:00
if(!this.events[event]){
2015-06-17 16:10:23 +00:00
return;
}
this.events[event].forEach(function(e){
2015-06-18 07:21:27 +00:00
var obj = e;
obj.cb = obj.cb.bind(ctx)
obj.cb.apply(ctx, obj.onArgs.concat(args));
2015-06-17 14:53:44 +00:00
});
2015-06-18 13:06:13 +00:00
this.update();
2015-06-17 14:53:44 +00:00
}
2015-06-17 16:10:23 +00:00
r.on = function(eventid, cb, ctx, args){
2015-06-18 07:21:27 +00:00
ctx = ctx || null;
args = args || [];
2015-06-17 16:10:23 +00:00
var event = "on" + eventid;
2015-06-18 07:21:27 +00:00
var obj = {};
2015-06-18 13:06:13 +00:00
if(!ctx){
2015-06-18 07:21:27 +00:00
obj.cb = cb;
2015-06-18 13:06:13 +00:00
}
else {
2015-06-18 07:21:27 +00:00
obj.cb = cb.bind(ctx);
}
obj.onArgs = args;
2015-06-17 16:10:23 +00:00
if(!(event in this.events)){
this.events[event] = [];
}
2015-06-18 07:21:27 +00:00
2015-06-17 19:18:14 +00:00
if(typeof cb !== "function"){
throw new Error("cb not a function");
}
2015-06-18 07:21:27 +00:00
2015-06-17 16:10:23 +00:00
if(args){
2015-06-18 07:21:27 +00:00
this.events[event].push(obj);
2015-06-17 16:10:23 +00:00
}
2015-06-18 07:21:27 +00:00
2015-06-17 16:10:23 +00:00
else {
2015-06-18 07:21:27 +00:00
this.events[event].push(obj);
2015-06-17 14:53:44 +00:00
}
}
2015-06-17 16:10:23 +00:00
r.off = function(eventid){
var event = "on" + eventid;
2015-06-18 13:06:13 +00:00
if(!this.events[event]) return;
2015-06-17 16:10:23 +00:00
this.events[event].forEach(function(e){
e = null;
2015-06-17 14:53:44 +00:00
});
2015-06-17 16:10:23 +00:00
delete this.events[event];
2015-06-17 14:53:44 +00:00
}
2015-06-17 19:18:14 +00:00
r.checkIfIsOver = function(){
return !(this.p1.getRubies() && this.p2.getRubies());
}
r.checkRubies = function(){
var scoreP1 = this.p1.getScore();
var scoreP2 = this.p2.getScore();
if(scoreP1 > scoreP2){
this.p2.removeRuby();
return this.p2;
}
if(scoreP2 > scoreP1){
this.p1.removeRuby();
return this.p1;
}
//tie
this.p1.removeRuby();
this.p2.removeRuby();
2015-06-18 13:06:13 +00:00
return Math.random() > 0.5 ? this.p1 : this.p2;
2015-06-17 19:18:14 +00:00
}
2015-06-18 13:31:36 +00:00
r.userLeft = function(sideName) {
var side = this[sideName];
side.foe.send("foe:left", null, true);
}
2015-06-15 19:03:12 +00:00
r.shutDown = function() {
this.channel = null;
}
2015-06-10 16:12:52 +00:00
return Battle;
})();
module.exports = Battle;