codecombat/app/models/LevelSystem.coffee

41 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
CocoModel = require('./CocoModel')
2014-04-18 16:02:14 -04:00
SystemNameLoader = require('lib/SystemNameLoader')
2014-01-03 13:32:13 -05:00
module.exports = class LevelSystem extends CocoModel
@className: "LevelSystem"
@schema: require 'schemas/models/level_system'
2014-01-03 13:32:13 -05:00
urlRoot: "/db/level.system"
set: (key, val, options) ->
if _.isObject key
[attrs, options] = [key, val]
else
(attrs = {})[key] = val
if 'code' of attrs and not ('js' of attrs)
attrs.js = @compile attrs.code
super attrs, options
2014-02-11 18:47:59 -05:00
onLoaded: ->
2014-01-03 13:32:13 -05:00
super()
@set 'js', @compile(@get 'code') unless @get 'js'
2014-04-18 16:02:14 -04:00
SystemNameLoader.setName @
2014-01-03 13:32:13 -05:00
compile: (code) ->
if @get('codeLanguage') and @get('codeLanguage') isnt 'coffeescript'
return console.error("Can't compile", @get('codeLanguage'), "-- only CoffeeScript.", @)
2014-01-03 13:32:13 -05:00
try
js = CoffeeScript.compile(code, bare: true)
catch e
#console.log "couldn't compile", code, "for", @get('name'), "because", e
js = @get 'js'
js
getDependencies: (allSystems) ->
results = []
for dep in @get('dependencies') or []
system = _.find allSystems, (sys) ->
sys.get('original') is dep.original and sys.get('version').major is dep.majorVersion
for result in system.getDependencies(allSystems).concat [system]
results.push result unless result in results
results