2015-06-15 19:03:12 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var express = require('express');
|
|
|
|
var serveStatic = require('serve-static');
|
|
|
|
var path = require('path');
|
|
|
|
|
|
|
|
var User = require("./User");
|
|
|
|
var Connections = require("./Connections");
|
|
|
|
var Battle = require("./Battle");
|
|
|
|
var Room = require("./Room");
|
|
|
|
|
|
|
|
module.exports.run = function(worker){
|
|
|
|
console.log(' >> Worker PID:', process.pid);
|
|
|
|
|
|
|
|
var app = require('express')();
|
|
|
|
|
|
|
|
var httpServer = worker.httpServer;
|
|
|
|
var scServer = worker.scServer;
|
|
|
|
|
|
|
|
app.use(serveStatic(path.resolve(__dirname, 'public')));
|
|
|
|
|
|
|
|
httpServer.on('request', app);
|
|
|
|
|
2015-06-18 14:54:44 +00:00
|
|
|
//var roomCollection = {};
|
|
|
|
global.connections = Connections(/*roomCollection*/);
|
2015-06-15 19:03:12 +00:00
|
|
|
|
|
|
|
scServer.on('connection', function(socket){
|
|
|
|
var user = User(socket);
|
|
|
|
connections.add(user);
|
|
|
|
console.log("new user ", user.getName());
|
|
|
|
|
|
|
|
socket.on("request:name", function(data){
|
|
|
|
if(data && data.name){
|
|
|
|
user.setName(data.name);
|
|
|
|
}
|
|
|
|
socket.emit("response:name", {name: user.getName()});
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:gameLoaded", function(data){
|
|
|
|
console.log(data);
|
2015-06-18 14:54:44 +00:00
|
|
|
connections.roomCollection[data._roomID].setReady(user);
|
2015-06-15 19:03:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:createRoom", function(){
|
2015-06-18 14:54:44 +00:00
|
|
|
if(user.getRoom()) return;
|
|
|
|
if(user._searching) return;
|
2015-06-15 19:03:12 +00:00
|
|
|
var room = Room(worker.getSCServer());
|
2015-06-18 14:54:44 +00:00
|
|
|
connections.roomCollection[room.getID()] = room;
|
2015-06-15 19:03:12 +00:00
|
|
|
room.join(user);
|
2015-06-18 14:54:44 +00:00
|
|
|
user._searching = true;
|
2015-06-15 19:03:12 +00:00
|
|
|
console.log("room %s created by %s", room.getID(), user.getName());
|
|
|
|
user.send("response:createRoom", room.getID());
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:joinRoom", function(){
|
2015-06-18 14:54:44 +00:00
|
|
|
if(user._searching) return;
|
|
|
|
user._searching = true;
|
2015-06-15 19:03:12 +00:00
|
|
|
console.log("joinroom");
|
|
|
|
var interval = setInterval(function(){
|
2015-06-18 14:54:44 +00:00
|
|
|
if(!user || user.disconnected) {
|
|
|
|
clearInterval(interval);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(var key in connections.roomCollection) {
|
|
|
|
var room = connections.roomCollection[key];
|
|
|
|
if(!room) continue;
|
2015-06-15 19:03:12 +00:00
|
|
|
if(!room.isOpen()) continue;
|
|
|
|
room.join(user);
|
|
|
|
clearInterval(interval);
|
2015-06-18 14:54:44 +00:00
|
|
|
user._searching = false;
|
2015-06-15 19:03:12 +00:00
|
|
|
console.log("user %s joined room %s", user.getName(), room.getID());
|
|
|
|
user.send("response:joinRoom", room.getID());
|
|
|
|
}
|
2015-06-18 13:31:36 +00:00
|
|
|
}, 500);
|
2015-06-15 19:03:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
socket.on("request:roomData", function(){
|
|
|
|
var room = user.getRoom();
|
|
|
|
var players = room.getPlayers();
|
|
|
|
user.send("response:roomData", {players: players});
|
|
|
|
})
|
|
|
|
|
|
|
|
socket.on('disconnect', function(){
|
|
|
|
connections.remove(user);
|
|
|
|
user.disconnect();
|
2015-06-18 14:54:44 +00:00
|
|
|
console.log("user ", user.getName(), " disconnected");
|
|
|
|
user = null;
|
2015-06-15 19:03:12 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|