// Generated by CoffeeScript 1.3.3 var MODIFIER, async, content, cron, expModifier, helpers, hpModifier, model, moment, score, setModel, setupNotifications, updateStats, user, _; async = require('async'); moment = require('moment'); _ = require('lodash'); content = require('./content'); helpers = require('./helpers'); MODIFIER = .03; user = void 0; model = void 0; setModel = function(m) { model = m; user = model.at('_user'); return setupNotifications(); }; setupNotifications = function() { var statsNotification; if (typeof jQuery === "undefined" || jQuery === null) { return; } statsNotification = function(html, type) { if (user.get('stats.lvl') === 0) { return; } return $.bootstrapGrowl(html, { type: type, top_offset: 20, align: 'right', width: 250, delay: 3000, allow_dismiss: true, stackup_spacing: 10 }); }; user.on('set', 'stats.hp', function(captures, args, out, isLocal, passed) { var num, rounded; num = captures - args; rounded = Math.abs(num.toFixed(1)); if (num < 0) { return statsNotification("HP -" + rounded, 'error'); } }); user.on('set', 'stats.money', function(captures, args, out, isLocal, passed) { var num, rounded; num = captures - args; rounded = Math.abs(num.toFixed(1)); if (num < 0) { return statsNotification("GP -" + rounded, 'success'); } else if (num > 0) { num = Math.abs(num); return statsNotification("Exp,GP +" + rounded, 'success'); } }); return user.on('set', 'stats.lvl', function(captures, args, out, isLocal, passed) { if (captures > args) { return statsNotification(' Level Up!', 'info'); } }); }; expModifier = function(value) { var dmg, modified; dmg = user.get('items.weapon') * MODIFIER; dmg += user.get('stats.lvl') * MODIFIER; modified = value + (value * dmg); return modified; }; hpModifier = function(value) { var ac, modified; ac = user.get('items.armor') * MODIFIER; ac += user.get('stats.lvl') * MODIFIER; modified = value - (value * ac); return modified; }; updateStats = function(stats) { var money, tnl; if (user.get('stats.lvl') === 0) { return; } if (stats.hp != null) { if (stats.hp <= 0) { user.set('stats.lvl', 0); user.set('stast.hp', 0); return; } 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); } 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: "
" + content.items.unlockedMessage.content + " [Close]
" }); $('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); } }; score = function(taskId, direction, options) { var adjustvalue, delta, exp, hp, lvl, modified, money, num, sign, task, taskObj, taskPath, type, userObj, value, _ref, _ref1, _ref2; if (options == null) { options = { cron: false, times: 1 }; } taskPath = "_user.tasks." + taskId; _ref = [model.at(taskPath), model.get(taskPath)], task = _ref[0], taskObj = _ref[1]; type = taskObj.type, value = taskObj.value; userObj = user.get(); if (!task) { _ref1 = userObj.stats, money = _ref1.money, hp = _ref1.hp, exp = _ref1.exp; if (direction === "up") { modified = expModifier(1); money += modified; exp += modified; } else { modified = hpModifier(1); hp -= modified; } updateStats({ hp: hp, exp: exp, money: money }); return; } sign = direction === "up" ? 1 : -1; delta = value < 0 ? (-0.1 * value + 1) * sign : (Math.pow(0.9, value)) * sign; adjustvalue = type !== 'reward'; if ((type === 'habit') && (taskObj.up === false || taskObj.down === false)) { adjustvalue = false; } if (adjustvalue) { value += delta; } value *= options.times; if (type === 'habit') { if (taskObj.value !== value) { task.push('history', { date: new Date(), value: value }); } } task.set('value', value); _ref2 = userObj.stats, money = _ref2.money, hp = _ref2.hp, exp = _ref2.exp, lvl = _ref2.lvl; if (type === 'reward') { money -= task.get('value'); num = parseFloat(task.get('value')).toFixed(2); if (money < 0) { hp += money; money = 0; } } if ((delta > 0 || (type === 'daily' || type === 'todo')) && !options.cron) { modified = expModifier(delta); exp += modified; money += modified; } else if (type !== 'reward' && type !== 'todo') { modified = hpModifier(delta); hp += modified; } updateStats({ hp: hp, exp: exp, money: money }); return delta; }; cron = function() { var daysPassed, expTally, lastCron, lvl, tallyTask, today, todoTally; today = new Date(); user.setNull('lastCron', today); lastCron = user.get('lastCron'); daysPassed = helpers.daysBetween(today, lastCron); if (daysPassed > 0) { user.set('lastCron', today); todoTally = 0; tallyTask = function(taskObj, next) { var absVal, completed, dayMapping, daysFailed, id, repeat, task, type, value; id = taskObj.id, type = taskObj.type, completed = taskObj.completed, repeat = taskObj.repeat; if (id == null) { return; } task = user.at("tasks." + id); if (type === 'todo' || type === 'daily') { if (!completed) { daysFailed = daysPassed; if (type === 'daily' && repeat) { dayMapping = { 0: 'su', 1: 'm', 2: 't', 3: 'w', 4: 'th', 5: 'f', 6: 's', 7: 'su' }; daysFailed = 0; _.times(daysPassed, function(n) { var thatDay; thatDay = moment().subtract('days', n + 1); if (repeat[dayMapping[thatDay.day()]] === true) { return daysFailed++; } }); } score(id, 'down', { cron: true, times: daysFailed }); } value = task.get('value'); if (type === 'daily') { task.push("history", { date: today, value: value }); } else { absVal = completed ? Math.abs(value) : value; todoTally += absVal; } if (type === 'daily') { task.pass({ cron: true }).set('completed', false); } } return next(); }; _.each(user.get('tasks'), function(taskObj) { return tallyTask(taskObj, function() {}); }); user.push('history.todos', { date: today, 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: today, value: expTally }); } }; module.exports = { MODIFIER: MODIFIER, setModel: setModel, score: score, cron: cron };