From c9ea301b29c33b1c6d1854f018f76f89041e5ba8 Mon Sep 17 00:00:00 2001 From: Michael Schmatz Date: Thu, 1 May 2014 11:12:44 -0700 Subject: [PATCH] Fixed various bugs with debug worker --- .../javascripts/workers/worker_debug.js | 43 +++++++------------ app/lib/God.coffee | 6 ++- app/lib/world/world.coffee | 3 +- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/app/assets/javascripts/workers/worker_debug.js b/app/assets/javascripts/workers/worker_debug.js index 8b6c6cf58..c1b961cfc 100644 --- a/app/assets/javascripts/workers/worker_debug.js +++ b/app/assets/javascripts/workers/worker_debug.js @@ -33,9 +33,9 @@ if (!Function.prototype.bind) { // assign global window so that Brunch's require (in world.js) can go into it self.window = self; -self.workerID = "Worker"; +self.workerID = "DebugWorker"; -self.logLimit = 200; +self.logLimit = 2000; self.logsLogged = 0; var console = { log: function() { @@ -85,41 +85,25 @@ var GoalManager = self.require('lib/world/GoalManager'); self.getCurrentFrame = function getCurrentFrame(args) { return self.world.frames.length; }; -self.runWorld = function runWorld(args) { - self.postedErrors = {}; - self.t0 = new Date(); - self.firstWorld = args.firstWorld; - self.postedErrors = false; - self.logsLogged = 0; - - try { - self.world = new World(args.worldName, args.userCodeMap); - if(args.level) - self.world.loadFromLevel(args.level, true); - self.goalManager = new GoalManager(self.world); - self.goalManager.setGoals(args.goals); - self.goalManager.setCode(args.userCodeMap); - self.goalManager.worldGenerationWillBegin(); - self.world.setGoalManager(self.goalManager); - } - catch (error) { - self.onWorldError(error); - return; - } - Math.random = self.world.rand.randf; // so user code is predictable - self.world.loadFrames(self.onWorldLoaded, self.onWorldError, self.onWorldLoadProgress); -}; +//optimize this later +self.currentUserCodeMap = {}; +self.currentWorldFrame = 0; self.runWorldUntilFrame = function runWorldUntilFrame(args) { + console.log("Running world until frame " + args.frame); self.postedErrors = {}; self.t0 = new Date(); self.firstWorld = args.firstWorld; self.postedErrors = false; self.logsLogged = 0; - if (!self.world) + + var userCodeMapHasChanged = _.isEqual(self.currentUserCodeMap, args.userCodeMap); + self.currentUserCodeMap = args.userCodeMap; + console.log("User codemap has changed: " + userCodeMapHasChanged); + if (!self.world || userCodeMapHasChanged || args.frame < self.currentWorldFrame) { try { - self.world = new World(args.worldName, args.userCodeMap); + self.world = new World(args.worldName, self.currentUserCodeMap); if(args.level) self.world.loadFromLevel(args.level, true); self.goalManager = new GoalManager(self.world); @@ -138,6 +122,8 @@ self.runWorldUntilFrame = function runWorldUntilFrame(args) { self.world.totalFrames = args.frame; //hack to work around error checking self.world.loadFramesUntilFrame(args.frame, self.onWorldLoaded, self.onWorldError, self.onWorldLoadProgress); + self.currentWorldFrame = args.frame; + }; self.onWorldLoaded = function onWorldLoaded() { @@ -204,6 +190,7 @@ self.reportIn = function reportIn() { } self.addEventListener('message', function(event) { + console.log("received message!") self[event.data.func](event.data.args); }); diff --git a/app/lib/God.coffee b/app/lib/God.coffee index 983b264c6..36854563c 100644 --- a/app/lib/God.coffee +++ b/app/lib/God.coffee @@ -25,6 +25,7 @@ module.exports = class God @fillWorkerPool() #TODO: have this as a constructor option @debugWorker = @createDebugWorker() + @currentUserCodeMap = {} onTomeCast: (e) -> return if @dead @@ -71,7 +72,7 @@ module.exports = class God when "worker-initialized" worker.initialized = true when 'new-debug-world' - console.log "Created new debug world!" + console.log "New Debug world!" when 'console-log' console.log "|" + @id + "'s " + @id + "|", event.data.args... @@ -131,7 +132,7 @@ module.exports = class God func : 'runWorldUntilFrame' args: worldName: @level.name - userCodeMap: @getUserCodeMap() + userCodeMap: @currentUserCodeMap level: @level firstWorld: @firstWorld goals: @goalManager?.getGoals() @@ -170,6 +171,7 @@ module.exports = class God for spellKey, spell of @spells for thangID, spellThang of spell.thangs (userCodeMap[thangID] ?= {})[spell.name] = spellThang.aether.serialize() + @currentUserCodeMap = userCodeMap userCodeMap destroy: -> diff --git a/app/lib/world/world.coffee b/app/lib/world/world.coffee index ed694cd9a..8b065eb7f 100644 --- a/app/lib/world/world.coffee +++ b/app/lib/world/world.coffee @@ -40,6 +40,7 @@ module.exports = class World else if frameIndex frame = frames[frameIndex - 1].getNextFrame() frames.push frame + console.log "Pushed frame #{frameIndex}" else frame = frames[0] @age = frameIndex * @dt @@ -129,7 +130,6 @@ module.exports = class World @t0 = t2 setTimeout((=> @loadFrames(loadedCallback, errorCallback, loadProgressCallback)), 0) return - @ended = true loadProgressCallback? 1 loadedCallback() @@ -278,6 +278,7 @@ module.exports = class World serialize: -> # Code hotspot; optimize it + console.log("Frames length: #{@frames.length}, total frames: #{@totalFrames}") if @frames.length < @totalFrames then throw new Error("World Should Be Over Before Serialization") [transferableObjects, nontransferableObjects] = [0, 0] o = {name: @name, totalFrames: @totalFrames, maxTotalFrames: @maxTotalFrames, frameRate: @frameRate, dt: @dt, victory: @victory, userCodeMap: {}, trackedProperties: {}}