LevelLoader and Simulator fixes for simulating hero-ladder matches. Commented out some logging that hasn't been very useful. Fixed some tests.

This commit is contained in:
Nick Winter 2014-10-19 21:56:26 -07:00
parent 1fb48f2b85
commit 08d9e39764
9 changed files with 38 additions and 26 deletions

View file

@ -61,7 +61,6 @@ module.exports = class LevelLoader extends CocoClass
# Session Loading # Session Loading
loadSession: -> loadSession: ->
return if @headless
if @sessionID if @sessionID
url = "/db/level.session/#{@sessionID}" url = "/db/level.session/#{@sessionID}"
else else
@ -71,6 +70,11 @@ module.exports = class LevelLoader extends CocoClass
session = new LevelSession().setURL url session = new LevelSession().setURL url
@sessionResource = @supermodel.loadModel(session, 'level_session', {cache: false}) @sessionResource = @supermodel.loadModel(session, 'level_session', {cache: false})
@session = @sessionResource.model @session = @sessionResource.model
if @opponentSessionID
opponentSession = new LevelSession().setURL "/db/level.session/#{@opponentSessionID}"
@opponentSessionResource = @supermodel.loadModel(opponentSession, 'opponent_session')
@opponentSession = @opponentSessionResource.model
if @session.loaded if @session.loaded
@session.setURL '/db/level.session/' + @session.id @session.setURL '/db/level.session/' + @session.id
@loadDependenciesForSession @session @loadDependenciesForSession @session
@ -78,11 +82,7 @@ module.exports = class LevelLoader extends CocoClass
@listenToOnce @session, 'sync', -> @listenToOnce @session, 'sync', ->
@session.setURL '/db/level.session/' + @session.id @session.setURL '/db/level.session/' + @session.id
@loadDependenciesForSession @session @loadDependenciesForSession @session
if @opponentSession
if @opponentSessionID
opponentSession = new LevelSession().setURL "/db/level.session/#{@opponentSessionID}"
@opponentSessionResource = @supermodel.loadModel(opponentSession, 'opponent_session')
@opponentSession = @opponentSessionResource.model
if @opponentSession.loaded if @opponentSession.loaded
@loadDependenciesForSession @opponentSession @loadDependenciesForSession @opponentSession
else else
@ -92,11 +92,13 @@ module.exports = class LevelLoader extends CocoClass
if session is @session if session is @session
Backbone.Mediator.publish 'level:session-loaded', level: @level, session: @session Backbone.Mediator.publish 'level:session-loaded', level: @level, session: @session
@consolidateFlagHistory() if @opponentSession?.loaded @consolidateFlagHistory() if @opponentSession?.loaded
else else if session is @opponentSession
@consolidateFlagHistory() if @session.loaded @consolidateFlagHistory() if @session.loaded
return unless @level.get('type', true) in ['hero', 'hero-ladder', 'hero-coop'] return unless @level.get('type', true) in ['hero', 'hero-ladder', 'hero-coop']
@sessionDependenciesRegistered ?= {}
heroConfig = session.get('heroConfig') heroConfig = session.get('heroConfig')
heroConfig ?= me.get('heroConfig') if session is @session heroConfig ?= me.get('heroConfig') if session is @session and not @headless
console.error @level.get('name'), session.get('team'), session.get('creatorName'), 'had hero', heroConfig.thangType
heroConfig ?= {inventory: {}, thangType: '529ffbf1cf1818f2be000001'} # If all else fails, assign Tharin as the hero. heroConfig ?= {inventory: {}, thangType: '529ffbf1cf1818f2be000001'} # If all else fails, assign Tharin as the hero.
session.set 'heroConfig', heroConfig unless _.isEqual heroConfig, session.get('heroConfig') session.set 'heroConfig', heroConfig unless _.isEqual heroConfig, session.get('heroConfig')
url = "/db/thang.type/#{heroConfig.thangType}/version" url = "/db/thang.type/#{heroConfig.thangType}/version"
@ -115,6 +117,9 @@ module.exports = class LevelLoader extends CocoClass
itemThangType = @supermodel.getModel url itemThangType = @supermodel.getModel url
@loadDefaultComponentsForThangType itemThangType @loadDefaultComponentsForThangType itemThangType
@loadThangsRequiredByThangType itemThangType @loadThangsRequiredByThangType itemThangType
@sessionDependenciesRegistered[session.id] = true
if _.size(@sessionDependenciesRegistered) is 2 and not (r for r in @worldNecessities when r?).length
@onWorldNecessitiesLoaded()
consolidateFlagHistory: -> consolidateFlagHistory: ->
state = @session.get('state') ? {} state = @session.get('state') ? {}
@ -172,7 +177,7 @@ module.exports = class LevelLoader extends CocoClass
url = "/db/level/#{obj.original}/version/#{obj.majorVersion}" url = "/db/level/#{obj.original}/version/#{obj.majorVersion}"
@maybeLoadURL url, Level, 'level' @maybeLoadURL url, Level, 'level'
unless @headless unless @headless or @level.get('type', true) in ['hero', 'hero-ladder', 'hero-coop']
wizard = ThangType.loadUniversalWizard() wizard = ThangType.loadUniversalWizard()
@supermodel.loadModel wizard, 'thang' @supermodel.loadModel wizard, 'thang'
@ -216,9 +221,10 @@ module.exports = class LevelLoader extends CocoClass
return unless index >= 0 return unless index >= 0
@worldNecessities.splice(index, 1) @worldNecessities.splice(index, 1)
@worldNecessities = (r for r in @worldNecessities when r?) @worldNecessities = (r for r in @worldNecessities when r?)
@onWorldNecessitiesLoaded() if @worldNecessities.length is 0 if @worldNecessities.length is 0 and (not @sessionDependenciesRegistered or @sessionDependenciesRegistered[@session.id] and (not @opponentSession or @sessionDependenciesRegistered[@opponentSession.id]))
@onWorldNecessitiesLoaded()
onWorldNecessitiesLoaded: => onWorldNecessitiesLoaded: ->
@initWorld() @initWorld()
@supermodel.clearMaxProgress() @supermodel.clearMaxProgress()
@trigger 'world-necessities-loaded' @trigger 'world-necessities-loaded'

View file

@ -45,7 +45,7 @@ module.exports = class Simulator extends CocoClass
@supermodel ?= new SuperModel() @supermodel ?= new SuperModel()
@supermodel.resetProgress() @supermodel.resetProgress()
@stopListening @supermodel, 'loaded-all' @stopListening @supermodel, 'loaded-all'
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: @task.getLevelName(), sessionID: @task.getFirstSessionID(), headless: true @levelLoader = new LevelLoader supermodel: @supermodel, levelID: @task.getLevelName(), sessionID: @task.getFirstSessionID(), opponentSessionID: @task.getSecondSessionID(), headless: true
if @supermodel.finished() if @supermodel.finished()
@simulateSingleGame() @simulateSingleGame()
@ -165,7 +165,7 @@ module.exports = class Simulator extends CocoClass
@supermodel ?= new SuperModel() @supermodel ?= new SuperModel()
@supermodel.resetProgress() @supermodel.resetProgress()
@stopListening @supermodel, 'loaded-all' @stopListening @supermodel, 'loaded-all'
@levelLoader = new LevelLoader supermodel: @supermodel, levelID: levelID, sessionID: @task.getFirstSessionID(), headless: true @levelLoader = new LevelLoader supermodel: @supermodel, levelID: levelID, sessionID: @task.getFirstSessionID(), opponentSessionID: @task.getSecondSessionID(), headless: true
if @supermodel.finished() if @supermodel.finished()
@simulateGame() @simulateGame()
else else
@ -189,11 +189,13 @@ module.exports = class Simulator extends CocoClass
@world = @levelLoader.world @world = @levelLoader.world
@task.setWorld(@world) @task.setWorld(@world)
@level = @levelLoader.level @level = @levelLoader.level
@session = @levelLoader.session
@otherSession = @levelLoader.opponentSession
@levelLoader.destroy() @levelLoader.destroy()
@levelLoader = null @levelLoader = null
setupGod: -> setupGod: ->
@god.setLevel @level.serialize @supermodel, @task.getSessions()[0], @task.getSessions()[1] @god.setLevel @level.serialize(@supermodel, @session, @otherSession)
@god.setLevelSessionIDs (session.sessionID for session in @task.getSessions()) @god.setLevelSessionIDs (session.sessionID for session in @task.getSessions())
@god.setWorldClassMap @world.classMap @god.setWorldClassMap @world.classMap
@god.setGoalManager new GoalManager(@world, @level.get 'goals') @god.setGoalManager new GoalManager(@world, @level.get 'goals')
@ -424,6 +426,8 @@ class SimulationTask
getFirstSessionID: -> @rawData.sessions[0].sessionID getFirstSessionID: -> @rawData.sessions[0].sessionID
getSecondSessionID: -> @rawData.sessions[1].sessionID
getTaskID: -> @rawData.taskID getTaskID: -> @rawData.taskID
getReceiptHandle: -> @rawData.receiptHandle getReceiptHandle: -> @rawData.receiptHandle

View file

@ -78,7 +78,8 @@ class CocoModel extends Backbone.Model
thisTV4.addSchema('metaschema', require('schemas/metaschema')) thisTV4.addSchema('metaschema', require('schemas/metaschema'))
TreemaNode.utils.populateDefaults(clone, @schema(), thisTV4) TreemaNode.utils.populateDefaults(clone, @schema(), thisTV4)
@attributesWithDefaults = clone @attributesWithDefaults = clone
console.debug "Populated defaults for #{@attributes.name or @type()} in #{new Date() - t0}ms" duration = new Date() - t0
console.debug "Populated defaults for #{@attributes.name or @type()} in #{duration}ms" if duration > 10
loadFromBackup: -> loadFromBackup: ->
return unless @saveBackups return unless @saveBackups

View file

@ -58,7 +58,6 @@ module.exports = class Level extends CocoModel
# If it's a hero and there's another session, find the right session for it. # If it's a hero and there's another session, find the right session for it.
# If there is no other session (playing against default code, or on single player), clone all placeholders. # If there is no other session (playing against default code, or on single player), clone all placeholders.
# TODO: actually look at the teams on these Thangs to determine which session should go with which placeholder. # TODO: actually look at the teams on these Thangs to determine which session should go with which placeholder.
console.error 'There is no session but there is a hero and an otherSession?', session, otherSession unless session?.get
if levelThang.id is 'Hero Placeholder 1' and session.get('team') is 'humans' if levelThang.id is 'Hero Placeholder 1' and session.get('team') is 'humans'
session = otherSession session = otherSession
else if levelThang.id is 'Hero Placeholder' and session.get('team') is 'ogres' else if levelThang.id is 'Hero Placeholder' and session.get('team') is 'ogres'

View file

@ -121,7 +121,7 @@ module.exports = class SuperModel extends Backbone.Model
if cachedModel if cachedModel
clone = $.extend true, {}, model.attributes clone = $.extend true, {}, model.attributes
cachedModel.set(clone, {silent: true, fromMerge: true}) cachedModel.set(clone, {silent: true, fromMerge: true})
console.debug "Updated cached model <#{cachedModel.get('name') or cachedModel.getURL()}> with new data" #console.debug "Updated cached model <#{cachedModel.get('name') or cachedModel.getURL()}> with new data"
else else
@registerModel(model) @registerModel(model)
collection collection

View file

@ -49,6 +49,7 @@ module.exports = class SimulateTabView extends CocoView
fetchAndSimulateTaskOriginal = @simulator.fetchAndSimulateTask fetchAndSimulateTaskOriginal = @simulator.fetchAndSimulateTask
@simulator.fetchAndSimulateTask = => @simulator.fetchAndSimulateTask = =>
if @simulator.simulatedByYou >= 5 if @simulator.simulatedByYou >= 5
console.log '------------------- Destroying Simulator and making a new one -----------------'
@simulator.destroy() @simulator.destroy()
@simulator = null @simulator = null
@simulateNextGame() @simulateNextGame()

View file

@ -124,7 +124,7 @@ module.exports.getTwoGames = (req, res) ->
#if userIsAnonymous req then return errors.unauthorized(res, 'You need to be logged in to get games.') #if userIsAnonymous req then return errors.unauthorized(res, 'You need to be logged in to get games.')
humansGameID = req.body.humansGameID humansGameID = req.body.humansGameID
ogresGameID = req.body.ogresGameID ogresGameID = req.body.ogresGameID
ladderGameIDs = ['greed', 'criss-cross', 'brawlwood', 'dungeon-arena', 'gold-rush', 'sky-span'] ladderGameIDs = ['greed', 'criss-cross', 'brawlwood', 'dungeon-arena', 'gold-rush', 'sky-span', 'dueling-grounds', 'cavern-survival']
levelID = _.sample ladderGameIDs levelID = _.sample ladderGameIDs
unless ogresGameID and humansGameID unless ogresGameID and humansGameID
#fetch random games here #fetch random games here

View file

@ -98,7 +98,7 @@ describe 'LevelLoader', ->
levelLoader.loadDependenciesForSession(session) levelLoader.loadDependenciesForSession(session)
requests = jasmine.Ajax.requests.all() requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests) urls = (r.url for r in requests)
expect('/db/thang.type/gloves/version?project=name,components,original' in urls).toBeTruthy() expect('/db/thang.type/gloves/version?project=name,components,original,rasterIcon,kind' in urls).toBeTruthy()
expect('/db/thang.type/anya/version' in urls).toBeTruthy() expect('/db/thang.type/anya/version' in urls).toBeTruthy()
it 'loads components for the hero in the heroConfig in the given session', -> it 'loads components for the hero in the heroConfig in the given session', ->
@ -141,7 +141,7 @@ describe 'LevelLoader', ->
jasmine.Ajax.requests.sendResponses(responses) jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all() requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests) urls = (r.url for r in requests)
expect('/db/thang.type/mace/version?project=name,components,original' in urls).toBeTruthy() expect('/db/thang.type/mace/version?project=name,components,original,rasterIcon,kind' in urls).toBeTruthy()
it 'loads components which are inherited by level thangs from thang type default components', -> it 'loads components which are inherited by level thangs from thang type default components', ->
new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'}) new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
@ -165,7 +165,7 @@ describe 'LevelLoader', ->
jasmine.Ajax.requests.sendResponses(responses) jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all() requests = jasmine.Ajax.requests.all()
urls = (r.url for r in requests) urls = (r.url for r in requests)
expect('/db/thang.type/wand/version?project=name,components,original' in urls).toBeTruthy() expect('/db/thang.type/wand/version?project=name,components,original,rasterIcon,kind' in urls).toBeTruthy()
it 'loads components for item thang types which are inherited by level thangs from thang type default equips component configs', -> 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'}) new LevelLoader({supermodel:new SuperModel(), sessionID: 'id', levelID: 'id'})
@ -173,7 +173,7 @@ describe 'LevelLoader', ->
responses = responses =
'/db/level/id': levelWithShaman '/db/level/id': levelWithShaman
'/db/thang.type/names': [thangTypeShamanWithWandEquipped] '/db/thang.type/names': [thangTypeShamanWithWandEquipped]
'/db/thang.type/wand/version?project=name,components,original': thangTypeWand '/db/thang.type/wand/version?project=name,components,original,rasterIcon,kind': thangTypeWand
jasmine.Ajax.requests.sendResponses(responses) jasmine.Ajax.requests.sendResponses(responses)
requests = jasmine.Ajax.requests.all() requests = jasmine.Ajax.requests.all()

View file

@ -10,7 +10,7 @@ describe 'Problem', ->
addMarker: -> addMarker: ->
} }
} }
aether = { aether = {
raw: "this.say('hi');\nthis.sad('bye');" raw: "this.say('hi');\nthis.sad('bye');"
language: { id: 'javascript' } language: { id: 'javascript' }
} }
@ -27,7 +27,8 @@ describe 'Problem', ->
} }
levelID = 'awesome' levelID = 'awesome'
it 'save user code problem', -> # TODO: Problems are no longer saved when creating Problems; instead it's in SpellView. Update tests?
xit 'save user code problem', ->
new Problem aether, aetherProblem, ace, false, true, levelID new Problem aether, aetherProblem, ace, false, true, levelID
expect(jasmine.Ajax.requests.count()).toBe(1) expect(jasmine.Ajax.requests.count()).toBe(1)
@ -46,7 +47,7 @@ describe 'Problem', ->
expect(params.language).toEqual(aether.language.id) expect(params.language).toEqual(aether.language.id)
expect(params.levelID).toEqual(levelID) expect(params.levelID).toEqual(levelID)
it 'save user code problem no range', -> xit 'save user code problem no range', ->
aetherProblem.range = null aetherProblem.range = null
new Problem aether, aetherProblem, ace, false, true, levelID new Problem aether, aetherProblem, ace, false, true, levelID
expect(jasmine.Ajax.requests.count()).toBe(1) expect(jasmine.Ajax.requests.count()).toBe(1)
@ -68,7 +69,7 @@ describe 'Problem', ->
expect(params.codeSnippet).toBeUndefined() expect(params.codeSnippet).toBeUndefined()
expect(params.errRange).toBeUndefined() expect(params.errRange).toBeUndefined()
it 'save user code problem multi-line snippet', -> xit 'save user code problem multi-line snippet', ->
aether.raw = "this.say('hi');\nthis.sad\n('bye');" aether.raw = "this.say('hi');\nthis.sad\n('bye');"
aetherProblem.range = [ { row: 1 }, { row: 2 } ] aetherProblem.range = [ { row: 1 }, { row: 2 } ]