From b9f4093b083d0cdd2114fd8cb99197b2136b19a1 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Mon, 28 Apr 2014 14:58:58 -0700 Subject: [PATCH] Set up the LevelLoader to load names instead of whole ThangTypes. --- app/collections/ThangNamesCollection.coffee | 11 +++++++++++ app/lib/LevelLoader.coffee | 18 ++++++++++++------ app/locale/en.coffee | 1 + app/models/SuperModel.coffee | 3 +-- app/views/play/level_view.coffee | 5 ----- server/commons/Handler.coffee | 8 ++++---- 6 files changed, 29 insertions(+), 17 deletions(-) create mode 100644 app/collections/ThangNamesCollection.coffee diff --git a/app/collections/ThangNamesCollection.coffee b/app/collections/ThangNamesCollection.coffee new file mode 100644 index 000000000..1897d8b12 --- /dev/null +++ b/app/collections/ThangNamesCollection.coffee @@ -0,0 +1,11 @@ +ThangType = require 'models/ThangType' +CocoCollection = require 'collections/CocoCollection' + +module.exports = class ThangNamesCollection extends CocoCollection + url: '/db/thang.type/names' + model: ThangType + isCachable: false + + constructor: (ids) -> + console.log 'data', {type:'POST', data:{ids:ids}} + super([], {type:'POST', data:{ids:ids}}) diff --git a/app/lib/LevelLoader.coffee b/app/lib/LevelLoader.coffee index 5063982d9..4404acb17 100644 --- a/app/lib/LevelLoader.coffee +++ b/app/lib/LevelLoader.coffee @@ -4,6 +4,7 @@ LevelSystem = require 'models/LevelSystem' Article = require 'models/Article' LevelSession = require 'models/LevelSession' ThangType = require 'models/ThangType' +ThangNamesCollection = require 'collections/ThangNamesCollection' CocoClass = require 'lib/CocoClass' AudioPlayer = require 'lib/AudioPlayer' @@ -21,6 +22,7 @@ World = require 'lib/world/world' module.exports = class LevelLoader extends CocoClass constructor: (options) -> + @t0 = new Date().getTime() super() @supermodel = options.supermodel @levelID = options.levelID @@ -98,12 +100,15 @@ module.exports = class LevelLoader extends CocoClass articleVersions.push _.pick(article, ['original', 'majorVersion']) objUniq = (array) -> _.uniq array, false, (arg) -> JSON.stringify(arg) - - for thangID in _.uniq thangIDs - url = "/db/thang.type/#{thangID}/version" - url += "?project=true" if @headless and not @editorMode - res = @maybeLoadURL url, ThangType, 'thang' - @listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res + + thangNames = new ThangNamesCollection(thangIDs) + @supermodel.loadCollection thangNames, 'thang_names' +# for thangID in _.uniq thangIDs +# url = "/db/thang.type/#{thangID}/version" +# url += "?project=true" if @headless and not @editorMode +# res = @maybeLoadURL url, ThangType, 'thang' +# @listenToOnce res.model, 'sync', @buildSpriteSheetsForThangType if res + for obj in objUniq componentVersions url = "/db/level.component/#{obj.original}/version/#{obj.majorVersion}" @maybeLoadURL url, LevelComponent, 'component' @@ -192,6 +197,7 @@ module.exports = class LevelLoader extends CocoClass @world = new World @level.get('name') serializedLevel = @level.serialize(@supermodel) @world.loadFromLevel serializedLevel, false + console.log 'INITIALIZED!', (new Date().getTime() - @t0) / 1000 # Initial Sound Loading diff --git a/app/locale/en.coffee b/app/locale/en.coffee index 0f2194e8d..3a4b332a7 100644 --- a/app/locale/en.coffee +++ b/app/locale/en.coffee @@ -695,5 +695,6 @@ opponent_session: "Opponent Session" article: "Article" user_names: "User Names" + thang_names: "Thang Names" files: "Files" top_simulators: "Top Simulators" diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index 4a296d4a6..0ca6fd7fd 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -80,7 +80,6 @@ module.exports = class SuperModel extends Backbone.Model return _.values @models registerModel: (model) -> - console.log 'register model?', model, model.getURL @models[model.getURL()] = model getCollection: (collection) -> @@ -94,7 +93,7 @@ module.exports = class SuperModel extends Backbone.Model @registerCollection(collection) registerCollection: (collection) -> - @collections[collection.getURL()] = collection + @collections[collection.getURL()] = collection if collection.isCachable # consolidate models for model, i in collection.models cachedModel = @getModelByURL(model.getURL()) diff --git a/app/views/play/level_view.coffee b/app/views/play/level_view.coffee index 0118b5242..3d976e032 100644 --- a/app/views/play/level_view.coffee +++ b/app/views/play/level_view.coffee @@ -83,7 +83,6 @@ module.exports = class PlayLevelView extends View @sessionID = @getQueryVariable 'session' $(window).on('resize', @onWindowResize) - @listenToOnce(@supermodel, 'error', @onLevelLoadError) @saveScreenshot = _.throttle @saveScreenshot, 30000 if @isEditorPreview @@ -96,10 +95,6 @@ module.exports = class PlayLevelView extends View @load() application.tracker?.trackEvent 'Started Level Load', level: @levelID, label: @levelID - onLevelLoadError: (e) -> - # TODO NOW: remove this in favor of the supermodel handling it - application.router.navigate "/play?not_found=#{@levelID}", {trigger: true} - setLevel: (@level, @supermodel) -> @god?.level = @level.serialize @supermodel if @world diff --git a/server/commons/Handler.coffee b/server/commons/Handler.coffee index b4590b2f0..e3b7911d8 100644 --- a/server/commons/Handler.coffee +++ b/server/commons/Handler.coffee @@ -112,7 +112,7 @@ module.exports = class Handler ids = ids.split(',') if _.isString ids ids = _.uniq ids - project = {name:1} + project = {name:1, original:1} sort = {'version.major':-1, 'version.minor':-1} makeFunc = (id) => @@ -120,8 +120,8 @@ module.exports = class Handler criteria = {original:mongoose.Types.ObjectId(id)} @modelClass.findOne(criteria, project).sort(sort).exec (err, document) -> return done(err) if err - callback(null, document?.toObject() or {}) - + callback(null, document?.toObject() or null) + funcs = {} for id in ids return errors.badInput(res, "Given an invalid id: #{id}") unless Handler.isID(id) @@ -129,7 +129,7 @@ module.exports = class Handler async.parallel funcs, (err, results) -> return errors.serverError err if err - res.send results + res.send (d for d in _.values(results) when d) res.end() getPatchesFor: (req, res, id) ->