Goal states are now tracked in level sessions (state.goalStates)

This commit is contained in:
Ruben Vereecken 2014-08-08 13:08:13 +02:00
parent 9f0add22a5
commit 3bfd341363
3 changed files with 30 additions and 0 deletions

View file

@ -1,6 +1,7 @@
Bus = require './Bus'
{me} = require 'lib/auth'
LevelSession = require 'models/LevelSession'
utils = require 'lib/utils'
module.exports = class LevelBus extends Bus
@ -22,6 +23,7 @@ module.exports = class LevelBus extends Bus
'tome:spell-changed': 'onSpellChanged'
'tome:spell-created': 'onSpellCreated'
'application:idle-changed': 'onIdleChanged'
'goal-manager:new-goal-states': 'onNewGoalStates'
constructor: ->
super(arguments...)
@ -192,6 +194,14 @@ module.exports = class LevelBus extends Bus
@changedSessionProperties.state = true
@saveSession()
onNewGoalStates: ({goalStates})->
state = @session.get 'state'
unless utils.kindaEqual state.goalStates, goalStates # Only save when goals really change
state.goalStates = goalStates
@session.set 'state', state
@changedSessionProperties.state = true
@saveSession()
onPlayerJoined: (snapshot) =>
super(arguments...)
return unless @onPoint()

View file

@ -118,3 +118,15 @@ module.exports.grayscale = (imageData) ->
v = 0.2126*r + 0.7152*g + 0.0722*b
d[i] = d[i+1] = d[i+2] = v
imageData
# Deep compares l with r, with the exception that undefined values are considered equal to missing values
# Very practical for comparing Mongoose documents where undefined is not allowed, instead fields get deleted
module.exports.kindaEqual = compare = (l, r) ->
if _.isObject(l) and _.isObject(r)
for key in _.union Object.keys(l), Object.keys(r)
return false unless compare l[key], r[key]
return true
else if l is r
return true
else
return false

View file

@ -97,6 +97,14 @@ _.extend LevelSessionSchema.properties,
type: 'object'
source:
type: 'string'
goalStates:
type: 'object'
description: 'Maps Goal ID on a goal state object'
additionalProperties:
title: 'Goal State'
type: 'object'
properties:
status: enum: ['failure', 'incomplete', 'success']
code:
type: 'object'