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

287 lines
6.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-19 19:53:48 +00:00
var shortid = require("shortid");
2015-06-23 16:12:11 +00:00
var Promise = require("jquery-deferred");
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 19:53:48 +00:00
this.on("Update", this.update);
/*
this.on("AfterPlace", this.checkAbilityOnAfterPlace)*/
2015-06-15 19:03:12 +00:00
2015-06-24 16:22:56 +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-25 15:51:21 +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-20 11:08:28 +00:00
this.p1.draw(10);
this.p2.draw(10);
2015-06-22 15:42:48 +00:00
2015-06-26 11:17:28 +00:00
this.p1.placeCard("ves");
this.p2.placeCard("ves");
this.p1.placeCard("yarpen_zigrin");
this.p2.placeCard("yarpen_zigrin");
this.p1.hand.add(Card("scorch"));
this.p2.hand.add(Card("scorch"));
this.p1.hand.add(Card("villentretenmerth"));
this.p2.hand.add(Card("villentretenmerth"));
2015-06-26 06:51:30 +00:00
2015-06-27 15:20:20 +00:00
this.p1.hand.add(Card("impenetrable_fog"));
2015-06-26 06:29:01 +00:00
this.p2.hand.add(Card("impenetrable_fog"));
this.p1.hand.add(Card("biting_frost"));
this.p2.hand.add(Card("biting_frost"));
this.p1.hand.add(Card("torrential_rain"));
this.p2.hand.add(Card("torrential_rain"));
this.p1.hand.add(Card("clear_weather"));
2015-06-27 15:20:20 +00:00
this.p2.hand.add(Card("clear_weather"));
2015-06-26 06:29:01 +00:00
2015-06-23 16:12:11 +00:00
this.update();
2015-06-19 12:14:37 +00:00
2015-06-14 18:50:53 +00:00
2015-06-23 16:12:11 +00:00
Promise.when(this.p1.reDraw(2), this.p2.reDraw(2))
2015-06-24 13:10:54 +00:00
.then(function(){
2015-06-23 16:12:11 +00:00
this.on("NextTurn", this.switchTurn);
this.switchTurn(Math.random() > .5 ? this.p1 : this.p2);
}.bind(this));
2015-06-14 18:50:53 +00:00
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-24 13:10:54 +00:00
2015-06-17 16:10:23 +00:00
this.runEvent("EachTurn");
2015-06-23 13:01:39 +00:00
2015-06-17 16:10:23 +00:00
this.runEvent("Turn" + side.getID());
2015-06-14 14:01:25 +00:00
2015-06-24 13:10:54 +00:00
console.log("current Turn: ", side.getName());
2015-06-14 14:01:25 +00:00
}
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!");
this.gameOver(loser.foe);
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.gameOver = function(winner) {
this.send("gameover", {
winner: winner.getName()
})
}
2015-06-14 18:50:53 +00:00
r.update = function(){
2015-06-23 16:12:11 +00:00
console.log("update called");
2015-06-14 18:50:53 +00:00
this._update(this.p1);
this._update(this.p2);
}
2015-06-24 16:22:56 +00:00
r.updateSelf = function(side) {
this._update(side, true);
}
r._update = function(p, isPrivate){
isPrivate = isPrivate || false;
2015-06-14 18:50:53 +00:00
p.send("update:info", {
info: p.getInfo(),
leader: p.field[Card.TYPE.LEADER].get()[0]
2015-06-24 16:22:56 +00:00
}, isPrivate)
2015-06-14 18:50:53 +00:00
p.send("update:hand", {
cards: JSON.stringify(p.hand.getCards())
2015-06-24 16:22:56 +00:00
},isPrivate);
2015-06-14 18:50:53 +00:00
p.send("update:fields", {
2015-06-21 14:50:50 +00:00
close: p.field[Card.TYPE.CLOSE_COMBAT].getInfo(),
ranged: p.field[Card.TYPE.RANGED].getInfo(),
siege: p.field[Card.TYPE.SIEGE].getInfo(),
weather: p.field[Card.TYPE.WEATHER].getInfo()
2015-06-24 16:22:56 +00:00
}, isPrivate);
2015-06-14 18:50:53 +00:00
}
2015-06-14 14:01:25 +00:00
r.send = function(event, data){
2015-06-24 16:22:56 +00:00
/*this.channel.publish({
2015-06-15 19:03:12 +00:00
event: event,
data: data
2015-06-24 16:22:56 +00:00
});*/
io.sockets.in(this._id).emit(event, data);
2015-06-13 19:36:02 +00:00
}
2015-06-10 16:12:52 +00:00
2015-06-19 19:53:48 +00:00
r.runEvent = function(eventid, ctx, args, uid){
2015-06-17 19:18:14 +00:00
ctx = ctx || this;
2015-06-19 19:53:48 +00:00
uid = uid || null;
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;
}
2015-06-19 19:53:48 +00:00
if(uid){
var obj = this.events[event][uid];
2015-06-18 07:21:27 +00:00
obj.cb = obj.cb.bind(ctx)
obj.cb.apply(ctx, obj.onArgs.concat(args));
2015-06-19 19:53:48 +00:00
}
else {
for(var _uid in this.events[event]) {
var obj = this.events[event][_uid];
obj.cb = obj.cb.bind(ctx)
obj.cb.apply(ctx, obj.onArgs.concat(args));
}
}
2015-06-24 13:10:54 +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-19 19:53:48 +00:00
var uid_event = shortid.generate();
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)){
2015-06-19 19:53:48 +00:00
/*this.events[event] = [];*/
this.events[event] = {};
2015-06-17 16:10:23 +00:00
}
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-19 19:53:48 +00:00
this.events[event][uid_event] = obj;
2015-06-18 07:21:27 +00:00
2015-06-19 19:53:48 +00:00
return uid_event;
2015-06-17 14:53:44 +00:00
}
2015-06-19 19:53:48 +00:00
r.off = function(eventid, uid){
uid = uid || null;
2015-06-17 16:10:23 +00:00
var event = "on" + eventid;
2015-06-18 13:06:13 +00:00
if(!this.events[event]) return;
2015-06-19 19:53:48 +00:00
if(uid){
this.events[event][uid] = null;
delete this.events[event][uid];
return;
}
2015-06-23 13:01:39 +00:00
for(var _uid in this.events[event]) {
2015-06-19 19:53:48 +00:00
this.events[event][_uid] = null;
delete this.events[event][_uid];
}
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-19 19:53:48 +00:00
r.userLeft = function(sideName){
2015-06-18 13:31:36 +00:00
var side = this[sideName];
side.foe.send("foe:left", null, true);
}
2015-06-15 19:03:12 +00:00
2015-06-19 19:53:48 +00:00
r.shutDown = function(){
this.channel = null;
}
2015-06-10 16:12:52 +00:00
return Battle;
})();
module.exports = Battle;