mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-02-25 14:04:54 -05:00
Fix the headless simulator
Shims `lscache`, support io.js < v3.0.0. Make simulating `one-game` possible. Tested on Linux with Node.js v0.10.40 and v0.12.7; as well as io.js v2.5.0. It still won't work with io.js v3.0.0 because `webworker-threads` doesn't compile properly, so that's another problem.
This commit is contained in:
parent
bbe6dd7783
commit
7d64087e29
4 changed files with 11 additions and 12 deletions
|
@ -50,13 +50,14 @@ Worker::removeEventListener = (what) ->
|
||||||
if what is 'message'
|
if what is 'message'
|
||||||
@onmessage = -> #This webworker api has only one event listener at a time.
|
@onmessage = -> #This webworker api has only one event listener at a time.
|
||||||
GLOBAL.tv4 = require('tv4').tv4
|
GLOBAL.tv4 = require('tv4').tv4
|
||||||
GLOBAL.TreemaUtils = require './bower_components/treema/treema-utils.js'
|
GLOBAL.TreemaUtils = require bowerComponentsPath + 'treema/treema-utils'
|
||||||
GLOBAL.marked = setOptions: ->
|
GLOBAL.marked = setOptions: ->
|
||||||
store = {}
|
store = {}
|
||||||
GLOBAL.localStorage =
|
GLOBAL.localStorage =
|
||||||
getItem: (key) => store[key]
|
getItem: (key) => store[key]
|
||||||
setItem: (key, s) => store[key] = s
|
setItem: (key, s) => store[key] = s
|
||||||
removeItem: (key) => delete store[key]
|
removeItem: (key) => delete store[key]
|
||||||
|
GLOBAL.lscache = require bowerComponentsPath + 'lscache/lscache'
|
||||||
|
|
||||||
# Hook node.js require. See https://github.com/mfncooper/mockery/blob/master/mockery.js
|
# Hook node.js require. See https://github.com/mfncooper/mockery/blob/master/mockery.js
|
||||||
# The signature of this function *must* match that of Node's Module._load,
|
# The signature of this function *must* match that of Node's Module._load,
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
# This function needs to run inside an environment that has a 'self'.
|
# This function needs to run inside an environment that has a 'self'.
|
||||||
# This specific worker is targeted towards the node.js headless_client environment.
|
# This specific worker is targeted towards the node.js headless_client environment.
|
||||||
|
|
||||||
JASON = require 'JASON'
|
|
||||||
fs = require 'fs'
|
fs = require 'fs'
|
||||||
GLOBAL.Aether = Aether = require 'aether'
|
GLOBAL.Aether = Aether = require 'aether'
|
||||||
GLOBAL._ = _ = require 'lodash'
|
GLOBAL._ = _ = require 'lodash'
|
||||||
|
@ -10,12 +9,12 @@ GLOBAL.CoffeeScript = require 'coffee-script'
|
||||||
|
|
||||||
betterConsole = () ->
|
betterConsole = () ->
|
||||||
|
|
||||||
self.logLimit = 200;
|
self.logLimit = 200
|
||||||
self.logsLogged = 0;
|
self.logsLogged = 0
|
||||||
|
|
||||||
self.transferableSupported = () -> true
|
self.transferableSupported = () -> true
|
||||||
|
|
||||||
self.console = log: ->
|
self.console = log: (args...) ->
|
||||||
if self.logsLogged++ is self.logLimit
|
if self.logsLogged++ is self.logLimit
|
||||||
self.postMessage
|
self.postMessage
|
||||||
type: 'console-log'
|
type: 'console-log'
|
||||||
|
@ -23,7 +22,6 @@ betterConsole = () ->
|
||||||
id: self.workerID
|
id: self.workerID
|
||||||
|
|
||||||
else if self.logsLogged < self.logLimit
|
else if self.logsLogged < self.logLimit
|
||||||
args = [].slice.call(arguments)
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while i < args.length
|
while i < args.length
|
||||||
|
@ -111,7 +109,7 @@ work = () ->
|
||||||
t1 = new Date()
|
t1 = new Date()
|
||||||
diff = t1 - self.t0
|
diff = t1 - self.t0
|
||||||
if (self.world.headless)
|
if (self.world.headless)
|
||||||
return console.log("Headless simulation completed in #{diff}ms.");
|
return console.log("Headless simulation completed in #{diff}ms.")
|
||||||
|
|
||||||
transferableSupported = self.transferableSupported()
|
transferableSupported = self.transferableSupported()
|
||||||
try
|
try
|
||||||
|
@ -191,11 +189,11 @@ for codeFile in [
|
||||||
'app/vendor/aether-lua.js'
|
'app/vendor/aether-lua.js'
|
||||||
'app/vendor/aether-python.js'
|
'app/vendor/aether-python.js'
|
||||||
]
|
]
|
||||||
codeFileContents.push fs.readFileSync("./public/javascripts/#{codeFile}", 'utf8')
|
codeFileContents.push fs.readFileSync(__dirname + "/../public/javascripts/#{codeFile}", 'utf8')
|
||||||
|
|
||||||
#window.BOX2D_ENABLED = true;
|
#window.BOX2D_ENABLED = true;
|
||||||
|
|
||||||
newConsole = "newConsole = #{}JASON.stringify newConsole}()";
|
newConsole = "newConsole = #{}(newConsole.toString())}()"
|
||||||
|
|
||||||
ret = """
|
ret = """
|
||||||
|
|
||||||
|
@ -204,7 +202,7 @@ ret = """
|
||||||
|
|
||||||
self.workerID = 'Worker';
|
self.workerID = 'Worker';
|
||||||
|
|
||||||
console = #{JASON.stringify betterConsole}();
|
console = (#{betterConsole.toString()})();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// the world javascript file
|
// the world javascript file
|
||||||
|
@ -215,7 +213,7 @@ ret = """
|
||||||
self.native_fs_ = native_fs_ = null;
|
self.native_fs_ = native_fs_ = null;
|
||||||
|
|
||||||
// the actual function
|
// the actual function
|
||||||
#{JASON.stringify work}();
|
(#{work.toString()})();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
self.postMessage({'type': 'console-log', args: ['An unhandled error occured: ', error.toString(), error.stack], id: -1});
|
self.postMessage({'type': 'console-log', args: ['An unhandled error occured: ', error.toString(), error.stack], id: -1});
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,6 @@
|
||||||
"multiplayer"
|
"multiplayer"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"JASON": "~0.1.3",
|
|
||||||
"JQDeferred": "~2.1.0",
|
"JQDeferred": "~2.1.0",
|
||||||
"aether": "~0.3.0",
|
"aether": "~0.3.0",
|
||||||
"async": "0.2.x",
|
"async": "0.2.x",
|
||||||
|
|
|
@ -169,6 +169,7 @@ module.exports.getTwoGames = (req, res) ->
|
||||||
sendResponseObject req, res, taskObject
|
sendResponseObject req, res, taskObject
|
||||||
else
|
else
|
||||||
#console.log "Directly simulating #{humansGameID} vs. #{ogresGameID}."
|
#console.log "Directly simulating #{humansGameID} vs. #{ogresGameID}."
|
||||||
|
selection = 'team totalScore transpiledCode submittedCodeLanguage teamSpells levelID creatorName creator submitDate'
|
||||||
LevelSession.findOne(_id: humansGameID).select(selection).lean().exec (err, humanSession) =>
|
LevelSession.findOne(_id: humansGameID).select(selection).lean().exec (err, humanSession) =>
|
||||||
if err? then return errors.serverError(res, 'Couldn\'t find the human game')
|
if err? then return errors.serverError(res, 'Couldn\'t find the human game')
|
||||||
LevelSession.findOne(_id: ogresGameID).select(selection).lean().exec (err, ogreSession) =>
|
LevelSession.findOne(_id: ogresGameID).select(selection).lean().exec (err, ogreSession) =>
|
||||||
|
|
Loading…
Reference in a new issue