Files
habitica/website/common/script/fns/randomDrop.js
Matteo Pagliazzi e04d4e8bea Drop Cap Notification, Modal and A/B Test (#12651)
* add drop cap notification

* add drop cap notification

* add dismissible notification

* fix(notification): correct remove icon positioning

* track events

* add modal

* add back files

* fix links and add missing analytics

* fix rounded borders and hide sub info for subscribers

* a/b test

* fix comparison

* Translated using Weblate (Spanish)

Currently translated at 98.2% (55 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/es/

Translated using Weblate (Spanish)

Currently translated at 99.4% (179 of 180 strings)

Translation: Habitica/Settings
Translate-URL: https://translate.habitica.com/projects/habitica/settings/es/

Merge branch 'origin/develop' into Weblate.

Translated using Weblate (Spanish)

Currently translated at 99.4% (175 of 176 strings)

Translation: Habitica/Subscriber
Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/es/

Translated using Weblate (Spanish (Latin America))

Currently translated at 98.6% (359 of 364 strings)

Translation: Habitica/Groups
Translate-URL: https://translate.habitica.com/projects/habitica/groups/es_419/

Translated using Weblate (Spanish)

Currently translated at 85.7% (151 of 176 strings)

Translation: Habitica/Subscriber
Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/es/

Translated using Weblate (Spanish)

Currently translated at 95.3% (538 of 564 strings)

Translation: Habitica/Backgrounds
Translate-URL: https://translate.habitica.com/projects/habitica/backgrounds/es/

Translated using Weblate (Spanish (Latin America))

Currently translated at 98.6% (359 of 364 strings)

Translation: Habitica/Groups
Translate-URL: https://translate.habitica.com/projects/habitica/groups/es_419/

Translated using Weblate (French)

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/fr/

Translated using Weblate (German)

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/de/

Translated using Weblate (French)

Currently translated at 100.0% (718 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/fr/

Translated using Weblate (German)

Currently translated at 100.0% (718 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/de/

Translated using Weblate (Czech)

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Spells
Translate-URL: https://translate.habitica.com/projects/habitica/spells/cs/

Translated using Weblate (Japanese)

Currently translated at 100.0% (175 of 175 strings)

Translation: Habitica/Subscriber
Translate-URL: https://translate.habitica.com/projects/habitica/subscriber/ja/

Translated using Weblate (Italian)

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/it/

Translated using Weblate (Italian)

Currently translated at 100.0% (718 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/it/

Translated using Weblate (Czech)

Currently translated at 100.0% (180 of 180 strings)

Translation: Habitica/Settings
Translate-URL: https://translate.habitica.com/projects/habitica/settings/cs/

Translated using Weblate (Basque)

Currently translated at 100.0% (2 of 2 strings)

Translation: Habitica/Noscript
Translate-URL: https://translate.habitica.com/projects/habitica/noscript/eu/

Translated using Weblate (Basque)

Currently translated at 6.5% (8 of 123 strings)

Translation: Habitica/Communityguidelines
Translate-URL: https://translate.habitica.com/projects/habitica/communityguidelines/eu/

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/zh_Hans/

Translated using Weblate (Japanese)

Currently translated at 100.0% (56 of 56 strings)

Translation: Habitica/Messages
Translate-URL: https://translate.habitica.com/projects/habitica/messages/ja/

Translated using Weblate (Chinese (Simplified))

Currently translated at 100.0% (718 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/zh_Hans/

Translated using Weblate (Portuguese (Brazil))

Currently translated at 100.0% (718 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/pt_BR/

Translated using Weblate (Portuguese (Brazil))

Currently translated at 99.8% (717 of 718 strings)

Translation: Habitica/Questscontent
Translate-URL: https://translate.habitica.com/projects/habitica/questscontent/pt_BR/

* clarify a/b test values

* add tests

* refactor user dropdown

* fix hover state

* fix user dropdown

* fix user menu hierarchy

* restore i18n files to release version

Co-authored-by: Melior <admin@habitica.com>
2020-10-16 19:50:54 +02:00

202 lines
6.9 KiB
JavaScript

import cloneDeepWith from 'lodash/cloneDeepWith';
import isFunction from 'lodash/isFunction';
import min from 'lodash/min';
import reduce from 'lodash/reduce';
import filter from 'lodash/filter';
import pickBy from 'lodash/pickBy';
import size from 'lodash/size';
import moment from 'moment';
import content from '../content/index';
import i18n from '../i18n';
import { daysSince } from '../cron';
import { diminishingReturns } from '../statHelpers';
import randomVal from '../libs/randomVal';
import statsComputed from '../libs/statsComputed';
import firstDrops from './firstDrops';
// TODO This is only used on the server
// move to user model as an instance method?
// Clone a drop object maintaining its functions
// so that we can change it without affecting the original item
function cloneDropItem (drop) {
return cloneDeepWith(drop, val => (isFunction(val) ? val : undefined));
}
function trueRandom () {
return Math.random();
}
export default function randomDrop (user, options, req = {}, analytics) {
let acceptableDrops;
let drop;
let dropMultiplier;
let rarity;
if (
size(user.items.eggs) < 1
&& size(user.items.hatchingPotions) < 1
) {
// @TODO why are we using both _tmp.firstDrops and the FIRST_DROPS notification?
user._tmp.firstDrops = firstDrops(user);
return;
}
const predictableRandom = options.predictableRandom || trueRandom;
const { task } = options;
let chance = min([Math.abs(task.value - 21.27), 37.5]) / 150 + 0.02;
chance *= task.priority // Task priority: +50% for Medium, +100% for Hard
// start users with +75% drops, diminishing by 5% per level gained
* (1 + Math.max(0, 80 - (5 * user.stats.lvl)) / 100)
* (1 + (task.streak / 100 || 0)) // Streak bonus: +1% per streak
* (1 + statsComputed(user).per / 100) // PERception: +1% per point
* (1 + (user.contributor.level / 40 || 0)) // Contrib levels: +2.5% per level
* (1 + (user.achievements.rebirths / 20 || 0)) // Rebirths: +5% per achievement
* (1 + (user.achievements.streak / 200 || 0)) // Streak achievements: +0.5% per achievement
// +50% per checklist item complete. TODO: make this into X individual drop chances instead
* (user._tmp.crit || 1)
* (1 + 0.5 * (reduce(
task.checklist, (m, i) => m + (i.completed ? 1 : 0), // eslint-disable-line indent
0,
) || 0)); // eslint-disable-line indent
chance = diminishingReturns(chance, 0.75);
if (predictableRandom() < chance) {
user.party.quest.progress.collectedItems = user.party.quest.progress.collectedItems || 0;
user.party.quest.progress.collectedItems += 1;
user._tmp.quest = user._tmp.quest || {};
user._tmp.quest.collection = 1;
if (user.markModified) user.markModified('party.quest.progress');
}
if (user.purchased && user.purchased.plan && user.purchased.plan.customerId) {
dropMultiplier = 2;
} else {
dropMultiplier = 1;
}
const maxDropCount = dropMultiplier * (5 + Math.floor(statsComputed(user).per / 25) + (user.contributor.level || 0)); // eslint-disable-line max-len
if (
daysSince(user.items.lastDrop.date, user.preferences) === 0
&& user.items.lastDrop.count >= maxDropCount
) {
return;
}
const firstFoodDrop = size(user.items.food) < 1;
if (firstFoodDrop || predictableRandom() < chance) {
rarity = predictableRandom();
if (firstFoodDrop || rarity > 0.6) { // food 40% chance
drop = cloneDropItem(randomVal(filter(content.food, {
canDrop: true,
})));
user.items.food = {
...user.items.food,
[drop.key]: user.items.food[drop.key] || 0,
};
user.items.food[drop.key] += 1;
if (user.markModified) user.markModified('items.food');
drop.type = 'Food';
drop.dialog = i18n.t('messageDropFood', {
dropText: drop.textA(req.language),
dropNotes: drop.notes(req.language),
}, req.language);
} else if (rarity > 0.3) { // eggs 30% chance
drop = cloneDropItem(randomVal(content.dropEggs));
user.items.eggs = {
...user.items.eggs,
[drop.key]: user.items.eggs[drop.key] || 0,
};
user.items.eggs[drop.key] += 1;
if (user.markModified) user.markModified('items.eggs');
drop.type = 'Egg';
drop.dialog = i18n.t('messageDropEgg', {
dropText: drop.text(req.language),
dropNotes: drop.notes(req.language),
}, req.language);
} else { // Hatching Potion, 30% chance - break down by rarity.
if (rarity < 0.02) { // Very Rare: 10% (of 30%)
acceptableDrops = ['Golden'];
} else if (rarity < 0.09) { // Rare: 20% of 30%
acceptableDrops = ['Zombie', 'CottonCandyPink', 'CottonCandyBlue'];
} else if (rarity < 0.18) { // uncommon: 30% of 30%
acceptableDrops = ['Red', 'Shade', 'Skeleton'];
} else { // common, 40% of 30%
acceptableDrops = ['Base', 'White', 'Desert'];
}
drop = cloneDropItem(
randomVal(pickBy(content.hatchingPotions, (v, k) => acceptableDrops.indexOf(k) >= 0)),
);
user.items.hatchingPotions = {
...user.items.hatchingPotions,
[drop.key]: user.items.hatchingPotions[drop.key] || 0,
};
user.items.hatchingPotions[drop.key] += 1;
if (user.markModified) user.markModified('items.hatchingPotions');
drop.type = 'HatchingPotion';
drop.dialog = i18n.t('messageDropPotion', {
dropText: drop.text(req.language),
dropNotes: drop.notes(req.language),
}, req.language);
}
// @TODO use notifications
user._tmp.drop = drop;
user.items.lastDrop.date = Number(new Date());
user.items.lastDrop.count += 1;
const dropN = user.items.lastDrop.count;
const dropCapReached = dropN === maxDropCount;
const isEnrolledInDropCapTest = user._ABtests.dropCapNotif
&& user._ABtests.dropCapNotif !== 'drop-cap-notif-not-enrolled';
const hasActiveDropCapNotif = isEnrolledInDropCapTest
&& user._ABtests.dropCapNotif === 'drop-cap-notif-enabled';
// Unsubscribed users get a notification when they reach the drop cap
// One per day
if (
hasActiveDropCapNotif && dropCapReached
&& user.addNotification
&& user.isSubscribed && !user.isSubscribed()
) {
const prevNotifIndex = user.notifications.findIndex(n => n.type === 'DROP_CAP_REACHED');
if (prevNotifIndex !== -1) user.notifications.splice(prevNotifIndex, 1);
user.addNotification('DROP_CAP_REACHED', {
message: i18n.t('dropCapReached', req.language),
items: dropN,
});
}
if (isEnrolledInDropCapTest) {
analytics.track('drop cap reached', {
uuid: user._id,
dropCap: maxDropCount,
category: 'behavior',
headers: req.headers,
});
}
if (analytics && moment().diff(user.auth.timestamps.created, 'days') < 7) {
analytics.track('dropped item', {
uuid: user._id,
itemKey: drop.key,
acquireMethod: 'Drop',
category: 'behavior',
headers: req.headers,
});
}
}
}