challenges: add basic task stats for challenge subscribers

This commit is contained in:
Tyler Renelle 2013-05-31 09:35:50 -04:00
parent 842ac45ab5
commit 1411d04518
4 changed files with 64 additions and 5 deletions

View file

@ -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',

View file

@ -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

View file

@ -95,10 +95,35 @@
todos={@challenge.todos}
rewards={@challenge.rewards} />
</div>
<h3>Statistics</h3>
{#each @challenge.users as :member}
<h4>{:member.name}</h4>
<div class="grid">
<div class="module">
<app:challenges:stats header=Habits challenge={@challenge} member={:member} taskType=habit />
</div>
<div class="module">
<app:challenges:stats header=Dailies challenge={@challenge} member={:member} taskType=daily />
</div>
<div class="module">
<app:challenges:stats header=Todos challenge={@challenge} member={:member} taskType=todos />
</div>
</div>
{/}
</div>
</div>
</div>
<stats:>
<h5>{@header}</h5>
{#each @challenge[@taskType]s as :task}
<div>
<strong>{:task.text}</strong>: {round(@member[@taskType]s[:task.id].value)}
<div style='height:75px' class="challenge-{{@challenge.id}}-member-{{@member.id}}-history-{{:task.id}}"></div>
</div>
{/}
<create-button:>
<a x-bind='click:challengeCreate' class='btn btn-large btn-success' data-type={{@type}} data-gid={{@gid}} >Create {{@text}} Challenge</a>

View file

@ -14,7 +14,7 @@
{/if}
<li><a data-toggle='tab' data-target="#profile-tavern"><i class='icon-eye-close'></i> Tavern</a></li>
<li><a data-toggle='tab' data-target="#profile-achievements"><i class='icon-certificate'></i> Achievements</a></li>
<li><a data-toggle='tab' data-target="#profile-challenges"><i class='icon-bullhorn'></i> Challenges</a></li>
<li><a data-toggle='tab' data-target="#profile-challenges" x-bind=click:renderChallengeGraphs ><i class='icon-bullhorn'></i> Challenges</a></li>
<li><a data-toggle='tab' data-target="#profile-settings"><i class='icon-wrench'></i> Settings</a></li>
</ul>