scoring function refactoring & testing
This commit is contained in:
+7
-15
@@ -39,14 +39,14 @@ get('/:uidParam?', function(page, model, _arg, next) {
|
||||
potion: content.items.potion,
|
||||
reroll: content.items.reroll
|
||||
});
|
||||
model.fn('_user._tnl', '_user.stats.lvl', function(lvl) {
|
||||
return (lvl * 100) / 5;
|
||||
});
|
||||
model.refList("_habitList", "_user.tasks", "_user.habitIds");
|
||||
model.refList("_dailyList", "_user.tasks", "_user.dailyIds");
|
||||
model.refList("_todoList", "_user.tasks", "_user.todoIds");
|
||||
model.refList("_completedList", "_user.tasks", "_user.completedIds");
|
||||
model.refList("_rewardList", "_user.tasks", "_user.rewardIds");
|
||||
model.fn('_user._tnl', '_user.stats.lvl', function(lvl) {
|
||||
return (lvl * 100) / 5;
|
||||
});
|
||||
return page.render();
|
||||
});
|
||||
});
|
||||
@@ -102,7 +102,7 @@ ready(function(model) {
|
||||
}
|
||||
tour.start();
|
||||
model.on('set', '_user.tasks.*.completed', function(i, completed, previous, isLocal, passed) {
|
||||
var direction, from, fromIds, task, to, toIds, _ref3, _ref4;
|
||||
var direction, from, fromIds, to, toIds, _ref3, _ref4;
|
||||
if ((passed != null) && passed.cron) {
|
||||
return;
|
||||
}
|
||||
@@ -115,11 +115,7 @@ ready(function(model) {
|
||||
}
|
||||
throw new Error("Direction neither 'up' nor 'down' on checkbox set.");
|
||||
};
|
||||
task = model.at("_user.tasks." + i);
|
||||
scoring.score({
|
||||
task: task,
|
||||
direction: direction()
|
||||
});
|
||||
scoring.score(i, direction());
|
||||
if (task.get('type') === 'todo') {
|
||||
_ref3 = direction() === 'up' ? ['todo', 'completed'] : ['completed', 'todo'], from = _ref3[0], to = _ref3[1];
|
||||
_ref4 = ["_user." + from + "Ids", "_user." + to + "Ids"], from = _ref4[0], to = _ref4[1];
|
||||
@@ -195,8 +191,7 @@ ready(function(model) {
|
||||
return;
|
||||
} else {
|
||||
task.set('type', 'habit');
|
||||
scoring.score({
|
||||
task: task,
|
||||
scoring.score(task.get('id'), {
|
||||
direction: 'down'
|
||||
});
|
||||
}
|
||||
@@ -290,10 +285,7 @@ ready(function(model) {
|
||||
direction = 'down';
|
||||
}
|
||||
task = model.at($(el).parents('li')[0]);
|
||||
return scoring.score({
|
||||
task: task,
|
||||
direction: direction
|
||||
});
|
||||
return scoring.score(task.get('id'), direction);
|
||||
};
|
||||
exports.revive = function(e, el) {
|
||||
var stats;
|
||||
|
||||
+17
-22
@@ -1,5 +1,7 @@
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
var MODIFIER, content, cron, expModifier, helpers, hpModifier, model, score, setModel, setupNotifications, tally, updateStats, user;
|
||||
var MODIFIER, content, cron, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, tally, updateStats, user;
|
||||
|
||||
moment = require('moment');
|
||||
|
||||
content = require('./content');
|
||||
|
||||
@@ -120,18 +122,17 @@ updateStats = function(stats) {
|
||||
}
|
||||
};
|
||||
|
||||
score = function(spec) {
|
||||
var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, num, sign, task, type, value, _ref, _ref1, _ref2;
|
||||
if (spec == null) {
|
||||
spec = {
|
||||
task: null,
|
||||
direction: null,
|
||||
cron: null
|
||||
};
|
||||
score = function(taskId, direction, cron) {
|
||||
var adjustvalue, delta, exp, hp, lvl, modified, money, num, sign, task, taskObj, taskPath, type, userObj, value, _ref, _ref1, _ref2, _ref3;
|
||||
if (cron == null) {
|
||||
cron = false;
|
||||
}
|
||||
_ref = [spec.task, spec.direction, spec.cron], task = _ref[0], direction = _ref[1], cron = _ref[2];
|
||||
taskPath = "_user.tasks." + taskId;
|
||||
_ref = [model.at(taskPath), model.get(taskPath)], task = _ref[0], taskObj = _ref[1];
|
||||
_ref1 = [taskObj.type, taskObj.value], type = _ref1[0], value = _ref1[1];
|
||||
userObj = user.get();
|
||||
if (!task) {
|
||||
_ref1 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp')], money = _ref1[0], hp = _ref1[1], exp = _ref1[2];
|
||||
_ref2 = [userObj.stats.money, userObj.stats.hp, userObj.stats.exp], money = _ref2[0], hp = _ref2[1], exp = _ref2[2];
|
||||
if (direction === "up") {
|
||||
modified = expModifier(1);
|
||||
money += modified;
|
||||
@@ -148,26 +149,24 @@ score = function(spec) {
|
||||
return;
|
||||
}
|
||||
sign = direction === "up" ? 1 : -1;
|
||||
value = task.get('value');
|
||||
delta = value < 0 ? (-0.1 * value + 1) * sign : (Math.pow(0.9, value)) * sign;
|
||||
type = task.get('type');
|
||||
adjustvalue = type !== 'reward';
|
||||
if ((type === 'habit') && (task.get("up") === false || task.get("down") === false)) {
|
||||
if ((type === 'habit') && (taskObj.up === false || taskObj.down === false)) {
|
||||
adjustvalue = false;
|
||||
}
|
||||
if (adjustvalue) {
|
||||
value += delta;
|
||||
}
|
||||
if (type === 'habit') {
|
||||
if (task.get('value') !== value) {
|
||||
if (taskObj.value !== value) {
|
||||
task.push('history', {
|
||||
date: new Date(),
|
||||
date: moment().sod().toDate(),
|
||||
value: value
|
||||
});
|
||||
}
|
||||
}
|
||||
task.set('value', value);
|
||||
_ref2 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp'), user.get('stats.lvl')], money = _ref2[0], hp = _ref2[1], exp = _ref2[2], lvl = _ref2[3];
|
||||
_ref3 = [userObj.stats.money, userObj.stats.hp, userObj.stats.exp, userObj.stats.lvl], money = _ref3[0], hp = _ref3[1], exp = _ref3[2], lvl = _ref3[3];
|
||||
if (type === 'reward') {
|
||||
money -= task.get('value');
|
||||
num = parseFloat(task.get('value')).toFixed(2);
|
||||
@@ -232,11 +231,7 @@ tally = function(momentDate) {
|
||||
};
|
||||
dueToday = repeat && repeat[dayMapping[momentDate.day()]] === true;
|
||||
if (dueToday || type === 'todo') {
|
||||
score({
|
||||
task: task,
|
||||
direction: 'down',
|
||||
cron: true
|
||||
});
|
||||
score(taskId, 'down', true);
|
||||
}
|
||||
}
|
||||
if (type === 'daily') {
|
||||
|
||||
Reference in New Issue
Block a user