From 14511b35fc6699d2c7b1f279e6c74e1ec821a8f7 Mon Sep 17 00:00:00 2001 From: exane Date: Wed, 17 Jun 2015 16:53:44 +0200 Subject: [PATCH] implement event system --- server/Battle.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/Battle.js b/server/Battle.js index 12ebea7..12d30bd 100644 --- a/server/Battle.js +++ b/server/Battle.js @@ -36,6 +36,7 @@ var Battle = (function(){ r._id = null; + r.events = null; r.init = function(){ PubSub.subscribe("update", this.update.bind(this)); @@ -134,6 +135,27 @@ var Battle = (function(){ }); } + 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]; + } + /*r._setUpChannel = function() { var self = this; this._abilityChannel.watch(function(d) {