-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #313 from novoda/BOT-296/socket_io_client
BOT-296/Server Modules
- Loading branch information
Showing
22 changed files
with
670 additions
and
530 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
function BotLocator(rooms) { | ||
this.rooms = rooms; | ||
} | ||
module.exports = function (locationRooms) { | ||
|
||
BotLocator.prototype.locateFirstAvailableBotIn = function(room) { | ||
var botsInRoom = this.rooms[room]; | ||
|
||
if(!botsInRoom) { | ||
return; | ||
const roomsThatMatch = function (toMatch) { | ||
return function (room) { | ||
return toMatch == room; | ||
}; | ||
} | ||
|
||
for (socketId in botsInRoom.sockets) { | ||
var socketsInBotRoom = this.rooms[socketId]; | ||
const roomsThatContainProperty = function (property) { | ||
return function (roomName) { | ||
return locationRooms[roomName].hasOwnProperty(property); | ||
}; | ||
} | ||
|
||
if(botNotConnectedToHuman(socketsInBotRoom)) { | ||
return socketId; | ||
} | ||
const roomsThatAreNotAlreadyConnectedToOtherSockets = function () { | ||
return function (roomName) { | ||
return locationRooms[roomName].length == 1; | ||
}; | ||
} | ||
} | ||
|
||
function botNotConnectedToHuman(socketsInBotRoom) { | ||
return socketsInBotRoom.length === 1; | ||
} | ||
const asEmptyBotRoom = function () { | ||
return function (locationRoom) { | ||
return Object.keys(locationRooms[locationRoom].sockets) | ||
.filter(roomsThatContainProperty('length')) | ||
.filter(roomsThatAreNotAlreadyConnectedToOtherSockets()) | ||
.pop(); | ||
}; | ||
} | ||
|
||
module.exports = BotLocator; | ||
return { | ||
locateFirstAvailableBotIn: function (locationRoom) { | ||
return Object.keys(locationRooms) | ||
.filter(roomsThatMatch(locationRoom)) | ||
.filter(roomsThatContainProperty('sockets')) | ||
.map(asEmptyBotRoom()) | ||
.pop(); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
ClientType = { | ||
BOT : "bot", | ||
HUMAN : "human", | ||
TEST : "test", | ||
from : function(rawClientType) { | ||
for(var key in this) { | ||
if(ClientType[key] == rawClientType) { | ||
module.exports = ClientType = { | ||
BOT: 'bot', | ||
HUMAN: 'human', | ||
TEST: 'test', | ||
from: function (rawClientType) { | ||
for (let key in this) { | ||
if (ClientType[key] == rawClientType) { | ||
return ClientType[key]; | ||
} | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
module.exports = ClientType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module.exports = function Disconnector(rooms, connectedClients) { | ||
|
||
const roomsThatMatch = function (toMatch) { | ||
return function (room) { | ||
return toMatch == room; | ||
}; | ||
} | ||
|
||
const roomsThatContainProperty = function (property) { | ||
return function (roomName) { | ||
return rooms[roomName].hasOwnProperty(property); | ||
}; | ||
} | ||
|
||
const thosePresentIn = function (objectToSearch) { | ||
return function (key) { | ||
return objectToSearch[key] | ||
}; | ||
} | ||
|
||
const asConnectedClient = function () { | ||
return function (key) { | ||
return connectedClients[key]; | ||
}; | ||
} | ||
|
||
const connectedClientsThatContainProperty = function (property) { | ||
return function (connectedClient) { | ||
return connectedClient.hasOwnProperty(property); | ||
} | ||
} | ||
|
||
const disconnectClient = function () { | ||
return function (connectedClient) { | ||
connectedClient.disconnect(); | ||
} | ||
} | ||
|
||
return { | ||
disconnectRoom: function (roomName) { | ||
Object.keys(rooms) | ||
.filter(roomsThatMatch(roomName)) | ||
.filter(roomsThatContainProperty('sockets')) | ||
.map(function (roomKey) { | ||
return Object.keys(rooms[roomKey].sockets) | ||
.filter(thosePresentIn(connectedClients)) | ||
.map(asConnectedClient()) | ||
.filter(connectedClientsThatContainProperty('disconnect')) | ||
.every(disconnectClient()); | ||
}); | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module.exports = function Mover(clientsAndRooms, emitter) { | ||
|
||
const emitToRoom = function (direction) { | ||
return function (room) { | ||
emitter.to(room).emit('direction', direction); | ||
} | ||
} | ||
|
||
return { | ||
moveIn: function (clientId, direction) { | ||
const rooms = clientsAndRooms[clientId]; | ||
|
||
Object.keys(rooms || {}) | ||
.every(emitToRoom(direction)); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = function Observer() { | ||
return { | ||
notify: function (eventName, eventData) { | ||
return true; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const ClientType = require('./clientType.js') | ||
|
||
module.exports = function Router(botLocator) { | ||
return { | ||
route: function (query, next) { | ||
const roomName = query.room; | ||
const rawClientType = query.clientType; | ||
const clientType = ClientType.from(rawClientType); | ||
|
||
switch (clientType) { | ||
case ClientType.BOT: | ||
return next(); | ||
case ClientType.HUMAN: | ||
const availableBot = botLocator.locateFirstAvailableBotIn(roomName); | ||
|
||
if (availableBot) { | ||
query.room = availableBot; | ||
return next(); | ||
} else { | ||
return next(new Error('No bots available')); | ||
} | ||
|
||
default: | ||
return next(new Error('Unrecognised clientType: ' + rawClientType)); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,4 @@ | ||
var io = require('socket.io').listen(5000); | ||
var ClientType = require("./clientType.js"); | ||
var LoggingClient = require("./loggingClient.js"); | ||
var BotLocator = require("./botLocator.js"); | ||
const ServerCreator = require('./serverCreator'); | ||
|
||
var testClient = new LoggingClient(); | ||
var botLocator = new BotLocator(io.sockets.adapter.rooms); | ||
|
||
io.use(function(client, next){ | ||
|
||
var roomName = client.handshake.query.room; | ||
var rawClientType = client.handshake.query.clientType; | ||
var clientType = ClientType.from(rawClientType); | ||
|
||
switch(clientType) { | ||
case ClientType.TEST: | ||
case ClientType.BOT: | ||
return next(); | ||
case ClientType.HUMAN: | ||
var human = client; | ||
var availableBot = botLocator.locateFirstAvailableBotIn(roomName); | ||
|
||
if(availableBot) { | ||
human.handshake.query.room = availableBot; | ||
return next(); | ||
} else { | ||
return next(new Error('No bots available')); | ||
} | ||
default: | ||
return next(new Error('Unrecognised clientType: ' + rawClientType)); | ||
|
||
} | ||
|
||
}); | ||
|
||
io.sockets.on('connection', function (client) { | ||
|
||
var roomName = client.handshake.query.room; | ||
var rawClientType = client.handshake.query.clientType; | ||
var clientType = ClientType.from(rawClientType); | ||
|
||
switch(clientType) { | ||
case ClientType.HUMAN: | ||
client.join(roomName); | ||
testClient.emit('connected_human', asRoomsWithSocketIds()); | ||
break; | ||
case ClientType.BOT: | ||
client.join(roomName); | ||
testClient.emit('connected_bot', asRoomsWithSocketIds()); | ||
break; | ||
case ClientType.TEST: | ||
console.log('switching test client'); | ||
testClient = new LoggingClient(client); | ||
testClient.emit('connected', asRoomsWithSocketIds()); | ||
break; | ||
default: | ||
throw 'Unexpected rawClientType: ' + clientType; | ||
} | ||
|
||
client.on('disconnect', function() { | ||
disconnectRoom(client.id); | ||
testClient.emit('disconnected_human', asRoomsWithSocketIds()); | ||
testClient.emit('disconnected_bot', asRoomsWithSocketIds()); | ||
}); | ||
|
||
client.on('move_in', function(direction) { | ||
var rooms = Object.keys(io.sockets.adapter.sids[client.id]); | ||
for(var i = 0; i < rooms.length; i++) { | ||
io.to(rooms[i]).emit('direction', direction); | ||
testClient.emit('direction', direction); | ||
} | ||
}); | ||
}); | ||
|
||
function disconnectRoom(name) { | ||
var room = io.sockets.adapter.rooms[name]; | ||
|
||
if(!room) { | ||
return; | ||
} | ||
|
||
var clients = io.sockets.adapter.rooms[name].sockets; | ||
|
||
for(var client in clients) { | ||
var connectedClient = io.sockets.connected[client]; | ||
connectedClient.disconnect(); | ||
} | ||
} | ||
|
||
function asRoomsWithSocketIds() { | ||
var roomNames = Object.keys(io.sockets.adapter.rooms); | ||
var roomsWithSockets = {}; | ||
|
||
for(var i = 0; i < roomNames.length; i++) { | ||
var room = io.sockets.adapter.rooms[roomNames[i]]; | ||
roomsWithSockets[roomNames[i]] = Object.keys(room.sockets); | ||
} | ||
|
||
return roomsWithSockets; | ||
} | ||
const serverCreator = new ServerCreator(); | ||
serverCreator.create(); |
Oops, something went wrong.