Files
habitica/lib/app/scoring.js
2012-09-06 12:28:06 -04:00

215 lines
6.1 KiB
JavaScript

// Generated by CoffeeScript 1.3.3
var content, expModifier, hpModifier, score, statsNotification, updateStats;
content = require('./content');
statsNotification = function(html, type) {
return $.bootstrapGrowl(html, {
type: type,
top_offset: 20,
align: 'right',
width: 250,
delay: 3000,
allow_dismiss: true,
stackup_spacing: 10
});
};
expModifier = function(user, value) {
var dmg, modified;
dmg = user.get('items.weapon') * .03;
dmg += user.get('stats.lvl') * .03;
modified = value + (value * dmg);
return modified;
};
hpModifier = function(user, value) {
var ac, modified;
ac = user.get('items.armor') * .03;
ac += user.get('stats.lvl') * .03;
modified = value - (value * ac);
return modified;
};
updateStats = function(user, stats) {
var money, tnl;
if (stats.hp != null) {
if (stats.hp < 0) {
user.set('stats.lvl', 0);
} else {
user.set('stats.hp', stats.hp);
}
}
if (stats.exp != null) {
tnl = user.get('_tnl');
if (stats.exp >= tnl) {
stats.exp -= tnl;
user.set('stats.lvl', user.get('stats.lvl') + 1);
user.set('stats.hp', 50);
statsNotification('<i class="icon-chevron-up"></i> Level Up!', 'info');
}
if (!user.get('items.itemsEnabled') && stats.exp >= 15) {
user.set('items.itemsEnabled', true);
$('ul.items').popover({
title: content.items.unlockedMessage.title,
placement: 'left',
trigger: 'manual',
html: true,
content: "<div class='item-store-popover'> <img src='/img/BrowserQuest/chest.png' /> " + content.items.unlockedMessage.content + " <a href='#' onClick=\"$('ul.items').popover('hide');return false;\">[Close]</a> </div>"
});
$('ul.items').popover('show');
}
user.set('stats.exp', stats.exp);
}
if (stats.money != null) {
if (!(typeof money !== "undefined" && money !== null) || money < 0) {
money = 0.0;
}
return user.set('stats.money', stats.money);
}
};
module.exports.score = score = function(spec) {
var adjustvalue, cron, delta, direction, exp, hp, lvl, modified, money, num, sign, task, type, user, value, _ref, _ref1, _ref2;
if (spec == null) {
spec = {
user: null,
task: null,
direction: null,
cron: null
};
}
_ref = [spec.user, spec.task, spec.direction, spec.cron], user = _ref[0], task = _ref[1], direction = _ref[2], cron = _ref[3];
if (!task) {
_ref1 = [user.get('stats.money'), user.get('stats.hp'), user.get('stats.exp')], money = _ref1[0], hp = _ref1[1], exp = _ref1[2];
if (direction === "up") {
modified = expModifier(user, 1);
money += modified;
exp += modified;
} else {
modified = hpModifier(user, 1);
hp -= modified;
}
updateStats(user, {
hp: hp,
exp: exp,
money: money
});
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)) {
adjustvalue = false;
}
if (adjustvalue) {
value += delta;
}
if (type === 'habit') {
if (task.get('value') !== value) {
task.push('history', {
date: new Date(),
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];
if (type === 'reward') {
money -= task.get('value');
num = task.get('value').toFixed(2);
statsNotification("<i class='icon-star'></i>GP -" + num, 'success');
if (money < 0) {
hp += money;
statsNotification("<i class='icon-heart'></i>HP " + (money.toFixed(2)), 'error');
money = 0;
}
}
if ((delta > 0 || (type === 'daily' || type === 'todo')) && !cron) {
modified = expModifier(user, delta);
exp += modified;
money += modified;
if (modified > 0) {
statsNotification("<i class='icon-star'></i>Exp,GP +" + (modified.toFixed(2)), 'success');
} else {
statsNotification("<i class='icon-star'></i>Exp,GP " + (modified.toFixed(2)), 'warning');
}
} else if (type !== 'reward' && type !== 'todo') {
modified = hpModifier(user, delta);
hp += modified;
statsNotification("<i class='icon-heart'></i>HP " + (modified.toFixed(2)), 'error');
}
updateStats(user, {
hp: hp,
exp: exp,
money: money
});
return delta;
};
module.exports.tally = function(user, momentDate) {
var expTally, lvl, todoTally;
todoTally = 0;
_.each(user.get('tasks'), function(taskObj, taskId, list) {
var absVal, completed, dayMapping, repeat, task, type, value, _ref;
if (taskObj.id == null) {
return;
}
_ref = [taskObj.type, taskObj.value, taskObj.completed, taskObj.repeat], type = _ref[0], value = _ref[1], completed = _ref[2], repeat = _ref[3];
task = user.at("tasks." + taskId);
if (type === 'todo' || type === 'daily') {
if (!completed) {
dayMapping = {
0: 'su',
1: 'm',
2: 't',
3: 'w',
4: 'th',
5: 'f',
6: 's',
7: 'su'
};
if (repeat && repeat[dayMapping[momentDate.day()]] === true) {
score({
user: user,
task: task,
direction: 'down',
cron: true
});
}
}
if (type === 'daily') {
task.push("history", {
date: new Date(momentDate),
value: value
});
} else {
absVal = completed ? Math.abs(value) : value;
todoTally += absVal;
}
if (type === 'daily') {
return task.pass({
cron: true
}).set('completed', false);
}
}
});
user.push('history.todos', {
date: new Date(momentDate),
value: todoTally
});
expTally = user.get('stats.exp');
lvl = 0;
while (lvl < (user.get('stats.lvl') - 1)) {
lvl++;
expTally += (lvl * 100) / 5;
}
return user.push('history.exp', {
date: new Date(),
value: expTally
});
};