2014-02-14 13:49:16 -05:00
|
|
|
SuperModel = require 'models/SuperModel'
|
2014-03-16 04:07:17 -04:00
|
|
|
CocoClass = require 'lib/CocoClass'
|
2014-02-14 13:49:16 -05:00
|
|
|
LevelLoader = require 'lib/LevelLoader'
|
|
|
|
GoalManager = require 'lib/world/GoalManager'
|
2014-05-10 21:24:50 -04:00
|
|
|
God = require 'lib/God'
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-05-08 14:43:00 -04:00
|
|
|
Aether.addGlobal 'Vector', require 'lib/world/vector'
|
|
|
|
Aether.addGlobal '_', _
|
|
|
|
|
2014-03-16 04:07:17 -04:00
|
|
|
module.exports = class Simulator extends CocoClass
|
2014-02-14 17:55:30 -05:00
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
constructor: (@options) ->
|
|
|
|
@options ?= {}
|
2014-02-20 11:06:11 -05:00
|
|
|
_.extend @, Backbone.Events
|
|
|
|
@trigger 'statusUpdate', 'Starting simulation!'
|
2014-02-14 13:49:16 -05:00
|
|
|
@retryDelayInSeconds = 10
|
2014-02-14 17:55:30 -05:00
|
|
|
@taskURL = '/queue/scoring'
|
2014-05-05 23:07:34 -04:00
|
|
|
@simulatedByYou = 0
|
2014-05-10 21:24:50 -04:00
|
|
|
@god = new God maxAngels: 1, workerCode: @options.workerCode, headless: true # Start loading worker.
|
2014-03-11 22:17:58 -04:00
|
|
|
|
2014-03-07 15:15:16 -05:00
|
|
|
destroy: ->
|
|
|
|
@off()
|
|
|
|
@cleanupSimulation()
|
2014-05-10 21:24:50 -04:00
|
|
|
@god?.destroy()
|
2014-03-16 04:07:17 -04:00
|
|
|
super()
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
fetchAndSimulateOneGame: (humanGameID, ogresGameID) =>
|
|
|
|
return if @destroyed
|
|
|
|
$.ajax
|
|
|
|
url: "/queue/scoring/getTwoGames"
|
|
|
|
type: "POST"
|
|
|
|
parse: true
|
|
|
|
data:
|
|
|
|
"humansGameID": humanGameID
|
|
|
|
"ogresGameID": ogresGameID
|
|
|
|
error: (errorData) ->
|
2014-05-22 00:56:11 -04:00
|
|
|
console.warn "There was an error fetching two games! #{JSON.stringify errorData}"
|
2014-05-19 13:11:20 -04:00
|
|
|
success: (taskData) =>
|
2014-05-20 11:00:44 -04:00
|
|
|
return if @destroyed
|
2014-05-19 21:58:45 -04:00
|
|
|
@trigger 'statusUpdate', 'Setting up simulation...'
|
2014-05-19 13:11:20 -04:00
|
|
|
#refactor this
|
|
|
|
@task = new SimulationTask(taskData)
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
@supermodel ?= new SuperModel()
|
|
|
|
@supermodel.resetProgress()
|
|
|
|
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @task.getLevelName(), sessionID: @task.getFirstSessionID(), headless: true
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
if @supermodel.finished()
|
|
|
|
@simulateSingleGame()
|
|
|
|
else
|
|
|
|
@listenToOnce @supermodel, 'loaded-all', @simulateSingleGame
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
simulateSingleGame: ->
|
|
|
|
return if @destroyed
|
2014-05-19 21:58:45 -04:00
|
|
|
@trigger 'statusUpdate', 'Simulating...'
|
2014-05-19 13:11:20 -04:00
|
|
|
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
|
|
|
@setupGod()
|
|
|
|
try
|
|
|
|
@commenceSingleSimulation()
|
2014-05-22 00:56:11 -04:00
|
|
|
catch error
|
|
|
|
@handleSingleSimulationError error
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
commenceSingleSimulation: ->
|
|
|
|
@god.createWorld @generateSpellsObject()
|
|
|
|
Backbone.Mediator.subscribeOnce 'god:infinite-loop', @handleSingleSimulationInfiniteLoop, @
|
|
|
|
Backbone.Mediator.subscribeOnce 'god:goals-calculated', @processSingleGameResults, @
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-22 00:56:11 -04:00
|
|
|
handleSingleSimulationError: (error) ->
|
|
|
|
console.error "There was an error simulating a single game!", error
|
2014-05-19 13:11:20 -04:00
|
|
|
if @options.headlessClient
|
|
|
|
console.log "GAMERESULT:tie"
|
|
|
|
process.exit(0)
|
|
|
|
@cleanupSimulation()
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
handleSingleSimulationInfiniteLoop: ->
|
|
|
|
console.log "There was an infinite loop in the single game!"
|
|
|
|
if @options.headlessClient
|
|
|
|
console.log "GAMERESULT:tie"
|
|
|
|
process.exit(0)
|
|
|
|
@cleanupSimulation()
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
processSingleGameResults: (simulationResults) ->
|
|
|
|
taskResults = @formTaskResultsObject simulationResults
|
2014-05-22 00:56:11 -04:00
|
|
|
console.log "Processing results:", taskResults
|
2014-05-19 13:11:20 -04:00
|
|
|
humanSessionRank = taskResults.sessions[0].metrics.rank
|
|
|
|
ogreSessionRank = taskResults.sessions[1].metrics.rank
|
|
|
|
if @options.headlessClient
|
|
|
|
if humanSessionRank is ogreSessionRank
|
|
|
|
console.log "GAMERESULT:tie"
|
|
|
|
else if humanSessionRank < ogreSessionRank
|
|
|
|
console.log "GAMERESULT:humans"
|
|
|
|
else if ogreSessionRank < humanSessionRank
|
|
|
|
console.log "GAMERESULT:ogres"
|
|
|
|
process.exit(0)
|
2014-05-19 21:58:45 -04:00
|
|
|
else
|
|
|
|
@sendSingleGameBackToServer(taskResults)
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 13:11:20 -04:00
|
|
|
@cleanupSimulation()
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 21:58:45 -04:00
|
|
|
sendSingleGameBackToServer: (results) ->
|
|
|
|
@trigger 'statusUpdate', 'Simulation completed, sending results back to server!'
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-05-19 21:58:45 -04:00
|
|
|
$.ajax
|
|
|
|
url: "/queue/scoring/recordTwoGames"
|
|
|
|
data: results
|
|
|
|
type: "PUT"
|
|
|
|
parse: true
|
|
|
|
success: @handleTaskResultsTransferSuccess
|
|
|
|
error: @handleTaskResultsTransferError
|
|
|
|
complete: @cleanupAndSimulateAnotherTask
|
2014-05-20 11:00:44 -04:00
|
|
|
|
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
fetchAndSimulateTask: =>
|
2014-03-16 23:36:02 -04:00
|
|
|
return if @destroyed
|
2014-05-05 23:07:34 -04:00
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
if @options.headlessClient
|
2014-05-05 23:07:34 -04:00
|
|
|
if @dumpThisTime # The first heapdump would be useless to find leaks.
|
|
|
|
console.log "Writing snapshot."
|
2014-05-06 12:49:04 -04:00
|
|
|
@options.heapdump.writeSnapshot()
|
|
|
|
@dumpThisTime = true if @options.heapdump
|
2014-05-05 23:07:34 -04:00
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
if @options.testing
|
|
|
|
_.delay @setupSimulationAndLoadLevel, 0, @options.testFile, "Testing...", status: 400
|
2014-05-05 23:07:34 -04:00
|
|
|
return
|
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
@trigger 'statusUpdate', 'Fetching simulation data!'
|
2014-02-14 13:49:16 -05:00
|
|
|
$.ajax
|
|
|
|
url: @taskURL
|
|
|
|
type: "GET"
|
2014-05-10 21:24:50 -04:00
|
|
|
parse: true
|
2014-02-14 13:49:16 -05:00
|
|
|
error: @handleFetchTaskError
|
|
|
|
success: @setupSimulationAndLoadLevel
|
|
|
|
|
|
|
|
handleFetchTaskError: (errorData) =>
|
2014-03-11 22:17:58 -04:00
|
|
|
console.error "There was a horrible Error: #{JSON.stringify errorData}"
|
|
|
|
@trigger 'statusUpdate', 'There was an error fetching games to simulate. Retrying in 10 seconds.'
|
|
|
|
@simulateAnotherTaskAfterDelay()
|
2014-05-20 11:00:44 -04:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-03-11 22:17:58 -04:00
|
|
|
handleNoGamesResponse: ->
|
2014-05-19 21:58:45 -04:00
|
|
|
info = 'Finding game to simulate...'
|
2014-05-05 23:07:34 -04:00
|
|
|
console.log info
|
|
|
|
@trigger 'statusUpdate', info
|
2014-05-19 21:58:45 -04:00
|
|
|
@fetchAndSimulateOneGame()
|
2014-05-15 18:10:50 -04:00
|
|
|
application.tracker?.trackEvent 'Simulator Result', label: "No Games"
|
2014-02-14 17:55:30 -05:00
|
|
|
|
|
|
|
simulateAnotherTaskAfterDelay: =>
|
2014-03-11 22:17:58 -04:00
|
|
|
console.log "Retrying in #{@retryDelayInSeconds}"
|
2014-02-14 17:55:30 -05:00
|
|
|
retryDelayInMilliseconds = @retryDelayInSeconds * 1000
|
|
|
|
_.delay @fetchAndSimulateTask, retryDelayInMilliseconds
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-03-11 22:17:58 -04:00
|
|
|
setupSimulationAndLoadLevel: (taskData, textStatus, jqXHR) =>
|
|
|
|
return @handleNoGamesResponse() if jqXHR.status is 204
|
2014-02-20 11:06:11 -05:00
|
|
|
@trigger 'statusUpdate', 'Setting up simulation!'
|
2014-02-14 13:49:16 -05:00
|
|
|
@task = new SimulationTask(taskData)
|
2014-03-26 15:12:43 -04:00
|
|
|
try
|
|
|
|
levelID = @task.getLevelName()
|
|
|
|
catch err
|
|
|
|
console.error err
|
|
|
|
@trigger 'statusUpdate', "Error simulating game: #{err}. Trying another game in #{@retryDelayInSeconds} seconds."
|
|
|
|
@simulateAnotherTaskAfterDelay()
|
|
|
|
return
|
|
|
|
|
2014-03-18 14:26:15 -04:00
|
|
|
@supermodel ?= new SuperModel()
|
2014-05-14 14:13:36 -04:00
|
|
|
@supermodel.resetProgress()
|
2014-03-26 15:12:43 -04:00
|
|
|
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: levelID, sessionID: @task.getFirstSessionID(), headless: true
|
2014-04-29 19:54:57 -04:00
|
|
|
if @supermodel.finished()
|
|
|
|
@simulateGame()
|
|
|
|
else
|
|
|
|
@listenToOnce @supermodel, 'loaded-all', @simulateGame
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-03-24 12:58:34 -04:00
|
|
|
simulateGame: ->
|
2014-03-16 04:07:17 -04:00
|
|
|
return if @destroyed
|
2014-05-05 23:07:34 -04:00
|
|
|
info = 'All resources loaded, simulating!'
|
|
|
|
console.log info
|
|
|
|
@trigger 'statusUpdate', info, @task.getSessions()
|
2014-02-14 13:49:16 -05:00
|
|
|
@assignWorldAndLevelFromLevelLoaderAndDestroyIt()
|
|
|
|
@setupGod()
|
2014-02-14 17:55:30 -05:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
try
|
|
|
|
@commenceSimulationAndSetupCallback()
|
2014-02-14 17:55:30 -05:00
|
|
|
catch err
|
2014-02-15 18:45:53 -05:00
|
|
|
console.log "There was an error in simulation(#{err}). Trying again in #{@retryDelayInSeconds} seconds"
|
2014-02-14 17:55:30 -05:00
|
|
|
@simulateAnotherTaskAfterDelay()
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
assignWorldAndLevelFromLevelLoaderAndDestroyIt: ->
|
|
|
|
@world = @levelLoader.world
|
2014-05-15 19:43:16 -04:00
|
|
|
@task.setWorld(@world)
|
2014-02-14 13:49:16 -05:00
|
|
|
@level = @levelLoader.level
|
|
|
|
@levelLoader.destroy()
|
2014-02-15 18:44:45 -05:00
|
|
|
@levelLoader = null
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
setupGod: ->
|
2014-05-10 21:24:50 -04:00
|
|
|
@god.setLevel @level.serialize @supermodel
|
2014-05-20 11:00:44 -04:00
|
|
|
@god.setLevelSessionIDs (session.sessionID for session in @task.getSessions())
|
2014-05-05 23:07:34 -04:00
|
|
|
@god.setWorldClassMap @world.classMap
|
|
|
|
@god.setGoalManager new GoalManager(@world, @level.get 'goals')
|
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
commenceSimulationAndSetupCallback: ->
|
2014-05-05 20:37:14 -04:00
|
|
|
@god.createWorld @generateSpellsObject()
|
2014-03-26 14:25:05 -04:00
|
|
|
Backbone.Mediator.subscribeOnce 'god:infinite-loop', @onInfiniteLoop, @
|
2014-05-10 21:24:50 -04:00
|
|
|
Backbone.Mediator.subscribeOnce 'god:goals-calculated', @processResults, @
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-05-05 23:07:34 -04:00
|
|
|
#Search for leaks, headless-client only.
|
2014-05-06 12:49:04 -04:00
|
|
|
if @options.headlessClient and @options.leakTest and not @memwatch?
|
2014-05-05 23:07:34 -04:00
|
|
|
leakcount = 0
|
|
|
|
maxleakcount = 0
|
|
|
|
console.log "Setting leak callbacks."
|
|
|
|
@memwatch = require 'memwatch'
|
|
|
|
|
|
|
|
@memwatch.on 'leak', (info) =>
|
|
|
|
console.warn "LEAK!!\n" + JSON.stringify(info)
|
|
|
|
|
|
|
|
unless @hd?
|
|
|
|
if (leakcount++ is maxleakcount)
|
|
|
|
@hd = new @memwatch.HeapDiff()
|
|
|
|
|
|
|
|
@memwatch.on 'stats', (stats) =>
|
|
|
|
console.warn "stats callback: " + stats
|
|
|
|
diff = @hd.end()
|
|
|
|
console.warn "HeapDiff:\n" + JSON.stringify(diff)
|
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
if @options.exitOnLeak
|
2014-05-05 23:07:34 -04:00
|
|
|
console.warn "Exiting because of Leak."
|
|
|
|
process.exit()
|
|
|
|
@hd = new @memwatch.HeapDiff()
|
|
|
|
|
2014-03-26 14:25:05 -04:00
|
|
|
onInfiniteLoop: ->
|
|
|
|
console.warn "Skipping infinitely looping game."
|
2014-03-26 15:12:43 -04:00
|
|
|
@trigger 'statusUpdate', "Infinite loop detected; grabbing a new game in #{@retryDelayInSeconds} seconds."
|
2014-03-26 15:34:45 -04:00
|
|
|
_.delay @cleanupAndSimulateAnotherTask, @retryDelayInSeconds * 1000
|
2014-03-26 14:25:05 -04:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
processResults: (simulationResults) ->
|
|
|
|
taskResults = @formTaskResultsObject simulationResults
|
2014-02-14 18:50:42 -05:00
|
|
|
@sendResultsBackToServer taskResults
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-05-10 21:24:50 -04:00
|
|
|
sendResultsBackToServer: (results) ->
|
2014-02-20 11:06:11 -05:00
|
|
|
@trigger 'statusUpdate', 'Simulation completed, sending results back to server!'
|
2014-05-22 00:56:11 -04:00
|
|
|
console.log "Sending result back to server:", results
|
2014-04-25 19:57:42 -04:00
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
if @options.headlessClient and @options.testing
|
2014-05-05 23:07:34 -04:00
|
|
|
return @fetchAndSimulateTask()
|
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
$.ajax
|
2014-02-18 14:46:14 -05:00
|
|
|
url: "/queue/scoring"
|
2014-02-14 13:49:16 -05:00
|
|
|
data: results
|
|
|
|
type: "PUT"
|
2014-05-10 21:24:50 -04:00
|
|
|
parse: true
|
2014-02-14 13:49:16 -05:00
|
|
|
success: @handleTaskResultsTransferSuccess
|
|
|
|
error: @handleTaskResultsTransferError
|
2014-02-14 19:53:34 -05:00
|
|
|
complete: @cleanupAndSimulateAnotherTask
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
handleTaskResultsTransferSuccess: (result) =>
|
2014-05-20 11:00:44 -04:00
|
|
|
return if @destroyed
|
2014-02-14 13:49:16 -05:00
|
|
|
console.log "Task registration result: #{JSON.stringify result}"
|
2014-02-20 11:06:11 -05:00
|
|
|
@trigger 'statusUpdate', 'Results were successfully sent back to server!'
|
2014-05-22 00:56:11 -04:00
|
|
|
console.log "Simulated by you:", @simulatedByYou
|
2014-05-05 23:07:34 -04:00
|
|
|
@simulatedByYou++
|
2014-05-06 12:49:04 -04:00
|
|
|
unless @options.headlessClient
|
2014-05-05 23:07:34 -04:00
|
|
|
simulatedBy = parseInt($('#simulated-by-you').text(), 10) + 1
|
|
|
|
$('#simulated-by-you').text(simulatedBy)
|
2014-05-15 18:10:50 -04:00
|
|
|
application.tracker?.trackEvent 'Simulator Result', label: "Success"
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-02-20 11:06:11 -05:00
|
|
|
handleTaskResultsTransferError: (error) =>
|
2014-05-20 11:00:44 -04:00
|
|
|
return if @destroyed
|
2014-02-20 11:06:11 -05:00
|
|
|
@trigger 'statusUpdate', 'There was an error sending the results back to the server.'
|
2014-02-14 13:49:16 -05:00
|
|
|
console.log "Task registration error: #{JSON.stringify error}"
|
|
|
|
|
|
|
|
cleanupAndSimulateAnotherTask: =>
|
2014-05-20 11:00:44 -04:00
|
|
|
return if @destroyed
|
2014-05-06 12:49:04 -04:00
|
|
|
@cleanupSimulation()
|
2014-02-14 13:49:16 -05:00
|
|
|
@fetchAndSimulateTask()
|
|
|
|
|
2014-05-06 12:49:04 -04:00
|
|
|
cleanupSimulation: ->
|
|
|
|
@world = null
|
|
|
|
@level = null
|
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
formTaskResultsObject: (simulationResults) ->
|
|
|
|
taskResults =
|
|
|
|
taskID: @task.getTaskID()
|
|
|
|
receiptHandle: @task.getReceiptHandle()
|
2014-02-26 15:14:02 -05:00
|
|
|
originalSessionID: @task.getFirstSessionID()
|
|
|
|
originalSessionRank: -1
|
2014-02-14 13:49:16 -05:00
|
|
|
calculationTime: 500
|
|
|
|
sessions: []
|
|
|
|
|
2014-02-14 18:50:42 -05:00
|
|
|
for session in @task.getSessions()
|
2014-02-14 13:49:16 -05:00
|
|
|
sessionResult =
|
|
|
|
sessionID: session.sessionID
|
2014-02-18 14:46:14 -05:00
|
|
|
submitDate: session.submitDate
|
|
|
|
creator: session.creator
|
2014-05-16 17:52:49 -04:00
|
|
|
name: session.creatorName
|
|
|
|
totalScore: session.totalScore
|
2014-02-14 13:49:16 -05:00
|
|
|
metrics:
|
2014-02-14 18:50:42 -05:00
|
|
|
rank: @calculateSessionRank session.sessionID, simulationResults.goalStates, @task.generateTeamToSessionMap()
|
2014-02-26 15:14:02 -05:00
|
|
|
if session.sessionID is taskResults.originalSessionID
|
|
|
|
taskResults.originalSessionRank = sessionResult.metrics.rank
|
|
|
|
taskResults.originalSessionTeam = session.team
|
2014-02-14 13:49:16 -05:00
|
|
|
taskResults.sessions.push sessionResult
|
|
|
|
|
|
|
|
return taskResults
|
|
|
|
|
2014-02-14 18:50:42 -05:00
|
|
|
calculateSessionRank: (sessionID, goalStates, teamSessionMap) ->
|
2014-04-08 17:58:34 -04:00
|
|
|
ogreGoals = (goalState for key, goalState of goalStates when goalState.team is 'ogres')
|
|
|
|
humanGoals = (goalState for key, goalState of goalStates when goalState.team is 'humans')
|
|
|
|
ogresWon = _.all ogreGoals, {status: 'success'}
|
|
|
|
humansWon = _.all humanGoals, {status: 'success'}
|
|
|
|
if ogresWon is humansWon
|
2014-02-14 13:49:16 -05:00
|
|
|
return 0
|
2014-04-08 17:58:34 -04:00
|
|
|
else if ogresWon and teamSessionMap["ogres"] is sessionID
|
2014-02-14 13:49:16 -05:00
|
|
|
return 0
|
2014-04-08 17:58:34 -04:00
|
|
|
else if ogresWon and teamSessionMap["ogres"] isnt sessionID
|
2014-02-14 13:49:16 -05:00
|
|
|
return 1
|
2014-04-08 17:58:34 -04:00
|
|
|
else if humansWon and teamSessionMap["humans"] is sessionID
|
2014-02-14 13:49:16 -05:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
|
2014-02-14 18:50:42 -05:00
|
|
|
generateSpellsObject: ->
|
|
|
|
@currentUserCodeMap = @task.generateSpellKeyToSourceMap()
|
2014-02-14 13:49:16 -05:00
|
|
|
@spells = {}
|
|
|
|
for thang in @level.attributes.thangs
|
|
|
|
continue if @thangIsATemplate thang
|
|
|
|
@generateSpellKeyToSourceMapPropertiesFromThang thang
|
2014-05-10 21:24:50 -04:00
|
|
|
@spells
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
thangIsATemplate: (thang) ->
|
|
|
|
for component in thang.components
|
|
|
|
continue unless @componentHasProgrammableMethods component
|
|
|
|
for methodName, method of component.config.programmableMethods
|
2014-02-14 18:50:42 -05:00
|
|
|
return true if @methodBelongsToTemplateThang method
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
componentHasProgrammableMethods: (component) -> component.config? and _.has component.config, 'programmableMethods'
|
|
|
|
|
|
|
|
methodBelongsToTemplateThang: (method) -> typeof method is 'string'
|
|
|
|
|
|
|
|
generateSpellKeyToSourceMapPropertiesFromThang: (thang) =>
|
|
|
|
for component in thang.components
|
|
|
|
continue unless @componentHasProgrammableMethods component
|
|
|
|
for methodName, method of component.config.programmableMethods
|
|
|
|
spellKey = @generateSpellKeyFromThangIDAndMethodName thang.id, methodName
|
|
|
|
|
|
|
|
@createSpellAndAssignName spellKey, methodName
|
|
|
|
@createSpellThang thang, method, spellKey
|
|
|
|
@transpileSpell thang, spellKey, methodName
|
|
|
|
|
|
|
|
generateSpellKeyFromThangIDAndMethodName: (thang, methodName) ->
|
2014-02-26 12:21:41 -05:00
|
|
|
spellKeyComponents = [thang, methodName]
|
2014-02-14 18:50:42 -05:00
|
|
|
spellKeyComponents[0] = _.string.slugify spellKeyComponents[0]
|
2014-02-26 12:21:41 -05:00
|
|
|
spellKey = spellKeyComponents.join '/'
|
|
|
|
spellKey
|
2014-02-26 20:45:08 -05:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
createSpellAndAssignName: (spellKey, spellName) ->
|
|
|
|
@spells[spellKey] ?= {}
|
2014-02-14 18:50:42 -05:00
|
|
|
@spells[spellKey].name = spellName
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
createSpellThang: (thang, method, spellKey) ->
|
|
|
|
@spells[spellKey].thangs ?= {}
|
|
|
|
@spells[spellKey].thangs[thang.id] ?= {}
|
2014-04-11 20:11:55 -04:00
|
|
|
spellTeam = @task.getSpellKeyToTeamMap()[spellKey]
|
|
|
|
playerTeams = @task.getPlayerTeams()
|
|
|
|
useProtectAPI = true
|
|
|
|
if spellTeam not in playerTeams then useProtectAPI = false
|
|
|
|
@spells[spellKey].thangs[thang.id].aether = @createAether @spells[spellKey].name, method, useProtectAPI
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-05-14 13:14:18 -04:00
|
|
|
|
2014-05-15 19:44:16 -04:00
|
|
|
transpileSpell: (thang, spellKey, methodName) ->
|
2014-02-14 13:49:16 -05:00
|
|
|
slugifiedThangID = _.string.slugify thang.id
|
2014-05-15 19:43:16 -04:00
|
|
|
generatedSpellKey = [slugifiedThangID,methodName].join '/'
|
|
|
|
source = @currentUserCodeMap[generatedSpellKey] ? ""
|
2014-03-04 11:30:50 -05:00
|
|
|
aether = @spells[spellKey].thangs[thang.id].aether
|
2014-05-15 19:43:16 -04:00
|
|
|
unless _.contains(@task.spellKeysToTranspile, generatedSpellKey)
|
|
|
|
aether.pure = source
|
|
|
|
else
|
|
|
|
try
|
|
|
|
aether.transpile source
|
|
|
|
catch e
|
|
|
|
console.log "Couldn't transpile #{spellKey}:\n#{source}\n", e
|
|
|
|
aether.transpile ''
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-04-11 20:11:55 -04:00
|
|
|
createAether: (methodName, method, useProtectAPI) ->
|
2014-02-14 13:49:16 -05:00
|
|
|
aetherOptions =
|
|
|
|
functionName: methodName
|
2014-04-11 20:11:55 -04:00
|
|
|
protectAPI: useProtectAPI
|
2014-02-14 13:49:16 -05:00
|
|
|
includeFlow: false
|
2014-05-14 13:14:18 -04:00
|
|
|
yieldConditionally: methodName is "plan"
|
2014-05-08 14:43:00 -04:00
|
|
|
globals: ['Vector', '_']
|
2014-02-22 15:01:05 -05:00
|
|
|
problems:
|
|
|
|
jshint_W040: {level: "ignore"}
|
|
|
|
jshint_W030: {level: "ignore"} # aether_NoEffect instead
|
|
|
|
aether_MissingThis: {level: 'error'}
|
|
|
|
#functionParameters: # TODOOOOO
|
|
|
|
if methodName is 'hear'
|
|
|
|
aetherOptions.functionParameters = ['speaker', 'message', 'data']
|
2014-02-25 17:46:48 -05:00
|
|
|
#console.log "creating aether with options", aetherOptions
|
2014-02-14 13:49:16 -05:00
|
|
|
return new Aether aetherOptions
|
|
|
|
|
2014-05-10 21:24:50 -04:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
class SimulationTask
|
|
|
|
constructor: (@rawData) ->
|
2014-04-11 20:11:55 -04:00
|
|
|
@spellKeyToTeamMap = {}
|
2014-05-15 19:43:16 -04:00
|
|
|
@spellKeysToTranspile = []
|
2014-02-14 13:49:16 -05:00
|
|
|
|
|
|
|
getLevelName: ->
|
2014-04-26 17:21:26 -04:00
|
|
|
levelName = @rawData.sessions?[0]?.levelID
|
2014-02-14 13:49:16 -05:00
|
|
|
return levelName if levelName?
|
|
|
|
@throwMalformedTaskError "The level name couldn't be deduced from the task."
|
|
|
|
|
|
|
|
generateTeamToSessionMap: ->
|
|
|
|
teamSessionMap = {}
|
|
|
|
for session in @rawData.sessions
|
|
|
|
@throwMalformedTaskError "Two players share the same team" if teamSessionMap[session.team]?
|
|
|
|
teamSessionMap[session.team] = session.sessionID
|
|
|
|
|
|
|
|
teamSessionMap
|
|
|
|
|
|
|
|
throwMalformedTaskError: (errorString) ->
|
|
|
|
throw new Error "The task was malformed, reason: #{errorString}"
|
|
|
|
|
|
|
|
getFirstSessionID: -> @rawData.sessions[0].sessionID
|
|
|
|
|
|
|
|
getTaskID: -> @rawData.taskID
|
|
|
|
|
|
|
|
getReceiptHandle: -> @rawData.receiptHandle
|
|
|
|
|
|
|
|
getSessions: -> @rawData.sessions
|
2014-04-25 19:57:42 -04:00
|
|
|
|
2014-04-11 20:11:55 -04:00
|
|
|
getSpellKeyToTeamMap: -> @spellKeyToTeamMap
|
2014-04-25 19:57:42 -04:00
|
|
|
|
2014-04-11 20:11:55 -04:00
|
|
|
getPlayerTeams: -> _.pluck @rawData.sessions, 'team'
|
2014-02-14 13:49:16 -05:00
|
|
|
|
2014-05-15 19:44:16 -04:00
|
|
|
setWorld: (@world) ->
|
|
|
|
|
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
generateSpellKeyToSourceMap: ->
|
2014-04-11 20:11:55 -04:00
|
|
|
playerTeams = _.pluck @rawData.sessions, 'team'
|
2014-02-14 13:49:16 -05:00
|
|
|
spellKeyToSourceMap = {}
|
|
|
|
for session in @rawData.sessions
|
|
|
|
teamSpells = session.teamSpells[session.team]
|
2014-04-11 20:11:55 -04:00
|
|
|
allTeams = _.keys session.teamSpells
|
|
|
|
nonPlayerTeams = _.difference allTeams, playerTeams
|
|
|
|
for team in allTeams
|
|
|
|
for spell in session.teamSpells[team]
|
|
|
|
@spellKeyToTeamMap[spell] = team
|
|
|
|
for nonPlayerTeam in nonPlayerTeams
|
2014-05-15 19:43:16 -04:00
|
|
|
for spell in session.teamSpells[nonPlayerTeam]
|
|
|
|
spellKeyToSourceMap[spell] ?= @getWorldProgrammableSource(spell, @world)
|
|
|
|
@spellKeysToTranspile.push spell
|
2014-02-26 12:21:41 -05:00
|
|
|
teamCode = {}
|
2014-04-11 20:11:55 -04:00
|
|
|
|
2014-05-15 19:43:16 -04:00
|
|
|
for thangName, thangSpells of session.transpiledCode
|
2014-02-26 12:21:41 -05:00
|
|
|
for spellName, spell of thangSpells
|
|
|
|
fullSpellName = [thangName,spellName].join '/'
|
|
|
|
if _.contains(teamSpells, fullSpellName)
|
|
|
|
teamCode[fullSpellName]=spell
|
2014-04-25 19:57:42 -04:00
|
|
|
|
2014-02-26 12:21:41 -05:00
|
|
|
_.merge spellKeyToSourceMap, teamCode
|
2014-02-26 20:45:08 -05:00
|
|
|
|
2014-02-14 13:49:16 -05:00
|
|
|
spellKeyToSourceMap
|
2014-05-15 19:43:16 -04:00
|
|
|
|
|
|
|
getWorldProgrammableSource: (desiredSpellKey ,world) ->
|
|
|
|
programmableThangs = _.filter world.thangs, 'isProgrammable'
|
|
|
|
language = @getSessions()[0]['codeLanguage'] ? me.get('aceConfig')?.language ? 'javascript'
|
|
|
|
@spells ?= {}
|
|
|
|
@thangSpells ?= {}
|
|
|
|
for thang in programmableThangs
|
|
|
|
continue if @thangSpells[thang.id]?
|
|
|
|
@thangSpells[thang.id] = []
|
|
|
|
for methodName, method of thang.programmableMethods
|
|
|
|
pathComponents = [thang.id, methodName]
|
|
|
|
if method.cloneOf
|
|
|
|
pathComponents[0] = method.cloneOf # referencing another Thang's method
|
|
|
|
pathComponents[0] = _.string.slugify pathComponents[0]
|
|
|
|
spellKey = pathComponents.join '/'
|
|
|
|
@thangSpells[thang.id].push spellKey
|
|
|
|
if not method.cloneOf and spellKey is desiredSpellKey
|
2014-05-22 00:56:11 -04:00
|
|
|
#console.log "Setting #{desiredSpellKey} from world!"
|
2014-05-15 19:43:16 -04:00
|
|
|
return method.source
|