2014-07-01 10:16:26 +08:00
CocoModel = require ' ./CocoModel '
2014-01-03 10:32:13 -08:00
module.exports = class LevelSession extends CocoModel
2014-07-01 10:16:26 +08:00
@className: ' LevelSession '
2014-04-22 11:11:08 -07:00
@schema: require ' schemas/models/level_session '
2014-07-01 10:16:26 +08:00
urlRoot: ' /db/level.session '
2014-01-03 10:32:13 -08:00
initialize: ->
super ( )
@ on ' sync ' , (e) =>
state = @ get ( ' state ' ) or { }
state . scripts ? = { }
@ set ( ' state ' , state )
2014-04-22 11:11:08 -07:00
2014-01-03 10:32:13 -08:00
updatePermissions: ->
2014-09-01 09:11:10 -07:00
permissions = @ get ' permissions ' , true
2014-01-03 10:32:13 -08:00
permissions = ( p for p in permissions when p . target isnt ' public ' )
if @ get ( ' multiplayer ' )
2014-07-01 10:16:26 +08:00
permissions . push { target: ' public ' , access: ' write ' }
2014-01-03 10:32:13 -08:00
@ set ' permissions ' , permissions
getSourceFor: (spellKey) ->
# spellKey ex: 'tharin/plan'
code = @ get ( ' code ' )
parts = spellKey . split ' / '
code ? [ parts [ 0 ] ] ? [ parts [ 1 ] ]
2014-05-05 16:59:12 -07:00
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-07-01 10:16:26 +08:00
thangSpellArr = ( s . split ( ' / ' ) for s in @ get ( ' teamSpells ' ) [ team ] )
2014-05-05 16:59:12 -07:00
for item in thangSpellArr
thang = item [ 0 ]
spell = item [ 1 ]
2014-06-20 20:52:50 -07:00
return true if c1 [ thang ] [ spell ] isnt c2 [ thang ] ? [ spell ]
2014-05-05 16:59:12 -07:00
false
2014-07-29 16:50:07 +02:00
2014-08-03 23:58:51 +02:00
isMultiplayer: ->
2014-07-29 20:11:45 +02:00
@ get ( ' team ' ) ? # Only multiplayer level sessions have teams defined
2014-08-07 22:03:00 +02:00
completed: ->
@ get ( ' state ' ) ? . complete || false
2014-09-04 11:04:16 -07:00
shouldAvoidCorruptData: (attrs) ->
return false unless me . team is ' humans '
if _ . string . startsWith ( attrs ? . code ? @ get ( ' code ' ) ) ? . anya ? . makeBid ? ' ' , ' var __interceptThis '
noty text: " Not saving session--it ' s trying to overwrite Anya ' s code with transpiled output. Please let us know and help us reproduce this bug! " , layout: ' topCenter ' , type: ' error ' , killer: false , timeout: 120000
return true
false
save: (attrs, options) ->
return if @ shouldAvoidCorruptData attrs
super attrs , options
2015-01-05 10:44:17 -08:00
increaseDifficulty: ->
state = @ get ( ' state ' ) ? { }
state.difficulty = ( state . difficulty ? 0 ) + 1
delete state . lastUnsuccessfulSubmissionTime
@ set ' state ' , state
2015-01-06 12:40:10 -08:00
@ save ( )
2015-01-05 10:44:17 -08:00
timeUntilResubmit: ->
state = @ get ( ' state ' ) ? { }
return 0 unless last = state . lastUnsuccessfulSubmissionTime
last = new Date ( last ) if _ . isString last
# Wait at least this long before allowing submit button active again.
( last - new Date ( ) ) + 22 * 60 * 60 * 1000