attempt fix using _.each in place of for loops

This commit is contained in:
Tyler Renelle
2012-08-06 16:04:49 -04:00
parent 203632b6a1
commit e3700bc183
4 changed files with 23 additions and 23 deletions

View File

@@ -290,16 +290,16 @@ ready(function(model) {
return model.set('_items.weapon', content.items.weapon[1]);
};
exports.poormanscron = poormanscron = function() {
var daysPassed, lastCron, n, today, _k;
var daysPassed, lastCron, today;
today = new Date();
model.setNull('_user.lastCron', today);
lastCron = model.get('_user.lastCron');
daysPassed = helpers.daysBetween(lastCron, today);
if (daysPassed > 0) {
for (n = _k = 1; 1 <= daysPassed ? _k <= daysPassed : _k >= daysPassed; n = 1 <= daysPassed ? ++_k : --_k) {
scoring.tally(model);
}
return model.set('_user.lastCron', today);
model.set('_user.lastCron', today);
return _.times(daysPassed, (function() {
return scoring.tally(model);
}));
}
};
setTimeout((function() {

View File

@@ -1,5 +1,5 @@
// Generated by CoffeeScript 1.3.3
var content, expModifier, hpModifier, updateStats;
var content, expModifier, hpModifier, score, updateStats;
content = require('./content');
@@ -55,7 +55,7 @@ updateStats = function(user, stats) {
}
};
module.exports.score = function(spec) {
module.exports.score = score = function(spec) {
var adjustvalue, cron, delta, direction, exp, hp, lvl, money, sign, task, type, user, value, _ref, _ref1;
if (spec == null) {
spec = {
@@ -109,15 +109,16 @@ module.exports.score = function(spec) {
};
module.exports.tally = function(model) {
var absVal, completed, expTally, key, lvl, task, todoTally, type, user, value, _ref;
var expTally, lvl, todoTally, user;
user = model.at('_user');
todoTally = 0;
for (key in model.get('_user.tasks')) {
task = model.at("_user.tasks." + key);
_ref = [task.get('type'), task.get('value'), task.get('completed')], type = _ref[0], value = _ref[1], completed = _ref[2];
_.each(user.get('tasks'), function(taskObj, taskId, list) {
var absVal, completed, task, type, value, _ref;
_ref = [taskObj.type, taskObj.value, taskObj.completed], type = _ref[0], value = _ref[1], completed = _ref[2];
task = model.at("_user.tasks." + taskId);
if (type === 'todo' || type === 'daily') {
if (!completed) {
module.exports.score({
score({
user: user,
task: task,
direction: 'down',
@@ -134,12 +135,12 @@ module.exports.tally = function(model) {
todoTally += absVal;
}
if (type === 'daily') {
task.pass({
return task.pass({
cron: true
}).set('completed', false);
}
}
}
});
model.push('_user.history.todos', {
date: new Date(),
value: todoTally

View File

@@ -241,9 +241,10 @@ ready (model) ->
lastCron = model.get('_user.lastCron')
daysPassed = helpers.daysBetween(lastCron, today)
if daysPassed > 0
for n in [1..daysPassed]
scoring.tally(model)
model.set('_user.lastCron', today) # reset cron
_.times daysPassed, (->
scoring.tally(model)
)
# FIXME seems can't call poormanscron() instantly, have to call after some time (2s here)
# Doesn't do anything otherwise. Don't know why... model not initialized enough yet?
setTimeout (-> # Run once on refresh

View File

@@ -48,7 +48,7 @@ updateStats = (user, stats) ->
money = 0.0 if (!money? or money<0)
user.set 'stats.money', stats.money
module.exports.score = (spec = {user:null, task:null, direction:null, cron:null}) ->
module.exports.score = score = (spec = {user:null, task:null, direction:null, cron:null}) ->
# console.log spec, "scoring.coffee: score( ->spec<- )"
[user, task, direction, cron] = [spec.user, spec.task, spec.direction, spec.cron]
@@ -99,17 +99,15 @@ module.exports.score = (spec = {user:null, task:null, direction:null, cron:null}
# At end of day, add value to all incomplete Daily & Todo tasks (further incentive)
# For incomplete Dailys, deduct experience
module.exports.tally = (model) ->
# users = model.at('users') #TODO this isn't working, iterate over all users
# for user in users
user = model.at '_user'
todoTally = 0
for key of model.get '_user.tasks'
task = model.at "_user.tasks.#{key}"
[type, value, completed] = [task.get('type'), task.get('value'), task.get('completed')]
_.each user.get('tasks'), (taskObj, taskId, list) ->
[type, value, completed] = [taskObj.type, taskObj.value, taskObj.completed]
task = model.at("_user.tasks.#{taskId}")
if type in ['todo', 'daily']
# Deduct experience for missed Daily tasks,
# but not for Todos (just increase todo's value)
module.exports.score({user:user, task:task, direction:'down', cron:true}) unless completed
score({user:user, task:task, direction:'down', cron:true}) unless completed
if type == 'daily'
task.push "history", { date: new Date(), value: value }
else