codecombat/app/views/play/level/tome/Spell.coffee

217 lines
8.9 KiB
CoffeeScript
Raw Normal View History

SpellView = require './SpellView'
SpellListTabEntryView = require './SpellListTabEntryView'
2014-01-03 13:32:13 -05:00
{me} = require 'lib/auth'
2014-08-30 16:43:56 -04:00
{createAetherOptions} = require 'lib/aether_utils'
2014-05-08 14:43:00 -04:00
2014-01-03 13:32:13 -05:00
module.exports = class Spell
loaded: false
view: null
entryView: null
2014-01-28 18:24:08 -05:00
constructor: (options) ->
@spellKey = options.spellKey
@pathComponents = options.pathComponents
@session = options.session
@otherSession = options.otherSession
@spectateView = options.spectateView
@spectateOpponentCodeLanguage = options.spectateOpponentCodeLanguage
2014-01-28 18:24:08 -05:00
@supermodel = options.supermodel
@skipProtectAPI = options.skipProtectAPI
@worker = options.worker
2014-08-14 14:55:43 -04:00
@levelID = options.levelID
@levelType = options.level.get('type', true)
2014-01-28 18:24:08 -05:00
p = options.programmableMethod
@languages = p.languages ? {}
@languages.javascript ?= p.source
2014-01-03 13:32:13 -05:00
@name = p.name
@permissions = read: p.permissions?.read ? [], readwrite: p.permissions?.readwrite ? [] # teams
if @canWrite()
@setLanguage options.language
2014-06-20 21:45:57 -04:00
else if @isEnemySpell()
@setLanguage @otherSession?.get('submittedCodeLanguage') ? @spectateOpponentCodeLanguage
else
@setLanguage 'javascript'
@useTranspiledCode = @shouldUseTranspiledCode()
2014-06-30 22:16:26 -04:00
console.log 'Spell', @spellKey, 'is using transpiled code (should only happen if it\'s an enemy/spectate writable method).' if @useTranspiledCode
@source = @originalSource
2014-04-26 17:21:26 -04:00
@parameters = p.parameters
if @permissions.readwrite.length and sessionSource = @session.getSourceFor(@spellKey)
if sessionSource isnt '// Should fill in some default source\n' # TODO: figure out why session is getting this default source in there and stop it
@source = sessionSource
2014-01-03 13:32:13 -05:00
@thangs = {}
if @canRead() # We can avoid creating these views if we'll never use them.
@view = new SpellView {spell: @, level: options.level, session: @session, worker: @worker}
@view.render() # Get it ready and code loaded in advance
@tabView = new SpellListTabEntryView spell: @, supermodel: @supermodel, codeLanguage: @language, level: options.level
@tabView.render()
2014-06-30 22:16:26 -04:00
@team = @permissions.readwrite[0] ? 'common'
Backbone.Mediator.publish 'tome:spell-created', spell: @
Backbone.Mediator.subscribe 'real-time-multiplayer:new-opponent-code', @onNewOpponentCode
2014-01-03 13:32:13 -05:00
2014-02-11 15:10:21 -05:00
destroy: ->
@view?.destroy()
@tabView?.destroy()
2014-02-11 18:38:36 -05:00
@thangs = null
@worker = null
setLanguage: (@language) ->
#console.log 'setting language to', @language, 'so using original source', @languages[language] ? @languages.javascript
@originalSource = @languages[language] ? @languages.javascript
2014-01-03 13:32:13 -05:00
addThang: (thang) ->
if @thangs[thang.id]
@thangs[thang.id].thang = thang
else
@thangs[thang.id] = {thang: thang, aether: @createAether(thang), castAether: null}
2014-01-03 13:32:13 -05:00
removeThangID: (thangID) ->
delete @thangs[thangID]
2014-01-03 13:32:13 -05:00
canRead: (team) ->
(team ? me.team) in @permissions.read or (team ? me.team) in @permissions.readwrite
canWrite: (team) ->
(team ? me.team) in @permissions.readwrite
getSource: ->
@view?.getSource() ? @source
2014-01-03 13:32:13 -05:00
transpile: (source) ->
if source
@source = source
else
source = @getSource()
[pure, problems] = [null, null]
if @useTranspiledCode
transpiledCode = @session.get('code')
for thangID, spellThang of @thangs
unless pure
if @useTranspiledCode and transpiledSpell = transpiledCode[@spellKey.split('/')[0]]?[@name]
spellThang.aether.pure = transpiledSpell
else
pure = spellThang.aether.transpile source
problems = spellThang.aether.problems
2014-06-30 22:16:26 -04:00
#console.log 'aether transpiled', source.length, 'to', spellThang.aether.pure.length, 'for', thangID, @spellKey
else
spellThang.aether.raw = source
spellThang.aether.pure = pure
spellThang.aether.problems = problems
2014-06-30 22:16:26 -04:00
#console.log 'aether reused transpilation for', thangID, @spellKey
null
2014-01-03 13:32:13 -05:00
hasChanged: (newSource=null, currentSource=null) ->
(newSource ? @originalSource) isnt (currentSource ? @source)
hasChangedSignificantly: (newSource=null, currentSource=null, cb) ->
2014-01-03 13:32:13 -05:00
for thangID, spellThang of @thangs
aether = spellThang.aether
break
unless aether
2014-06-30 22:16:26 -04:00
console.error @toString(), 'couldn\'t find a spellThang with aether of', @thangs
2014-04-22 14:04:56 -04:00
cb false
if @worker
workerMessage =
function: 'hasChangedSignificantly'
a: (newSource ? @originalSource)
spellKey: @spellKey
b: (currentSource ? @source)
careAboutLineNumbers: true
careAboutLint: true
@worker.addEventListener 'message', (e) =>
workerData = JSON.parse e.data
if workerData.function is 'hasChangedSignificantly' and workerData.spellKey is @spellKey
@worker.removeEventListener 'message', arguments.callee, false
cb(workerData.hasChanged)
@worker.postMessage JSON.stringify(workerMessage)
else
cb(aether.hasChangedSignificantly((newSource ? @originalSource), (currentSource ? @source), true, true))
2014-04-26 17:21:26 -04:00
2014-01-03 13:32:13 -05:00
createAether: (thang) ->
aceConfig = me.get('aceConfig') ? {}
writable = @permissions.readwrite.length > 0
2014-08-30 16:43:56 -04:00
skipProtectAPI = @skipProtectAPI or not writable
problemContext = @createProblemContext thang
aetherOptions = createAetherOptions functionName: @name, codeLanguage: @language, functionParameters: @parameters, skipProtectAPI: skipProtectAPI, includeFlow: @levelType is 'hero', problemContext: problemContext
2014-01-03 13:32:13 -05:00
aether = new Aether aetherOptions
if @worker
workerMessage =
function: 'createAether'
spellKey: @spellKey
options: aetherOptions
@worker.postMessage JSON.stringify workerMessage
2014-01-03 13:32:13 -05:00
aether
updateLanguageAether: (@language) ->
for thangId, spellThang of @thangs
spellThang.aether?.setLanguage @language
spellThang.castAether = null
Backbone.Mediator.publish 'tome:spell-changed-language', spell: @, language: @language
if @worker
workerMessage =
function: 'updateLanguageAether'
newLanguage: @language
@worker.postMessage JSON.stringify workerMessage
@transpile()
2014-01-03 13:32:13 -05:00
toString: ->
"<Spell: #{@spellKey}>"
isEnemySpell: ->
2014-06-20 21:45:57 -04:00
return false unless @permissions.readwrite.length
return false unless @otherSession or @spectateView
teamSpells = @session.get('teamSpells')
team = @session.get('team') ? 'humans'
teamSpells and not _.contains(teamSpells[team], @spellKey)
shouldUseTranspiledCode: ->
# Determine whether this code has already been transpiled, or whether it's raw source needing transpilation.
return true if @spectateView # Use transpiled code for both teams if we're just spectating.
return true if @isEnemySpell() # Use transpiled for enemy spells.
# Players without permissions can't view the raw code.
return true if @session.get('creator') isnt me.id and not (me.isAdmin() or 'employer' in me.get('permissions', true))
false
onNewOpponentCode: (e) =>
return unless @spellKey and @canWrite e.team
if e.codeLanguage and e.code
[thangSlug, methodSlug] = @spellKey.split '/'
if opponentCode = e.code[thangSlug]?[methodSlug]
@source = opponentCode
@updateLanguageAether e.codeLanguage
else
console.error 'Spell onNewOpponentCode did not receive code', e
createProblemContext: (thang) ->
# Create problemContext Aether can use to craft better error messages
# stringReferences: values that should be referred to as a string instead of a variable (e.g. "Brak", not Brak)
# thisMethods: methods available on the 'this' object
# thisProperties: properties available on the 'this' object
2014-10-14 20:53:17 -04:00
# commonThisMethods: methods that are available sometimes, but not awlays
# NOTE: Assuming the first createProblemContext call has everything we need, and we'll use that forevermore
return @problemContext if @problemContext?
@problemContext = { stringReferences: [], thisMethods: [], thisProperties: [] }
2014-10-14 20:53:17 -04:00
# TODO: These should be read from the database
@problemContext.commonThisMethods = ['moveRight', 'moveLeft', 'moveUp', 'moveDown', 'attack', 'findNearestEnemy', 'buildXY', 'moveXY', 'say', 'move', 'distance', 'findEnemies', 'getFriends', 'addFlag', 'getFlag', 'removeFlag', 'getFlags', 'attackRange', 'cast', 'buildTypes', 'jump', 'jumpTo', 'attackXY']
return @problemContext unless thang?
# Populate stringReferences
for key, value of thang.world?.thangMap
if (value.isAttackable or value.isSelectable) and value.id not in @problemContext.stringReferences
@problemContext.stringReferences.push value.id
# Populate thisMethods and thisProperties
if thang.programmableProperties?
for prop in thang.programmableProperties
if _.isFunction(thang[prop])
@problemContext.thisMethods.push prop
else
@problemContext.thisProperties.push prop
# TODO: See SpellPaletteView.createPalette() for other interesting contextual properties
@problemContext