diff --git a/public/js/client.js b/public/js/client.js
index aefd75a..d8aa4e7 100644
--- a/public/js/client.js
+++ b/public/js/client.js
@@ -444,6 +444,11 @@ var User = Backbone.Model.extend({
self.set("passing", passing);
})
+ app.receive("foe:left", function() {
+ console.log("your foe left the room");
+ $(".container").prepend('
Your foe left the battle!
')
+ })
+
app.on("createRoom", this.createRoom, this);
app.on("joinRoom", this.joinRoom, this);
diff --git a/server/Battle.js b/server/Battle.js
index 3f02d65..a354428 100644
--- a/server/Battle.js
+++ b/server/Battle.js
@@ -75,11 +75,8 @@ var Battle = (function(){
r.switchTurn = function(side, __flag){
__flag = typeof __flag == "undefined" ? 0 : 1;
- //var side = this.turn++ % 2 ? this.p1 : this.p2;
- /*if(__flag instanceof Battleside) {
- side = __flag;
- }*/
+
if(!(side instanceof Battleside)){
console.trace("side is not a battleside!");
return
@@ -198,12 +195,10 @@ var Battle = (function(){
delete this.events[event];
}
-
r.checkIfIsOver = function(){
return !(this.p1.getRubies() && this.p2.getRubies());
}
-
r.checkRubies = function(){
var scoreP1 = this.p1.getScore();
var scoreP2 = this.p2.getScore();
@@ -223,6 +218,12 @@ var Battle = (function(){
return Math.random() > 0.5 ? this.p1 : this.p2;
}
+ r.userLeft = function(sideName) {
+ var side = this[sideName];
+
+ side.foe.send("foe:left", null, true);
+
+ }
return Battle;
})();
diff --git a/server/Room.js b/server/Room.js
index d17529d..957ce1a 100644
--- a/server/Room.js
+++ b/server/Room.js
@@ -15,14 +15,6 @@ var Room = (function(){
this._users = [];
this._ready = {};
this.socket = scServer.global;
-/*
- this._channel = this.socket.subscribe(this._id);*/
-
- /*this._channel.watch(function(data) {
- *//*self._users.forEach(function(user) {
-
- })*//*
- });*/
};
var r = Room.prototype;
@@ -46,7 +38,6 @@ var Room = (function(){
if(this._users.length >= 2) return;
this._users.push(user);
user.addRoom(this);
- /*user.joinRoom(this.getID());*/
if(!this.isOpen()){
this.initBattle();
@@ -62,8 +53,6 @@ var Room = (function(){
}
r.initBattle = function(){
- var self = this;
- var side = 0;
this._battle = Battle(this._id, this._users[0], this._users[1], this.socket);
this._users[0].send("init:battle", {side: "p1"});
this._users[1].send("init:battle", {side: "p2"});
@@ -81,7 +70,15 @@ var Room = (function(){
r.bothReady = function(){
return !!this._ready[this._users[0].getID()] && !!this._ready[this._users[1].getID()];
}
-
+
+ r.leave = function(user) {
+ var p = "p2";
+ if(user.getID() === this._users[0].getID()) {
+ p = "p1";
+ }
+ this._battle.userLeft(p);
+ }
+
return Room;
})();
diff --git a/server/User.js b/server/User.js
index 7ab70c1..a125218 100644
--- a/server/User.js
+++ b/server/User.js
@@ -63,18 +63,24 @@ var User = (function(){
console.log("user name changed from %s to %s", this._name, name);
this._name = name;
}
+
r.getName = function() {
return this._name;
}
+
r.getRoom = function() {
return this._rooms[0];
}
+
r.addRoom = function(room) {
this._rooms.push(room);
}
r.disconnect = function() {
-
+ var self = this;
+ this._rooms.forEach(function(room) {
+ room.leave(self);
+ })
}
diff --git a/server/worker.js b/server/worker.js
index 177fb2e..4a1d529 100644
--- a/server/worker.js
+++ b/server/worker.js
@@ -23,8 +23,6 @@ module.exports.run = function(worker){
var connections = Connections();
var roomCollection = {};
-
-
scServer.on('connection', function(socket){
var user = User(socket);
connections.add(user);
@@ -61,7 +59,7 @@ module.exports.run = function(worker){
console.log("user %s joined room %s", user.getName(), room.getID());
user.send("response:joinRoom", room.getID());
}
- }, 1000);
+ }, 500);
})
socket.on("request:roomData", function(){