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 '
2014-04-22 14:11:08 -04:00
@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-04-22 14:11:08 -04:00
2014-01-03 13:32:13 -05:00
updatePermissions: ->
2014-09-01 12:11:10 -04:00
permissions = @ get ' permissions ' , true
2014-01-03 13:32:13 -05:00
permissions = ( p for p in permissions when p . target isnt ' public ' )
@ set ' permissions ' , permissions
getSourceFor: (spellKey) ->
# spellKey ex: 'tharin/plan'
code = @ get ( ' code ' )
parts = spellKey . split ' / '
code ? [ parts [ 0 ] ] ? [ parts [ 1 ] ]
2014-05-05 19:59:12 -04: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-06-30 22:16:26 -04:00
thangSpellArr = ( s . split ( ' / ' ) for s in @ get ( ' teamSpells ' ) [ team ] )
2015-02-17 14:51:52 -05:00
for item in thangSpellArr
2014-05-05 19:59:12 -04:00
thang = item [ 0 ]
spell = item [ 1 ]
2014-06-20 23:52:50 -04:00
return true if c1 [ thang ] [ spell ] isnt c2 [ thang ] ? [ spell ]
2014-05-05 19:59:12 -04:00
false
2014-07-29 10:50:07 -04:00
2014-08-03 17:58:51 -04:00
isMultiplayer: ->
2015-02-17 12:49:56 -05:00
@ get ( ' submittedCodeLanguage ' ) ? and @ get ( ' team ' ) ?
2014-08-07 16:03:00 -04:00
completed: ->
2016-06-17 18:15:13 -04:00
@ get ( ' state ' ) ? . complete || @ get ( ' submitted ' ) || false
2014-09-04 14:04:16 -04: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 13:44:17 -05:00
2015-01-22 15:26:37 -05:00
increaseDifficulty: (callback) ->
2015-01-05 13:44:17 -05:00
state = @ get ( ' state ' ) ? { }
state.difficulty = ( state . difficulty ? 0 ) + 1
delete state . lastUnsuccessfulSubmissionTime
@ set ' state ' , state
2015-01-16 15:50:10 -05:00
@ trigger ' change-difficulty '
2015-01-22 15:26:37 -05:00
@ save null , success: callback
2015-01-05 13:44:17 -05: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.
2016-03-23 18:56:45 -04:00
wait = ( last - new Date ( ) ) + 22 * 60 * 60 * 1000
if wait > 24 * 60 * 60 * 1000
# System clock must've gotten busted; max out at one day's wait.
wait = 24 * 60 * 60 * 1000
state.lastUnsuccessfulSubmissionTime = new Date ( )
@ set ' state ' , state
wait
2015-01-31 00:36:36 -05:00
recordScores: (scores, level) ->
2016-07-15 12:53:08 -04:00
return unless scores
2015-01-31 00:36:36 -05:00
state = @ get ' state '
oldTopScores = state . topScores ? [ ]
newTopScores = [ ]
now = new Date ( )
for scoreType in level . get ( ' scoreTypes ' ) ? [ ]
oldTopScore = _ . find oldTopScores , type: scoreType
newScore = scores [ scoreType ]
unless newScore ?
newTopScores . push oldTopScore
continue
newScore *= - 1 if scoreType in [ ' time ' , ' damage-taken ' ] # Make it so that higher is better
if not oldTopScore ? or newScore > oldTopScore . score
newTopScores . push type: scoreType , date: now , score: newScore
else
newTopScores . push oldTopScore
state.topScores = newTopScores
@ set ' state ' , state
2016-07-13 14:43:25 -04:00
generateSpellsObject: ->
2016-07-13 16:46:03 -04:00
{ createAetherOptions } = require ' lib/aether_utils '
2016-07-13 14:43:25 -04:00
aetherOptions = createAetherOptions functionName: ' plan ' , codeLanguage: @ get ( ' codeLanguage ' )
spellThang = aether: new Aether aetherOptions
spells = " hero-placeholder/plan " : thangs: { ' Hero Placeholder ' : spellThang } , name: ' plan '
source = @ get ( ' code ' ) [ ' hero-placeholder ' ] . plan
try
spellThang . aether . transpile source
catch e
console . log " Couldn ' t transpile! \n #{ source } \n " , e
spellThang . aether . transpile ' '
spells