Refactored LevelLoader to load sessions after levels, so that we can do the right thing with hero levels' sessions. Made levels default to type: 'hero'.

This commit is contained in:
Nick Winter 2014-09-01 20:53:53 -07:00
parent 9094c0ce19
commit 632559f7db
4 changed files with 48 additions and 42 deletions

View file

@ -35,7 +35,6 @@ module.exports = class LevelLoader extends CocoClass
@worldNecessities = []
@listenTo @supermodel, 'resource-loaded', @onWorldNecessityLoaded
@loadSession()
@loadLevel()
@loadAudio()
@playJingle()
@ -44,14 +43,19 @@ module.exports = class LevelLoader extends CocoClass
else
@listenToOnce @supermodel, 'loaded-all', @onSupermodelLoaded
playJingle: ->
return if @headless
# Apparently the jingle, when it tries to play immediately during all this loading, you can't hear it.
# Add the timeout to fix this weird behavior.
f = ->
jingles = ['ident_1', 'ident_2']
AudioPlayer.playInterfaceSound jingles[Math.floor Math.random() * jingles.length]
setTimeout f, 500
# Supermodel (Level) Loading
loadLevel: ->
@level = @supermodel.getModel(Level, @levelID) or new Level _id: @levelID
if @level.loaded
@onLevelLoaded()
else
@level = @supermodel.loadModel(@level, 'level').model
@listenToOnce @level, 'sync', @onLevelLoaded
onLevelLoaded: ->
@loadSession()
@populateLevel()
# Session Loading
@ -83,29 +87,19 @@ module.exports = class LevelLoader extends CocoClass
@listenToOnce @opponentSession, 'sync', @loadDependenciesForSession
loadDependenciesForSession: (session) ->
return if @levelID is 'sky-span' # TODO
# TODO: I think this runs afoul of https://github.com/codecombat/codecombat/issues/1108
# TODO: this shouldn't happen when it's not a hero level, but we don't have level loaded yet,
# and the sessions are being created with default hero config regardless of whether it's a hero level.
if heroConfig = session.get('heroConfig')
url = "/db/thang.type/#{heroConfig.thangType}/version?project=name,components,original"
return unless @level.get('type', true) is 'hero'
heroConfig = session.get('heroConfig')
unless heroConfig
heroConfig = {inventory: {}, thangType: '529ffbf1cf1818f2be000001'} # Temp: assign Tharin as the hero
session.set 'heroConfig', heroConfig
url = "/db/thang.type/#{heroConfig.thangType}/version?project=name,components,original"
@worldNecessities.push @maybeLoadURL(url, ThangType, 'thang')
for itemThangType in _.values(heroConfig.inventory)
url = "/db/thang.type/#{itemThangType}/version?project=name,components,original"
@worldNecessities.push @maybeLoadURL(url, ThangType, 'thang')
for itemThangType in _.values(heroConfig.inventory)
url = "/db/thang.type/#{itemThangType}/version?project=name,components,original"
@worldNecessities.push @maybeLoadURL(url, ThangType, 'thang')
# Supermodel (Level) Loading
loadLevel: ->
@level = @supermodel.getModel(Level, @levelID) or new Level _id: @levelID
if @level.loaded
@populateLevel()
else
@level = @supermodel.loadModel(@level, 'level').model
@listenToOnce @level, 'sync', @onLevelLoaded
onLevelLoaded: ->
@populateLevel()
# Grabbing the rest of the required data for the level
populateLevel: ->
thangIDs = []
@ -332,6 +326,15 @@ module.exports = class LevelLoader extends CocoClass
# Initial Sound Loading
playJingle: ->
return if @headless
# Apparently the jingle, when it tries to play immediately during all this loading, you can't hear it.
# Add the timeout to fix this weird behavior.
f = ->
jingles = ['ident_1', 'ident_2']
AudioPlayer.playInterfaceSound jingles[Math.floor Math.random() * jingles.length]
setTimeout f, 500
loadAudio: ->
return if @headless
AudioPlayer.preloadInterfaceSounds ['victory']

View file

@ -30,7 +30,7 @@ module.exports = class Level extends CocoModel
denormalize: (supermodel, session) ->
o = $.extend true, {}, @attributes
if @get('type') is 'hero'
if @get('type', true) is 'hero'
# TOOD: figure out if/when/how we are doing this for non-Hero levels that aren't expecting denormalization.
for levelThang in o.thangs
@denormalizeThang(levelThang, supermodel, session)

View file

@ -212,6 +212,7 @@ LevelSchema = c.object {
thangs: []
systems: []
victory: {}
type: 'hero'
}
c.extendNamedProperties LevelSchema # let's have the name be the first property
_.extend LevelSchema.properties,

View file

@ -112,12 +112,14 @@ describe 'LevelLoader', ->
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/level.component/jumps/version/0' in urls).toBeTruthy()
it 'is idempotent', ->
levelLoader = new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
# first load Tharin by the 'normal' session load
responses = { '/db/level_session/id': sessionWithTharinWithHelmet }
responses = '/db/level/id': levelWithOgreWithMace
jasmine.Ajax.requests.sendResponses(responses)
responses = '/db/level_session/id': sessionWithTharinWithHelmet
jasmine.Ajax.requests.sendResponses(responses)
numRequestsBefore = jasmine.Ajax.requests.count()
@ -126,21 +128,21 @@ describe 'LevelLoader', ->
levelLoader.loadDependenciesForSession(session)
levelLoader.loadDependenciesForSession(session)
levelLoader.loadDependenciesForSession(session)
numRequestsAfter = jasmine.Ajax.requests.count()
expect(numRequestsBefore).toBe(numRequestsAfter)
numRequestsAfter = jasmine.Ajax.requests.count()
expect(numRequestsAfter).toBe(numRequestsBefore)
it 'loads thangs for items that the level thangs have in their Equips component configs', ->
new LevelLoader({supermodel:supermodel = new SuperModel(), sessionID: 'id', levelID: 'id'})
responses = {
'/db/level/id': levelWithOgreWithMace
responses = {
'/db/level/id': levelWithOgreWithMace
}
jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/thang.type/mace/version?project=name,components,original' in urls).toBeTruthy()
it 'loads components which are inherited by level thangs from thang type default components', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
@ -152,7 +154,7 @@ describe 'LevelLoader', ->
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/level.component/physical/version/0' in urls).toBeTruthy()
it 'loads item thang types which are inherited by level thangs from thang type default equips component configs', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
@ -164,7 +166,7 @@ describe 'LevelLoader', ->
requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests)
expect('/db/thang.type/wand/version?project=name,components,original' in urls).toBeTruthy()
it 'loads components for item thang types which are inherited by level thangs from thang type default equips component configs', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})