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

383 lines
9.8 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-28 14:10:25 +00:00
var Deck = require("./Deck");
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-30 10:11:50 +00:00
var CardManager = require("./CardManager")
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-30 10:11:50 +00:00
this.cm = CardManager();
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;
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-30 10:11:50 +00:00
r.cm = null;
2015-06-15 19:03:12 +00:00
r.socket = 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-19 19:53:48 +00:00
this.on("Update", this.update);
2015-06-15 19:03:12 +00:00
2015-06-30 08:46:31 +00:00
this.p1 = Battleside(this._user1, 0, this);
this.p2 = Battleside(this._user2, 1, this);
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-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-07-01 09:38:31 +00:00
/*this.p1.placeCard("ves");
this.p2.placeCard("ves");
this.p1.placeCard("yarpen_zigrin");
this.p2.placeCard("yarpen_zigrin");
this.p1.hand.add(this.p1.createCard("scorch"));
this.p2.hand.add(this.p2.createCard("scorch"));
this.p1.hand.add(this.p1.createCard("villentretenmerth"));
this.p2.hand.add(this.p2.createCard("villentretenmerth"));*/
2015-07-01 08:40:57 +00:00
/*this.p1.hand.add(this.p1.createCard("blue_stripes_commando"));
this.p2.hand.add(this.p2.createCard("blue_stripes_commando"));
this.p1.hand.add(this.p1.createCard("blue_stripes_commando"));
this.p2.hand.add(this.p2.createCard("blue_stripes_commando"));
this.p1.hand.add(this.p1.createCard("blue_stripes_commando"));
this.p2.hand.add(this.p2.createCard("blue_stripes_commando"));
this.p1.hand.add(this.p1.createCard("blue_stripes_commando"));
this.p2.hand.add(this.p2.createCard("blue_stripes_commando"));
this.p1.hand.add(this.p1.createCard("blue_stripes_commando"));
this.p2.hand.add(this.p2.createCard("blue_stripes_commando"));
this.p1.hand.add(this.p1.createCard("dandelion"));
this.p2.hand.add(this.p2.createCard("dandelion"));*/
2015-06-30 16:26:24 +00:00
2015-06-30 12:47:09 +00:00
/*this.p1.placeCard("ves");
this.p2.placeCard("ves");
this.p1.placeCard("yarpen_zigrin");
this.p2.placeCard("yarpen_zigrin");
this.p1.hand.add(this.p1.createCard("scorch"));
this.p2.hand.add(this.p2.createCard("scorch"));
this.p1.hand.add(this.p1.createCard("villentretenmerth"));
this.p2.hand.add(this.p2.createCard("villentretenmerth"));
this.p1.hand.add(this.p1.createCard("impenetrable_fog"));
this.p2.hand.add(this.p2.createCard("impenetrable_fog"));
this.p1.hand.add(this.p1.createCard("biting_frost"));
this.p2.hand.add(this.p2.createCard("biting_frost"));
this.p1.hand.add(this.p1.createCard("torrential_rain"));
this.p2.hand.add(this.p2.createCard("torrential_rain"));
this.p1.hand.add(this.p1.createCard("clear_weather"));
this.p2.hand.add(this.p2.createCard("clear_weather"));
*/
2015-06-30 10:34:57 +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);
2015-07-01 18:18:09 +00:00
this.switchTurn(Math.random() > 0.5 ? this.p1 : this.p2);
2015-06-23 16:12:11 +00:00
}.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-07-01 18:18:09 +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-29 17:57:51 +00:00
var lastRound = this.checkRubies();
var loser = lastRound.loser;
2015-06-28 14:10:25 +00:00
var winner = loser.foe;
2015-06-17 19:18:14 +00:00
if(this.checkIfIsOver()){
2015-07-01 18:18:09 +00:00
//console.log("its over!");
2015-06-28 14:10:25 +00:00
this.gameOver(winner);
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();
2015-07-01 18:18:09 +00:00
//console.log("start new round!");
2015-06-29 17:57:51 +00:00
this.sendNotification("Start new round!");
2015-06-14 18:50:53 +00:00
2015-06-29 17:57:51 +00:00
if(winner.deck.getFaction() === Deck.FACTION.NORTHERN_REALM && !lastRound.isTie){
2015-06-28 14:10:25 +00:00
winner.draw(1);
2015-07-01 18:18:09 +00:00
//console.log(winner.getName() + " draws 1 extra card! (Northern ability)");
2015-06-29 17:57:51 +00:00
this.sendNotification(winner.getName() + " draws 1 extra card! (Northern ability)");
2015-06-28 14:10:25 +00:00
}
2015-06-17 19:18:14 +00:00
this.update();
2015-06-28 14:10:25 +00:00
if(winner.deck.getFaction() === Deck.FACTION.SCOIATAEL){
this.waitForScoiatael(winner);
}
else if(this.p1.deck.getFaction() === Deck.FACTION.SCOIATAEL){
this.waitForScoiatael(this.p1);
}
else if(this.p2.deck.getFaction() === Deck.FACTION.SCOIATAEL){
this.waitForScoiatael(this.p2);
}
else {
2015-06-30 11:43:03 +00:00
this.sendNotification(loser.getName() + " begins!");
2015-06-28 14:10:25 +00:00
this.switchTurn(loser);
}
2015-06-14 18:50:53 +00:00
}
2015-06-28 14:10:25 +00:00
r.waitForScoiatael = function(side){
var self = this;
2015-07-01 18:18:09 +00:00
side.turn();
side.foe.wait();
self.sendNotification(side.getName() + " decides who starts first");
2015-06-28 14:10:25 +00:00
side.send("request:chooseWhichSideBegins", null, true);
side.socket.once("response:chooseWhichSideBegins", function(data){
2015-07-01 18:18:09 +00:00
//console.log("which side? ", data.side);
2015-06-28 14:10:25 +00:00
if(data.side !== "p1" && data.side !== "p2")
throw new Error("Unknown side property! - ", data.side);
2015-06-30 11:43:03 +00:00
self.sendNotification(side.getName() + " choose " + self[data.side].getName());
2015-06-28 14:10:25 +00:00
self.switchTurn(self[data.side]);
})
}
r.gameOver = function(winner){
this.send("gameover", {
winner: winner.getName()
})
}
2015-06-14 18:50:53 +00:00
r.update = function(){
2015-07-01 18:18:09 +00:00
//console.("update called");
2015-06-14 18:50:53 +00:00
this._update(this.p1);
this._update(this.p2);
}
2015-06-28 14:10:25 +00:00
r.updateSelf = function(side){
2015-06-24 16:22:56 +00:00
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-28 14:10:25 +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();
2015-06-29 17:57:51 +00:00
return {
loser: this.p2,
isTie: false
}
2015-06-17 19:18:14 +00:00
}
if(scoreP2 > scoreP1){
this.p1.removeRuby();
2015-06-29 17:57:51 +00:00
return {
loser: this.p1,
isTie: false
}
2015-06-17 19:18:14 +00:00
}
//tie
2015-06-28 14:10:25 +00:00
//check if is nilfgaardian faction ability
if(this.p1.deck.getFaction() === Deck.FACTION.NILFGAARDIAN_EMPIRE && this.p1.deck.getFaction() !== this.p2.deck.getFaction()){
this.p2.removeRuby();
2015-07-01 18:18:09 +00:00
//console.log(this.p1.getName() + " wins the tie! (nilfgaardian ability)");
2015-07-01 09:38:31 +00:00
this.sendNotification(this.p1.getName() + " wins the tie! (nilfgaardian ability)");
2015-06-29 17:57:51 +00:00
return {
loser: this.p2,
isTie: false
}
2015-06-28 14:10:25 +00:00
}
if(this.p2.deck.getFaction() === Deck.FACTION.NILFGAARDIAN_EMPIRE && this.p1.deck.getFaction() !== this.p2.deck.getFaction()){
this.p1.removeRuby();
2015-07-01 18:18:09 +00:00
//console.log(this.p2.getName() + " wins the tie! (nilfgaardian ability)");
2015-07-01 09:38:31 +00:00
this.sendNotification(this.p2.getName() + " wins the tie! (nilfgaardian ability)");
2015-06-29 17:57:51 +00:00
return {
loser: this.p1,
isTie: false
}
2015-06-28 14:10:25 +00:00
}
2015-06-17 19:18:14 +00:00
this.p1.removeRuby();
this.p2.removeRuby();
2015-06-29 17:57:51 +00:00
return {
loser: Math.random() > 0.5 ? this.p1 : this.p2,
isTie: true
}
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-29 17:57:51 +00:00
r.sendNotification = function(msg){
this.send("notification", {
message: msg
})
}
2015-06-10 16:12:52 +00:00
return Battle;
})();
module.exports = Battle;