From a2ca58084469e6066a71721df700fc74087f4180 Mon Sep 17 00:00:00 2001 From: exane Date: Sat, 13 Jun 2015 09:58:55 +0200 Subject: [PATCH] update --- package.json | 3 +- public/index.html | 76 ++++++++++++------- public/js/Gwent.js | 173 ++++++++++++++++++++++++++++++++++++++++-- public/js/Player.js | 23 ------ public/js/main.js | 2 + public/scss/main.scss | 3 +- server/Battle.js | 10 +-- server/Connections.js | 4 + server/Matchmaker.js | 27 ++++++- server/Room.js | 59 ++++++++++++++ server/Socket.js | 70 ++++++++++++++--- server/User.js | 34 ++++++++- 12 files changed, 400 insertions(+), 84 deletions(-) delete mode 100644 public/js/Player.js create mode 100644 server/Room.js diff --git a/package.json b/package.json index 8b57475..61bb868 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "devDependencies": { "handlebars": "^3.0.3", "jquery": "^2.1.4", - "promise": "^7.0.1" + "promise": "^7.0.1", + "shortid": "^2.2.2" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" diff --git a/public/index.html b/public/index.html index 77c1506..fc0ccee 100644 --- a/public/index.html +++ b/public/index.html @@ -10,7 +10,7 @@
-
+
@@ -37,35 +37,11 @@ field
-
-
-
-
-
Points:
-
0
-
-
-
Hand:
-
0
-
-
-
- - -
-
-
-
Passed
-
-
-
- -
-
+
0
@@ -102,7 +78,7 @@
-
+
discard deck
@@ -120,6 +96,52 @@
+ + diff --git a/public/js/Gwent.js b/public/js/Gwent.js index f493a30..ff092c3 100644 --- a/public/js/Gwent.js +++ b/public/js/Gwent.js @@ -1,8 +1,152 @@ var io = require("socket.io-client")("http://localhost:16918"); var Backbone = require("backbone"); -var Player = require("./Player"); +var Handlebars = require("handlebars"); +var $ = require("jquery"); + +Handlebars.registerHelper("health", function(lives, options){ + var out = ""; + + for(var i = 0; i < 2; i++) { + out += "= 2) return; + this._room.push(user); + user.setRoom(this);/* + user.socket.join(this._id);*/ + user.joinRoom(this.getID()); + + if(!this.isOpen()) { + this._room.forEach(function(user) { + user.send("init:battle"); + }) + } + } + + r.isOpen = function() { + return !(this._room.length >= 2); + } + + r.send = function(event, data) { + io.to(this._id).emit(event, data); + } + + r.getPlayers = function() { + return this._room; + } + + + return Room; +})(); + +module.exports = Room; \ No newline at end of file diff --git a/server/Socket.js b/server/Socket.js index e3ea23f..759e7d1 100644 --- a/server/Socket.js +++ b/server/Socket.js @@ -1,10 +1,14 @@ var app = require('http').createServer(); -var io = require("socket.io")(app); +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 Matchmaker = require("./Matchmaker"); +*/ var Socket = (function(){ var Socket = function(){ @@ -14,8 +18,11 @@ var Socket = (function(){ /** * constructor here */ - this.matchmaker = Matchmaker(); this.connections = Connections(); +/* + this.matchmaker = Matchmaker(this.connections); +*/ + this._roomCollection = []; app.listen(this.port); this.io = io; this._events(); @@ -29,27 +36,66 @@ var Socket = (function(){ r.io = null; r.port = 16918; r.connections = null; + r._roomCollection = null; +/* r.matchmaker = null; +*/ - r._events = function() { + r._events = function(){ var self = this; - this.io.on("connection", function(socket) { + this.io.on("connection", function(socket){ var user = User(socket); self.connections.add(user); - console.log("new user ", user.getID()); + console.log("new user ", user.getName()); - this.matchmaker.findOpponent(user) - .then(function(p1, p2, roomID) { - var battle = Battle(); - battle.init(p1, p2); + /* self.matchmaker.findOpponent(user) + .then(function(p1, p2, roomID) { + console.log("yo"); + var battle = Battle(); + battle.init(p1, p2); + })*/ + + socket.on("request:name", function(data){ + if(data && data.name){ + user.setName(data.name); + } + socket.emit("response:name", {name: user.getName()}); }) - socket.on("disconnect", function() { + socket.on("request:createRoom", function() { + var room = Room(); + self._roomCollection.push(room); + room.join(user); + console.log("room %s created by %s", room.getID(), user.getName()); + user.send("response:createRoom", room.getID()); + }) + + socket.on("request:joinRoom", function() { + console.log("joinroom"); + var interval = setInterval(function(){ + self._roomCollection.forEach(function(room) { + if(room.isOpen()) { + room.join(user); + clearInterval(interval); + console.log("user %s joined room %s", user.getName(), room.getID()); + user.send("response:joinRoom", room.getID()); + } + }); + }, 1000); + }) + + socket.on("request:roomData", function() { + var room = user.getRoom(); + var players = room.getPlayers(); + user.send("response:roomData", {players: players}); + }) + + socket.on("disconnect", function(){ self.connections.remove(user); + user.disconnect(); }) - }) + }); } - return Socket; })(); diff --git a/server/User.js b/server/User.js index 667c80d..1436856 100644 --- a/server/User.js +++ b/server/User.js @@ -14,7 +14,7 @@ var User = (function(){ this.socket = socket; this._id = socket.id; - + this.generateName(); }; User.prototype = Object.create(Entity.prototype); var r = User.prototype; @@ -25,14 +25,16 @@ var User = (function(){ */ r._id = null; + r._name = null; + r._room = null; r.socket = null; r.getID = function(){ return this._id; } - r.joinRoom = function(room){ - this.socket.join(room); + r.joinRoom = function(roomid){ + this.socket.join(roomid); } r.send = function(event, data, room){ @@ -46,6 +48,32 @@ var User = (function(){ } } + r.generateName = function(){ + var name = "Player" + (((Math.random() * 8999) + 1000) | 0); + //if(lobby.hasUser(name)) return generateName(); + this._name = name; + return name; + } + + r.setName = function(name) { + 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._room; + } + r.setRoom = function(room) { + this._room = room; + } + + r.disconnect = function() { + + } + + return User; })();