From 8a4479ca3eefd5c986076079768a2e26a4f88a29 Mon Sep 17 00:00:00 2001 From: Jeffery R Date: Sat, 27 Jan 2018 14:36:37 -0500 Subject: [PATCH] Defined events for socket.io --- README.md | 1 + googletrendsgame/server/server.js | 98 ++++++++++++++++++++++---- googletrendsgame/server/serverUtils.js | 19 +++++ 3 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 googletrendsgame/server/serverUtils.js diff --git a/README.md b/README.md index 2096d85..0e1f6f1 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,5 @@ npm install socket.io npm install mysql npm install sanitizer npm install google-trends-api +npm install promise ```` \ No newline at end of file diff --git a/googletrendsgame/server/server.js b/googletrendsgame/server/server.js index 273fd20..eaf97d0 100644 --- a/googletrendsgame/server/server.js +++ b/googletrendsgame/server/server.js @@ -1,8 +1,6 @@ -var app = require('express')(); -var http = require('http').Server(app); -var io = require('socket.io')(http); -const port = 3000; +//list of all the rooms +var rooms = []; var room = function(capacityP, roomN) { @@ -15,27 +13,54 @@ var room = function(capacityP, roomN) //list of words used in the game this.words = []; - //list of clients sockets -- so we can push requests to them + //list players -- so we can push requests to them this.users = []; //increments when rounds pass this.currentRoom = 0; + // the password of the room -- null if no password + this.password = null; + /** * adds a user to a room - * @param socket + * @param p + * return 0 if they could join + */ + this.addUser = function(player) + { + //check if room is not full + if(this.users.length != this.capacity) + { + this.users.push(player) + } + else + { + + } + } + + /** + * + * @param p */ - this.addUser = function(socket) + this.removeUser = function(p) { + this.users.remove(p); + //if room is empty remove the room from rooms list + rooms.remove(this); } } -var player = function(name) +var player = function(s) { //name of the user - this.name = name; + this.name = null; + + //players socket + this.socket = s; //score of the player this.score = 0; @@ -48,19 +73,68 @@ var player = function(name) this.sumbission = null; } -//list of all the rooms -var rooms = []; + +var app = require('express')(); +var http = require('http').Server(app); +var io = require('socket.io')(http); + +const port = 3000; + +const utils = require('./serverUtils.js'); //Whenever someone connects this gets executed io.on('connection', function(socket) { + var player = new player(socket); + console.log('A user connected'); + /** + *Register user nickname/handle (register) Client => Server + */ + socket.on('register', function(data) { + console.log("Register event called"); + console.log(data); + console.log(" "); + + //checks for user name in use + if(utils.userAvailable(data, rooms)) + { + player.name = data; + } + else + { + socket.emit('registerFailed', 'User name taken'); + } + + }); - socket.on('clientEvent', function(data) { + /** + *Create Room (createRoom) Client => Server + * data {password: , capacity: } + */ + socket.on('createRoom', function(data) { + console.log("create room event called"); console.log(data); + console.log(" "); + + }); + /** + * + */ + socket.on('joinRoom', function(data) { + console.log("join room event called"); + console.log(data); + console.log(" "); + }); + + socket.on('submitWord', function(data) { + console.log("submitWord called"); + console.log(data); + console.log(" "); + }); //Whenever someone disconnects this piece of code executed socket.on('disconnect', function () { diff --git a/googletrendsgame/server/serverUtils.js b/googletrendsgame/server/serverUtils.js new file mode 100644 index 0000000..18d527c --- /dev/null +++ b/googletrendsgame/server/serverUtils.js @@ -0,0 +1,19 @@ +module.exports= + { + /** + * Returns a random word + * @returns {string} + */ + roomOpen : function(name, rooms) + { + return true; + }, + userAvailable : function(name, rooms) + { + return true; + }, + exists: function(name, rooms) + { + + } + }; \ No newline at end of file