From 7853fdb56e5198e62ec9c539402633fcc88a6b09 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 17 Feb 2018 13:33:16 -0500 Subject: [PATCH] Refactored players with es6 --- README.md | 29 +- googletrendsgame/server/.babelrc | 3 + googletrendsgame/server/package.json | 32 ++ googletrendsgame/server/player.js | 73 +++++ googletrendsgame/server/room.js | 455 +++++++++++++-------------- googletrendsgame/server/server.js | 360 +-------------------- 6 files changed, 370 insertions(+), 582 deletions(-) create mode 100644 googletrendsgame/server/.babelrc create mode 100644 googletrendsgame/server/package.json create mode 100644 googletrendsgame/server/player.js diff --git a/README.md b/README.md index 54d1db6..a00d7a8 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,27 @@ Guess the trends beat your friends ```` -npm init -npm install express -npm install socket.io -npm install mysql -npm install sanitizer -npm install google-trends-api -npm install promise -npm install async +npm init -y +npm install --save express morgan +npm install socket.io --save +npm install mysql --save +npm install sanitizer --save +npm install google-trends-api --save +npm install promise --save +npm install async --save + +npm install --save-dev babel-cli babel-preset-es2015 rimraf +```` + +###Configure Babel +``` +touch .babelrc +``` +In that file put: +```` +{ + "presets": ["es2015"] +} ```` ##Database Construction diff --git a/googletrendsgame/server/.babelrc b/googletrendsgame/server/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/googletrendsgame/server/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/googletrendsgame/server/package.json b/googletrendsgame/server/package.json new file mode 100644 index 0000000..ffb31bd --- /dev/null +++ b/googletrendsgame/server/package.json @@ -0,0 +1,32 @@ +{ + "name": "server", + "version": "1.0.0", + "description": "", + "main": "server.js", + "directories": { + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files", + "start": "npm run build && node dist/server.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "async": "^2.6.0", + "express": "^4.16.2", + "google-trends-api": "^4.5.0", + "morgan": "^1.9.0", + "mysql": "^2.15.0", + "promise": "^8.0.1", + "sanitizer": "^0.1.3", + "socket.io": "^2.0.4" + }, + "devDependencies": { + "babel-cli": "^6.26.0", + "babel-preset-es2015": "^6.24.1", + "rimraf": "^2.6.2" + } +} diff --git a/googletrendsgame/server/player.js b/googletrendsgame/server/player.js new file mode 100644 index 0000000..d2ab26f --- /dev/null +++ b/googletrendsgame/server/player.js @@ -0,0 +1,73 @@ +//gets the trending data +const trendingAPI = require("./trendsAPI.js"); + +class Player +{ + + constructor(s) + { + //name of the user + this.name = null; + + //players socket + this.socket = s; + + //score of the player + this.score = 0; + + //reference to the room -- might not need this + this.room = null; + + //the word the user selected for current round + this.submission = ''; + + this.roundScore = 0; + + //logs the user data so we can record it to data base at end of round + this.log = []; + } + + /** + * generate the json object used in 'roomUpdate' socket io event + * + * return {name: score: word:} + */ + genJASON() + { + var result = new Object(); + result.name = this.name; + result.score = this.score; + result.word = this.submission; + + return result; + } + + /** + * data -- literally a string + * @param data + */ + selectWord(data) + { + var w = data + " " + this.room.currentWord; + this.submission = data; + + //console.log(w); + + this.room.update(); + + return new Promise(function(resolve, reject) + { + trendingAPI.getPopularity(w).then(function(result) + { + console.log("api result for " + result + w); + resolve(result); + }).catch(function(err){ + console.log(err); + }) + }); + + + } +} + +module.exports = Player; \ No newline at end of file diff --git a/googletrendsgame/server/room.js b/googletrendsgame/server/room.js index 0c48eb7..aeef02e 100644 --- a/googletrendsgame/server/room.js +++ b/googletrendsgame/server/room.js @@ -1,286 +1,281 @@ +//used for the getting the word array +const utils = require("./utils.js"); + /** * Object used for storing rooms * @param capacityP -- the number of people that can be in room * @param pass -- the room password -- null if none * @param owner -- the person who is creating the room */ - -//used for the getting the word array -const utils = require("./utils.js"); - - - class Room { - //max capacity of room -- default is 4 for now - var capacity; -//name of the room - var roomName; + constructor(capacityP, pass, owner) + { + //max capacity of room -- default is 4 for now + this.capacity = capacityP; -//list of words used in the game -//7 for now will change later to be room specific - var words = utils.getRandomWords(7); + //name of the room + this.roomName = owner.name; - var currentWord = words.pop(); + //list of words used in the game + //7 for now will change later to be room specific + this.words = utils.getRandomWords(7); -//list players -- so we can push requests to them - var users = []; + this.currentWord = this.words.pop(); -//increments when rounds pass - var currentRound = 0; + //list players -- so we can push requests to them + this.users = []; -// the password of the room -- null if no password - var password; + //increments when rounds pass + this.currentRound = 0; - /** - 1 = Waiting for users - 2 = Word shown, Waiting for response from users - 3 = Showing Result - 4 = Game Over, Display Final Results - */ - var state = 1; -} - -module.exports = - { - /** - * Constructor for the module to initialise variables - * @param capacityP - * @param pass - * @param owner - */ - createRoom: function(capacityP, pass, owner) - { - this.addUser(owner); - password = pass; - capacity = capacityP; - roomName = owner.name; - }, + // the password of the room -- null if no password + this.password = pass; /** - * creates json to send in the 'roomUpdate' socket event - * - * {users: gameState: roundWinner: currentWord: } + 1 = Waiting for users + 2 = Word shown, Waiting for response from users + 3 = Showing Result + 4 = Game Over, Display Final Results */ - generateRoomUpdate: function() - { - var result = new Object(); + this.state = 1; - result.users = []; + this.addUser(owner); + } - users.forEach(function(u) - { - result.users.push(u.genJASON()); - }); - //sort the users based on score - var countOuter = 0; - var countInner = 0; - var countSwap = 0; - - // var swapped; - // do - // { - // countOuter++; - // swapped = false; - // for(var i = 0; i < result.users.length; i++) - // { - // countInner++; - // if(result.users[i].score && result.users[i + 1].score && - // result.users[i].score > result.users[i + 1].score) - // { - // countSwap++; - // var temp = result.users[i]; - // result.users[i] = result.users[j]; - // result.users[j] = temp; - // swapped = true; - // } - // } - // } while(swapped); - - - result.gameState = state; - - - //sets round winner - var rWinner = -1; - - for(var i = 0; i < users.length; i++) - { - if(rWinner < users[i].roundScore) - { - result.roundWinner = users[i].name; - rWinner = users[i].roundScore; - } - } - - result.currentWord = currentWord; + /** + * creates json to send in the 'roomUpdate' socket event + * + * {users: gameState: roundWinner: currentWord: } + */ + generateRoomUpdate() + { + var result = new Object(); - return result; - }, + result.users = []; - /** - * grabs roomUpdate json and beams it to every user in the channel - */ - sendRoomUpdate: function() + this.users.forEach(function(u) { - var message = this.generateRoomUpdate(); - this.users.forEach(function(u) - { - //console.log("room update called"); - u.socket.emit('roomUpdate', message); - //console.log(message); - }); - }, - /** - * adds a user to a room - * @param p - * return 0 if they could join - */ - addUser: function(player) + result.users.push(u.genJASON()); + }); + + //sort the users based on score + var countOuter = 0; + var countInner = 0; + var countSwap = 0; + + // var swapped; + // do + // { + // countOuter++; + // swapped = false; + // for(var i = 0; i < result.users.length; i++) + // { + // countInner++; + // if(result.users[i].score && result.users[i + 1].score && + // result.users[i].score > result.users[i + 1].score) + // { + // countSwap++; + // var temp = result.users[i]; + // result.users[i] = result.users[j]; + // result.users[j] = temp; + // swapped = true; + // } + // } + // } while(swapped); + + + result.gameState = this.state; + + + //sets round winner + var rWinner = -1; + + for(var i = 0; i < this.users.length; i++) { - //console.log("user added"); - //check if room is not full - this.users.push(player); - player.room = this; - - if(this.users.length == this.capacity) + if(rWinner < this.users[i].roundScore) { - this.state = 2; + result.roundWinner = this.users[i].name; + rWinner = this.users[i].roundScore; } + } - console.log("user added to room " + player.name); - //console.log(this.users); + result.currentWord = this.currentWord; - this.update(); - }, + return result; + } - /** - * Removes a specific user from the room and adjusts the size of the array - * if the array is empty, the room closes - * @param p - */ - removeUser: function(p) + /** + * grabs roomUpdate json and beams it to every user in the channel + */ + sendRoomUpdate() + { + var message = this.generateRoomUpdate(); + this.users.forEach(function(u) { - console.log("remove users fnc called"); - var temp = new Array(); + //console.log("room update called"); + u.socket.emit('roomUpdate', message); + //console.log(message); + }); + } - for(var i = 0; i < temp.length; i++) - { - if(p.name === users[i].name) - { + /** + * adds a user to a room + * @param p + * return 0 if they could join + */ + addUser(player) + { + //console.log("user added"); + //check if room is not full + this.users.push(player); + player.room = this; - } - else - { - temp.push(users[i]); - } - } + if(this.users.length == this.capacity) + { + this.state = 2; + } - users = temp; + console.log("user added to room " + player.name); + //console.log(this.users); - //if room is empty remove the room from rooms list - if(users.length == 0) - { - console.log("room scrubbed"); - delete rooms[roomName]; - } + this.update(); + } - this.update(); - }, - /** - * Whether or not a user can join this room -- checks for number of people are - * already in the room and the password - * @param p - * @returns {boolean} - */ - canJoin: function(p) + + + /** + * Removes a specific user from the room and adjusts the size of the array + * if the array is empty, the room closes + * @param p + */ + removeUser(p) + { + console.log("remove users fnc called"); + var temp = new Array(); + + for(var i = 0; i < temp.length; i++) { - if(password == null) + if(p.name === this.users[i].name) { - return (users.length < capacity); + } else { - return (users.length < capacity) && (p === password); + temp.push(this.users[i]); } - }, - /** - * starts new round for the room -- called once all the players have submitted - */ - newRound: function() + } + + this.users = temp; + + //if room is empty remove the room from rooms list + if(this.users.length == 0) { - console.log("new round started"); - if(words.length == 0) - { - state == 4; - } - else + console.log("room scrubbed"); + delete rooms[this.roomName]; + } + + this.update(); + } + + /** + * Whether or not a user can join this room -- checks for number of people are + * already in the room and the password + * @param p + * @returns {boolean} + */ + canJoin(p) + { + if(this.password == null) + { + return (this.users.length < this.capacity); + } + else + { + return (this.users.length < this.capacity) && (p === this.password); + } + } + + /** + * starts new round for the room -- called once all the players have submitted + */ + newRound() + { + console.log("new round started"); + if(this.words.length == 0) + { + this.state == 4; + } + else + { + this.currentRound++; + this.users.forEach(function(u) { - currentRound++; - users.forEach(function(u) - { - u.submission = ''; - }); - currentWord = words.pop(); - state = 2; - } - this.sendRoomUpdate(); - }, - //updates room variables - update: function() + u.submission = ''; + }); + this.currentWord = this.words.pop(); + this.state = 2; + } + this.sendRoomUpdate(); + } + + //updates room variables + update() + { + switch(this.state) { - switch(state) + case 1: //waiting for users to join { - case 1: //waiting for users to join + if(this.users.length == this.capacity) { - if(users.length == capacity) - { - this.newRound(); - } - break; + this.newRound(); } - case 2: // waiting for responses + break; + } + case 2: // waiting for responses + { + var flag = true; + var test = ""; + this.users.forEach(function(u) { - var flag = true; - var test = ""; - users.forEach(function(u) - { - test+=u.submission; - if(u.submission === '') - { - flag = false; - } - }); - console.log("big stuff " + test); - if(flag) + test+=u.submission; + if(u.submission === '') { - state = 3; - this.newRound(); - // setTimeout(function() { - // - // }, 4000); + flag = false; } - break; - } - case 3: // showing results -- time out fnc - { - console.log("error &&&&&&&&&&&&&&&&&&"); - break; - } - case 4: //game over display final result - { - //sqlStuff.dumpRoom(this); - break; - } - default: + }); + console.log("big stuff " + test); + if(flag) { - console.log("You don goof up") + this.state = 3; + this.newRound(); + // setTimeout(function() { + // + // }, 4000); } + break; + } + case 3: // showing results -- time out fnc + { + console.log("error &&&&&&&&&&&&&&&&&&"); + break; + } + case 4: //game over display final result + { + //sqlStuff.dumpRoom(this); + break; + } + default: + { + console.log("You don goof up") } - console.log(state + " state"); - this.sendRoomUpdate(); } - }; + console.log(this.state + " state"); + this.sendRoomUpdate(); + } +} + +module.exports = Room; \ No newline at end of file diff --git a/googletrendsgame/server/server.js b/googletrendsgame/server/server.js index 3f3bef0..458e341 100644 --- a/googletrendsgame/server/server.js +++ b/googletrendsgame/server/server.js @@ -10,348 +10,31 @@ const serverUtils = require('./serverUtils.js'); //used for the getting the word array const utils = require("./utils.js"); -//gets the trending data -const trendingAPI = require("./trendsAPI.js"); - -//const sqlStuff = require("./sql.js"); - -/** - * Object used for storing rooms - * @param capacityP -- the number of people that can be in room - * @param pass -- the room password -- null if none - * @param owner -- the person who is creating the room - */ -var room = function(capacityP, pass, owner) -{ - //max capacity of room -- default is 4 for now - this.capacity = capacityP; - - //name of the room - this.roomName = owner.name; - - //list of words used in the game - //7 for now will change later to be room specific - this.words = utils.getRandomWords(7); - - this.currentWord = this.words.pop(); - - //list players -- so we can push requests to them - this.users = []; - - //increments when rounds pass - this.currentRound = 0; - - // the password of the room -- null if no password - this.password = pass; - - /** - 1 = Waiting for users - 2 = Word shown, Waiting for response from users - 3 = Showing Result - 4 = Game Over, Display Final Results - */ - this.state = 1; - - /** - * creates json to send in the 'roomUpdate' socket event - * - * {users: gameState: roundWinner: currentWord: } - */ - this.generateRoomUpdate = function() - { - var result = new Object(); - - result.users = []; - - this.users.forEach(function(u) - { - result.users.push(u.genJASON()); - }); - - //sort the users based on score - var countOuter = 0; - var countInner = 0; - var countSwap = 0; - - // var swapped; - // do - // { - // countOuter++; - // swapped = false; - // for(var i = 0; i < result.users.length; i++) - // { - // countInner++; - // if(result.users[i].score && result.users[i + 1].score && - // result.users[i].score > result.users[i + 1].score) - // { - // countSwap++; - // var temp = result.users[i]; - // result.users[i] = result.users[j]; - // result.users[j] = temp; - // swapped = true; - // } - // } - // } while(swapped); - - - result.gameState = this.state; - - - //sets round winner - var rWinner = -1; - - for(var i = 0; i < this.users.length; i++) - { - if(rWinner < this.users[i].roundScore) - { - result.roundWinner = this.users[i].name; - rWinner = this.users[i].roundScore; - } - } - - result.currentWord = this.currentWord; - - return result; - } - - /** - * grabs roomUpdate json and beams it to every user in the channel - */ - this.sendRoomUpdate = function() - { - var message = this.generateRoomUpdate(); - this.users.forEach(function(u) - { - //console.log("room update called"); - u.socket.emit('roomUpdate', message); - //console.log(message); - }); - } - - /** - * adds a user to a room - * @param p - * return 0 if they could join - */ - this.addUser = function(player) - { - //console.log("user added"); - //check if room is not full - this.users.push(player); - player.room = this; - - if(this.users.length == this.capacity) - { - this.state = 2; - } - - console.log("user added to room " + player.name); - //console.log(this.users); - - this.update(); - } +//const sqlStuff = require("./sql.js"); - /** - * Removes a specific user from the room and adjusts the size of the array - * if the array is empty, the room closes - * @param p - */ - this.removeUser = function(p) - { - console.log("remove users fnc called"); - var temp = new Array(); - - for(var i = 0; i < temp.length; i++) - { - if(p.name === this.users[i].name) - { - - } - else - { - temp.push(this.users[i]); - } - } - - this.users = temp; - - //if room is empty remove the room from rooms list - if(this.users.length == 0) - { - console.log("room scrubbed"); - delete rooms[this.roomName]; - } - - this.update(); - } - - /** - * Whether or not a user can join this room -- checks for number of people are - * already in the room and the password - * @param p - * @returns {boolean} - */ - this.canJoin = function(p) - { - if(this.password == null) - { - return (this.users.length < this.capacity); - } - else - { - return (this.users.length < this.capacity) && (p === this.password); - } - } - - /** - * starts new round for the room -- called once all the players have submitted - */ - this.newRound = function() - { - console.log("new round started"); - if(this.words.length == 0) - { - this.state == 4; - } - else - { - this.currentRound++; - this.users.forEach(function(u) - { - u.submission = ''; - }); - this.currentWord = this.words.pop(); - this.state = 2; - } - this.sendRoomUpdate(); - } - - //updates room variables - this.update = function() - { - switch(this.state) - { - case 1: //waiting for users to join - { - if(this.users.length == this.capacity) - { - this.newRound(); - } - break; - } - case 2: // waiting for responses - { - var flag = true; - var test = ""; - this.users.forEach(function(u) - { - test+=u.submission; - if(u.submission === '') - { - flag = false; - } - }); - console.log("big stuff " + test); - if(flag) - { - this.state = 3; - this.newRound(); - // setTimeout(function() { - // - // }, 4000); - } - break; - } - case 3: // showing results -- time out fnc - { - console.log("error &&&&&&&&&&&&&&&&&&"); - break; - } - case 4: //game over display final result - { - //sqlStuff.dumpRoom(this); - break; - } - default: - { - console.log("You don goof up") - } - } - console.log(this.state + " state"); - this.sendRoomUpdate(); - } - this.addUser(owner); - -} - - -var player = function(s) -{ - //name of the user - this.name = null; - - //players socket - this.socket = s; - - //score of the player - this.score = 0; +var Room = require("./room"); - //reference to the room -- might not need this - this.room = null; +var Player = require("./player"); - //the word the user selected for current round - this.submission = ''; +//import {Room} from 'room.js'; - this.roundScore = 0; +//import {Player} from 'player.js'; - //logs the user data so we can record it to data base at end of round - this.log = []; - /** - * generate the json object used in 'roomUpdate' socket io event - * - * return {name: score: word:} - */ - this.genJASON = function() - { - var result = new Object(); - result.name = this.name; - result.score = this.score; - result.word = this.submission; - - return result; - } - - /** - * data -- literally a string - * @param data - */ - this.selectWord = function(data) - { - var w = data + " " + this.room.currentWord; - this.sumbission = data; - - //console.log(w); - - this.room.update(); +//list of all players --accessed using names like a dic +var players = {}; - return new Promise(function(resolve, reject) - { - trendingAPI.getPopularity(w).then(function(result) - { - console.log("api result for " + result + w); - resolve(result); - }).catch(function(err){ - console.log(err); - }) - }); +//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; /** @@ -398,17 +81,6 @@ var generateSendRoomsJSON = function() return obj; } -//list of all players --accessed using names like a dic -var players = {}; - -//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; app.get('/', function(req, res) @@ -420,7 +92,7 @@ app.get('/', function(req, res) //Whenever someone connects this gets executed io.on('connection', function(socket) { - var p = new player(socket); + var p = new Player(socket); console.log('A user connected'); @@ -461,7 +133,7 @@ io.on('connection', function(socket) console.log(data + "create room"); // console.log(data); // console.log(" "); - rooms[p.name] = new room(data.capacity, data.password, p); + rooms[p.name] = new Room(data.capacity, data.password, p); //sends updated room list to all users not in a room var dd = generateSendRoomsJSON(); @@ -544,7 +216,7 @@ io.on('connection', function(socket) }); }); -http.listen(port, function() +http.listen(PORT, function() { console.log('listening on *:3000'); });