1
0
mirror of https://github.com/exane/not-gwent-online synced 2024-10-31 10:36:53 +00:00
not-gwent-online/server/worker.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

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);
//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);
connections.roomCollection[data._roomID].setReady(user);
2015-06-15 19:03:12 +00:00
})
socket.on("request:createRoom", function(){
if(user.getRoom()) return;
if(user._searching) return;
2015-06-15 19:03:12 +00:00
var room = Room(worker.getSCServer());
connections.roomCollection[room.getID()] = room;
2015-06-15 19:03:12 +00:00
room.join(user);
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(){
if(user._searching) return;
user._searching = true;
2015-06-15 19:03:12 +00:00
console.log("joinroom");
var interval = setInterval(function(){
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);
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();
console.log("user ", user.getName(), " disconnected");
user = null;
2015-06-15 19:03:12 +00:00
});
});
};