diff --git a/README.md b/README.md index 1e37786..c0cdc3f 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Guess the trends beat your friends +![alt text](http://www.devhumor.com/content/uploads/images/August2017/java-javascript.jpg) + ## Server dependencies diff --git a/googletrendsgame/server/index.html b/googletrendsgame/server/index.html index 9a36dd9..25ce7e4 100644 --- a/googletrendsgame/server/index.html +++ b/googletrendsgame/server/index.html @@ -7,14 +7,26 @@ Hello world \ No newline at end of file diff --git a/googletrendsgame/server/server.js b/googletrendsgame/server/server.js index 5dc1a79..f616c39 100644 --- a/googletrendsgame/server/server.js +++ b/googletrendsgame/server/server.js @@ -13,6 +13,8 @@ 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 @@ -40,7 +42,7 @@ var room = function(capacityP, pass, owner) this.currentRound = 0; // the password of the room -- null if no password - this.password = null; + this.password = pass; /** 1 = Waiting for users @@ -71,25 +73,25 @@ var room = function(capacityP, pass, owner) 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); + 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; @@ -146,10 +148,10 @@ var room = function(capacityP, pass, owner) console.log("rooms users"); console.log(this.users); - this.sendRoomUpdate(); + this.update(); } - this.addUser(owner); + /** @@ -265,6 +267,7 @@ var room = function(capacityP, pass, owner) } case 4: //game over display final result { + //sqlStuff.dumpRoom(this); break; } default: @@ -274,6 +277,7 @@ var room = function(capacityP, pass, owner) } this.sendRoomUpdate(); } + this.addUser(owner); } @@ -297,6 +301,9 @@ var player = function(s) 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 * @@ -319,9 +326,14 @@ var player = function(s) this.selectWord = function(data) { this.sumbission = data; - - trendingAPI.getPopularity(data + " " + this.room.currentWord).then(function(result) + var w = data + " " + this.room.currentWord; + trendingAPI.getPopularity(w).then(function(result) { + var obj = new Object(); + obj.word = w; + obj.score = result; + this.log.push(obj); + this.roundScore = result; this.score += result; console.log("api result for " + result); @@ -404,13 +416,15 @@ io.on('connection', function(socket) /** *Register user nickname/handle (register) Client => Server */ - socket.on('register', function(data) { + socket.on('register', function(data) + { console.log("Register event called"); console.log(data); console.log(" "); //checks for user name in use - if(serverUtils.userAvailable(data, players)) + //if(serverUtils.userAvailable(data, players)) + if(!(data in players)) { p.name = data; @@ -433,19 +447,39 @@ io.on('connection', function(socket) *Create Room (createRoom) Client => Server * data {password: , capacity: } */ - socket.on('createRoom', function(data) { + socket.on('createRoom', function(data) + { console.log("create room event called"); console.log(data); console.log(" "); rooms[p.name] = new room(data.capacity, data.password, p); + //sends updated room list to all users not in a room + var dd = generateSendRoomsJSON(); + + Object.keys(players).forEach(function(key) + { + if(players[key] != null) + { + if(players[key].room == null) + { + players[key].socket.emit('sendRooms', dd); + } + } + else + { + console.log("player was null Bad!"); + } + }); + }); /** * Room Selection (joinRoom) Client => Server * data {roomName: , password: } */ - socket.on('joinRoom', function(data) { + socket.on('joinRoom', function(data) + { console.log("join room event called"); console.log(data); console.log(" "); @@ -467,7 +501,8 @@ io.on('connection', function(socket) /** * data -- literally a string */ - socket.on('submitWord', function(data) { + socket.on('submitWord', function(data) + { console.log("submitWord called"); console.log(data); console.log(" "); @@ -476,7 +511,8 @@ io.on('connection', function(socket) }); //Whenever someone disconnects - socket.on('disconnect', function () { + socket.on('disconnect', function () + { console.log('A user disconnected'); if(rooms[p.name] != null) @@ -496,6 +532,7 @@ io.on('connection', function(socket) }); }); -http.listen(port, function() { +http.listen(port, function() +{ console.log('listening on *:3000'); }); diff --git a/googletrendsgame/server/sql.js b/googletrendsgame/server/sql.js index e828b07..b63da2f 100644 --- a/googletrendsgame/server/sql.js +++ b/googletrendsgame/server/sql.js @@ -4,6 +4,18 @@ const sanitizer = require('sanitizer'); var Promise = require('promise'); +const con = mysql.createConnection({ + host: "localhost", + user: "trendingUser", + password: "password", + database: "googleTrends" +}); + +con.connect(function(err) { + if (err) throw err; + console.log("Connected to database!"); +}); + module.exports= { /** @@ -38,7 +50,26 @@ module.exports= { return new Promise(function(resolve, reject) { - var q = ""; + var q = "insert into users(name) values('" + player.name + "')"; + this.insert(q).then(function(keyId) + { + player.log.forEach(function(data) + { + var q2 = "insert into data(user_id, word, score) values " + + "('" + keyId + "','" + data.word + "','" + data.score + "')"; + + this.insert(q2); + }); + resolve(); + }) }) + }, + + dumpRoom: function(room) + { + room.users.forEach(function(user) + { + this.insertData(user); + }); } }; \ No newline at end of file