codecombat/scripts/transpile.coffee

91 lines
3.2 KiB
CoffeeScript
Raw Normal View History

do (setupLodash = this) ->
GLOBAL._ = require 'lodash'
_.str = require 'underscore.string'
_.mixin _.str.exports()
2014-06-30 22:16:26 -04:00
Aether = require 'aether'
async = require 'async'
serverSetup = require '../server_setup'
Level = require '../server/levels/Level'
LevelSession = require '../server/levels/sessions/LevelSession'
Aether.addGlobal 'Vector', require '../app/lib/world/vector'
Aether.addGlobal '_', _
i = 0
transpileLevelSession = (sessionID, cb) ->
2014-06-30 22:16:26 -04:00
query = LevelSession.findOne('_id': sessionID).select('team teamSpells submittedCode submittedCodeLanguage').lean()
query.exec (err, session) ->
if err then return cb err
submittedCode = session.submittedCode
unless session.submittedCodeLanguage
2014-06-30 22:16:26 -04:00
throw 'SUBMITTED CODE LANGUAGE DOESN\'T EXIST'
else
console.log "Transpiling code for session #{i++} #{session._id} in language #{session.submittedCodeLanguage}"
transpiledCode = {}
#console.log "Updating session #{sessionID}"
for thang, spells of submittedCode
transpiledCode[thang] = {}
for spellID, spell of spells
2014-06-30 22:16:26 -04:00
spellName = thang + '/' + spellID
if session.teamSpells and not (spellName in session.teamSpells[session.team]) then continue
#console.log "Transpiling spell #{spellName}"
aetherOptions =
problems: {}
language: session.submittedCodeLanguage
functionName: spellID
2014-05-13 13:27:44 -04:00
functionParameters: []
2014-06-30 22:16:26 -04:00
yieldConditionally: spellID is 'plan'
globals: ['Vector', '_']
protectAPI: true
includeFlow: false
executionLimit: 1 * 1000 * 1000
2014-06-30 22:16:26 -04:00
if spellID is 'hear' then aetherOptions.functionParameters = ['speaker', 'message', 'data']
if spellID is 'makeBid' then aetherOptions.functionParameters = ['tileGroupLetter']
if spellID is 'findCentroids' then aetherOptions.functionParameters = ['centroids']
aether = new Aether aetherOptions
transpiledCode[thang][spellID] = aether.transpile spell
conditions =
2014-06-30 22:16:26 -04:00
'_id': sessionID
update =
2014-06-30 22:16:26 -04:00
'transpiledCode': transpiledCode
query = LevelSession.update(conditions, update)
query.exec (err, numUpdated) -> cb err
findLadderLevelSessions = (levelID, cb) ->
queryParameters =
2014-06-30 22:16:26 -04:00
'level.original': levelID + ''
submitted: true
2014-06-30 22:16:26 -04:00
selectString = '_id'
query = LevelSession.find(queryParameters).select(selectString).lean()
query.exec (err, levelSessions) ->
if err then return cb err
2014-06-30 22:16:26 -04:00
levelSessionIDs = _.pluck levelSessions, '_id'
async.eachSeries levelSessionIDs, transpileLevelSession, (err) ->
if err then return cb err
return cb null
transpileLadderSessions = ->
queryParameters =
2014-06-30 22:16:26 -04:00
type: 'ladder'
'version.isLatestMajor': true
'version.isLatestMinor': true
selectString = 'original'
query = Level.find(queryParameters).select(selectString).lean()
query.exec (err, ladderLevels) ->
throw err if err
2014-06-30 22:16:26 -04:00
ladderLevels = _.pluck ladderLevels, 'original'
async.eachSeries ladderLevels, findLadderLevelSessions, (err) ->
throw err if err
serverSetup.connectToDatabase()
transpileLadderSessions()
2014-06-22 01:31:10 -04:00
# 2014-06-21: took about an hour to do 5480 sessions, ~93/min
# eta: db.level.sessions.find({submitted: true}).count() / 93