mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
added checkpoint output, setting up a channel
This commit is contained in:
parent
dd154aa576
commit
c2ed00cb9b
10 changed files with 115 additions and 79 deletions
|
|
@ -26,6 +26,7 @@ function (http, nodeStatic) {
|
|||
switch(true) {
|
||||
case req.url == '/':
|
||||
fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
|
||||
console.checkpoint('HTTP Server serves index');
|
||||
break;
|
||||
|
||||
case req.url == '/client.js':
|
||||
|
|
@ -56,6 +57,7 @@ function (http, nodeStatic) {
|
|||
}
|
||||
);
|
||||
this.server.listen(options.port);
|
||||
console.checkpoint('start HTTP server');
|
||||
}
|
||||
|
||||
HttpServer.prototype.getServer = function () {
|
||||
|
|
|
|||
|
|
@ -23,8 +23,11 @@ function (io) {
|
|||
});
|
||||
|
||||
this.socket.on('connection', function (user) {
|
||||
console.checkpoint('socket receiving connection');
|
||||
self.onConnection(user);
|
||||
});
|
||||
|
||||
console.checkpoint('start Socket Listener');
|
||||
}
|
||||
|
||||
Socket.prototype.onConnection = function (socketLink) {
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ function (ProtocolHelper, GameController) {
|
|||
if(this.gameController) this.gameController.destruct();
|
||||
this.gameController = null;
|
||||
console.log('disconnected. game destroyed. no auto-reconnect');
|
||||
document.body.style.backgroundColor = '#aaaaaa';
|
||||
}
|
||||
|
||||
Networker.prototype.join = function (channelName) {
|
||||
|
|
|
|||
|
|
@ -5,56 +5,56 @@
|
|||
|
||||
function (GameController, NotificationCenter) {
|
||||
|
||||
function Channel (pipeToLobby) {
|
||||
function Channel (pipeToLobby, name) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.pipeToLobby = pipeToLobby;
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
|
||||
this.pipeToLobby = pipeToLobby;
|
||||
this.gameController = new GameController(this);
|
||||
|
||||
this.gameController.loadLevel("default.json");
|
||||
|
||||
//this.pipeToLobby.receive = function (message) { self.onMessage(message) };
|
||||
// !!! This should be done differently - use NotificationCenter.on('channel/dungeon/message') instead
|
||||
|
||||
|
||||
this.users = {};
|
||||
|
||||
this.gameController = new GameController();
|
||||
this.gameController.loadLevel("default.json");
|
||||
/*
|
||||
var self = this;
|
||||
NotificationCenter.on("processGameCommandFromUser", function (topic, args) {
|
||||
self.processGameCommandFromUser.apply(self, args);
|
||||
});
|
||||
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
Channel.validateName = function (name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Messages look like:
|
||||
// {channel: {setName: 'foo'}}
|
||||
// {user: {jupm: null}, id: 12}
|
||||
Channel.prototype.onMessage = function (message) {
|
||||
|
||||
for(var recipient in message) {
|
||||
|
||||
switch(recipient) {
|
||||
// Messages look like:
|
||||
// {channel: {setName: 'foo'}}
|
||||
// {user: {jupm: null}, id: 12}
|
||||
NotificationCenter.on('channel/message', function (message){
|
||||
|
||||
switch(message.recipient) {
|
||||
case 'user':
|
||||
this.forward(this.users[message.id], message.user);
|
||||
self.forward(self.users[message.id], message.user);
|
||||
break;
|
||||
case 'id': // Do nothing, it is needed by the user
|
||||
break;
|
||||
case 'channel':
|
||||
this.forward(this, message.channel);
|
||||
self.forward(self, message.channel);
|
||||
break;
|
||||
default:
|
||||
throw 'unknown recipient';
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
console.checkpoint('channel ' + name + ' created');
|
||||
}
|
||||
|
||||
Channel.validateName = function (name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Channel.prototype.forward = function (target, message) {
|
||||
for(var command in message) {
|
||||
|
|
@ -65,24 +65,19 @@
|
|||
}
|
||||
}
|
||||
};
|
||||
|
||||
Channel.prototype.setName = function (name) {
|
||||
this.name = name;
|
||||
console.log(' created channel ' + name);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Channel.prototype.addUser = function (user) {
|
||||
var userIds = Object.keys(this.users);
|
||||
|
||||
//var userIds = Object.keys(this.users);
|
||||
console.log('addUser', user);
|
||||
this.users[user.id] = user;
|
||||
|
||||
user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds});
|
||||
this.sendCommandToAllUsersExcept('userJoined', user.id, user);
|
||||
//user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds});
|
||||
//this.sendCommandToAllUsersExcept('userJoined', user.id, user);
|
||||
|
||||
NotificationCenter.trigger('user/joined', user);
|
||||
//NotificationCenter.trigger('user/joined', user);
|
||||
}
|
||||
|
||||
/*
|
||||
Channel.prototype.releaseUser = function (user) {
|
||||
this.gameController.userIdLeft(user.id);
|
||||
|
||||
|
|
|
|||
|
|
@ -9,16 +9,19 @@ define([
|
|||
|
||||
function (Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
|
||||
|
||||
function GameController () {
|
||||
function GameController (channel) {
|
||||
Parent.call(this, new PhysicsEngine());
|
||||
|
||||
this.inputControllers = {};
|
||||
this.channel = channel;
|
||||
|
||||
//this.update();
|
||||
//this.updateWorld();
|
||||
|
||||
//NotificationCenter.on('user/joined', this.userJoined, this);
|
||||
//NotificationCenter.on('user/left', this.userLeft, this);
|
||||
|
||||
console.checkpoint('starting game controller for channel ' + channel.name);
|
||||
}
|
||||
|
||||
GameController.prototype = Object.create(Parent.prototype);
|
||||
|
|
|
|||
|
|
@ -16,19 +16,21 @@ function (NotificationCenter, Channel) {
|
|||
|
||||
process.on('message', function (message, handle) {
|
||||
|
||||
switch(message.data) {
|
||||
case 'CREATE':
|
||||
self.channel = new Channel(self);
|
||||
break;
|
||||
for(var method in message.data) {
|
||||
switch(method) {
|
||||
case 'CREATE':
|
||||
self.channel = new Channel(this, message.data[method]);
|
||||
break;
|
||||
|
||||
case 'KILL':
|
||||
self.channel.destroy();
|
||||
process.exit(0);
|
||||
break;
|
||||
case 'KILL':
|
||||
self.channel.destroy();
|
||||
process.exit(0);
|
||||
break;
|
||||
|
||||
default:
|
||||
self.onMessage(message);
|
||||
break;
|
||||
default:
|
||||
self.onMessage(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -43,7 +45,7 @@ function (NotificationCenter, Channel) {
|
|||
};
|
||||
|
||||
PipeToLobby.prototype.onMessage = function (message) {
|
||||
NotificationCenter.trigger(message.recipient + '/message', message.data);
|
||||
NotificationCenter.trigger(message.recipient + '/message', message);
|
||||
}
|
||||
|
||||
return PipeToLobby;
|
||||
|
|
|
|||
|
|
@ -8,25 +8,29 @@ define([
|
|||
function (User, Channel, PipeToChannel, NotificationCenter) {
|
||||
|
||||
function Coordinator () {
|
||||
this.channels = {};
|
||||
this.channelPipes = {};
|
||||
this.lobbyUsers = {};
|
||||
|
||||
console.checkpoint('create Coordinator');
|
||||
}
|
||||
|
||||
Coordinator.prototype.createUser = function (socketLink) {
|
||||
var user = new User(socketLink, this);
|
||||
console.checkpoint('creating user');
|
||||
this.assignUserToLobby(user);
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToLobby = function (user) {
|
||||
if(user.channelProcess) {
|
||||
if(user.channelPipe) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
this.lobbyUsers[user.id] = user;
|
||||
console.checkpoint('assign user to lobby');
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToChannel = function (user, channelName) {
|
||||
|
||||
if(user.channelProcess) {
|
||||
if(user.channelPipe) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
|
||||
|
|
@ -35,40 +39,55 @@ function (User, Channel, PipeToChannel, NotificationCenter) {
|
|||
return false;
|
||||
}
|
||||
|
||||
var channel = this.channels[channelName];
|
||||
if(!channel) {
|
||||
channel = new PipeToChannel(channelName);
|
||||
this.channels[channelName] = channel;
|
||||
|
||||
NotificationCenter.on('channel/' + channelName + '/message', function (data) {
|
||||
channel.send('channel', data);
|
||||
}, this);
|
||||
|
||||
NotificationCenter.on('user/joined', function (user) {
|
||||
NotificationCenter.on('channel/' + channelName + '/user/' + user.id, function (recipient, data) {
|
||||
channel.send(recipient, data);
|
||||
}, this);
|
||||
}, this);
|
||||
|
||||
NotificationCenter.on('user/left', function (user) {
|
||||
|
||||
}, this);
|
||||
var channelPipe = this.channelPipes[channelName];
|
||||
if(!channelPipe) {
|
||||
this.createPipe(channelName);
|
||||
}
|
||||
|
||||
//channel.addUser(user);
|
||||
//user.setChannel(channel);
|
||||
NotificationCenter.trigger('user/joined', user);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
}
|
||||
|
||||
Coordinator.prototype.removeUser = function (user) {
|
||||
|
||||
user.channel.send('user/' + user.id + '/left');
|
||||
NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id);
|
||||
//user.channel.send('user/' + user.id + '/left');
|
||||
//NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
}
|
||||
|
||||
Coordinator.prototype.createPipe = function(channelName) {
|
||||
|
||||
var channelPipe = new PipeToChannel(channelName);
|
||||
this.channelPipes[channelName] = channelPipe;
|
||||
|
||||
|
||||
NotificationCenter.on('channel/' + channelName + '/message', function (data) {
|
||||
channelPipe.send('channel', data);
|
||||
}, this);
|
||||
|
||||
// sending info to user
|
||||
NotificationCenter.on('user/joined', function (user) {
|
||||
/*
|
||||
NotificationCenter.on('channel/' + channelName + '/user/' + user.id, function (recipient, data) {
|
||||
channelPipe.send(recipient, data);
|
||||
}, this);
|
||||
*/
|
||||
|
||||
channelPipe.send('channel', { addUser: user.id });
|
||||
|
||||
}, this);
|
||||
|
||||
NotificationCenter.on('user/left', function (user) {
|
||||
|
||||
}, this);
|
||||
|
||||
return channelPipe;
|
||||
};
|
||||
|
||||
return Coordinator;
|
||||
|
||||
});
|
||||
|
|
@ -1,24 +1,27 @@
|
|||
define([
|
||||
//"Game/Core/NotificationCenter",
|
||||
"Game/Core/NotificationCenter",
|
||||
"child_process"
|
||||
],
|
||||
|
||||
function (childProcess) {
|
||||
function (NotificationCenter, childProcess) {
|
||||
|
||||
var fork = childProcess.fork;
|
||||
|
||||
function PipeToChannel (channelName) {
|
||||
|
||||
this.channelProcess = null;
|
||||
this.channelPipe = null;
|
||||
|
||||
try {
|
||||
this.channelProcess = fork('channel.js');
|
||||
this.channelPipe = fork('channel.js');
|
||||
} catch (err) {
|
||||
throw 'Failed to fork channel! (' + err + ')';
|
||||
}
|
||||
|
||||
this.send('channel/' + channelName, 'CREATE');
|
||||
this.channelProcess.on('message', this.onMessage.bind(this));
|
||||
console.checkpoint('creating channel process for ' + channelName);
|
||||
|
||||
this.send('channel/' + channelName, { CREATE: channelName });
|
||||
|
||||
this.channelPipe.on('message', this.onMessage.bind(this));
|
||||
|
||||
var self = this;
|
||||
}
|
||||
|
|
@ -29,7 +32,7 @@ function (childProcess) {
|
|||
data: data
|
||||
}
|
||||
|
||||
this.channelProcess.send(message);
|
||||
this.channelPipe.send(message);
|
||||
}
|
||||
|
||||
PipeToChannel.prototype.onMessage = function (message) {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@ requirejs.config({
|
|||
|
||||
var inspector = {};
|
||||
|
||||
console.checkpoint = function (s) {
|
||||
console.log(' \033[34mbeep - \033[0m' + s);
|
||||
}
|
||||
|
||||
requirejs([
|
||||
"Game/Server/PipeToLobby",
|
||||
"Game/Core/NotificationCenter"
|
||||
|
|
|
|||
|
|
@ -18,6 +18,10 @@ var options = {
|
|||
logLevel: process.argv[3] || 0
|
||||
};
|
||||
|
||||
console.checkpoint = function (s) {
|
||||
console.log(' \033[34mbeep - \033[0m' + s);
|
||||
}
|
||||
|
||||
requirejs([
|
||||
"Bootstrap/HttpServer",
|
||||
"Bootstrap/Socket",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue