Files
habitica/website/common/script/ops/buy/abstractBuyOperation.js
Matteo Pagliazzi 0c27fb24a5 start fixing commong
2019-10-09 16:51:17 +02:00

222 lines
5.2 KiB
JavaScript

/* eslint-disable max-classes-per-file */
import _merge from 'lodash/merge';
import _get from 'lodash/get';
import i18n from '../../i18n';
import {
NotAuthorized,
NotImplementedError,
BadRequest,
} from '../../libs/errors';
export class AbstractBuyOperation {
/**
* @param {User} user - the User-Object
* @param {Request} req - the Request-Object
* @param {analytics} analytics
*/
constructor (user, req, analytics) {
this.user = user;
this.req = req || {};
this.analytics = analytics;
const quantity = _get(req, 'quantity');
this.quantity = quantity ? Number(quantity) : 1;
if (this.quantity < 1 || !Number.isInteger(this.quantity)) throw new BadRequest(this.i18n('invalidQuantity'));
}
/**
* Returns the item value
* @param item
* @returns {number}
*/
getItemValue (item) {
return item.value;
}
/**
* Returns the item key
* @param item
* @returns {String}
*/
getItemKey (item) {
return item.key;
}
/**
* Returns the item type
* @param item
* @returns {String}
*/
getItemType (item) {
if (!item.type) throw new NotImplementedError('item doesn\'t have a type property');
return item.type;
}
/**
* Shortcut to get the translated string without passing `req.language`
* @param {String} key - translation key
* @param {*=} params
* @returns {*|string}
*/
// eslint-disable-next-line no-unused-vars
i18n (key, params = {}) {
return i18n.t.apply(null, [...arguments, this.req.language]); // eslint-disable-line prefer-rest-params, max-len
}
/**
* If the Operation allows purchasing items by quantity
* @returns Boolean
*/
multiplePurchaseAllowed () {
throw new NotImplementedError('multiplePurchaseAllowed');
}
/**
* Method is called to save the params as class-fields in order to access them
*/
extractAndValidateParams () {
throw new NotImplementedError('extractAndValidateParams');
}
executeChanges () {
throw new NotImplementedError('executeChanges');
}
analyticsData () {
throw new NotImplementedError('sendToAnalytics');
}
purchase () {
if (!this.multiplePurchaseAllowed() && this.quantity > 1) {
throw new NotAuthorized(this.i18n('messageNotAbleToBuyInBulk'));
}
this.extractAndValidateParams(this.user, this.req);
const resultObj = this.executeChanges(this.user, this.item, this.req);
if (this.analytics) {
this.sendToAnalytics(this.analyticsData());
}
return resultObj;
}
analyticsLabel () {
return 'acquire item';
}
sendToAnalytics (additionalData = {}) {
// spread-operator produces an "unexpected token" error
const analyticsData = _merge(additionalData, {
// ...additionalData,
uuid: this.user._id,
category: 'behavior',
headers: this.req.headers,
});
if (this.multiplePurchaseAllowed()) {
analyticsData.quantityPurchased = this.quantity;
}
this.analytics.track(this.analyticsLabel(), analyticsData);
}
}
export class AbstractGoldItemOperation extends AbstractBuyOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
canUserPurchase (user, item) {
this.item = item;
const itemValue = this.getItemValue(item);
const userGold = user.stats.gp;
if (userGold < itemValue * this.quantity) {
throw new NotAuthorized(this.i18n('messageNotEnoughGold'));
}
if (item && item.canOwn && !item.canOwn(user)) {
throw new NotAuthorized(this.i18n('cannotBuyItem'));
}
}
subtractCurrency (user, item) {
const itemValue = this.getItemValue(item);
user.stats.gp -= itemValue * this.quantity;
}
analyticsData () {
return {
itemKey: this.getItemKey(this.item),
itemType: this.getItemType(this.item),
acquireMethod: 'Gold',
goldCost: this.getItemValue(this.item),
};
}
}
export class AbstractGemItemOperation extends AbstractBuyOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
canUserPurchase (user, item) {
this.item = item;
const itemValue = this.getItemValue(item);
if (!item.canBuy(user)) {
throw new NotAuthorized(this.i18n('messageNotAvailable'));
}
if (!user.balance || user.balance < itemValue * this.quantity) {
throw new NotAuthorized(this.i18n('notEnoughGems'));
}
}
subtractCurrency (user, item) {
const itemValue = this.getItemValue(item);
user.balance -= itemValue * this.quantity;
}
analyticsData () {
return {
itemKey: this.getItemKey(this.item),
itemType: this.getItemType(this.item),
acquireMethod: 'Gems',
gemCost: this.getItemValue(this.item) * 4,
};
}
}
export class AbstractHourglassItemOperation extends AbstractBuyOperation {
constructor (user, req, analytics) {
super(user, req, analytics);
}
canUserPurchase (user, item) {
this.item = item;
if (user.purchased.plan.consecutive.trinkets <= 0) {
throw new NotAuthorized(this.i18n('notEnoughHourglasses'));
}
}
subtractCurrency (user) {
user.purchased.plan.consecutive.trinkets--;
}
analyticsData () {
return {
itemKey: this.item.key,
acquireMethod: 'Hourglass',
};
}
}