mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Update Simulator to new, simple way of making UserCodeMap
This commit is contained in:
parent
6c9f351f01
commit
e3453cb0f3
1 changed files with 14 additions and 95 deletions
|
@ -114,7 +114,6 @@ module.exports = class Simulator extends CocoClass
|
|||
catch error
|
||||
console.log "Failed to form task results:", error
|
||||
return @cleanupAndSimulateAnotherTask()
|
||||
console.log 'Processing results:', taskResults
|
||||
humanSessionRank = taskResults.sessions[0].metrics.rank
|
||||
ogreSessionRank = taskResults.sessions[1].metrics.rank
|
||||
if @options.headlessClient and @options.simulateOnlyOneGame
|
||||
|
@ -377,78 +376,24 @@ module.exports = class Simulator extends CocoClass
|
|||
return 1
|
||||
|
||||
generateSpellsObject: ->
|
||||
@currentUserCodeMap = @task.generateSpellKeyToSourceMap()
|
||||
@spells = {}
|
||||
for thang in @level.attributes.thangs
|
||||
continue if @thangIsATemplate thang
|
||||
@generateSpellKeyToSourceMapPropertiesFromThang thang
|
||||
@spells
|
||||
spells = {}
|
||||
for {hero, team} in [{hero: 'Hero Placeholder', team: 'humans'}, {hero: 'Hero Placeholder 1', team: 'ogres'}]
|
||||
sessionInfo = _.filter(@task.getSessions(), {team: team})[0]
|
||||
fullSpellName = _.string.slugify(hero) + '/plan'
|
||||
submittedCodeLanguage = sessionInfo?.submittedCodeLanguage ? 'javascript'
|
||||
submittedCode = LZString.decompressFromUTF16 sessionInfo?.submittedCode?[_.string.slugify(hero)]?.plan ? ''
|
||||
aether = new Aether createAetherOptions functionName: 'plan', codeLanguage: submittedCodeLanguage, skipProtectAPI: false
|
||||
try
|
||||
aether.transpile submittedCode
|
||||
catch e
|
||||
console.log "Couldn't transpile #{fullSpellName}:\n#{submittedCode}\n", e
|
||||
aether.transpile ''
|
||||
spells[fullSpellName] = name: 'plan', team: team, thang: {thang: {id: hero}, aether: aether}
|
||||
spells
|
||||
|
||||
thangIsATemplate: (thang) ->
|
||||
for component in thang.components
|
||||
continue unless @componentHasProgrammableMethods component
|
||||
for methodName, method of component.config.programmableMethods
|
||||
return true if @methodBelongsToTemplateThang method
|
||||
|
||||
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) ->
|
||||
spellKeyComponents = [thang, methodName]
|
||||
spellKeyComponents[0] = _.string.slugify spellKeyComponents[0]
|
||||
spellKey = spellKeyComponents.join '/'
|
||||
spellKey
|
||||
|
||||
createSpellAndAssignName: (spellKey, spellName) ->
|
||||
@spells[spellKey] ?= {}
|
||||
@spells[spellKey].name = spellName
|
||||
|
||||
createSpellThang: (thang, method, spellKey) ->
|
||||
@spells[spellKey].thangs ?= {}
|
||||
@spells[spellKey].thangs[thang.id] ?= {}
|
||||
spellTeam = @task.getSpellKeyToTeamMap()[spellKey]
|
||||
playerTeams = @task.getPlayerTeams()
|
||||
useProtectAPI = true
|
||||
if spellTeam not in playerTeams
|
||||
useProtectAPI = false
|
||||
else
|
||||
spellSession = _.filter(@task.getSessions(), {team: spellTeam})[0]
|
||||
unless codeLanguage = spellSession?.submittedCodeLanguage
|
||||
console.warn 'Session', spellSession.creatorName, spellSession.team, 'didn\'t have submittedCodeLanguage, just:', spellSession
|
||||
@spells[spellKey].thangs[thang.id].aether = @createAether @spells[spellKey].name, method, useProtectAPI, codeLanguage ? 'javascript'
|
||||
|
||||
transpileSpell: (thang, spellKey, methodName) ->
|
||||
slugifiedThangID = _.string.slugify thang.id
|
||||
generatedSpellKey = [slugifiedThangID,methodName].join '/'
|
||||
source = @currentUserCodeMap[generatedSpellKey] ? ''
|
||||
aether = @spells[spellKey].thangs[thang.id].aether
|
||||
#unless _.contains(@task.spellKeysToTranspile, generatedSpellKey)
|
||||
try
|
||||
aether.transpile source
|
||||
catch e
|
||||
console.log "Couldn't transpile #{spellKey}:\n#{source}\n", e
|
||||
aether.transpile ''
|
||||
|
||||
createAether: (methodName, method, useProtectAPI, codeLanguage) ->
|
||||
aetherOptions = createAetherOptions functionName: methodName, codeLanguage: codeLanguage, skipProtectAPI: not useProtectAPI
|
||||
return new Aether aetherOptions
|
||||
|
||||
class SimulationTask
|
||||
constructor: (@rawData) ->
|
||||
@spellKeyToTeamMap = {}
|
||||
|
||||
getLevelName: ->
|
||||
levelName = @rawData.sessions?[0]?.levelID
|
||||
|
@ -476,30 +421,4 @@ class SimulationTask
|
|||
|
||||
getSessions: -> @rawData.sessions
|
||||
|
||||
getSpellKeyToTeamMap: -> @spellKeyToTeamMap
|
||||
|
||||
getPlayerTeams: -> _.pluck @rawData.sessions, 'team'
|
||||
|
||||
setWorld: (@world) ->
|
||||
|
||||
generateSpellKeyToSourceMap: ->
|
||||
# TODO: we always now only have hero-placeholder/plan vs. hero-placeholder-1/plan on humans vs. ogres, always just have to retranspile for Esper, and never need to transpile for NPCs or other methods, so we can get rid of almost all of this stuff.
|
||||
playerTeams = _.pluck @rawData.sessions, 'team'
|
||||
spellKeyToSourceMap = {}
|
||||
for session in @rawData.sessions
|
||||
teamSpells = session.teamSpells[session.team]
|
||||
allTeams = _.keys session.teamSpells
|
||||
for team in allTeams
|
||||
for spell in session.teamSpells[team]
|
||||
@spellKeyToTeamMap[spell] = team
|
||||
teamCode = {}
|
||||
|
||||
for thangName, thangSpells of session.submittedCode
|
||||
for spellName, spell of thangSpells
|
||||
fullSpellName = [thangName, spellName].join '/'
|
||||
if _.contains(teamSpells, fullSpellName)
|
||||
teamCode[fullSpellName] = LZString.decompressFromUTF16 spell
|
||||
|
||||
_.merge spellKeyToSourceMap, teamCode
|
||||
|
||||
spellKeyToSourceMap
|
||||
|
|
Loading…
Reference in a new issue