fixes #112 finished recorder

This commit is contained in:
Jeena 2015-03-15 21:06:44 +01:00
parent dfa71bc8e5
commit c88afc8b4c
11 changed files with 275 additions and 1765 deletions

View file

@ -9,6 +9,8 @@
function (GameController, Nc, User, ProtocolHelper, Options, Settings) {
"use strict";
function Channel (pipeToServer, options) {
var self = this;
@ -17,6 +19,7 @@
this.users = {};
this.pipeToServer = pipeToServer;
this.levelListIndex = -1;
this.gameController = null;
this.options = options = Options.merge(options, {
levelUids: Settings.CHANNEL_DEFAULT_LEVELS
@ -34,7 +37,7 @@
this.beginRound();
console.checkpoint('channel ' + this.name + ' created');
console.checkpoint("channel " + this.name + " created");
setTimeout(function() {
if(Object.keys(self.users).length < 1) {
@ -53,7 +56,7 @@
if(this.gameController) {
this.gameController.destroy();
delete this.gameController;
this.gameController = null;
}
var gameControllerOptions = {
@ -108,14 +111,14 @@
this.sendJoinSuccess(options);
this.users[options.id].sendControlCommand("beginRound", clientGameControllerOptions);
}
}
};
Channel.prototype.sendJoinSuccess = function(options) {
var user = new User(options.id, options);
var joinedUsers = [];
for(var userId in this.users) {
joinedUsers.push(this.users[userId].options)
joinedUsers.push(this.users[userId].options);
}
var levelUid = null;
@ -125,13 +128,13 @@
this.users[user.id] = user;
var options = {
options = {
user: user.options,
joinedUsers: joinedUsers,
levelUid: levelUid
};
//Nc.trigger('user/' + user.id + "/joinSuccess", options);
//Nc.trigger("user/" + user.id + "/joinSuccess", options);
user.sendControlCommand("joinSuccess", options);
Nc.trigger(Nc.ns.channel.events.user.joined, user);
@ -140,7 +143,6 @@
Channel.prototype.onReleaseUser = function (userId) {
var self = this;
var user = this.users[userId];
Nc.trigger(Nc.ns.channel.events.user.left, userId);
delete this.users[userId];
@ -158,7 +160,7 @@
}
}, Settings.CHANNEL_DESTRUCTION_TIME * 1000);
}
}
};
Channel.prototype.destroy = function() {
console.checkpoint("channel (" + this.name + ") destroyed");
@ -172,7 +174,7 @@
for(var id in this.users) {
this.users[id].sendControlCommand(command, options);
}
}
};
Channel.prototype.broadcastControlCommandExcept = function (command, options, exceptUser) {
for(var id in this.users) {
@ -180,13 +182,13 @@
this.users[id].sendControlCommand(command, options);
}
}
}
};
Channel.prototype.broadcastGameCommand = function (command, options) {
for(var id in this.users) {
this.users[id].sendGameCommand(command, options);
}
}
};
Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) {
for(var id in this.users) {
@ -194,7 +196,7 @@
this.users[id].sendGameCommand(command, options);
}
}
}
};
return Channel;

View file

@ -47,9 +47,9 @@ function(Parent, Nc, Parser, Settings) {
PlayerController.prototype.mePositionStateUpdate = function(update) {
if(!this.player.doll) {
console.warn("me state update, even though doll does not exist");
return;
if(!this.player.isSpawned()) {
// if someone still falls but is dead on the server already
return;
}
var difference = {

View file

@ -107,11 +107,11 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var update = this.getWorldUpdateObject(false);
if(Object.getOwnPropertyNames(update).length > 0) {
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update);
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "worldUpdate", update);
}
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.NETWORK_UPDATE_INTERVAL);
}
};
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
getSleeping = getSleeping || false;
@ -149,7 +149,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var spawnedPlayers = [];
for(var id in this.players) {
var player = this.players[id];
if(player.isSpawned) {
if(player.isSpawned()) {
var options = {
id: id,
@ -169,7 +169,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
};
GameController.prototype.getRuntimeItems = function() {
var objects = []
var objects = [];
for (var i = 0; i < this.gameObjects.animated.length; i++) {
if(this.gameObjects.animated[i] instanceof RagDoll) {
@ -182,7 +182,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
options: object.options
});
}
};
}
return objects;
};
@ -195,7 +195,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
worldUpdate: this.getWorldUpdateObject(true),
runtimeItems: this.getRuntimeItems(),
userId: userId
}
};
Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + userId, "clientReadyResponse", options);

View file

@ -2,11 +2,10 @@ define([
"Lib/Utilities/NotificationCenter",
"Game/Channel/Channel",
"Game/Config/Settings",
'fs',
'util',
"fs"
],
function (Nc, Channel, Settings, fs, util) {
function (Nc, Channel, Settings, fs) {
"use strict";
@ -17,32 +16,43 @@ function (Nc, Channel, Settings, fs, util) {
Nc.on(Nc.ns.channel.to.server.controlCommand.send, this.send, this);
process.on('message', this.onProcessMessage.bind(this));
process.on("message", this.onProcessMessage.bind(this));
}
PipeToServer.prototype.onProcessMessage = function (message, handle) {
PipeToServer.prototype.onProcessMessage = function (message, handle) { // jshint unused:false
if(message.data.hasOwnProperty('CREATE')) {
if(message.data.hasOwnProperty("CREATE")) {
this.channel = new Channel(this, message.data.options);
message.data.options.playingFileName = "Quickstart-1425229312283.log";
message.data.options.playingFileName = Settings.CHANNEL_PLAY_RECORDING;
if(message.data.options.playingFileName) {
var self = this;
setTimeout(function() {
console.log(message.data.options.playingFileName)
console.log(message.data.options.playingFileName);
self.play(message.data.options.playingFileName);
}, 2000);
}, 2000); // giving channel time to set everything up
}
if(Settings.CHANNEL_RECORD_SESSION) {
this.recordingFileName = Settings.CHANNEL_RECORDING_PATH + this.channel.name + "-" + Date.now() + ".log";
this.recordingFileName = Settings.CHANNEL_RECORDING_PATH +
this.channel.name +
"-" +
(new Date()).toISOString() +
"-" +
message.data.options.levelUids.join("_") +
".log";
if(!fs.existsSync(Settings.CHANNEL_RECORDING_PATH)) {
fs.mkdirSync(Settings.CHANNEL_RECORDING_PATH);
}
setInterval(this.recordWorldUpdate.bind(this), 1000);
console.checkpoint("Started recording to: " + this.recordingFileName);
}
} else if (message.data.hasOwnProperty('KILL')) {
} else if (message.data.hasOwnProperty("KILL")) {
this.channel.destroy();
} else {
this.onMessage(message);
@ -50,7 +60,7 @@ function (Nc, Channel, Settings, fs, util) {
if(Settings.CHANNEL_RECORD_SESSION && this.channel && this.channel.name) {
var m = JSON.stringify(message);
var timestamp = Date.now();
var line = timestamp + m + "\n";
var line = "m" + timestamp + m + "\n";
fs.appendFile(this.recordingFileName, line, function (err) {
if (err) throw err;
@ -64,14 +74,14 @@ function (Nc, Channel, Settings, fs, util) {
var message = {
recipient: recipient,
data: data
}
};
this.process.send(message);
};
PipeToServer.prototype.onMessage = function (message) {
switch(message.recipient) {
case 'channel':
case "channel":
Nc.trigger(Nc.ns.channel.events.controlCommand.channel, message);
break;
default:
@ -79,40 +89,59 @@ function (Nc, Channel, Settings, fs, util) {
break;
}
}
};
PipeToServer.prototype.play = function(playingFileName) {
var self = this;
var data = fs.readFileSync(Settings.CHANNEL_RECORDING_PATH + playingFileName);
var lines = data.toString().split("\n");
var now = Date.now();
var start = 0;
for (var i = 0; i < lines.length; i++) {
// bind message variable
(function() {
var line = lines[i];
if(line.length > 0) {
var time = parseInt(line.substring(0, Date.now().toString().length), 10);
if(i == 0) {
var type = line.substring(0, 1);
var time = parseInt(line.substring(1, Date.now().toString().length + 1), 10);
if(i === 0) {
start = time;
}
time -= start;
var jsonString = line.substring(Date.now().toString().length);
var jsonString = line.substring(Date.now().toString().length + 1);
var message = JSON.parse(jsonString);
setTimeout(function() {
console.log(" ");
console.log(util.inspect(message, { showHidden: true, depth: 4 }));
self.onProcessMessage(message, null);
console.log(line);
if(type == "m") {
self.onProcessMessage(message, null);
} else if(type == "w") {
if(self.channel.gameController) {
self.channel.gameController.onWorldUpdate(message);
}
}
}, time);
}
})();
};
}
};
PipeToServer.prototype.recordWorldUpdate = function() {
if(this.channel.gameController) {
var update = this.channel.gameController.getWorldUpdateObject(true);
var worldUpdate = JSON.stringify(update);
var timestamp = Date.now();
var line = "w" + timestamp + worldUpdate + "\n";
fs.appendFile(this.recordingFileName, line, function (err) {
if (err) throw err;
});
}
};
PipeToServer.prototype.destroy = function() {
this.send('coordinator', {destroy:this.channel.name});
this.send("coordinator", {destroy:this.channel.name});
this.process.exit(0);
};

View file

@ -107,42 +107,15 @@ function (Parent, Box2D, PhysicsEngine, ViewManager, PlayerController, Nc, reque
this.audioPlayer.play();
};
GameController.prototype.onWorldUpdate = function (updateData) {
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if (userData instanceof GameObject) {
var gameObject = userData;
if(updateData[gameObject.uid]) {
var update = updateData[gameObject.uid];
if (gameObject instanceof Doll) {
if(gameObject === this.me.doll) {
this.me.setLastServerPositionState(update);
if(!this.me.acceptPositionStateUpdateFromServer()) {
continue; // this is to ignore own doll updates from world update
}
}
gameObject.setActionState(update.as);
gameObject.lookAt(update.laxy.x, update.laxy.y);
}
Assert.number(update.p.x, update.p.y);
Assert.number(update.a);
Assert.number(update.lv.x, update.lv.y);
Assert.number(update.av);
body.SetAwake(true);
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
}
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
if(gameObject === this.me.doll) {
this.me.setLastServerPositionState(update);
if(!this.me.acceptPositionStateUpdateFromServer()) {
return; // this is to ignore own doll updates from world update
}
}
} while (body = body.GetNext());
Parent.prototype.onWorldUpdateGameObject.call(this, body, gameObject, update);
};
GameController.prototype.createMe = function(user) {

View file

@ -3,6 +3,8 @@ define([
function () {
"use strict";
var Settings = {
STAGE_WIDTH: 600,
STAGE_HEIGHT: 400,
@ -19,13 +21,14 @@ function () {
BOX2D_TIME_STEP: 1 / 60,
// PATHS
GRAPHICS_PATH: 'static/img/',
GRAPHICS_SUBPATH_ITEMS: 'Items/',
GRAPHICS_SUBPATH_CHARACTERS: 'Characters/',
GRAPHICS_SUBPATH_TILES: 'Tiles/',
MAPS_PATH: 'static/maps/tiled/',
AUDIO_PATH: 'static/sounds/',
CHANNEL_RECORDING_PATH: 'recordings/',
GRAPHICS_PATH: "static/img/",
GRAPHICS_SUBPATH_ITEMS: "Items/",
GRAPHICS_SUBPATH_CHARACTERS: "Characters/",
GRAPHICS_SUBPATH_TILES: "Tiles/",
MAPS_PATH: "static/maps/tiled/",
AUDIO_PATH: "static/sounds/",
CHANNEL_RECORDING_PATH: "recordings/",
CHANNEL_PLAY_RECORDING: false, //"Quickstart-2015-03-15T19:00:28.545Z-debug_residence_stones.log",
RATIO: 21, //35
// original tile size is 25 but we want it to resize to 20
@ -33,7 +36,7 @@ function () {
TILE_SIZE: 20,
CAMERA_IS_ORTHOGRAPHIC: true,
CAMERA_GLIDE: 12, // % of the way per frame
VIEW_CONTROLLER: 0 ? 'Three' : 'Pixi',
VIEW_CONTROLLER: 0 ? "Three" : "Pixi",
ARROW_GLIDE: 30, // % of the way per frame
SHOW_LAYER_INFO: false,
@ -69,15 +72,15 @@ function () {
ITEM_LINEAR_DAMPING: 0.02,
// BROWSER
CANVAS_DOM_ID: 'canvasContainer',
IS_BROWSER_ENVIRONMENT: typeof window !== 'undefined',
CANVAS_DOM_ID: "canvasContainer",
IS_BROWSER_ENVIRONMENT: typeof window !== "undefined",
USE_WEBGL: true,
// NETWORKING
NETWORK_UPDATE_INTERVAL: 70, // in milliseconds
NETWORK_LOG_INCOMING: false,
NETWORK_LOG_OUTGOING: false,
NETWORK_LOG_FILTER: ['ping', 'pong', 'worldUpdate', 'lookAt'],
NETWORK_LOG_FILTER: ["ping", "pong", "worldUpdate", "lookAt"],
// CHANNEL
CHANNEL_MAX_USERS: 20,
@ -85,13 +88,13 @@ function () {
CHANNEL_END_ROUND_TIME: 4, //10,
CHANNEL_DEFAULT_MAX_USERS: 40,
CHANNEL_DEFAULT_SCORE_LIMIT: 10,
CHANNEL_DEFAULT_LEVELS: ['debug'],
CHANNEL_RECORD_SESSION: false,
CHANNEL_DEFAULT_LEVELS: ["debug"],
CHANNEL_RECORD_SESSION: true,
// ME STATE
ME_STATE_MAX_DIFFERENCE_METERS: 1,
PUNKBUSTER_DIFFERENCE_METERS: 1
}
};
Settings.TILE_RATIO = Settings.ORIGINAL_TILE_SIZE / Settings.TILE_SIZE;

View file

@ -4,9 +4,11 @@ define([
"Game/" + GLOBALS.context + "/Player",
"Lib/Utilities/NotificationCenter",
"Game/" + GLOBALS.context + "/GameObjects/Doll",
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
"Lib/Utilities/Assert"
],
function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
function (PhysicsEngine, TiledLevel, Player, Nc, Doll, GameObject, Assert) {
"use strict";
@ -53,7 +55,7 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
GameController.prototype.getPhysicsEngine = function () {
return this.physicsEngine;
}
};
GameController.prototype.loadLevel = function (levelUid) {
@ -63,7 +65,50 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
}
this.level = new TiledLevel(levelUid, this.physicsEngine, this.gameObjects);
}
};
GameController.prototype.onWorldUpdate = function (updateData) {
var body = this.physicsEngine.world.GetBodyList();
do {
var userData = body.GetUserData();
if (userData instanceof GameObject) {
var gameObject = userData;
if(updateData[gameObject.uid]) {
var update = updateData[gameObject.uid];
this.onWorldUpdateGameObject(body, gameObject, update);
}
}
} while (body = body.GetNext());
};
GameController.prototype.onWorldUpdateGameObject = function(body, gameObject, update) {
if (gameObject instanceof Doll) {
/*
if(gameObject === this.me.doll) {
this.me.setLastServerPositionState(update);
if(!this.me.acceptPositionStateUpdateFromServer()) {
return; // this is to ignore own doll updates from world update
}
}
*/
gameObject.setActionState(update.as);
gameObject.lookAt(update.laxy.x, update.laxy.y);
}
Assert.number(update.p.x, update.p.y);
Assert.number(update.a);
Assert.number(update.lv.x, update.lv.y);
Assert.number(update.av);
body.SetAwake(true);
body.SetPosition(update.p);
body.SetAngle(update.a);
body.SetLinearVelocity(update.lv);
body.SetAngularVelocity(update.av);
};
GameController.prototype.onResetLevel = function() {
this.loadLevel(this.level.uid);
@ -84,7 +129,7 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
player.destroy();
delete this.players[userId];
}
};
GameController.prototype.createPlayer = function(user) {
var player = new Player(user.id, this.physicsEngine, user);
@ -93,17 +138,21 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
};
GameController.prototype.destroy = function () {
var i = 0;
/*
for(var player in this.players) {
// this.players[player].destroy();
// FIXME:
// commented out for now, because players are in gameObjects array.
// try using a real gameobject for the health bar
}
}*/
for (var i = 0; i < this.ncTokens.length; i++) {
for (i = 0; i < this.ncTokens.length; i++) {
Nc.off(this.ncTokens[i]);
};
}
/*
* Contents of gameObject: Players, Items, Tiles, RagDolls
@ -111,12 +160,12 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
*/
for (var key in this.gameObjects) {
for (var i = 0; i < this.gameObjects[key].length; i++) {
for (i = 0; i < this.gameObjects[key].length; i++) {
var gameObject = this.gameObjects[key][i];
gameObject.destroy();
};
};
}
}
this.gameObjects = {
fixed: [],
@ -124,7 +173,7 @@ function (PhysicsEngine, TiledLevel, Player, Nc, Doll) {
};
this.physicsEngine.destroy();
}
};
return GameController;
});

View file

@ -23,7 +23,7 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
this.playerController = null;
this.doll;
this.id = id;
this.isSpawned = false;
this.spawned = false;
this.holdingItem = null;
this.spectatorDoll = new SpectatorDoll(this.physicsEngine, "spectatorDoll-" + this.id, this);
@ -31,7 +31,7 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
}
Player.prototype.getActiveDoll = function() {
if(this.isSpawned) {
if(this.spawned) {
return this.doll;
} else if (this.ragDoll) {
return this.ragDoll;
@ -42,9 +42,13 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
Player.prototype.spawn = function (x, y) {
this.doll = new Doll(this.physicsEngine, "doll-" + this.id, this);
this.doll.spawn(x, y);
this.isSpawned = true;
this.spawned = true;
}
Player.prototype.isSpawned = function() {
return this.spawned;
};
Player.prototype.getPosition = function () {
return this.getActiveDoll().getPosition();
}
@ -55,47 +59,47 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
Player.prototype.move = function (direction) {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.move(direction);
}
Player.prototype.stop = function () {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.stop();
}
Player.prototype.jump = function () {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.jump();
}
Player.prototype.jumpStop = function () {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.jumpStop();
}
Player.prototype.lookAt = function (x, y) {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
// FIXME implement spectator movement here
this.doll.lookAt(x, y);
}
Player.prototype.grab = function(item) {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.grab(item);
item.beingGrabbed(this);
this.holdingItem = item;
};
Player.prototype.throw = function(options, item) {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
this.doll.throw(item, options);
item.beingReleased(this);
this.holdingItem = null;
};
Player.prototype.kill = function(killedByPlayer, ragDollId) {
if(!this.isSpawned) return false;
if(!this.spawned) return false;
// FIXME: do something better then just respawn in GameController
if(this.holdingItem) {
@ -125,7 +129,7 @@ function (Doll, Settings, Nc, Exception, SpectatorDoll, RagDoll) {
var ragDoll = new RagDoll(this.physicsEngine, "ragDoll-" + this.id + "-" + ragDollId, options);
ragDoll.setVelocities(this.doll.getVelocities());
this.isSpawned = false;
this.spawned = false;
this.doll.destroy();
this.doll = null;

View file

@ -204,7 +204,7 @@ function (Exception) {
this.validate(topic);
if (!this.topics[topic]) {
console.warn("No such topic " + topic + ". Could not trigger. arguments: " + arguments.join);
//console.warn("No such topic " + topic + ". Could not trigger. arguments: " + arguments.join);
}
var args = Array.prototype.slice.call(arguments, 1);