|
@ -1,14 +1,16 @@ |
|
|
|
|
|
|
|
|
//list of all the rooms
|
|
|
//list of all the rooms
|
|
|
var rooms = []; |
|
|
|
|
|
|
|
|
var rooms = {}; |
|
|
|
|
|
|
|
|
var room = function(capacityP, roomN) |
|
|
|
|
|
|
|
|
var room = function(capacityP, pass, owner) |
|
|
{ |
|
|
{ |
|
|
//max capacity of room -- default is 4 for now
|
|
|
//max capacity of room -- default is 4 for now
|
|
|
this.capacity = capacityP; |
|
|
this.capacity = capacityP; |
|
|
|
|
|
|
|
|
//name of the room
|
|
|
//name of the room
|
|
|
this.roomName = roomN; |
|
|
|
|
|
|
|
|
this.roomName = owner.name; |
|
|
|
|
|
|
|
|
|
|
|
this.addUser(owner); |
|
|
|
|
|
|
|
|
//list of words used in the game
|
|
|
//list of words used in the game
|
|
|
this.words = []; |
|
|
this.words = []; |
|
@ -17,11 +19,13 @@ var room = function(capacityP, roomN) |
|
|
this.users = []; |
|
|
this.users = []; |
|
|
|
|
|
|
|
|
//increments when rounds pass
|
|
|
//increments when rounds pass
|
|
|
this.currentRoom = 0; |
|
|
|
|
|
|
|
|
this.currentRound = 0; |
|
|
|
|
|
|
|
|
// the password of the room -- null if no password
|
|
|
// the password of the room -- null if no password
|
|
|
this.password = null; |
|
|
this.password = null; |
|
|
|
|
|
|
|
|
|
|
|
this.state = 1; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* adds a user to a room |
|
|
* adds a user to a room |
|
|
* @param p |
|
|
* @param p |
|
@ -30,14 +34,8 @@ var room = function(capacityP, roomN) |
|
|
this.addUser = function(player) |
|
|
this.addUser = function(player) |
|
|
{ |
|
|
{ |
|
|
//check if room is not full
|
|
|
//check if room is not full
|
|
|
if(this.users.length != this.capacity) |
|
|
|
|
|
{ |
|
|
|
|
|
this.users.push(player) |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
this.users.push(player); |
|
|
|
|
|
player.room = this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -46,10 +44,44 @@ var room = function(capacityP, roomN) |
|
|
*/ |
|
|
*/ |
|
|
this.removeUser = function(p) |
|
|
this.removeUser = function(p) |
|
|
{ |
|
|
{ |
|
|
this.users.remove(p); |
|
|
|
|
|
|
|
|
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 room is empty remove the room from rooms list
|
|
|
rooms.remove(this); |
|
|
|
|
|
|
|
|
if(this.users.length == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
rooms[this.roomName] = null; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.generateRoomUpdate() |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.canJoin = function(p) |
|
|
|
|
|
{ |
|
|
|
|
|
if(this.password == null) |
|
|
|
|
|
{ |
|
|
|
|
|
return (this.users.length < this.capacity); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
return (this.users.length < this.capacity) && (p === this.password); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
@ -68,11 +100,11 @@ var player = function(s) |
|
|
//reference to the room -- might not need this
|
|
|
//reference to the room -- might not need this
|
|
|
this.room = null; |
|
|
this.room = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
this.sumbission = null; |
|
|
this.sumbission = null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var players = {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var app = require('express')(); |
|
|
var app = require('express')(); |
|
|
var http = require('http').Server(app); |
|
|
var http = require('http').Server(app); |
|
@ -80,7 +112,7 @@ var io = require('socket.io')(http); |
|
|
|
|
|
|
|
|
const port = 3000; |
|
|
const port = 3000; |
|
|
|
|
|
|
|
|
const utils = require('./serverUtils.js'); |
|
|
|
|
|
|
|
|
const serverUtils = require('./serverUtils.js'); |
|
|
|
|
|
|
|
|
//Whenever someone connects this gets executed
|
|
|
//Whenever someone connects this gets executed
|
|
|
io.on('connection', function(socket) |
|
|
io.on('connection', function(socket) |
|
@ -98,15 +130,18 @@ io.on('connection', function(socket) |
|
|
console.log(" "); |
|
|
console.log(" "); |
|
|
|
|
|
|
|
|
//checks for user name in use
|
|
|
//checks for user name in use
|
|
|
if(utils.userAvailable(data, rooms)) |
|
|
|
|
|
|
|
|
if(serverUtils.userAvailable(data, players)) |
|
|
{ |
|
|
{ |
|
|
player.name = data; |
|
|
player.name = data; |
|
|
|
|
|
|
|
|
|
|
|
players[data] = player; |
|
|
|
|
|
|
|
|
|
|
|
socket.emit('sendRooms', serverUtils.generateSendRoomsJSON(rooms)); |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
socket.emit('registerFailed', 'User name taken'); |
|
|
socket.emit('registerFailed', 'User name taken'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -117,6 +152,7 @@ io.on('connection', function(socket) |
|
|
console.log("create room event called"); |
|
|
console.log("create room event called"); |
|
|
console.log(data); |
|
|
console.log(data); |
|
|
console.log(" "); |
|
|
console.log(" "); |
|
|
|
|
|
rooms[player.name] = new room(data.capacity, data.password, player); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
@ -128,6 +164,15 @@ io.on('connection', function(socket) |
|
|
console.log("join room event called"); |
|
|
console.log("join room event called"); |
|
|
console.log(data); |
|
|
console.log(data); |
|
|
console.log(" "); |
|
|
console.log(" "); |
|
|
|
|
|
|
|
|
|
|
|
if(rooms[data.name].canJoin(data.password)) |
|
|
|
|
|
{ |
|
|
|
|
|
rooms[data.name].addUser(player); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
socket.emit('registerFailed', 'Failed connecting to room'); |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
socket.on('submitWord', function(data) { |
|
|
socket.on('submitWord', function(data) { |
|
@ -139,6 +184,18 @@ io.on('connection', function(socket) |
|
|
//Whenever someone disconnects this piece of code executed
|
|
|
//Whenever someone disconnects this piece of code executed
|
|
|
socket.on('disconnect', function () { |
|
|
socket.on('disconnect', function () { |
|
|
console.log('A user disconnected'); |
|
|
console.log('A user disconnected'); |
|
|
|
|
|
|
|
|
|
|
|
if(rooms[player.name] != null) |
|
|
|
|
|
{ |
|
|
|
|
|
rooms[player.name] = null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//leave the room
|
|
|
|
|
|
if(player.room != null) |
|
|
|
|
|
{ |
|
|
|
|
|
player.room.removeUser(player); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|