From 1411d045180857d67e00c7b4cd2a60ca52523dd0 Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Fri, 31 May 2013 09:35:50 -0400 Subject: [PATCH] challenges: add basic task stats for challenge subscribers --- src/app/challenges.coffee | 15 +++++++++++++++ src/app/misc.coffee | 27 +++++++++++++++++++++++---- views/app/challenges.html | 25 +++++++++++++++++++++++++ views/app/game-pane.html | 2 +- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/app/challenges.coffee b/src/app/challenges.coffee index 25d1f696f4..dff8653269 100644 --- a/src/app/challenges.coffee +++ b/src/app/challenges.coffee @@ -5,6 +5,21 @@ module.exports.app = (appExports, model) -> browser = require './browser' user = model.at '_user' + appExports.renderChallengeGraphs = -> + challenges = model.get('_party.challenges') + _.each model.get('_guilds'), (g) -> challenges.concat(g.challenges) + _.each challenges, (chal) -> + _.each ['habit','daily','todo'], (type) -> + _.each chal["#{type}s"], (task) -> + _.each chal.users, (member) -> + if (history = member["#{type}s"][task.id].history) and !!history + data = google.visualization.arrayToDataTable _.map(history, (h)-> [h.date,h.value]) + options = + backgroundColor: { fill:'transparent' } + axisTitlesPosition: 'none' + chart = new google.visualization.LineChart $(".challenge-#{chal.id}-member-#{member.id}-history-#{task.id}")[0] + chart.draw(data, options) + appExports.challengeCreate = (e,el) -> [type, gid] = [$(el).attr('data-type'), $(el).attr('data-gid')] model.set '_challenge.new', diff --git a/src/app/misc.coffee b/src/app/misc.coffee index 0bc69625f5..1964b42625 100644 --- a/src/app/misc.coffee +++ b/src/app/misc.coffee @@ -44,8 +44,7 @@ taskInChallenge = (task) -> perform the updates while tracking paths, then all the values at those paths ### module.exports.score = (model, taskId, direction, allowUndo=false) -> - #return setTimeout( (-> score(taskId, direction)), 500) if model._txnQueue.length > 0 - batchTxn model, (uObj, paths) -> + delta = batchTxn model, (uObj, paths) -> tObj = uObj.tasks[taskId] # Stuff for undo @@ -62,7 +61,27 @@ module.exports.score = (model, taskId, direction, allowUndo=false) -> if uObj._tmp?.drop and $? model.set '_drop', uObj._tmp.drop $('#item-dropped-modal').modal 'show' - delta + + # Update challenge statistics + # FIXME put this in it's own batchTxn, make batchTxn model.at() ref aware (not just _user) + # FIXME use reflists for users & challenges + if (chalTask = taskInChallenge.call({model}, tObj)) and chalTask?.get() + model._dontPersist = false + chalTask.incr "value", delta + chal = model.at indexedPath.call({model}, "groups.#{tObj.group.id}.challenges", {id:tObj.challenge}) + chalUser = -> indexedPath.call({model}, chal.path(), 'users', {id:uObj.id}) + cu = model.at chalUser() + unless cu?.get() + chal.push "users", {id: uObj.id, name: helpers.username(uObj.auth, uObj.profile?.name)} + cu = model.at chalUser() + else + cu.set 'name', helpers.username(uObj.auth, uObj.profile?.name) # update their name incase it changed + cu.set "#{tObj.type}s.#{tObj.id}", + value: tObj.value + history: tObj.history + model._dontPersist = true + + delta ### Make sure model.get() returns all properties, see https://github.com/codeparty/racer/issues/116 @@ -105,7 +124,7 @@ module.exports.fixCorruptUser = (model) -> # 1. remove duplicates # 2. restore missing zombie tasks back into list idList = uObj["#{type}Ids"] - taskIds = _.pluck( _.where(tasks, {type:type}), 'id') + taskIds = _.pluck( _.where(tasks, {type}), 'id') union = _.union idList, taskIds # 2. remove empty (grey) tasks diff --git a/views/app/challenges.html b/views/app/challenges.html index 4f36bc8415..b281667f06 100644 --- a/views/app/challenges.html +++ b/views/app/challenges.html @@ -95,10 +95,35 @@ todos={@challenge.todos} rewards={@challenge.rewards} /> + +

Statistics

+ {#each @challenge.users as :member} +

{:member.name}

+
+
+ +
+
+ +
+
+ +
+
+ {/} + +
{@header}
+ {#each @challenge[@taskType]s as :task} +
+ {:task.text}: {round(@member[@taskType]s[:task.id].value)} +
+
+ {/} + Create {{@text}} Challenge diff --git a/views/app/game-pane.html b/views/app/game-pane.html index d96f1ca136..9211621806 100644 --- a/views/app/game-pane.html +++ b/views/app/game-pane.html @@ -14,7 +14,7 @@ {/if}
  • Tavern
  • Achievements
  • -
  • Challenges
  • +
  • Challenges
  • Settings