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

add own event system

This commit is contained in:
exane 2015-06-17 18:10:23 +02:00
parent 14511b35fc
commit f0cbb9de5d
2 changed files with 72 additions and 44 deletions

View File

@ -1,5 +1,5 @@
var Battleside = require("./Battleside"); var Battleside = require("./Battleside");
var PubSub = require("pubsub-js"); //var PubSub = require("pubsub-js");
var Card = require("./Card"); var Card = require("./Card");
/*var io = global.io;*/ /*var io = global.io;*/
@ -12,6 +12,7 @@ var Battle = (function(){
/** /**
* constructor here * constructor here
*/ */
this.events = {};
this._id = id; this._id = id;
this._user1 = p1; this._user1 = p1;
this._user2 = p2; this._user2 = p2;
@ -39,7 +40,8 @@ var Battle = (function(){
r.events = null; r.events = null;
r.init = function(){ r.init = function(){
PubSub.subscribe("update", this.update.bind(this)); /*PubSub.subscribe("update", this.update.bind(this));*/
this.on("Update", this.update);
this.channel = this.socket.subscribe(this._id); this.channel = this.socket.subscribe(this._id);
@ -53,7 +55,6 @@ var Battle = (function(){
this.start(); this.start();
} }
r.start = function(){ r.start = function(){
this.p1.setLeadercard(); this.p1.setLeadercard();
this.p2.setLeadercard(); this.p2.setLeadercard();
@ -68,7 +69,8 @@ var Battle = (function(){
this.update(); this.update();
PubSub.subscribe("nextTurn", this.switchTurn.bind(this)); /*PubSub.subscribe("nextTurn", this.switchTurn.bind(this));*/
this.on("NextTurn", this.switchTurn);
this.switchTurn(); this.switchTurn();
} }
@ -97,8 +99,11 @@ var Battle = (function(){
return this.switchTurn(1); return this.switchTurn(1);
} }
PubSub.publish("onEachTurn"); /*PubSub.publish("onEachTurn");*/
PubSub.publish("turn/" + side.getID()); this.runEvent("EachTurn");
/*
PubSub.publish("turn/" + side.getID());*/
this.runEvent("Turn" + side.getID());
console.log("current Turn: ", side.getName()); console.log("current Turn: ", side.getName());
} }
@ -135,37 +140,45 @@ var Battle = (function(){
}); });
} }
r.runEvent = function(eventid, target){ r.runEvent = function(eventid, target, args){
target = target || this; target = target || this;
this.events["on" + eventid].forEach(function(event) { args = args || [];
event.call(target); var event = "on" + eventid;
if(!this.events["on" + eventid]){
return;
}
this.events[event].forEach(function(e){
e.apply(target, args);
}); });
} }
r.on = function(eventid, cb){ r.on = function(eventid, cb, ctx, args){
if(!this.events["on" + eventid]) { ctx = ctx || this;
this.events["on" + eventid] = []; args = args || null;
var event = "on" + eventid;
if(!(event in this.events)){
this.events[event] = [];
}
/*if(!this.events[event]) {
this.events[event] = [];
}*/
if(args){
this.events[event].push(cb.bind(ctx, args));
}
else {
this.events[event].push(cb.bind(ctx));
} }
this.events["on" + eventid].push(cb);
} }
r.off = function(eventid){ r.off = function(eventid){
this.events["on" + eventid].forEach(function(event) { var event = "on" + eventid;
event = null; this.events[event].forEach(function(e){
e = null;
}); });
delete this.events["on" + eventid]; delete this.events[event];
} }
/*r._setUpChannel = function() {
var self = this;
this._abilityChannel.watch(function(d) {
var event = d.event, data = d.data;
if(event === "update") {
data();
}
})
}*/
return Battle; return Battle;
})(); })();

View File

@ -4,7 +4,7 @@ var Deck = require("./Deck");
var Hand = require("./Hand"); var Hand = require("./Hand");
var Card = require("./Card"); var Card = require("./Card");
var Field = require("./Field"); var Field = require("./Field");
var PubSub = require("pubsub-js"); //var PubSub = require("pubsub-js");
var Battleside; var Battleside;
@ -31,6 +31,10 @@ Battleside = (function(){
this.hand = Hand(); this.hand = Hand();
this.deck = Deck(DeckData["test"]); this.deck = Deck(DeckData["test"]);
this.runEvent = this.battle.runEvent.bind(this.battle);
this.on = this.battle.on.bind(this.battle);
this.off = this.battle.off.bind(this.battle);
this.receive("play:cardFromHand", function(data){ this.receive("play:cardFromHand", function(data){
if(self._isWaiting) return; if(self._isWaiting) return;
@ -43,16 +47,19 @@ Battleside = (function(){
this.receive("decoy:replaceWith", function(data){ this.receive("decoy:replaceWith", function(data){
if(self._isWaiting) return; if(self._isWaiting) return;
var card = self.findCardOnFieldByID(data.cardID); var card = self.findCardOnFieldByID(data.cardID);
if(card === -1) throw "decoy:replace | unknown card"; if(card === -1) throw new Error("decoy:replace | unknown card");
PubSub.publish("decoy:replaceWith", card); /*PubSub.publish("decoy:replaceWith", card);*/
self.runEvent("Decoy:replaceWith", self, [card]);
}) })
this.receive("set:passing", function() { this.receive("set:passing", function() {
self.setPassing(true); self.setPassing(true);
self.update(); self.update();/*
PubSub.publish("nextTurn"); PubSub.publish("nextTurn");*/
self.runEvent("NextTurn");
}) })
PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this)); /*PubSub.subscribe("turn/" + this.getID(), this.onTurnStart.bind(this));*/
this.on("Turn" + this.getID(), this.onTurnStart, this);
}; };
var r = Battleside.prototype; var r = Battleside.prototype;
/** /**
@ -174,7 +181,8 @@ Battleside = (function(){
} }
r.update = function(){ r.update = function(){
PubSub.publish("update"); //PubSub.publish("update");
this.runEvent("Update");
} }
r.onTurnStart = function(){ r.onTurnStart = function(){
@ -195,7 +203,8 @@ Battleside = (function(){
this.update(); this.update();
PubSub.publish("nextTurn"); //PubSub.publish("nextTurn");
this.runEvent("NextTurn");
} }
r.placeCard = function(card){ r.placeCard = function(card){
@ -207,7 +216,8 @@ Battleside = (function(){
var field = obj.targetSide.field[card.getType()]; var field = obj.targetSide.field[card.getType()];
field.add(card); field.add(card);
PubSub.publish("onEachCardPlace"); //PubSub.publish("onEachCardPlace");
this.runEvent("OnEachCardPlace");
this.checkAbilityOnAfterPlace(card); this.checkAbilityOnAfterPlace(card);
@ -226,13 +236,15 @@ Battleside = (function(){
if(ability.replaceWith){ if(ability.replaceWith){
obj._canclePlacement = true; obj._canclePlacement = true;
var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){ //var decoy = PubSub.subscribe("decoy:replaceWith", function(event, replaceCard){
this.on("Decoy:replaceWith", function(replaceCard) {
if(replaceCard.getType() == Card.TYPE.LEADER || if(replaceCard.getType() == Card.TYPE.LEADER ||
replaceCard.getType() == Card.TYPE.WEATHER || replaceCard.getType() == Card.TYPE.WEATHER ||
replaceCard.getType() == Card.TYPE.SPECIAL){ replaceCard.getType() == Card.TYPE.SPECIAL){
return; return;
} }
PubSub.unsubscribe(decoy); /*PubSub.unsubscribe(decoy);*/
self.off("Decoy:replaceWith");
var field = self.field[replaceCard.getType()]; var field = self.field[replaceCard.getType()];
field.replaceWith(replaceCard, card); field.replaceWith(replaceCard, card);
@ -241,14 +253,17 @@ Battleside = (function(){
self.update(); self.update();
PubSub.publish("nextTurn"); /*PubSub.publish("nextTurn");*/
self.runEvent("NextTurn");
}) })
} }
if(ability.onEachTurn){ if(ability.onEachTurn){
PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card)); //PubSub.subscribe("onEachTurn", ability.onEachTurn.bind(this, card));
this.on("EachTurn", ability.onEachTurn, this, [card])
} }
if(ability.onEachCardPlace){ if(ability.onEachCardPlace){
PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card)); //PubSub.subscribe("onEachCardPlace", ability.onEachCardPlace.bind(this, card));
this.on("EachCardPlace", ability.onEachCardPlace, this, [card]);
} }
} }