minor improvements to cron code for clarity; fix inaccurate comments; add TODOs for rest-in-inn actions

This commit is contained in:
Alys
2016-05-14 13:26:26 +10:00
parent 1fd7df7521
commit 6acaef50e6
2 changed files with 15 additions and 15 deletions

View File

@@ -87,7 +87,6 @@ function _gainMP (user, val) {
if (user.stats.mp >= user._statsComputed.maxMP) user.stats.mp = user._statsComputed.maxMP; if (user.stats.mp >= user._statsComputed.maxMP) user.stats.mp = user._statsComputed.maxMP;
if (user.stats.mp < 0) { if (user.stats.mp < 0) {
user.stats.mp = 0; user.stats.mp = 0;
return user.stats.mp;
} }
} }
@@ -181,10 +180,9 @@ module.exports = function scoreTask (options = {}, req = {}) {
// the API consumer, then cleared afterwards // the API consumer, then cleared afterwards
user._tmp = {}; user._tmp = {};
// If they're trying to purhcase a too-expensive reward, don't allow them to do that. // If they're trying to purchase a too-expensive reward, don't allow them to do that.
if (task.value > user.stats.gp && task.type === 'reward') throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language)); if (task.value > user.stats.gp && task.type === 'reward') throw new NotAuthorized(i18n.t('messageNotEnoughGold', req.language));
// ===== starting to actually do stuff, most of above was definitions =====
if (task.type === 'habit') { if (task.type === 'habit') {
delta += _changeTaskValue(user, task, direction, times, cron); delta += _changeTaskValue(user, task, direction, times, cron);

View File

@@ -37,8 +37,10 @@ function grantEndOfTheMonthPerks (user, now) {
if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; // cap it at 50 (hard 25 limit + extra 25) if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; // cap it at 50 (hard 25 limit + extra 25)
} }
} }
}
// If user cancelled subscription, we give them until 30day's end until it terminates function removeTerminatedSubscription (user) {
// If subscription's termination date has arrived
if (plan.dateTerminated && moment(plan.dateTerminated).isBefore(new Date())) { if (plan.dateTerminated && moment(plan.dateTerminated).isBefore(new Date())) {
_.merge(plan, { _.merge(plan, {
planId: null, planId: null,
@@ -71,17 +73,14 @@ function performSleepTasks (user, tasksByType, now) {
}); });
} }
// At end of day, add value to all incomplete Daily & Todo tasks (further incentive) // Perform various beginning-of-day reset actions.
// For incomplete Dailys, deduct experience
// Make sure to run this function once in a while as server will not take care of overnight calculations.
// And you have to run it every time client connects.
export function cron (options = {}) { export function cron (options = {}) {
let {user, tasksByType, analytics, now = new Date(), daysMissed, timezoneOffsetFromUserPrefs} = options; let {user, tasksByType, analytics, now = new Date(), daysMissed, timezoneOffsetFromUserPrefs} = options;
user.auth.timestamps.loggedin = now; user.auth.timestamps.loggedin = now;
user.lastCron = now; user.lastCron = now;
user.preferences.timezoneOffsetAtLastCron = timezoneOffsetFromUserPrefs; user.preferences.timezoneOffsetAtLastCron = timezoneOffsetFromUserPrefs;
// Reset the lastDrop count to zero // Allow user to get drops again
if (user.items.lastDrop.count > 0) user.items.lastDrop.count = 0; if (user.items.lastDrop.count > 0) user.items.lastDrop.count = 0;
// "Perfect Day" achievement for perfect-days // "Perfect Day" achievement for perfect-days
@@ -89,6 +88,7 @@ export function cron (options = {}) {
if (user.isSubscribed()) { if (user.isSubscribed()) {
grantEndOfTheMonthPerks(user, now); grantEndOfTheMonthPerks(user, now);
removeTerminatedSubscription(user);
} }
// User is resting at the inn. // User is resting at the inn.
@@ -105,7 +105,7 @@ export function cron (options = {}) {
// Tally each task // Tally each task
let todoTally = 0; let todoTally = 0;
tasksByType.todos.forEach(task => { // make uncompleted todos redder tasksByType.todos.forEach(task => { // make uncompleted To-Dos redder (further incentive to complete them)
scoreTask({ scoreTask({
task, task,
user, user,
@@ -117,8 +117,9 @@ export function cron (options = {}) {
todoTally += task.value; todoTally += task.value;
}); });
// For incomplete Dailys, add value (further incentive), deduct health, keep records for later decreasing the nightly mana gain
let dailyChecked = 0; // how many dailies were checked? let dailyChecked = 0; // how many dailies were checked?
let dailyDueUnchecked = 0; // how many dailies were cun-hecked? let dailyDueUnchecked = 0; // how many dailies were un-checked?
if (!user.party.quest.progress.down) user.party.quest.progress.down = 0; if (!user.party.quest.progress.down) user.party.quest.progress.down = 0;
tasksByType.dailys.forEach((task) => { tasksByType.dailys.forEach((task) => {
@@ -186,6 +187,7 @@ export function cron (options = {}) {
} }
}); });
// move singleton Habits towards yellow.
tasksByType.habits.forEach((task) => { // slowly reset 'onlies' value to 0 tasksByType.habits.forEach((task) => { // slowly reset 'onlies' value to 0
if (task.up === false || task.down === false) { if (task.up === false || task.down === false) {
task.value = Math.abs(task.value) < 0.1 ? 0 : task.value = task.value / 2; task.value = Math.abs(task.value) < 0.1 ? 0 : task.value = task.value / 2;
@@ -206,8 +208,8 @@ export function cron (options = {}) {
user.history.exp.push({date: now, value: expTally}); user.history.exp.push({date: now, value: expTally});
// preen user history so that it doesn't become a performance problem // preen user history so that it doesn't become a performance problem
// also for subscribed users but differentyly // also for subscribed users but differently
// premium subscribers can keep their full history. // TODO also do while resting in the inn. Note that later we'll be allowing the value/color of tasks to change while sleeping (https://github.com/HabitRPG/habitrpg/issues/5232), so the code in performSleepTasks() might be best merged back into here for that. Perhaps wait until then to do preen history for sleeping users.
preenUserHistory(user, tasksByType, user.preferences.timezoneOffset); preenUserHistory(user, tasksByType, user.preferences.timezoneOffset);
if (perfect) { if (perfect) {
@@ -242,7 +244,7 @@ export function cron (options = {}) {
_.merge(progress, {down: 0, up: 0}); _.merge(progress, {down: 0, up: 0});
progress.collect = _.transform(progress.collect, (m, v, k) => m[k] = 0); progress.collect = _.transform(progress.collect, (m, v, k) => m[k] = 0);
// @TODO: Clean PMs - keep 200 for subscribers and 50 for free users // @TODO: Clean PMs - keep 200 for subscribers and 50 for free users. Should also be done while resting in the inn
// let numberOfPMs = Object.keys(user.inbox.messages).length; // let numberOfPMs = Object.keys(user.inbox.messages).length;
// if (numberOfPMs > maxPMs) { // if (numberOfPMs > maxPMs) {
// _(user.inbox.messages) // _(user.inbox.messages)
@@ -257,7 +259,7 @@ export function cron (options = {}) {
// Analytics // Analytics
user.flags.cronCount++; user.flags.cronCount++;
analytics.track('Cron', { analytics.track('Cron', { // TODO also do while resting in the inn. https://github.com/HabitRPG/habitrpg/issues/7161#issuecomment-218214191
category: 'behavior', category: 'behavior',
gaLabel: 'Cron Count', gaLabel: 'Cron Count',
gaValue: user.flags.cronCount, gaValue: user.flags.cronCount,