From 39fb2cb1b4873cc61391349db0ee7b07e9eb33a6 Mon Sep 17 00:00:00 2001 From: Ruben Vereecken Date: Tue, 24 Jun 2014 13:49:54 +0200 Subject: [PATCH] Added achievement preview, exp test, stuff --- app/models/User.coffee | 7 ++--- app/styles/editor/achievement/edit.sass | 8 +++++ app/styles/notify.sass | 14 ++++++++- app/templates/achievement_notify.jade | 5 ++-- app/templates/editor/achievement/edit.jade | 3 +- app/views/editor/achievement/edit.coffee | 29 +++++++++++++++++-- app/views/kinds/RootView.coffee | 27 +++++++++++++++-- server/achievements/Achievement.coffee | 3 ++ server/plugins/achievements.coffee | 2 +- test/app/models/User.spec.coffee | 16 ++++++++++ .../achievement/AchievementGet.demo.coffee | 2 +- .../server/functional/achievement.spec.coffee | 6 ++-- 12 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 test/app/models/User.spec.coffee diff --git a/app/models/User.coffee b/app/models/User.coffee index f224c85c9..755c8e097 100644 --- a/app/models/User.coffee +++ b/app/models/User.coffee @@ -7,6 +7,9 @@ module.exports = class User extends CocoModel @schema: require 'schemas/models/user' urlRoot: "/db/user" + defaults: + points: 0 + initialize: -> super() @migrateEmails() @@ -88,7 +91,3 @@ module.exports = class User extends CocoModel level: -> User.levelFromExp(@get('points')) - - levelFromExp: (xp) -> User.levelFromExp(xp) - - expForLevel: (level) -> User.expForLevel(level) diff --git a/app/styles/editor/achievement/edit.sass b/app/styles/editor/achievement/edit.sass index 7177978d3..68208ae3f 100644 --- a/app/styles/editor/achievement/edit.sass +++ b/app/styles/editor/achievement/edit.sass @@ -1,4 +1,6 @@ #editor-achievement-edit-view + height: 100% + .treema-root margin: 28px 0px 20px @@ -10,3 +12,9 @@ textarea width: 92% height: 300px + + #achievement-view + min-height: 200px + + .notifyjs-container + clear: both diff --git a/app/styles/notify.sass b/app/styles/notify.sass index 9e077e9ab..2d888b351 100644 --- a/app/styles/notify.sass +++ b/app/styles/notify.sass @@ -9,7 +9,7 @@ position: absolute width: 200px height: 200px - left: -150px + left: -140px top: 0px .achievement-image @@ -66,18 +66,30 @@ &:empty display: none + + .progress-wrapper position: absolute padding-right: 30px bottom: 0px + .achievement-level + float: left + font-size: 15px + .progress-bar-wrapper + padding-left: 30px width: 100% .progress border-radius: 20px + .progress-bar-border + padding-left: 30px position: relative top: -44px + + .progress-bar-border img + width: 100% /*.earned-exp padding-left: 5px font-family: Bangers diff --git a/app/templates/achievement_notify.jade b/app/templates/achievement_notify.jade index cc9779340..a384e4eed 100644 --- a/app/templates/achievement_notify.jade +++ b/app/templates/achievement_notify.jade @@ -10,6 +10,7 @@ div .achievement-message(data-notify-html="message") .progress-wrapper //.earned-exp(data-notify-html="earnedExp") + span.achievement-level.badge(data-notify-html="level") .progress-bar-wrapper(data-notify-html="progressBar") - .progress-bar-border - img(src='/images/achievements/bar_border.png' width='100%') + .progress-bar-border(data-notify-html="barBorder") + //img(src='/images/achievements/bar_border.png' width='100%') diff --git a/app/templates/editor/achievement/edit.jade b/app/templates/editor/achievement/edit.jade index 42b59de17..aac62cbf0 100644 --- a/app/templates/editor/achievement/edit.jade +++ b/app/templates/editor/achievement/edit.jade @@ -21,10 +21,11 @@ block content #achievement-treema #achievement-view + #achievement-view-inner hr - div#error-view + #error-view else .alert.alert-danger diff --git a/app/views/editor/achievement/edit.coffee b/app/views/editor/achievement/edit.coffee index 76988a586..6d34ed757 100644 --- a/app/views/editor/achievement/edit.coffee +++ b/app/views/editor/achievement/edit.coffee @@ -49,15 +49,38 @@ module.exports = class AchievementEditView extends View @treema.build() - pushChangesToPreview: => - 'TODO' # TODO might want some intrinsic preview thing - getRenderData: (context={}) -> context = super(context) context.achievement = @achievement context.authorized = me.isAdmin() context + afterRender: -> + super(arguments...) + @pushChangesToPreview() + + pushChangesToPreview: => + $('.notifyjs-wrapper').trigger 'notify-hide' + + if @treema? + for key, value of @treema.data + @achievement.set key, value + + earned = + earnedPoints: @achievement.get 'worth' + + data = @createNotifyData @achievement, earned + options = + style: 'achievement' + autoHide: false + clickToHide: false + arrowShow: false + elementPosition: 'bottom center' + hideDuration: 0 + showDuration: 0 +l + $('#achievement-view-inner').notify data, options + openSaveModal: -> 'Maybe later' # TODO diff --git a/app/views/kinds/RootView.coffee b/app/views/kinds/RootView.coffee index cdbe0913f..ea01e6303 100644 --- a/app/views/kinds/RootView.coffee +++ b/app/views/kinds/RootView.coffee @@ -51,12 +51,32 @@ module.exports = class RootView extends CocoView newlyAchievedBar = $("
") emptyBar = $("
") progressBar = $('
').append(alreadyAchievedBar).append(newlyAchievedBar).append(emptyBar) - message = if (currentLevel isnt 1) and leveledUp then "Reached level #{currentLevel}!" else null + #message = if (currentLevel isnt 1) and leveledUp then "Reached level #{currentLevel}!" else null alreadyAchievedBar.tooltip(title: "#{currentExp} XP in total") newlyAchievedBar.tooltip(title: "#{achievedExp} XP earned") emptyBar.tooltip(title: "#{nextLevelExp - currentExp} XP until level #{nextLevel}") + barBorder = $('') + + barBorder.hover (e) -> + #console.debug e + x = e.pageX + y = e.pageY + $actualHover = _.find [$('.progress-bar-warning'), $('.progress-bar-success'), $('.progress-bar-white')], (el) -> + offset = el.offset() + l = offset.left + t = offset.top + h = el.height() + 10 + w = el.width() + 10 + + maxx = l + w + maxy = t + h + + return (y <= maxy && y >= t) && (x <= maxx && x >= l) ? true : null + #console.debug $actualHover + $actualHover.trigger e if $actualHover + # TODO a default should be linked here imageURL = '/file/' + achievement.get('icon') data = @@ -65,7 +85,10 @@ module.exports = class RootView extends CocoView description: achievement.get('description') progressBar: progressBar earnedExp: "+ #{achievedExp} XP" - message: message + #message: message + level: currentLevel + barBorder: barBorder + data showNewAchievement: (achievement, earnedAchievement) -> diff --git a/server/achievements/Achievement.coffee b/server/achievements/Achievement.coffee index cd0850731..9c9fae51f 100644 --- a/server/achievements/Achievement.coffee +++ b/server/achievements/Achievement.coffee @@ -32,6 +32,8 @@ AchievementSchema.methods.getExpFunction = -> AchievementSchema.statics.jsonschema = jsonschema AchievementSchema.statics.achievements = {} +# Reloads all achievements into memory. +# TODO might want to tweak this to only load new achievements AchievementSchema.statics.loadAchievements = (done) -> AchievementSchema.statics.resetAchievements() Achievement = require('../achievements/Achievement') @@ -49,6 +51,7 @@ AchievementSchema.statics.getLoadedAchievements = -> AchievementSchema.statics.resetAchievements = -> delete AchievementSchema.statics.achievements[category] for category of AchievementSchema.statics.achievements +# Queries are stored as JSON strings, objectify them upon loading AchievementSchema.post 'init', (doc) -> doc.objectifyQuery() AchievementSchema.pre 'save', (next) -> diff --git a/server/plugins/achievements.coffee b/server/plugins/achievements.coffee index dbb09a825..ca1248de1 100644 --- a/server/plugins/achievements.coffee +++ b/server/plugins/achievements.coffee @@ -25,7 +25,7 @@ AchievablePlugin = (schema, options) -> if doc.isInit('_id') and not doc.id of before log.warn 'document was already initialized but did not go through `init` and is therefore treated as new while it might not be' - category = doc.constructor.modelName + category = doc.constructor.collection.name loadedAchievements = Achievement.getLoadedAchievements() #log.debug 'about to save ' + category + ', number of achievements is ' + Object.keys(loadedAchievements).length diff --git a/test/app/models/User.spec.coffee b/test/app/models/User.spec.coffee new file mode 100644 index 000000000..b751690ed --- /dev/null +++ b/test/app/models/User.spec.coffee @@ -0,0 +1,16 @@ +User = require 'models/User' + +describe 'UserModel', -> + it 'experience functions are correct', -> + expect(User.expForLevel(User.levelFromExp 0)).toBe 0 + expect(User.expForLevel(User.levelFromExp 50)).toBe 50 + expect(User.expForLevel 1).toBe 0 + expect(User.expForLevel 2).toBeGreaterThan User.expForLevel 1 + + it 'level is calculated correctly', -> + me.set 'points', 0 + expect(me.level()).toBe 1 + + me.set 'points', 50 + expect(me.level()).toBe User.levelFromExp 50 + diff --git a/test/demo/views/achievement/AchievementGet.demo.coffee b/test/demo/views/achievement/AchievementGet.demo.coffee index be46199fc..9fcdd9539 100644 --- a/test/demo/views/achievement/AchievementGet.demo.coffee +++ b/test/demo/views/achievement/AchievementGet.demo.coffee @@ -8,7 +8,7 @@ class MockServer module.exports = -> - me.set('points', 48) + me.set 'points', 48 unlockableObj = name: 'Dungeon Arena Started' diff --git a/test/server/functional/achievement.spec.coffee b/test/server/functional/achievement.spec.coffee index ea33c3075..1e815f137 100644 --- a/test/server/functional/achievement.spec.coffee +++ b/test/server/functional/achievement.spec.coffee @@ -4,7 +4,7 @@ unlockable = name: 'Dungeon Arena Started' description: 'Started playing Dungeon Arena.' worth: 3 - collection: 'level.session' + collection: 'level.sessions' query: "{\"level.original\":\"dungeon-arena\"}" userField: 'creator' @@ -12,7 +12,7 @@ repeatable = name: 'Simulated' description: 'Simulated Games.' worth: 1 - collection: 'User' + collection: 'users' query: "{\"simulatedBy\":{\"$gt\":\"0\"}}" userField: '_id' proportionalTo: 'simulatedBy' @@ -20,7 +20,7 @@ repeatable = diminishing = name: 'Simulated2' worth: 1.5 - collection: 'User' + collection: 'users' query: "{\"simulatedBy\":{\"$gt\":\"0\"}}" userField: '_id' proportionalTo: 'simulatedBy'