codecombat/app/models/LevelSession.coffee

39 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2014-06-30 22:16:26 -04:00
CocoModel = require './CocoModel'
2014-01-03 13:32:13 -05:00
module.exports = class LevelSession extends CocoModel
2014-06-30 22:16:26 -04:00
@className: 'LevelSession'
@schema: require 'schemas/models/level_session'
2014-06-30 22:16:26 -04:00
urlRoot: '/db/level.session'
2014-01-03 13:32:13 -05:00
initialize: ->
super()
@on 'sync', (e) =>
state = @get('state') or {}
state.scripts ?= {}
@set('state', state)
2014-01-03 13:32:13 -05:00
updatePermissions: ->
permissions = @get 'permissions'
permissions = (p for p in permissions when p.target isnt 'public')
if @get('multiplayer')
2014-06-30 22:16:26 -04:00
permissions.push {target: 'public', access: 'write'}
2014-01-03 13:32:13 -05:00
@set 'permissions', permissions
getSourceFor: (spellKey) ->
# spellKey ex: 'tharin/plan'
code = @get('code')
parts = spellKey.split '/'
code?[parts[0]]?[parts[1]]
readyToRank: ->
return false unless @get('levelID') # If it hasn't been denormalized, then it's not ready.
return false unless c1 = @get('code')
return false unless team = @get('team')
return true unless c2 = @get('submittedCode')
2014-06-30 22:16:26 -04:00
thangSpellArr = (s.split('/') for s in @get('teamSpells')[team])
for item in thangSpellArr
thang = item[0]
spell = item[1]
2014-06-20 23:52:50 -04:00
return true if c1[thang][spell] isnt c2[thang]?[spell]
false