Browse Source

fixed server side bugs

master
Jeffery R 7 years ago
parent
commit
157bc26507
4 changed files with 116 additions and 34 deletions
  1. +2
    -0
      README.md
  2. +14
    -2
      googletrendsgame/server/index.html
  3. +68
    -31
      googletrendsgame/server/server.js
  4. +32
    -1
      googletrendsgame/server/sql.js

+ 2
- 0
README.md View File

@ -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

+ 14
- 2
googletrendsgame/server/index.html View File

@ -7,14 +7,26 @@
<script>
var socket = io();
socket.emit('register', 'jeffery');
socket.emit('register', 'jeff');
socket.on('sendRooms', function(data)
{
console.log("got rooms");
console.log(data);
document.write(data);
//document.write(data);
data.rooms.forEach(function(r)
{
document.write(r.name)
document.write("<br />");
});
var tt = new Object();
tt.roomName = "den";
tt.password = "pp";
socket.emit('joinRoom', tt);
});
</script>
<body>Hello world</body>
</html>

+ 68
- 31
googletrendsgame/server/server.js View File

@ -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');
});

+ 32
- 1
googletrendsgame/server/sql.js View File

@ -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);
});
}
};

Loading…
Cancel
Save