From 03fd73fab2d99acf6c8c13c3877dfc6520f90496 Mon Sep 17 00:00:00 2001 From: exane Date: Wed, 24 Jun 2015 18:22:56 +0200 Subject: [PATCH] replace socketcluster with socket.io --- client/js/client.js | 80 ++++++++++++++++++++++++------ client/templates/lobby.handlebars | 4 +- package.json | 4 +- server/Battle.js | 20 +++++--- server/Battleside.js | 11 +++-- server/Matchmaker.js | 82 +++++++++++++++---------------- server/Room.js | 10 ++-- server/Socket.js | 36 +++++--------- server/User.js | 73 +++++++++++++++++++++++---- server/server.js | 35 ++++++++++++- 10 files changed, 242 insertions(+), 113 deletions(-) diff --git a/client/js/client.js b/client/js/client.js index 621de28..50cbd92 100644 --- a/client/js/client.js +++ b/client/js/client.js @@ -1,5 +1,4 @@ -/*("http://localhost:16918")*/ -var socketCluster = require("socketcluster-client"); +var socket = require("socket.io-client"); var Backbone = require("backbone"); require("./backbone.modal-min"); var Handlebars = require("handlebars"); @@ -21,7 +20,7 @@ var App = Backbone.Router.extend({ Backbone.history.start(); }, connect: function(){ - this.socket = socketCluster.connect(Config.Server); + this.socket = socket(Config.Server.hostname + ":" + Config.Server.port); }, receive: function(event, cb){ this.socket.on(event, cb); @@ -235,7 +234,7 @@ var BattleView = Backbone.View.extend({ var interval = setInterval(function(){ if(!user.get("room")) return; - this.setUpBattleEvents(user.get("room")); + this.setUpBattleEvents(); this.app.send("request:gameLoaded", {_roomID: user.get("room")}); clearInterval(interval); }.bind(this), 10); @@ -421,12 +420,50 @@ var BattleView = Backbone.View.extend({ this.app.send("activate:leader") }, - setUpBattleEvents: function(channelName){ - this.battleChannel = this.app.socket.subscribe(channelName); + setUpBattleEvents: function(){ var self = this; var user = this.user; + var app = user.get("app"); - this.battleChannel.watch(function(d){ + app.on("update:hand", function(data) { + if(user.get("roomSide") == data._roomSide){ + self.handCards = JSON.parse(data.cards); + self.user.set("handCards", app.handCards); + self.render(); + } + }) + app.on("update:info", function(data) { + var _side = data._roomSide; + var infoData = data.info; + var leader = data.leader; + + var side = self.yourSide; + if(user.get("roomSide") != _side){ + side = self.otherSide; + } + side.infoData = infoData; + side.leader = leader; + + side.infoData.discard = JSON.parse(side.infoData.discard); + + side.render(); + }) + + app.on("update:fields", function(data) { + var _side = data._roomSide; + + var side = self.yourSide; + if(user.get("roomSide") != _side){ + side = self.otherSide; + } + side.field.close = data.close; + side.field.ranged = data.ranged; + side.field.siege = data.siege; + side.field.weather = data.weather; + side.render(); + }) + + /*this.battleChannel.watch(function(d){ var event = d.event, data = d.data; if(event === "update:hand"){ @@ -465,7 +502,7 @@ var BattleView = Backbone.View.extend({ side.field.weather = data.weather; side.render(); } - }) + })*/ } }); @@ -525,7 +562,8 @@ var User = Backbone.Model.extend({ }, initialize: function(){ var self = this; - var app = this.get("app"); + var user = this; + var app = user.get("app"); this.listenTo(this.attributes, "change:room", this.subscribeRoom); @@ -592,7 +630,17 @@ var User = Backbone.Model.extend({ self.set("isReDrawing", false); }) - app.on("createRoom", this.createRoom, this); + app.receive("update:hand", function(data){ + app.trigger("update:hand", data); + }) + app.receive("update:fields", function(data){ + app.trigger("update:fields", data); + }) + app.receive("update:info", function(data){ + app.trigger("update:info", data); + }) + + app.on("startMatchmaking", this.startMatchmaking, this); app.on("joinRoom", this.joinRoom, this); app.on("setName", this.setName, this); app.on("setDeck", this.setDeck, this); @@ -600,8 +648,8 @@ var User = Backbone.Model.extend({ app.send("request:name", this.get("name") == "unnamed" ? null : {name: this.get("name")}); }, - createRoom: function(){ - this.get("app").send("request:createRoom"); + startMatchmaking: function(){ + this.get("app").send("request:matchmaking"); }, joinRoom: function(){ this.get("app").send("request:joinRoom"); @@ -609,7 +657,7 @@ var User = Backbone.Model.extend({ subscribeRoom: function(){ var room = this.get("room"); var app = this.get("app"); - app.socket.subscribe(room); + //app.socket.subscribe(room); }, setName: function(name){ this.get("app").send("request:name", {name: name}); @@ -636,7 +684,7 @@ var Lobby = Backbone.View.extend({ this.render(); }, events: { - "click .create-room": "createRoom", + "click .startMatchmaking": "startMatchmaking", "click .join-room": "joinRoom", "blur .name-input": "changeName", "change #deckChoice": "setDeck" @@ -646,8 +694,8 @@ var Lobby = Backbone.View.extend({ /*this.$el.find("#deckChoice option[value='" + this.app.user.get("setDeck") + "']").attr("selected", "selected")*/ return this; }, - createRoom: function(){ - this.app.trigger("createRoom"); + startMatchmaking: function(){ + this.app.trigger("startMatchmaking"); }, joinRoom: function(){ this.app.trigger("joinRoom"); diff --git a/client/templates/lobby.handlebars b/client/templates/lobby.handlebars index 0349e6c..b8d7211 100644 --- a/client/templates/lobby.handlebars +++ b/client/templates/lobby.handlebars @@ -14,8 +14,8 @@
- - + +
diff --git a/package.json b/package.json index 7d2295a..c233cfe 100644 --- a/package.json +++ b/package.json @@ -10,8 +10,8 @@ "minimist": "1.1.0", "serve-static": "1.8.0", "shortid": "^2.2.2", - "socketcluster": "2.2.x", - "socketcluster-client": "2.2.x", + "socket.io": "^1.3.5", + "socket.io-client": "^1.3.5", "underscore": "^1.8.3" }, "devDependencies": { diff --git a/server/Battle.js b/server/Battle.js index 56bca98..570d193 100644 --- a/server/Battle.js +++ b/server/Battle.js @@ -46,7 +46,7 @@ var Battle = (function(){ this.on("AfterPlace", this.checkAbilityOnAfterPlace)*/ - this.channel = this.socket.subscribe(this._id); + //this.channel = this.socket.subscribe(this._id); this.p1 = Battleside(this._user1.getName(), 0, this, this._user1); this.p2 = Battleside(this._user2.getName(), 1, this, this._user2); this.p1.foe = this.p2; @@ -120,27 +120,33 @@ var Battle = (function(){ this._update(this.p2); } - r._update = function(p){ + r.updateSelf = function(side) { + this._update(side, true); + } + + r._update = function(p, isPrivate){ + isPrivate = isPrivate || false; p.send("update:info", { info: p.getInfo(), leader: p.field[Card.TYPE.LEADER].get()[0] - }) + }, isPrivate) p.send("update:hand", { cards: JSON.stringify(p.hand.getCards()) - }); + },isPrivate); p.send("update:fields", { 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() - }) + }, isPrivate); } r.send = function(event, data){ - this.channel.publish({ + /*this.channel.publish({ event: event, data: data - }); + });*/ + io.sockets.in(this._id).emit(event, data); } r.runEvent = function(eventid, ctx, args, uid){ diff --git a/server/Battleside.js b/server/Battleside.js index 8da5e5d..6426288 100644 --- a/server/Battleside.js +++ b/server/Battleside.js @@ -620,7 +620,7 @@ Battleside = (function(){ this.send("redraw:cards", null, true); - var h1 = this.receive("redraw:reDrawCard", function(data){ + this.receive("redraw:reDrawCard", function(data){ var id = data.cardID; if(!left) return; left--; @@ -633,15 +633,16 @@ Battleside = (function(){ self.send("redraw:close", null, true); console.log("redraw finished"); deferred.resolve("done"); - self.socket.off("redraw:reDrawCard", h1); + //self.socket.off("redraw:reDrawCard", h1); } - self.update(self); + /*self.update(self);*/ + self.battle.updateSelf(self); }) - var h2 = this.receive("redraw:close_client", function() { + this.receive("redraw:close_client", function() { console.log("redraw finished!"); deferred.resolve("done"); - self.socket.off("redraw:close_client", h2); + //self.socket.off("redraw:close_client", h2); }) return deferred; diff --git a/server/Matchmaker.js b/server/Matchmaker.js index acf1c57..3052f5c 100644 --- a/server/Matchmaker.js +++ b/server/Matchmaker.js @@ -1,9 +1,9 @@ var Promise = require("promise"); var Matchmaker = (function(){ - var Matchmaker = function(connections){ + var Matchmaker = function(){ if(!(this instanceof Matchmaker)){ - return (new Matchmaker(connections)); + return (new Matchmaker()); } /** * constructor here @@ -23,47 +23,47 @@ var Matchmaker = (function(){ r._queue = null; r._connections = null; - r.findOpponent = function(user){ - var self = this; - - var promise = new Promise(function(resolve){ - self._queue.push(user); - self._checkForOpponent(resolve); - }); - return promise; - } - - r._checkForOpponent = function(resolve){ - if(this._queue.length <= 1) return; - console.log(this._queue.length); - if(!this._checkConnections()) return; - this._match(this._queue[0], this._queue[1], resolve); - } - - r._match = function(p1, p2, resolve){ - this._queue.splice(0, 2); - var roomID = p1.id + p2.id; - p1.send("get:opponent", {socketID: p2.getID()}); - p2.send("get:opponent", {socketID: p1.getID()}); - - p1.joinRoom(roomID); - p2.joinRoom(roomID); - - resolve(p1, p2, roomID); - } - - r._checkConnections = function() { - var res = true; - var self = this; - - this._queue.forEach(function(user, index) { - if(!self._connections.hasUser(user)) { - self._queue.splice(index, 1); - res = false; + r.removeFromQueue = function(user){ + for(var i = 0; i < this._queue.length; i++) { + var u = this._queue[i]; + if(u.getID() === user.getID()) { + user._inQueue = false; + return this._queue.splice(i, 1); } - }); + } + } - return res; + r.findOpponent = function(user){ + var c = connections; + + var found = this._checkForOpponent(); + + if(found){ + + var room = Room(); + c.roomCollection[room.getID()] = room; + room.join(user); + room.join(found); + user._inQueue = false; + found._inQueue = false; + return room; + } + + this._getInQueue(user); + } + + r._getInQueue = function(user){ + console.log(user.getName() + " joined in queue"); + this._queue.push(user); + user._inQueue = true; + } + + + r._checkForOpponent = function(){ + if(!this._queue.length) return null; + var foe = this._queue.splice(0, 1)[0]; + foe._inQueue = false; + return foe; } diff --git a/server/Room.js b/server/Room.js index 212dd1c..157356b 100644 --- a/server/Room.js +++ b/server/Room.js @@ -2,9 +2,9 @@ var shortid = require("shortid"); var Battle = require("./Battle"); var Room = (function(){ - var Room = function(scServer){ + var Room = function(){ if(!(this instanceof Room)){ - return (new Room(scServer)); + return (new Room()); } /** * constructor here @@ -14,7 +14,7 @@ var Room = (function(){ this._id = shortid.generate(); this._users = []; this._ready = {}; - this.socket = scServer.global; + //this.socket = scServer.global; console.log("room created: " + this.getID()); @@ -40,6 +40,8 @@ var Room = (function(){ if(this._users.length >= 2) return; this._users.push(user); user.addRoom(this); + user.socket.join(this.getID()); + user.send("response:joinRoom", this.getID()); if(!this.isOpen()){ this.initBattle(); @@ -55,7 +57,7 @@ var Room = (function(){ } r.initBattle = function(){ - this._battle = Battle(this._id, this._users[0], this._users[1], this.socket); + this._battle = Battle(this._id, this._users[0], this._users[1], io); this._users[0].send("init:battle", {side: "p1"}); this._users[1].send("init:battle", {side: "p2"}); } diff --git a/server/Socket.js b/server/Socket.js index efb40b5..cc601e5 100644 --- a/server/Socket.js +++ b/server/Socket.js @@ -1,11 +1,8 @@ -/* -var app = require('http').createServer(); -global.io = require("socket.io")(app); -var User = require("./User"); +/*var app = require('http').createServer(); +global.io = require("socket.io")(app);*/ +/*var User = require("./User"); var Connections = require("./Connections"); -var Battle = require("./Battle"); -var Npc = require("./Npc"); -var Room = require("./Room"); +var Room = require("./Room");*/ var Socket = (function(){ @@ -13,39 +10,28 @@ var Socket = (function(){ if(!(this instanceof Socket)){ return (new Socket()); } - */ -/** + /** * constructor here - *//* - + */ this.connections = Connections(); - */ -/* - this.matchmaker = Matchmaker(this.connections); - *//* - this.roomCollection = {}; app.listen(this.port); this.io = io; this._events(); }; var r = Socket.prototype; - */ -/** + /** * methods && properties here * r.property = null; * r.getProperty = function() {...} - *//* - + */ r.io = null; r.port = 16918; r.connections = null; r.roomCollection = null; - */ -/* + /* r.matchmaker = null; - *//* - + */ r._events = function(){ var self = this; @@ -102,4 +88,4 @@ var Socket = (function(){ return Socket; })(); -module.exports = Socket;*/ +module.exports = Socket; \ No newline at end of file diff --git a/server/User.js b/server/User.js index 4ce88fd..587fdbb 100644 --- a/server/User.js +++ b/server/User.js @@ -12,6 +12,8 @@ var User = (function(){ this._rooms = []; this._id = socket.id; this.generateName(); + + this._events(); }; var r = User.prototype; /** @@ -23,7 +25,7 @@ var User = (function(){ r._id = null; r._name = null; r._rooms = null; - r._searching = false; + r._inQueue = false; r.socket = null; r.disconnected = false; @@ -31,13 +33,13 @@ var User = (function(){ return this._id; } - r.joinRoom = function(roomid){ + /*r.joinRoom = function(roomid){ var self = this; - /*this.socket.on(roomid, function(d) { + *//*this.socket.on(roomid, function(d) { var event = d.event, data = d.data; self.socket.on(event, data); - });*/ - } + });*//* + }*/ r.send = function(event, data, room){ room = room || null; @@ -45,12 +47,12 @@ var User = (function(){ if(!room){ this.socket.emit(event, data); } - else {/* - this.socket.to(room).emit(event, data);*/ - this.socket.global.publish(room, { + else { + this.socket.to(room).emit(event, data); + /*this.socket.global.publish(room, { event: event, data: data - }) + })*/ } } @@ -102,6 +104,8 @@ var User = (function(){ var self = this; this.disconnected = true; + matchmaking.removeFromQueue(this); + this._rooms.forEach(function(room) { room.leave(self); if(!room.hasUser()) { @@ -113,6 +117,57 @@ var User = (function(){ this.cleanUp(); } + r._events = function() { + var socket = this.socket; + var self = this; + + socket.on("request:name", function(data){ + if(data && data.name){ + self.setName(data.name); + } + socket.emit("response:name", {name: self.getName()}); + }) + + /*socket.on("request:createRoom", function(){ + var room = Room(); + connections.roomCollection[room.getID()] = room; + room.join(self); + console.log("room %s created by %s", room.getID(), self.getName()); + self.send("response:createRoom", room.getID()); + }) + + socket.on("request:joinRoom", function(){ + console.log("joinroom"); + var interval = setInterval(function(){ + for(var key in connections.roomCollection) { + var room = connections.roomCollection[key]; + if(!room.isOpen()) continue; + room.join(self); + clearInterval(interval); + console.log("user %s joined room %s", self.getName(), room.getID()); + self.send("response:joinRoom", room.getID()); + } + }, 1000); + })*/ + + socket.on("request:matchmaking", function() { + if(self._inQueue) return; + matchmaking.findOpponent(self); + }); + + socket.on("request:gameLoaded", function(data){ + console.log(data); + connections.roomCollection[data._roomID].setReady(self); + }) + + socket.on("set:deck", function(data) { + console.log(data); + if(data && data.deck){ + self.setDeck(data.deck); + } + }) + + } return User; })(); diff --git a/server/server.js b/server/server.js index e7ea4b7..32eab77 100644 --- a/server/server.js +++ b/server/server.js @@ -1,6 +1,7 @@ var argv = require('minimist')(process.argv.slice(2)); -var SocketCluster = require('socketcluster').SocketCluster; +/*var SocketCluster = require('socketcluster').SocketCluster;*/ +/* var socketCluster = new SocketCluster({ workers: Number(argv.w) || 1, stores: Number(argv.s) || 1, @@ -10,4 +11,34 @@ var socketCluster = new SocketCluster({ storeController: __dirname + '/store.js', socketChannelLimit: 100, rebootWorkerOnCrash: argv['auto-reboot'] != false -}); \ No newline at end of file +});*/ + +global.connections = require("./Connections")(); + +global.matchmaking = require("./Matchmaker")(); + +global.Room = require("./Room"); + +global.User = require("./User"); + +/*global.Socket = require("./Socket");*/ + + +var app = require('http').createServer(); +global.io = require("socket.io")(app); + +app.listen(16918); + +io.on("connection", function(socket) { + var user; + connections.add(user = User(socket)); + console.log("new user ", user.getName()); + + socket.on("disconnect", function() { + connections.remove(user); + user.disconnect(); + console.log("user ", user.getName(), " disconnected"); + user = null; + }) + +}) \ No newline at end of file