some more renaming

This commit is contained in:
logsol 2012-07-21 21:37:24 +02:00
parent 9de3147406
commit bd45f538c7
34 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,58 @@
define(["Chuck/ServerGame"], function(ServerGame) {
function Channel(name) {
this.name = name;
this.users = {};
this.serverGame = this.factory.new(ServerGame, this);
console.log("server game " + this.serverGame);
this.serverGame.loadLevel("default.json");
var self = this;
this.notificationCenter.on("processGameCommandFromUser", function(topic, args) {
self.processGameCommandFromUser.apply(self, args);
});
}
Channel.validateName = function(name){
return true;
}
Channel.prototype.addUser = function(user){
var userIds = Object.keys(this.users);
this.users[user.id] = user;
user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds});
this.sendCommandToAllUsersExcept('userJoined', user.id, user);
this.serverGame.createPlayerForUser(user)
}
Channel.prototype.releaseUser = function(user) {
this.serverGame.userIdLeft(user.id);
this.sendCommandToAllUsersExcept("userLeft", user.id, user);
delete this.users[user.id];
}
Channel.prototype.sendCommandToAllUsers = function(command, options) {
for(var id in this.users) {
this.users[id].sendCommand(command, options);
}
}
Channel.prototype.sendCommandToAllUsersExcept = function(command, options, except_user) {
for(var id in this.users) {
if (id != except_user.id) {
this.users[id].sendCommand(command, options);
}
}
}
Channel.prototype.processGameCommandFromUser = function(command, options, user) {
this.serverGame.progressGameCommandFromUser(command, options, user);
}
return Channel;
});

View file

106
app/Game/Server/GameController.js Executable file
View file

@ -0,0 +1,106 @@
var requires = [
"Chuck/Physics/Engine",
"Chuck/Settings",
"Chuck/Player",
"Vendor/Box2D",
"Chuck/Loader/Level",
"Chuck/Control/InputController",
"RequestAnimationFrame"
];
define(requires, function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame){
function ServerProcessor (serverGame) {
this.serverGame = serverGame;
this.players = {};
this.init();
}
ServerProcessor.prototype.init = function() {
this.physicsEngine = this.factory.new(PhysicsEngine);
this.update();
this.updateWorld();
}
ServerProcessor.prototype.loadLevel = function(path) {
if (this.level) {
this.level.unload();
}
this.level = new Level(path, this.physicsEngine);
this.level.loadLevelInToEngine();
}
ServerProcessor.prototype.getPhysicsEngine = function() {
return this.physicsEngine;
}
ServerProcessor.prototype.update = function() {
requestAnimFrame(this.update.bind(this));
this.physicsEngine.update();
for(var id in this.players) {
this.players[id].player.update();
}
}
ServerProcessor.prototype.destruct = function() {
}
ServerProcessor.prototype.createPlayerWithId = function(id) {
var player = new Player(this.physicsEngine, id, null);
this.players[id] = {
player: player,
inputController: new InputController(player)
};
player.spawn(100, 0);
this.physicsEngine.setCollisionDetector(player);
}
ServerProcessor.prototype.progressGameCommandFromId = function(command, options, id) {
var inputController = this.players[id].inputController;
if (typeof inputController[command] == 'function') {
inputController[command](options);
}
}
ServerProcessor.prototype.userIdLeft = function(id) {
var player = this.players[id].player;
player.destroy();
delete this.players[id];
}
ServerProcessor.prototype.updateWorld = function() {
var update = {};
var isUpdateNeeded = false;
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if(userData && body.IsAwake()){
update[userData] = {
p: body.GetPosition(),
a: body.GetAngle(),
lv: body.GetLinearVelocity(),
av: body.GetAngularVelocity()
};
isUpdateNeeded = true;
}
} while (body = body.GetNext());
if(isUpdateNeeded) {
//this.serverGame.updateClientsWorld(update);
this.notificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]);
}
setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
}
return ServerProcessor;
});

View file

@ -0,0 +1,53 @@
define(function() {
function NotificationCenter() {
this.topics = {};
this.subUid = -1;
}
NotificationCenter.prototype.trigger = function(topic, args) {
if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger.";
}
var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0;
while (len--) {
subscribers[len].func(topic, args);
}
return this;
}
NotificationCenter.prototype.on = function(topic, func) {
if (!this.topics[topic]) {
this.topics[topic] = [];
}
var token = ( ++this.subUid ).toString();
this.topics[topic].push({
token: token,
func: func
});
return token;
}
NotificationCenter.prototype.off = function(token) {
for(var m in this.topics) {
if (this.topics[m]) {
for(var i = 0, j = this.topics[m].length; i < j; i++) {
if (this.topics[m][i].token === token) {
this.topics[m].splice(i, 1);
return token;
}
}
}
}
return this;
}
return NotificationCenter;
});

89
app/Game/Server/User.js Normal file
View file

@ -0,0 +1,89 @@
define(["Protocol/Helper"], function(ProtocolHelper) {
function User(socketLink, coordinator) {
this.id = socketLink.id;
this.socketLink = socketLink;
this.coordinator = coordinator;
this.channel = null;
this.init(socketLink);
}
User.prototype.init = function(socketLink){
var self = this;
socketLink.on('message', function(message){
self.onMessage(message);
});
socketLink.on('disconnect', function(){
self.onDisconnect();
});
}
User.prototype.setChannel = function(channel) {
if (this.notificationCenter) {
this.notificationCenter.off("updateClientsWorld");
}
this.channel = channel;
// Use the right factory and nc
this.notificationCenter = this.channel.notificationCenter;
this.factory = this.channel.factory;
var self = this;
this.notificationCenter.on("sendCommandToAllUsers", function(topic, args) {
self.sendCommand.apply(self, args);
});
}
User.prototype.sendCommand = function(command, options) {
var message = ProtocolHelper.encodeCommand(command, options);
this.socketLink.send(message);
}
User.prototype.onMessage = function(message){
var self = this;
ProtocolHelper.runCommands(message, function(command, options){
self.processControlCommand(command, options);
});
}
User.prototype.onDisconnect = function(){
this.coordinator.removeUser(this);
}
User.prototype.processControlCommand = function(command, options){
switch(command) {
case 'join':
this.coordinator.assignUserToChannel(this, options);
break;
case 'leave':
this.coordinator.assignUserToLobby(this);
break;
case 'gameCommand':
for(var gameCommand in options) {
this.notificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]);
//this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this);
}
break;
default:
break;
}
}
User.prototype.toString = function() {
return "[User " + this.id + "]";
};
return User;
});